From 637fc6f15c532645a5a428aa50a1a2ab932dcf0e Mon Sep 17 00:00:00 2001 From: Vlammar Date: Sat, 12 Dec 2020 13:47:26 +0100 Subject: [PATCH] started rewrite of rename command --- .../commands/maptool/RenameCommand.java | 71 ++++++++++++++++--- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/src/main/java/fr/moribus/imageonmap/commands/maptool/RenameCommand.java b/src/main/java/fr/moribus/imageonmap/commands/maptool/RenameCommand.java index e4b23e6..d347df7 100644 --- a/src/main/java/fr/moribus/imageonmap/commands/maptool/RenameCommand.java +++ b/src/main/java/fr/moribus/imageonmap/commands/maptool/RenameCommand.java @@ -21,27 +21,82 @@ package fr.moribus.imageonmap.commands.maptool; import fr.moribus.imageonmap.Permissions; import fr.moribus.imageonmap.commands.IoMCommand; import fr.moribus.imageonmap.map.ImageMap; +import fr.moribus.imageonmap.map.MapManager; import fr.zcraft.quartzlib.components.commands.CommandException; import fr.zcraft.quartzlib.components.commands.CommandInfo; import fr.zcraft.quartzlib.components.i18n.I; +import fr.zcraft.quartzlib.tools.PluginLogger; +import java.util.ArrayList; import java.util.List; import org.bukkit.command.CommandSender; @CommandInfo(name = "rename", usageParameters = " ") public class RenameCommand extends IoMCommand { + + private ArrayList getArgs() { + ArrayList arguments = new ArrayList<>(); + + //State of the automaton, can read word like: + //name_here; "name here" + int state = 0; + StringBuilder s = new StringBuilder(); + for (String arg : args) { + + PluginLogger.info("arg " + arg); + switch (state) { + case 0: + if (arg.startsWith("\"")) { + PluginLogger.info("start with "); + state = 1; + arg = arg.substring(1); + + s = s.append(arg); + } else { + PluginLogger.info("not start with "); + arguments.add(arg.toString()); + } + break; + case 1: + if (arg.endsWith("\"")) { + PluginLogger.info("end with "); + arg = arg.substring(0, arg.length() - 1); + s = s.append(" " + arg); + arguments.add(s.toString()); + s = new StringBuilder(); + state = 0; + } else { + PluginLogger.info("not end with "); + s = s.append(" " + arg); + } + break; + default: + throw new IllegalStateException("Unexpected value: " + state); + } + //arguments.add(arg.toString()); + + + } + return arguments; + } + @Override protected void run() throws CommandException { - if (args.length != 4) { + + ArrayList argList = getArgs(); + + if (argList.size() != 2) { warning(I.t("Not enough or too many arguments! Usage: /maptool rename ")); return; } - //if(args.length == 2) - //{ - ImageMap map = getMapFromArgs(); - map.rename(args[2]); - // } else { - // info(I.t("Not enough or too many arguments")); - // } + + + ImageMap map = MapManager.getMap(playerSender().getUniqueId(), argList.get(0)); + if (map == null) { + error(I.t("This map does not exist.")); + return; + } + map.rename(argList.get(1)); + } @Override