Clean up.

This commit is contained in:
benwoo1110 2020-12-17 20:58:34 +08:00
parent 7493ce5dd8
commit a77ad6d7ab
14 changed files with 93 additions and 78 deletions

View File

@ -34,6 +34,7 @@ import com.onarandombox.MultiverseCore.api.MultiverseMessaging;
import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
import com.onarandombox.MultiverseCore.commands_acf.CoordCommand; import com.onarandombox.MultiverseCore.commands_acf.CoordCommand;
import com.onarandombox.MultiverseCore.commands_acf.CreateCommand; 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.ListCommand;
import com.onarandombox.MultiverseCore.commands_acf.ReloadCommand; import com.onarandombox.MultiverseCore.commands_acf.ReloadCommand;
import com.onarandombox.MultiverseCore.commands_acf.RemoveCommand; 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 RemoveCommand(this));
this.commandHandler.registerCommand(new ListCommand(this)); this.commandHandler.registerCommand(new ListCommand(this));
this.commandHandler.registerCommand(new ScriptCommand(this)); this.commandHandler.registerCommand(new ScriptCommand(this));
this.commandHandler.registerCommand(new GamerulesCommand(this));
} }
/** /**

View File

@ -12,6 +12,8 @@ import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
@CommandAlias("mv") @CommandAlias("mv")
@ -26,20 +28,18 @@ public class ConfigCommand extends MultiverseCommand {
@Subcommand("show") @Subcommand("show")
@Description("Show multiverse config values.") @Description("Show multiverse config values.")
public void onShowCommand(@NotNull CommandSender sender) { public void onShowCommand(@NotNull CommandSender sender) {
StringBuilder builder = new StringBuilder(); List<String> configList = new ArrayList<>();
Map<String, Object> serializedConfig = this.plugin.getMVConfig().serialize(); Map<String, Object> serializedConfig = this.plugin.getMVConfig().serialize();
for (Map.Entry<String, Object> entry : serializedConfig.entrySet()) { for (Map.Entry<String, Object> entry : serializedConfig.entrySet()) {
builder.append(ChatColor.GREEN)
.append(entry.getKey()) configList.add(ChatColor.GREEN + entry.getKey()
.append(ChatColor.WHITE).append(" = ").append(ChatColor.GOLD) + ChatColor.WHITE + " = "
.append(entry.getValue().toString()) + ChatColor.GOLD + entry.getValue().toString()
.append(ChatColor.WHITE).append(", "); + ChatColor.WHITE);
} }
String message = builder.toString(); sender.sendMessage(String.join(", ", configList));
message = message.substring(0, message.length() - 2);
sender.sendMessage(message);
} }
@Subcommand("set") @Subcommand("set")
@ -58,7 +58,7 @@ public class ConfigCommand extends MultiverseCommand {
} }
if (!this.plugin.saveMVConfigs()) { 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; return;
} }
@ -68,6 +68,7 @@ public class ConfigCommand extends MultiverseCommand {
this.plugin.getMVWorldManager().setFirstSpawnWorld(value); this.plugin.getMVWorldManager().setFirstSpawnWorld(value);
} }
//TODO: Show properties and values that where updated.
sender.sendMessage(ChatColor.GREEN + "SUCCESS!" + ChatColor.WHITE + " Values were updated successfully!"); sender.sendMessage(ChatColor.GREEN + "SUCCESS!" + ChatColor.WHITE + " Values were updated successfully!");
this.plugin.loadConfigs(); this.plugin.loadConfigs();
} }

View File

@ -48,6 +48,7 @@ public class DebugCommand extends MultiverseCommand {
displayDebugMode(sender); displayDebugMode(sender);
} }
//TODO: See if can move this to CommandContext Integer.class
private int parseDebugLevel(@NotNull String debugLevel) { private int parseDebugLevel(@NotNull String debugLevel) {
if (debugLevel.equalsIgnoreCase("off")) { if (debugLevel.equalsIgnoreCase("off")) {
return 0; return 0;

View File

@ -3,8 +3,10 @@ package com.onarandombox.MultiverseCore.commands_acf;
import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Conditions;
import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags; import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Single;
import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax; import co.aikar.commands.annotation.Syntax;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
@ -23,22 +25,22 @@ public class DeleteCommand extends MultiverseCommand {
@Subcommand("delete") @Subcommand("delete")
@CommandPermission("multiverse.core.delete") @CommandPermission("multiverse.core.delete")
@Syntax("<world>") @Syntax("<world>")
@CommandCompletion("@MVWorlds") @CommandCompletion("@MVWorlds|@unloadedWorlds")
@Description("") @Description("")
public void onDeleteCommand(@NotNull CommandSender sender, 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, private Runnable deleteRunnable(@NotNull CommandSender sender,
@NotNull MultiverseWorld world) { @NotNull String worldName) {
return () -> { return () -> {
//TODO: deleteWorld method should take world object directly //TODO: deleteWorld method should take world object directly
String resultMessage = (this.plugin.getMVWorldManager().deleteWorld(world.getName())) String resultMessage = (this.plugin.getMVWorldManager().deleteWorld(worldName))
? ChatColor.GREEN + "World '" + world.getName() + "' Deleted!" ? ChatColor.GREEN + "World '" + worldName + "' Deleted!"
: ChatColor.RED + "World '" + world.getName() + "' could NOT be deleted!"; : ChatColor.RED + "World '" + worldName + "' could NOT be deleted!";
sender.sendMessage(resultMessage); sender.sendMessage(resultMessage);
}; };

View File

@ -10,7 +10,6 @@ import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax; import co.aikar.commands.annotation.Syntax;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.commands_helper.WorldAndPage;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;

View File

@ -25,6 +25,7 @@ public class ListCommand extends MultiverseCommand {
public void onListCommand(@NotNull CommandSender sender, public void onListCommand(@NotNull CommandSender sender,
@Default("1") int page) { @Default("1") int page) {
//TODO: Do the actual fancy list display
sender.sendMessage(this.plugin.getMVWorldManager().getMVWorlds().toString()); sender.sendMessage(this.plugin.getMVWorldManager().getMVWorlds().toString());
sender.sendMessage("Page of: " + page); sender.sendMessage("Page of: " + page);
} }

View File

@ -3,7 +3,9 @@ package com.onarandombox.MultiverseCore.commands_acf;
import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Conditions;
import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Single; import co.aikar.commands.annotation.Single;
import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax; import co.aikar.commands.annotation.Syntax;
@ -26,12 +28,12 @@ public class LoadCommand extends MultiverseCommand {
@CommandCompletion("@unloadedWorlds") @CommandCompletion("@unloadedWorlds")
@Description("Loads a world into Multiverse.") @Description("Loads a world into Multiverse.")
public void onLoadCommand(@NotNull CommandSender sender, public void onLoadCommand(@NotNull CommandSender sender,
@NotNull @Single String world) { @NotNull @Single @Conditions("isUnloadedWorld") String worldName) {
if (!this.plugin.getMVWorldManager().loadWorld(world)) { if (!this.plugin.getMVWorldManager().loadWorld(worldName)) {
sender.sendMessage("Error trying to load world '" + world + "'!"); sender.sendMessage("Error trying to load world '" + worldName + "'!");
return; return;
} }
Command.broadcastCommandMessage(sender, "Loaded world '" + world + "'!"); Command.broadcastCommandMessage(sender, "Loaded world '" + worldName + "'!");
} }
} }

View File

@ -29,7 +29,7 @@ public class ReloadCommand extends MultiverseCommand {
this.plugin.getAnchorManager().loadAnchors(); this.plugin.getAnchorManager().loadAnchors();
this.plugin.getMVWorldManager().loadWorlds(true); this.plugin.getMVWorldManager().loadWorlds(true);
List<String> configsLoaded = new ArrayList<String>(); List<String> configsLoaded = new ArrayList<>();
configsLoaded.add("Multiverse-Core - config.yml"); configsLoaded.add("Multiverse-Core - config.yml");
configsLoaded.add("Multiverse-Core - worlds.yml"); configsLoaded.add("Multiverse-Core - worlds.yml");
configsLoaded.add("Multiverse-Core - anchors.yml"); configsLoaded.add("Multiverse-Core - anchors.yml");
@ -37,10 +37,7 @@ public class ReloadCommand extends MultiverseCommand {
MVConfigReloadEvent configReload = new MVConfigReloadEvent(configsLoaded); MVConfigReloadEvent configReload = new MVConfigReloadEvent(configsLoaded);
this.plugin.getServer().getPluginManager().callEvent(configReload); this.plugin.getServer().getPluginManager().callEvent(configReload);
for (String s : configReload.getAllConfigsLoaded()) { configReload.getAllConfigsLoaded().forEach(sender::sendMessage);
sender.sendMessage(s);
}
sender.sendMessage(ChatColor.GREEN + "Reload Complete!"); sender.sendMessage(ChatColor.GREEN + "Reload Complete!");
} }
} }

View File

@ -3,12 +3,12 @@ package com.onarandombox.MultiverseCore.commands_acf;
import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Conditions;
import co.aikar.commands.annotation.Description; 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.Subcommand;
import co.aikar.commands.annotation.Syntax; import co.aikar.commands.annotation.Syntax;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -25,12 +25,10 @@ public class RemoveCommand extends MultiverseCommand {
@Syntax("<world>") @Syntax("<world>")
@Description("Unloads a world from Multiverse and removes it from worlds.yml, this does NOT DELETE the world folder.") @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, 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!" ? "World removed from config!"
: "Error trying to remove world from config!"; : "Error trying to remove world from config!");
sender.sendMessage(resultMessage);
} }
} }

View File

@ -45,7 +45,7 @@ public class SpawnCommand extends MultiverseCommand {
spawnAccurately(player); spawnAccurately(player);
String senderName = (sender instanceof ConsoleCommandSender) String senderName = (sender instanceof ConsoleCommandSender)
? ChatColor.LIGHT_PURPLE + "the console" ? ChatColor.LIGHT_PURPLE + "console"
: ChatColor.YELLOW + sender.getName(); : ChatColor.YELLOW + sender.getName();
player.sendMessage("You were teleported by " + senderName); player.sendMessage("You were teleported by " + senderName);

View File

@ -34,9 +34,9 @@ public class CommandQueueManager {
cancelPreviousInQueue(sender); cancelPreviousInQueue(sender);
QueuedCommand queuedCommand = new QueuedCommand(sender, runnable, validPeriod); QueuedCommand queuedCommand = new QueuedCommand(sender, runnable);
queuedCommands.put(sender, queuedCommand); 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("The command you are trying to run is deemed dangerous.");
sender.sendMessage("Run /mv confirm to continue."); sender.sendMessage("Run /mv confirm to continue.");
@ -47,15 +47,16 @@ public class CommandQueueManager {
if (previousCommand == null) { if (previousCommand == null) {
return; return;
} }
previousCommand.cancelExpiryTask(); previousCommand.cancelExpiryTask();
queuedCommands.remove(sender); queuedCommands.remove(sender);
} }
private BukkitTask runExpireLater(@NotNull QueuedCommand queuedCommand) { private BukkitTask runExpireLater(@NotNull QueuedCommand queuedCommand, int validPeriod) {
return Bukkit.getScheduler().runTaskLater( return Bukkit.getScheduler().runTaskLater(
this.plugin, this.plugin,
expireRunnable(queuedCommand), expireRunnable(queuedCommand),
queuedCommand.getValidInterval() validPeriod
); );
} }
@ -66,8 +67,9 @@ public class CommandQueueManager {
Logging.finer("This is an old command already."); Logging.finer("This is an old command already.");
return; return;
} }
Logging.finer("Command is expired, removing...");
this.queuedCommands.remove(queuedCommand.getSender()); this.queuedCommands.remove(queuedCommand.getSender());
Logging.finer("Command expired and is removed.");
}; };
} }

View File

@ -66,9 +66,10 @@ public class CommandTools {
this::suggestPotentialWorlds this::suggestPotentialWorlds
); );
this.commandHandler.getCommandCompletions().registerStaticCompletion( //TODO: Change to static
this.commandHandler.getCommandCompletions().registerAsyncCompletion(
"MVConfigs", "MVConfigs",
suggestMVConfig() this::suggestMVConfig
); );
this.commandHandler.getCommandCompletions().registerStaticCompletion( this.commandHandler.getCommandCompletions().registerStaticCompletion(
@ -139,7 +140,7 @@ public class CommandTools {
@NotNull @NotNull
private Set<String> suggestMVConfig() { private Set<String> suggestMVConfig(@NotNull BukkitCommandCompletionContext context) {
return this.plugin.getMVConfig().serialize().keySet(); return this.plugin.getMVConfig().serialize().keySet();
} }
@ -509,6 +510,18 @@ public class CommandTools {
this::checkIsMVWorld this::checkIsMVWorld
); );
this.commandHandler.getCommandConditions().addCondition(
String.class,
"isUnloadedWorld",
this::checkIsUnloadedWorld
);
this.commandHandler.getCommandConditions().addCondition(
String.class,
"isWorldInConfig",
this::checkIsWorldInConfig
);
this.commandHandler.getCommandConditions().addCondition( this.commandHandler.getCommandConditions().addCondition(
String.class, String.class,
"worldFolderExist", "worldFolderExist",
@ -522,12 +535,13 @@ public class CommandTools {
); );
} }
//TODO: Message seems a bit too targeted to create world only
private void checkIsMVWorld(@NotNull ConditionContext<BukkitCommandIssuer> context, private void checkIsMVWorld(@NotNull ConditionContext<BukkitCommandIssuer> context,
@NotNull BukkitCommandExecutionContext executionContext, @NotNull BukkitCommandExecutionContext executionContext,
@NotNull String worldName) { @NotNull String worldName) {
boolean shouldBeMVWorld = Boolean.parseBoolean(context.getConfig()); boolean shouldBeMVWorld = Boolean.parseBoolean(context.getConfig());
boolean isMVWorld = this.plugin.getMVWorldManager().isMVWorld(worldName); boolean isMVWorld = this.worldManager.isMVWorld(worldName);
if (isMVWorld && !shouldBeMVWorld) { if (isMVWorld && !shouldBeMVWorld) {
executionContext.getSender().sendMessage(ChatColor.RED + "Multiverse cannot create " + ChatColor.GOLD + ChatColor.UNDERLINE executionContext.getSender().sendMessage(ChatColor.RED + "Multiverse cannot create " + ChatColor.GOLD + ChatColor.UNDERLINE
@ -541,9 +555,32 @@ public class CommandTools {
} }
} }
private void checkWorldFolderExist(@NotNull ConditionContext<BukkitCommandIssuer> context, private void checkIsUnloadedWorld(@NotNull ConditionContext<BukkitCommandIssuer> context,
@NotNull BukkitCommandExecutionContext executionContext, @NotNull BukkitCommandExecutionContext executionContext,
@NotNull String worldFolder) { @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<BukkitCommandIssuer> 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<BukkitCommandIssuer> context,
@NotNull BukkitCommandExecutionContext executionContext,
@NotNull String worldFolder) {
boolean shouldExist = Boolean.parseBoolean(context.getConfig()); boolean shouldExist = Boolean.parseBoolean(context.getConfig());
boolean worldFileExist = new File(this.plugin.getServer().getWorldContainer(), worldFolder).exists(); boolean worldFileExist = new File(this.plugin.getServer().getWorldContainer(), worldFolder).exists();
@ -560,9 +597,9 @@ public class CommandTools {
} }
} }
private void checkValidWorldFolder (@NotNull ConditionContext<BukkitCommandIssuer> context, private void checkValidWorldFolder(@NotNull ConditionContext<BukkitCommandIssuer> context,
@NotNull BukkitCommandExecutionContext executionContext, @NotNull BukkitCommandExecutionContext executionContext,
@NotNull String worldName) { @NotNull String worldName) {
File worldFolder = new File(this.plugin.getServer().getWorldContainer(), worldName); File worldFolder = new File(this.plugin.getServer().getWorldContainer(), worldName);
if (!worldFolder.isDirectory()) { if (!worldFolder.isDirectory()) {

View File

@ -6,13 +6,11 @@ import org.bukkit.scheduler.BukkitTask;
public class QueuedCommand { public class QueuedCommand {
private final CommandSender sender; private final CommandSender sender;
private final Runnable runnable; private final Runnable runnable;
private final int validInterval;
private BukkitTask expireTask; private BukkitTask expireTask;
public QueuedCommand(CommandSender sender, Runnable runnable, int validPeriod) { public QueuedCommand(CommandSender sender, Runnable runnable) {
this.sender = sender; this.sender = sender;
this.runnable = runnable; this.runnable = runnable;
this.validInterval = validPeriod;
} }
public void runCommand() { public void runCommand() {
@ -31,8 +29,4 @@ public class QueuedCommand {
public CommandSender getSender() { public CommandSender getSender() {
return sender; return sender;
} }
public int getValidInterval() {
return validInterval;
}
} }

View File

@ -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;
}
}