add magic command, fix arg parser single digit negative numbers getting interpreted as flags
This commit is contained in:
parent
f7bb1b0fe7
commit
f579eee88b
26 changed files with 142 additions and 63 deletions
|
@ -36,7 +36,7 @@ public record ArgumentSplitter(Map<String, Argument> arguments, String command,
|
|||
parse = false;
|
||||
continue;
|
||||
}
|
||||
else if(parse && (arg.startsWith("--") || (arg.startsWith("-") && arg.length() == 2))) {
|
||||
else if(parse && (arg.startsWith("--") || (arg.startsWith("-") && arg.length() == 2 && (arg.charAt(1) < '0' || arg.charAt(1) > '9')))) {
|
||||
param = parameters.get(arg.substring(arg.startsWith("--") ? 2 : 1));
|
||||
if(param != null && param.positional() && !args.containsKey(param.name())) {
|
||||
for(int n = 0; n < positionals.size(); n++) {
|
||||
|
@ -75,7 +75,7 @@ public record ArgumentSplitter(Map<String, Argument> arguments, String command,
|
|||
for(int n = pos ? 0 : 1; n < nargs + (pos ? 0 : 1); n++) {
|
||||
String par = inputs[n] = argv[z + n];
|
||||
ArgumentParser parser = param.parsers().get(apos);
|
||||
if(parse && (par.startsWith("--") || (par.startsWith("-") && par.length() == 2)))
|
||||
if(parse && (par.startsWith("--") || (par.startsWith("-") && par.length() == 2 && (par.charAt(1) < '0' || par.charAt(1) > '9'))))
|
||||
if(nargs == 1 && param.name().equals(parser.getName()))
|
||||
throw new RunException("Position %d: Argument '%s': '%s' als Parameter verwendet", z + n, param.name(), par);
|
||||
else
|
||||
|
@ -151,7 +151,7 @@ public record ArgumentSplitter(Map<String, Argument> arguments, String command,
|
|||
parse = false;
|
||||
continue;
|
||||
}
|
||||
else if(parse && (arg.startsWith("--") || (arg.startsWith("-") && arg.length() == 2))) {
|
||||
else if(parse && (arg.startsWith("--") || (arg.startsWith("-") && arg.length() == 2 && (arg.charAt(1) < '0' || arg.charAt(1) > '9')))) {
|
||||
param = parameters.get(arg.substring(arg.startsWith("--") ? 2 : 1));
|
||||
if(param != null && param.positional() && !args.contains(param.name())) {
|
||||
for(int n = 0; n < positionals.size(); n++) {
|
||||
|
@ -182,7 +182,7 @@ public record ArgumentSplitter(Map<String, Argument> arguments, String command,
|
|||
return custom != null ? custom : param.parsers().get(apos).getCompletions(env);
|
||||
}
|
||||
String par = argv[z + n];
|
||||
if(parse && (par.startsWith("--") || (par.startsWith("-") && par.length() == 2)))
|
||||
if(parse && (par.startsWith("--") || (par.startsWith("-") && par.length() == 2 && (par.charAt(1) < '0' || par.charAt(1) > '9'))))
|
||||
return null;
|
||||
apos += 1;
|
||||
}
|
||||
|
|
|
@ -192,6 +192,14 @@ public abstract class Command implements Executable {
|
|||
return this.addParameter(new PlayerEntityParser(name, defaulted, policy));
|
||||
}
|
||||
|
||||
protected Command addPlayerItem(String name, boolean defaulted, UserPolicy policy) {
|
||||
return this.addParameter(new PlayerItemParser(name, defaulted, policy));
|
||||
}
|
||||
|
||||
protected Command addPlayerItem(String name, char shortName, boolean defaulted, UserPolicy policy) {
|
||||
return this.addParameter(shortName, new PlayerItemParser(name, defaulted, policy));
|
||||
}
|
||||
|
||||
protected Command addPlayerEntityList(String name, boolean defaulted, UserPolicy policy) {
|
||||
return this.addParameter(new PlayerEntityListParser(name, defaulted, policy));
|
||||
}
|
||||
|
|
|
@ -262,5 +262,6 @@ public class CommandEnvironment {
|
|||
this.registerExecutable(new CommandRunat());
|
||||
this.registerExecutable(new CommandRunas());
|
||||
this.registerExecutable(new CommandExp());
|
||||
this.registerExecutable(new CommandMagic());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import common.util.Identifyable;
|
||||
|
||||
public abstract class CompletingParser extends ArgumentParser {
|
||||
private final List<String> defCompletions;
|
||||
|
||||
|
@ -11,7 +13,7 @@ public abstract class CompletingParser extends ArgumentParser {
|
|||
super(name);
|
||||
this.defCompletions = new ArrayList<String>(completions.length);
|
||||
for(Object comp : completions) {
|
||||
this.defCompletions.add(String.valueOf(comp));
|
||||
this.defCompletions.add(comp instanceof Identifyable id ? id.getName() : String.valueOf(comp));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package server.command;
|
|||
import java.util.Map;
|
||||
|
||||
import common.collect.Maps;
|
||||
import common.util.Identifyable;
|
||||
|
||||
public class EnumParser<T> extends DefaultingParser {
|
||||
private final Class<T> clazz;
|
||||
|
@ -14,7 +15,7 @@ public class EnumParser<T> extends DefaultingParser {
|
|||
for(T obj : iter) {
|
||||
if(sb.length() > 1)
|
||||
sb.append("', '");
|
||||
sb.append(String.valueOf(obj));
|
||||
sb.append(obj instanceof Identifyable id ? id.getName() : String.valueOf(obj));
|
||||
}
|
||||
return sb.append("'").toString();
|
||||
}
|
||||
|
@ -24,7 +25,7 @@ public class EnumParser<T> extends DefaultingParser {
|
|||
this.clazz = clazz;
|
||||
this.selections = selections;
|
||||
for(T o : selections) {
|
||||
this.lookup.put(o.toString().toLowerCase(), o);
|
||||
this.lookup.put(o instanceof Identifyable id ? id.getName() : o.toString().toLowerCase(), o);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
27
server/src/main/java/server/command/PlayerItemParser.java
Normal file
27
server/src/main/java/server/command/PlayerItemParser.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package server.command;
|
||||
|
||||
import common.entity.npc.EntityNPC;
|
||||
import common.item.ItemStack;
|
||||
|
||||
public class PlayerItemParser extends PlayerEntityParser {
|
||||
public PlayerItemParser(String name, boolean useSender, UserPolicy policy) {
|
||||
super(name, useSender, policy);
|
||||
}
|
||||
|
||||
public Object parse(CommandEnvironment env, String input) {
|
||||
EntityNPC entity = (EntityNPC)super.parse(env, input);
|
||||
ItemStack stack = entity.getHeldItem();
|
||||
if(stack == null)
|
||||
throw new RunException("%s hält keinen Gegenstand in der Hand", entity.getCommandName());
|
||||
return stack;
|
||||
}
|
||||
|
||||
public Object getDefault(CommandEnvironment env) {
|
||||
EntityNPC entity = (EntityNPC)super.getDefault(env);
|
||||
return entity == null ? null : entity.getHeldItem();
|
||||
}
|
||||
|
||||
public Class<?> getTypeClass(boolean required) {
|
||||
return ItemStack.class;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package server.command.commands;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import common.enchantment.Enchantment;
|
||||
import common.enchantment.EnchantmentHelper;
|
||||
import common.item.ItemStack;
|
||||
import server.command.Command;
|
||||
import server.command.CommandEnvironment;
|
||||
import server.command.Executor;
|
||||
import server.command.RunException;
|
||||
import server.command.UserPolicy;
|
||||
|
||||
public class CommandMagic extends Command {
|
||||
public CommandMagic() {
|
||||
super("magic");
|
||||
|
||||
this.addEnum("enchantment", Enchantment.class, Enchantment.ENCHANTMENTS);
|
||||
this.setParamsOptional();
|
||||
this.addInt("level", 1, 32767);
|
||||
this.addFlag("remove", 'r');
|
||||
this.addFlag("force", 'f');
|
||||
this.setParamsRequired();
|
||||
this.addPlayerItem("player", 'p', true, UserPolicy.NON_ADMINS_OR_SELF);
|
||||
}
|
||||
|
||||
public void exec(CommandEnvironment env, Executor exec, Enchantment ench, Integer level, boolean remove, boolean force, ItemStack stack) {
|
||||
if(remove) {
|
||||
int current = EnchantmentHelper.getEnchantmentLevel(ench.effectId, stack);
|
||||
if(current == 0)
|
||||
throw new RunException("%s hat die Verzauberung %s nicht", stack.getDisplayName(), ench.getDisplay());
|
||||
stack.removeEnchantment(ench);
|
||||
exec.log("Verzauberung %s wurde von %s entfernt", ench.getFormattedName(current), stack.getDisplayName());
|
||||
return;
|
||||
}
|
||||
level = level == null ? ench.getMaxLevel() : level;
|
||||
if(!force) {
|
||||
if(level > ench.getMaxLevel())
|
||||
throw new RunException("Level %d ist zu hoch für %s, maximal %d erlaubt", level, ench.getDisplay(), ench.getMaxLevel());
|
||||
if(!ench.canApply(stack))
|
||||
throw new RunException("Verzauberung %s kann nicht auf %s verwendet werden", ench.getDisplay(), stack.getDisplayName());
|
||||
for(Entry<Integer, Integer> id : EnchantmentHelper.getEnchantments(stack).entrySet()) {
|
||||
Enchantment enc = Enchantment.getEnchantmentById(id.getKey());
|
||||
if(enc != ench && !ench.canApplyTogether(enc))
|
||||
throw new RunException("Verzauberung %s kann nicht zusammen mit %s verwendet werden", ench.getDisplay(), enc.getFormattedName(id.getValue()));
|
||||
}
|
||||
}
|
||||
stack.removeEnchantment(ench);
|
||||
stack.addEnchantment(ench, level);
|
||||
exec.log("Verzauberung %s wurde zu %s hinzugefügt", ench.getFormattedName(level), stack.getDisplayName());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue