diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 27296ec0..e6896382 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -34,6 +34,7 @@ import com.onarandombox.MultiverseCore.api.MultiverseMessaging; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.commands_acf.CoordCommand; import com.onarandombox.MultiverseCore.commands_acf.CreateCommand; +import com.onarandombox.MultiverseCore.commands_acf.GamerulesCommand; import com.onarandombox.MultiverseCore.commands_acf.ListCommand; import com.onarandombox.MultiverseCore.commands_acf.ReloadCommand; import com.onarandombox.MultiverseCore.commands_acf.RemoveCommand; @@ -744,6 +745,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { this.commandHandler.registerCommand(new RemoveCommand(this)); this.commandHandler.registerCommand(new ListCommand(this)); this.commandHandler.registerCommand(new ScriptCommand(this)); + this.commandHandler.registerCommand(new GamerulesCommand(this)); } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ConfigCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ConfigCommand.java index a44c7fbe..5f9d43f5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ConfigCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ConfigCommand.java @@ -12,6 +12,8 @@ import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.List; import java.util.Map; @CommandAlias("mv") @@ -26,20 +28,18 @@ public class ConfigCommand extends MultiverseCommand { @Subcommand("show") @Description("Show multiverse config values.") public void onShowCommand(@NotNull CommandSender sender) { - StringBuilder builder = new StringBuilder(); + List configList = new ArrayList<>(); Map serializedConfig = this.plugin.getMVConfig().serialize(); for (Map.Entry entry : serializedConfig.entrySet()) { - builder.append(ChatColor.GREEN) - .append(entry.getKey()) - .append(ChatColor.WHITE).append(" = ").append(ChatColor.GOLD) - .append(entry.getValue().toString()) - .append(ChatColor.WHITE).append(", "); + + configList.add(ChatColor.GREEN + entry.getKey() + + ChatColor.WHITE + " = " + + ChatColor.GOLD + entry.getValue().toString() + + ChatColor.WHITE); } - String message = builder.toString(); - message = message.substring(0, message.length() - 2); - sender.sendMessage(message); + sender.sendMessage(String.join(", ", configList)); } @Subcommand("set") @@ -58,7 +58,7 @@ public class ConfigCommand extends MultiverseCommand { } if (!this.plugin.saveMVConfigs()) { - sender.sendMessage(ChatColor.RED + "FAIL!" + ChatColor.WHITE + " Check your console for details!"); + sender.sendMessage(ChatColor.RED + "Failed to save config! Check your console for details."); return; } @@ -68,6 +68,7 @@ public class ConfigCommand extends MultiverseCommand { this.plugin.getMVWorldManager().setFirstSpawnWorld(value); } + //TODO: Show properties and values that where updated. sender.sendMessage(ChatColor.GREEN + "SUCCESS!" + ChatColor.WHITE + " Values were updated successfully!"); this.plugin.loadConfigs(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/DebugCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/DebugCommand.java index e34ce545..91b4c8cf 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/DebugCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/DebugCommand.java @@ -48,6 +48,7 @@ public class DebugCommand extends MultiverseCommand { displayDebugMode(sender); } + //TODO: See if can move this to CommandContext Integer.class private int parseDebugLevel(@NotNull String debugLevel) { if (debugLevel.equalsIgnoreCase("off")) { return 0; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/DeleteCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/DeleteCommand.java index 9bdf9da8..b9f1fd4b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/DeleteCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/DeleteCommand.java @@ -3,8 +3,10 @@ package com.onarandombox.MultiverseCore.commands_acf; import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Conditions; import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Flags; +import co.aikar.commands.annotation.Single; import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import com.onarandombox.MultiverseCore.MultiverseCore; @@ -23,22 +25,22 @@ public class DeleteCommand extends MultiverseCommand { @Subcommand("delete") @CommandPermission("multiverse.core.delete") @Syntax("") - @CommandCompletion("@MVWorlds") + @CommandCompletion("@MVWorlds|@unloadedWorlds") @Description("") public void onDeleteCommand(@NotNull CommandSender sender, - @NotNull @Flags("other") MultiverseWorld world) { + @NotNull @Single @Conditions("isWorldInConfig") String worldName) { - this.plugin.getCommandQueueManager().addToQueue(sender, deleteRunnable(sender, world)); + this.plugin.getCommandQueueManager().addToQueue(sender, deleteRunnable(sender, worldName)); } private Runnable deleteRunnable(@NotNull CommandSender sender, - @NotNull MultiverseWorld world) { + @NotNull String worldName) { return () -> { //TODO: deleteWorld method should take world object directly - String resultMessage = (this.plugin.getMVWorldManager().deleteWorld(world.getName())) - ? ChatColor.GREEN + "World '" + world.getName() + "' Deleted!" - : ChatColor.RED + "World '" + world.getName() + "' could NOT be deleted!"; + String resultMessage = (this.plugin.getMVWorldManager().deleteWorld(worldName)) + ? ChatColor.GREEN + "World '" + worldName + "' Deleted!" + : ChatColor.RED + "World '" + worldName + "' could NOT be deleted!"; sender.sendMessage(resultMessage); }; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/InfoCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/InfoCommand.java index fb5099f8..292a677f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/InfoCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/InfoCommand.java @@ -10,7 +10,6 @@ import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; -import com.onarandombox.MultiverseCore.commands_helper.WorldAndPage; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ListCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ListCommand.java index ad8e157e..e6686691 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ListCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ListCommand.java @@ -25,6 +25,7 @@ public class ListCommand extends MultiverseCommand { public void onListCommand(@NotNull CommandSender sender, @Default("1") int page) { + //TODO: Do the actual fancy list display sender.sendMessage(this.plugin.getMVWorldManager().getMVWorlds().toString()); sender.sendMessage("Page of: " + page); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/LoadCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/LoadCommand.java index c8f53b38..570597a8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/LoadCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/LoadCommand.java @@ -3,7 +3,9 @@ package com.onarandombox.MultiverseCore.commands_acf; import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Conditions; import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Flags; import co.aikar.commands.annotation.Single; import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; @@ -26,12 +28,12 @@ public class LoadCommand extends MultiverseCommand { @CommandCompletion("@unloadedWorlds") @Description("Loads a world into Multiverse.") public void onLoadCommand(@NotNull CommandSender sender, - @NotNull @Single String world) { + @NotNull @Single @Conditions("isUnloadedWorld") String worldName) { - if (!this.plugin.getMVWorldManager().loadWorld(world)) { - sender.sendMessage("Error trying to load world '" + world + "'!"); + if (!this.plugin.getMVWorldManager().loadWorld(worldName)) { + sender.sendMessage("Error trying to load world '" + worldName + "'!"); return; } - Command.broadcastCommandMessage(sender, "Loaded world '" + world + "'!"); + Command.broadcastCommandMessage(sender, "Loaded world '" + worldName + "'!"); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ReloadCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ReloadCommand.java index 72307bf2..0b7e2318 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ReloadCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/ReloadCommand.java @@ -29,7 +29,7 @@ public class ReloadCommand extends MultiverseCommand { this.plugin.getAnchorManager().loadAnchors(); this.plugin.getMVWorldManager().loadWorlds(true); - List configsLoaded = new ArrayList(); + List configsLoaded = new ArrayList<>(); configsLoaded.add("Multiverse-Core - config.yml"); configsLoaded.add("Multiverse-Core - worlds.yml"); configsLoaded.add("Multiverse-Core - anchors.yml"); @@ -37,10 +37,7 @@ public class ReloadCommand extends MultiverseCommand { MVConfigReloadEvent configReload = new MVConfigReloadEvent(configsLoaded); this.plugin.getServer().getPluginManager().callEvent(configReload); - for (String s : configReload.getAllConfigsLoaded()) { - sender.sendMessage(s); - } - + configReload.getAllConfigsLoaded().forEach(sender::sendMessage); sender.sendMessage(ChatColor.GREEN + "Reload Complete!"); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/RemoveCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/RemoveCommand.java index ac30eab2..0f02dce7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/RemoveCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/RemoveCommand.java @@ -3,12 +3,12 @@ package com.onarandombox.MultiverseCore.commands_acf; import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Conditions; import co.aikar.commands.annotation.Description; -import co.aikar.commands.annotation.Flags; +import co.aikar.commands.annotation.Single; import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.api.MultiverseWorld; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -25,12 +25,10 @@ public class RemoveCommand extends MultiverseCommand { @Syntax("") @Description("Unloads a world from Multiverse and removes it from worlds.yml, this does NOT DELETE the world folder.") public void onRemoveCommand(@NotNull CommandSender sender, - @NotNull @Flags("other") MultiverseWorld world) { + @NotNull @Single @Conditions("isWorldInConfig") String worldName) { - String resultMessage = (this.plugin.getMVWorldManager().removeWorldFromConfig(world.getName())) + sender.sendMessage((this.plugin.getMVWorldManager().removeWorldFromConfig(worldName)) ? "World removed from config!" - : "Error trying to remove world from config!"; - - sender.sendMessage(resultMessage); + : "Error trying to remove world from config!"); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/SpawnCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/SpawnCommand.java index 4481222d..6825cab5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_acf/SpawnCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_acf/SpawnCommand.java @@ -45,7 +45,7 @@ public class SpawnCommand extends MultiverseCommand { spawnAccurately(player); String senderName = (sender instanceof ConsoleCommandSender) - ? ChatColor.LIGHT_PURPLE + "the console" + ? ChatColor.LIGHT_PURPLE + "console" : ChatColor.YELLOW + sender.getName(); player.sendMessage("You were teleported by " + senderName); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/CommandQueueManager.java b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/CommandQueueManager.java index b4c79a41..c2311a9a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/CommandQueueManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/CommandQueueManager.java @@ -34,9 +34,9 @@ public class CommandQueueManager { cancelPreviousInQueue(sender); - QueuedCommand queuedCommand = new QueuedCommand(sender, runnable, validPeriod); + QueuedCommand queuedCommand = new QueuedCommand(sender, runnable); queuedCommands.put(sender, queuedCommand); - queuedCommand.setExpireTask(runExpireLater(queuedCommand)); + queuedCommand.setExpireTask(runExpireLater(queuedCommand, validPeriod)); sender.sendMessage("The command you are trying to run is deemed dangerous."); sender.sendMessage("Run /mv confirm to continue."); @@ -47,15 +47,16 @@ public class CommandQueueManager { if (previousCommand == null) { return; } + previousCommand.cancelExpiryTask(); queuedCommands.remove(sender); } - private BukkitTask runExpireLater(@NotNull QueuedCommand queuedCommand) { + private BukkitTask runExpireLater(@NotNull QueuedCommand queuedCommand, int validPeriod) { return Bukkit.getScheduler().runTaskLater( this.plugin, expireRunnable(queuedCommand), - queuedCommand.getValidInterval() + validPeriod ); } @@ -66,8 +67,9 @@ public class CommandQueueManager { Logging.finer("This is an old command already."); return; } + + Logging.finer("Command is expired, removing..."); this.queuedCommands.remove(queuedCommand.getSender()); - Logging.finer("Command expired and is removed."); }; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/CommandTools.java b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/CommandTools.java index 844bfda3..d002ebe3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/CommandTools.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/CommandTools.java @@ -66,9 +66,10 @@ public class CommandTools { this::suggestPotentialWorlds ); - this.commandHandler.getCommandCompletions().registerStaticCompletion( + //TODO: Change to static + this.commandHandler.getCommandCompletions().registerAsyncCompletion( "MVConfigs", - suggestMVConfig() + this::suggestMVConfig ); this.commandHandler.getCommandCompletions().registerStaticCompletion( @@ -139,7 +140,7 @@ public class CommandTools { @NotNull - private Set suggestMVConfig() { + private Set suggestMVConfig(@NotNull BukkitCommandCompletionContext context) { return this.plugin.getMVConfig().serialize().keySet(); } @@ -509,6 +510,18 @@ public class CommandTools { this::checkIsMVWorld ); + this.commandHandler.getCommandConditions().addCondition( + String.class, + "isUnloadedWorld", + this::checkIsUnloadedWorld + ); + + this.commandHandler.getCommandConditions().addCondition( + String.class, + "isWorldInConfig", + this::checkIsWorldInConfig + ); + this.commandHandler.getCommandConditions().addCondition( String.class, "worldFolderExist", @@ -522,12 +535,13 @@ public class CommandTools { ); } + //TODO: Message seems a bit too targeted to create world only private void checkIsMVWorld(@NotNull ConditionContext context, @NotNull BukkitCommandExecutionContext executionContext, @NotNull String worldName) { boolean shouldBeMVWorld = Boolean.parseBoolean(context.getConfig()); - boolean isMVWorld = this.plugin.getMVWorldManager().isMVWorld(worldName); + boolean isMVWorld = this.worldManager.isMVWorld(worldName); if (isMVWorld && !shouldBeMVWorld) { executionContext.getSender().sendMessage(ChatColor.RED + "Multiverse cannot create " + ChatColor.GOLD + ChatColor.UNDERLINE @@ -541,9 +555,32 @@ public class CommandTools { } } - private void checkWorldFolderExist(@NotNull ConditionContext context, - @NotNull BukkitCommandExecutionContext executionContext, - @NotNull String worldFolder) { + private void checkIsUnloadedWorld(@NotNull ConditionContext context, + @NotNull BukkitCommandExecutionContext executionContext, + @NotNull String worldName) { + + if (this.worldManager.isMVWorld(worldName)) { + throw new ConditionFailedException("World '" + worldName + "' is already loaded."); + } + + if (!this.worldManager.getUnloadedWorlds().contains(worldName)) { + throw new ConditionFailedException("World '" + worldName + "' not found."); + } + } + + private void checkIsWorldInConfig(@NotNull ConditionContext context, + @NotNull BukkitCommandExecutionContext executionContext, + @NotNull String worldName) { + + //TODO: Should have direct API for it, instead of check both loaded and unloaded. + if (!this.worldManager.isMVWorld(worldName) && !this.worldManager.getUnloadedWorlds().contains(worldName)) { + throw new ConditionFailedException("World '" + worldName + "' not found."); + } + } + + private void checkWorldFolderExist(@NotNull ConditionContext context, + @NotNull BukkitCommandExecutionContext executionContext, + @NotNull String worldFolder) { boolean shouldExist = Boolean.parseBoolean(context.getConfig()); boolean worldFileExist = new File(this.plugin.getServer().getWorldContainer(), worldFolder).exists(); @@ -560,9 +597,9 @@ public class CommandTools { } } - private void checkValidWorldFolder (@NotNull ConditionContext context, - @NotNull BukkitCommandExecutionContext executionContext, - @NotNull String worldName) { + private void checkValidWorldFolder(@NotNull ConditionContext context, + @NotNull BukkitCommandExecutionContext executionContext, + @NotNull String worldName) { File worldFolder = new File(this.plugin.getServer().getWorldContainer(), worldName); if (!worldFolder.isDirectory()) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/QueuedCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/QueuedCommand.java index b969b5da..0854f0e2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/QueuedCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/QueuedCommand.java @@ -6,13 +6,11 @@ import org.bukkit.scheduler.BukkitTask; public class QueuedCommand { private final CommandSender sender; private final Runnable runnable; - private final int validInterval; private BukkitTask expireTask; - public QueuedCommand(CommandSender sender, Runnable runnable, int validPeriod) { + public QueuedCommand(CommandSender sender, Runnable runnable) { this.sender = sender; this.runnable = runnable; - this.validInterval = validPeriod; } public void runCommand() { @@ -31,8 +29,4 @@ public class QueuedCommand { public CommandSender getSender() { return sender; } - - public int getValidInterval() { - return validInterval; - } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/WorldAndPage.java b/src/main/java/com/onarandombox/MultiverseCore/commands_helper/WorldAndPage.java deleted file mode 100644 index 4391c520..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/commands_helper/WorldAndPage.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.onarandombox.MultiverseCore.commands_helper; - -import com.onarandombox.MultiverseCore.api.MultiverseWorld; - -public class WorldAndPage { - private final MultiverseWorld world; - private final int page; - - public WorldAndPage(MultiverseWorld world, int page) { - this.world = world; - this.page = page; - } - - public MultiverseWorld getWorld() { - return world; - } - - public int getPage() { - return page; - } -}