started rewrite of rename command

This commit is contained in:
Vlammar 2020-12-12 13:47:26 +01:00
parent cc58b1d320
commit 637fc6f15c

View File

@ -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 = "<original map name> <new map name>")
public class RenameCommand extends IoMCommand {
private ArrayList<String> getArgs() {
ArrayList<String> 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<String> argList = getArgs();
if (argList.size() != 2) {
warning(I.t("Not enough or too many arguments! Usage: /maptool rename <map name> <new map name>"));
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