Skip to content

Commit

Permalink
Add aliases as an option to data.yml.
Browse files Browse the repository at this point in the history
  • Loading branch information
creatorfromhell committed Dec 10, 2023
1 parent 1266cdf commit 301d3cc
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 42 deletions.
25 changes: 13 additions & 12 deletions src/main/java/me/lokka30/phantomworlds/PhantomWorlds.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import me.lokka30.microlib.maths.QuickTimer;
import me.lokka30.microlib.other.UpdateChecker;
import me.lokka30.phantomworlds.commands.phantomworlds.PWCommand;
import me.lokka30.phantomworlds.commands.phantomworlds.parameters.resolvers.AliasWorldResolver;
import me.lokka30.phantomworlds.commands.phantomworlds.parameters.resolvers.WorldFolderResolver;
import me.lokka30.phantomworlds.commands.phantomworlds.parameters.suggestion.AliasWorldSuggestion;
import me.lokka30.phantomworlds.commands.phantomworlds.parameters.suggestion.WorldFolderSuggestion;
import me.lokka30.phantomworlds.commands.phantomworlds.utils.WorldFolder;
import me.lokka30.phantomworlds.listeners.player.PlayerChangeWorldListener;
Expand All @@ -18,14 +20,15 @@
import me.lokka30.phantomworlds.misc.UpdateCheckerResult;
import me.lokka30.phantomworlds.scheduler.BackupScheduler;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import revxrsal.commands.bukkit.BukkitCommandHandler;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

Expand Down Expand Up @@ -58,6 +61,8 @@ public class PhantomWorlds extends JavaPlugin {
*/
public static final String[] CONTRIBUTORS = new String[]{"madison-allen"};

public static final List<String> COMMAND_HELP = new LinkedList<>();

public static final String BACKUP_FOLDER = "backups";
public static final String ARCHIVE_FOLDER = "archives";

Expand Down Expand Up @@ -213,24 +218,20 @@ void registerCommands() {
this.command = BukkitCommandHandler.create(this);

//Set our command help writer
command.setHelpWriter((command, actor) -> {
if(command.getDescription() == null) {
return "";
}

final String description = PhantomWorlds.instance().messages.getConfig().getString(command.getDescription());
if(description == null) {
return "";
}
for(final String key : messages.getConfig().getConfigurationSection("command.phantomworlds.help").getKeys(false)) {
COMMAND_HELP.add(ChatColor.translateAlternateColorCodes('&', messages.getConfig().getString("command.phantomworlds.help." + key, "Missing help message. Key: " + key)));
}

return ChatColor.translateAlternateColorCodes('&', description);
});
//Override the help writer because it dupes commands for some reason.
command.setHelpWriter((command, actor) ->"");

//Register Resolvers
this.command.registerValueResolver(WorldFolder.class, new WorldFolderResolver());
this.command.registerValueResolver(World.class, new AliasWorldResolver());

//Register Suggestors
this.command.getAutoCompleter().registerParameterSuggestions(WorldFolder.class, new WorldFolderSuggestion());
this.command.getAutoCompleter().registerParameterSuggestions(World.class, new AliasWorldSuggestion());

this.command.register(new PWCommand());
this.command.registerBrigadier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import revxrsal.commands.bukkit.annotation.CommandPermission;
import revxrsal.commands.help.CommandHelp;

import java.util.ArrayList;
import java.util.Arrays;

/**
* PWCommand
Expand All @@ -59,107 +59,117 @@ public class PWCommand {
@Subcommand({"help", "?"})
@DefaultFor({"pw", "phantomworlds"})
public void help(BukkitCommandActor actor, CommandHelp<String> helpEntries, @Default("1") int page) {
for(final String entry : helpEntries.paginate(page, 5)) {
(new MultiMessage(
PhantomWorlds.instance().messages.getConfig().getStringList(entry), new ArrayList<>()
)).send(actor.getSender());

(new MultiMessage(
PhantomWorlds.instance().messages.getConfig()
.getStringList("command.phantomworlds.help-header"), Arrays.asList(
new MultiMessage.Placeholder("prefix",
PhantomWorlds.instance().messages.getConfig().getString("common.prefix", "&b&lPhantomWorlds: &7"),
true),
new MultiMessage.Placeholder("page", String.valueOf(page), false),
new MultiMessage.Placeholder("max", String.valueOf(helpEntries.paginate(page, 5).size()), false)

))).send(actor.getSender());

for(String str : PhantomWorlds.COMMAND_HELP) {
actor.getSender().sendMessage(str);
}
}

@Subcommand({"backup", "archive", "bu"})
@CommandPermission("phantomworlds.command.phantomworlds.backup")
@Description("command.phantomworlds.usages.backup")
@Description("command.phantomworlds.help.backup")
public void backup(BukkitCommandActor actor, @Optional final World world) {
BackupCommand.onCommand(actor, world);
}

@Subcommand({"create", "+", "new"})
@CommandPermission("phantomworlds.command.phantomworlds.create")
@Description("command.phantomworlds.usages.create")
@Description("command.phantomworlds.help.create")
public void create(BukkitCommandActor actor) {
//todo: port create command
}

@Subcommand({"compatibility"})
@CommandPermission("phantomworlds.command.phantomworlds.compatibility")
@Description("command.phantomworlds.usages.compatibility")
@Description("command.phantomworlds.help.compatibility")
public void compatibility(BukkitCommandActor actor) {
CompatibilityCommand.onCommand(actor);
}

@Subcommand({"debug"})
@CommandPermission("phantomworlds.command.phantomworlds.debug")
@Description("command.phantomworlds.usages.debug")
@Description("command.phantomworlds.help.debug")
public void debug(BukkitCommandActor actor, @Optional final String level) {
DebugCommand.onCommand(actor, level);
}

@Subcommand({"delete", "-", "remove", "del"})
@CommandPermission("phantomworlds.command.phantomworlds.delete")
@Description("command.phantomworlds.usages.delete")
@Description("command.phantomworlds.help.delete")
public void delete(BukkitCommandActor actor, @Optional final World world) {
DeleteCommand.onCommand(actor, world);
}

@Subcommand({"list", "l"})
@CommandPermission("phantomworlds.command.phantomworlds.list")
@Description("command.phantomworlds.usages.list")
@Description("command.phantomworlds.help.list")
public void list(BukkitCommandActor actor) {
ListCommand.onCommand(actor);
}

@Subcommand({"import", "im"})
@CommandPermission("phantomworlds.command.phantomworlds.import")
@Description("command.phantomworlds.usages.import")
@Description("command.phantomworlds.help.import")
public void importCMD(BukkitCommandActor actor, @Optional final World world) {
ImportCommand.onCommand(actor, world);
}

@Subcommand({"info", "i"})
@CommandPermission("phantomworlds.command.phantomworlds.info")
@Description("command.phantomworlds.usages.info")
@Description("command.phantomworlds.help.info")
public void info(BukkitCommandActor actor) {
InfoCommand.onCommand(actor);
}

@Subcommand({"load"})
@CommandPermission("phantomworlds.command.phantomworlds.load")
@Description("command.phantomworlds.usages.load")
@Description("command.phantomworlds.help.load")
public void load(BukkitCommandActor actor, @Optional final WorldFolder world) {
LoadCommand.onCommand(actor, world);
}

@Subcommand({"reload", "r"})
@CommandPermission("phantomworlds.command.phantomworlds.reload")
@Description("command.phantomworlds.usages.reload")
@Description("command.phantomworlds.help.reload")
public void reload(BukkitCommandActor actor) {
ReloadCommand.onCommand(actor);
}

@Subcommand({"setspawn"})
@CommandPermission("phantomworlds.command.phantomworlds.setspawn")
@Description("command.phantomworlds.usages.setspawn")
@Description("command.phantomworlds.help.setspawn")
public void setspawn(BukkitCommandActor actor, @Optional Double x, @Optional Double y, @Optional Double z, @Optional World world, @Optional Float yaw, @Optional Float pitch) {
SetSpawnCommand.onCommand(actor, x, y, z, world, yaw, pitch);
}

@Subcommand({"spawn"})
@CommandPermission("phantomworlds.command.phantomworlds.spawn")
@Description("command.phantomworlds.usages.spawn")
@Description("command.phantomworlds.help.spawn")
public void spawn(BukkitCommandActor actor, @Optional final World world, @Optional final Player player) {
SpawnCommand.onCommand(actor, world, player);
}

@Subcommand({"teleport", "tp"})
@CommandPermission("phantomworlds.command.phantomworlds.teleport")
@Description("command.phantomworlds.usages.tp")
@Description("command.phantomworlds.help.tp")
public void tp(BukkitCommandActor actor, @Optional final World world, @Optional final Player player) {
TeleportCommand.onCommand(actor, world, player);
}

@Subcommand({"unload", "u"})
@CommandPermission("phantomworlds.command.phantomworlds.unload")
@Description("command.phantomworlds.usages.unload")
@Description("command.phantomworlds.help.unload")
public void unload(BukkitCommandActor actor, @Optional final World world) {
UnloadCommand.onCommand(actor, world);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package me.lokka30.phantomworlds.commands.phantomworlds.parameters.resolvers;
/*
* Phantom Worlds
* Copyright (C) 2023 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import me.lokka30.phantomworlds.PhantomWorlds;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import revxrsal.commands.process.ValueResolver;

/**
* AliasWorldResolver
*
* @author creatorfromhell
* @since 2.0.5.0
*/
public class AliasWorldResolver implements ValueResolver<World> {

@Override
public World resolve(@NotNull ValueResolverContext context) throws Throwable {
final String value = context.arguments().pop();

final String name = PhantomWorlds.worldManager().aliases.getOrDefault(value, value);

return Bukkit.getWorld(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package me.lokka30.phantomworlds.commands.phantomworlds.parameters.resolvers;
/*
* Phantom Worlds
* Copyright (C) 2023 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import me.lokka30.phantomworlds.commands.phantomworlds.utils.WorldFolder;
import me.lokka30.phantomworlds.commands.phantomworlds.utils.WorldRule;
import org.bukkit.GameRule;
import org.jetbrains.annotations.NotNull;
import revxrsal.commands.process.ValueResolver;

import java.util.ArrayList;
import java.util.ListIterator;

/**
* GameRulesResolver
*
* @author creatorfromhell
* @since 2.0.5.0
*/
public class GameRulesResolver implements ValueResolver<WorldRule[]> {

@Override
public WorldRule[] resolve(@NotNull ValueResolverContext context) throws Throwable {
final WorldRule[] rules = new WorldRule[context.arguments().size()];
final ListIterator<String> it = context.arguments().listIterator();

while(it.hasNext()) {
final String value = it.next();


}
return rules;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package me.lokka30.phantomworlds.commands.phantomworlds.parameters.suggestion;
/*
* Phantom Worlds
* Copyright (C) 2023 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import me.lokka30.phantomworlds.PhantomWorlds;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import revxrsal.commands.autocomplete.SuggestionProvider;
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.command.ExecutableCommand;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* AliasWorldSuggestion
*
* @author creatorfromhell
* @since 2.0.5.0
*/
public class AliasWorldSuggestion implements SuggestionProvider {

@Override
public @NotNull Collection<String> getSuggestions(@NotNull List<String> list, @NotNull CommandActor commandActor, @NotNull ExecutableCommand executableCommand) throws Throwable {

final List<String> worlds = new ArrayList<>(PhantomWorlds.worldManager().aliases.keySet());

for(final World world : Bukkit.getWorlds()) {
worlds.add(world.getName());
}
return worlds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import me.lokka30.phantomworlds.PhantomWorlds;
import me.lokka30.phantomworlds.commands.phantomworlds.utils.WorldFolder;
import me.lokka30.phantomworlds.misc.WorldLoadResponse;
import org.bukkit.Bukkit;
import revxrsal.commands.bukkit.BukkitCommandActor;

import java.io.File;
import java.util.Arrays;

import static me.lokka30.phantomworlds.misc.WorldLoadResponse.ALREADY_LOADED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down
Loading

0 comments on commit 301d3cc

Please sign in to comment.