add magic command, fix arg parser single digit negative numbers getting interpreted as flags

This commit is contained in:
Sen 2025-06-17 11:41:25 +02:00
parent f7bb1b0fe7
commit f579eee88b
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
26 changed files with 142 additions and 63 deletions

View file

@ -10,26 +10,23 @@ import common.entity.DamageSource;
import common.entity.Entity; import common.entity.Entity;
import common.entity.types.EntityLiving; import common.entity.types.EntityLiving;
import common.item.ItemStack; import common.item.ItemStack;
import common.util.Displayable;
import common.util.Identifyable;
public abstract class Enchantment public abstract class Enchantment implements Displayable, Identifyable
{ {
private static final String[] LEVELS = new String[] {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"}; private static final String[] LEVELS = new String[] {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
private static final Enchantment[] enchantmentsList = new Enchantment[256]; private static final Enchantment[] LOOKUP = new Enchantment[256];
public static final Enchantment[] enchantmentsBookList; public static final Enchantment[] ENCHANTMENTS;
private static final Map<String, Enchantment> locationEnchantments = Maps.<String, Enchantment>newHashMap(); private static final Map<String, Enchantment> NAME_LOOKUP = Maps.<String, Enchantment>newHashMap();
public static final Enchantment protection = new EnchantmentProtection(0, "protection", 10, 0); public static final Enchantment protection = new EnchantmentProtection(0, "protection", 10, 0);
public static final Enchantment fireProtection = new EnchantmentProtection(1, "fire_protection", 5, 1); public static final Enchantment fireProtection = new EnchantmentProtection(1, "fire_protection", 5, 1);
public static final Enchantment featherFalling = new EnchantmentProtection(2, "feather_falling", 5, 2); public static final Enchantment featherFalling = new EnchantmentProtection(2, "feather_falling", 5, 2);
public static final Enchantment blastProtection = new EnchantmentProtection(3, "blast_protection", 2, 3); public static final Enchantment blastProtection = new EnchantmentProtection(3, "blast_protection", 2, 3);
public static final Enchantment projectileProtection = new EnchantmentProtection(4, "projectile_protection", 5, 4); public static final Enchantment projectileProtection = new EnchantmentProtection(4, "projectile_protection", 5, 4);
// public static final Enchantment respiration = new EnchantmentOxygen(5, "respiration", 2);
// public static final Enchantment aquaAffinity = new EnchantmentWaterWorker(6, "aqua_affinity", 2);
public static final Enchantment thorns = new EnchantmentThorns(7, "thorns", 1); public static final Enchantment thorns = new EnchantmentThorns(7, "thorns", 1);
// public static final Enchantment depthStrider = new EnchantmentWaterWalker(8, "depth_strider", 2);
public static final Enchantment sharpness = new EnchantmentDamage(16, "sharpness", 10); public static final Enchantment sharpness = new EnchantmentDamage(16, "sharpness", 10);
// public static final Enchantment smite = new EnchantmentDamage(17, "smite", 5, 1);
// public static final Enchantment baneOfArthropods = new EnchantmentDamage(18, "bane_of_arthropods", 5, 2);
public static final Enchantment knockback = new EnchantmentKnockback(19, "knockback", 5); public static final Enchantment knockback = new EnchantmentKnockback(19, "knockback", 5);
public static final Enchantment fireAspect = new EnchantmentFireAspect(20, "fire_aspect", 2); public static final Enchantment fireAspect = new EnchantmentFireAspect(20, "fire_aspect", 2);
public static final Enchantment looting = new EnchantmentLootBonus(21, "looting", 2, EnumEnchantmentType.WEAPON); public static final Enchantment looting = new EnchantmentLootBonus(21, "looting", 2, EnumEnchantmentType.WEAPON);
@ -43,40 +40,35 @@ public abstract class Enchantment
public static final Enchantment infinity = new EnchantmentArrowInfinite(51, "infinity", 1); public static final Enchantment infinity = new EnchantmentArrowInfinite(51, "infinity", 1);
public static final Enchantment luckOfTheSea = new EnchantmentLootBonus(61, "luck_of_the_sea", 2, EnumEnchantmentType.FISHING_ROD); public static final Enchantment luckOfTheSea = new EnchantmentLootBonus(61, "luck_of_the_sea", 2, EnumEnchantmentType.FISHING_ROD);
public static final Enchantment lure = new EnchantmentFishingSpeed(62, "lure", 2, EnumEnchantmentType.FISHING_ROD); public static final Enchantment lure = new EnchantmentFishingSpeed(62, "lure", 2, EnumEnchantmentType.FISHING_ROD);
public static final Enchantment draining = new EnchantmentDraining(64, "draining", 1); public static final Enchantment draining = new EnchantmentDraining(64, "draining", 1);
public final int effectId; public final int effectId;
private final String id;
private final int weight; private final int weight;
/** The EnumEnchantmentType given to this Enchantment. */
public EnumEnchantmentType type; public EnumEnchantmentType type;
/** Used in localisation and stats. */
protected String name; protected String name;
/**
* Retrieves an Enchantment from the enchantmentsList
*/
public static Enchantment getEnchantmentById(int enchID) public static Enchantment getEnchantmentById(int enchID)
{ {
return enchID >= 0 && enchID < enchantmentsList.length ? enchantmentsList[enchID] : null; return enchID >= 0 && enchID < LOOKUP.length ? LOOKUP[enchID] : null;
} }
protected Enchantment(int enchID, String enchName, int enchWeight, EnumEnchantmentType enchType) protected Enchantment(int enchID, String enchName, int enchWeight, EnumEnchantmentType enchType)
{ {
this.effectId = enchID; this.effectId = enchID;
this.id = enchName;
this.weight = enchWeight; this.weight = enchWeight;
this.type = enchType; this.type = enchType;
if (enchantmentsList[enchID] != null) if (LOOKUP[enchID] != null)
{ {
throw new IllegalArgumentException("Duplicate enchantment id!"); throw new IllegalArgumentException("Duplicate enchantment id!");
} }
else else
{ {
enchantmentsList[enchID] = this; LOOKUP[enchID] = this;
locationEnchantments.put(enchName, this); NAME_LOOKUP.put(enchName, this);
} }
} }
@ -85,12 +77,12 @@ public abstract class Enchantment
*/ */
public static Enchantment getEnchantmentByLocation(String location) public static Enchantment getEnchantmentByLocation(String location)
{ {
return locationEnchantments.get(location); return NAME_LOOKUP.get(location);
} }
public static Set<String> getNames() public static Set<String> getNames()
{ {
return locationEnchantments.keySet(); return NAME_LOOKUP.keySet();
} }
/** /**
@ -159,26 +151,27 @@ public abstract class Enchantment
return this != ench; return this != ench;
} }
/** public Enchantment setDisplay(String enchName)
* Sets the enchantment name
*/
public Enchantment setName(String enchName)
{ {
this.name = enchName; this.name = enchName;
return this; return this;
} }
public final String getName() {
return this.id;
}
/** /**
* Return the name of key of this enchantment. * Return the name of key of this enchantment.
*/ */
public String getName() public String getDisplay()
{ {
return this.name; return this.name;
} }
public String getFormattedName(int level) public String getFormattedName(int level)
{ {
return this.getName() + " " + ((level >= 1 && level <= 10) ? LEVELS[level - 1] : "" + level); return this.getDisplay() + " " + ((level >= 1 && level <= LEVELS.length) ? LEVELS[level - 1] : "" + level);
} }
/** /**
@ -208,7 +201,7 @@ public abstract class Enchantment
{ {
List<Enchantment> list = Lists.<Enchantment>newArrayList(); List<Enchantment> list = Lists.<Enchantment>newArrayList();
for (Enchantment enchantment : enchantmentsList) for (Enchantment enchantment : LOOKUP)
{ {
if (enchantment != null) if (enchantment != null)
{ {
@ -216,6 +209,6 @@ public abstract class Enchantment
} }
} }
enchantmentsBookList = (Enchantment[])list.toArray(new Enchantment[list.size()]); ENCHANTMENTS = (Enchantment[])list.toArray(new Enchantment[list.size()]);
} }
} }

View file

@ -7,7 +7,7 @@ public class EnchantmentArrowDamage extends Enchantment
public EnchantmentArrowDamage(int enchID, String enchName, int enchWeight) public EnchantmentArrowDamage(int enchID, String enchName, int enchWeight)
{ {
super(enchID, enchName, enchWeight, EnumEnchantmentType.BOW); super(enchID, enchName, enchWeight, EnumEnchantmentType.BOW);
this.setName("Stärke"); this.setDisplay("Stärke");
} }
/** /**

View file

@ -7,7 +7,7 @@ public class EnchantmentArrowFire extends Enchantment
public EnchantmentArrowFire(int enchID, String enchName, int enchWeight) public EnchantmentArrowFire(int enchID, String enchName, int enchWeight)
{ {
super(enchID, enchName, enchWeight, EnumEnchantmentType.BOW); super(enchID, enchName, enchWeight, EnumEnchantmentType.BOW);
this.setName("Flamme"); this.setDisplay("Flamme");
} }
/** /**

View file

@ -7,7 +7,7 @@ public class EnchantmentArrowInfinite extends Enchantment
public EnchantmentArrowInfinite(int enchID, String enchName, int enchWeight) public EnchantmentArrowInfinite(int enchID, String enchName, int enchWeight)
{ {
super(enchID, enchName, enchWeight, EnumEnchantmentType.BOW); super(enchID, enchName, enchWeight, EnumEnchantmentType.BOW);
this.setName("Unendlich"); this.setDisplay("Unendlich");
} }
/** /**

View file

@ -7,7 +7,7 @@ public class EnchantmentArrowKnockback extends Enchantment
public EnchantmentArrowKnockback(int enchID, String enchName, int enchWeight) public EnchantmentArrowKnockback(int enchID, String enchName, int enchWeight)
{ {
super(enchID, enchName, enchWeight, EnumEnchantmentType.BOW); super(enchID, enchName, enchWeight, EnumEnchantmentType.BOW);
this.setName("Schlag"); this.setDisplay("Schlag");
} }
/** /**

View file

@ -9,7 +9,7 @@ public class EnchantmentDamage extends Enchantment
public EnchantmentDamage(int enchID, String enchName, int enchWeight) public EnchantmentDamage(int enchID, String enchName, int enchWeight)
{ {
super(enchID, enchName, enchWeight, EnumEnchantmentType.WEAPON); super(enchID, enchName, enchWeight, EnumEnchantmentType.WEAPON);
this.setName("Schärfe"); this.setDisplay("Schärfe");
} }
/** /**

View file

@ -9,7 +9,7 @@ public class EnchantmentDigging extends Enchantment
protected EnchantmentDigging(int enchID, String enchName, int enchWeight) protected EnchantmentDigging(int enchID, String enchName, int enchWeight)
{ {
super(enchID, enchName, enchWeight, EnumEnchantmentType.DIGGER); super(enchID, enchName, enchWeight, EnumEnchantmentType.DIGGER);
this.setName("Effizienz"); this.setDisplay("Effizienz");
} }
/** /**

View file

@ -10,7 +10,7 @@ import common.item.ItemStack;
public class EnchantmentDraining extends Enchantment { public class EnchantmentDraining extends Enchantment {
public EnchantmentDraining(int enchID, String enchName, int enchWeight) { public EnchantmentDraining(int enchID, String enchName, int enchWeight) {
super(enchID, enchName, enchWeight, EnumEnchantmentType.WEAPON); super(enchID, enchName, enchWeight, EnumEnchantmentType.WEAPON);
this.setName("Seelenentzug"); this.setDisplay("Seelenentzug");
} }
public int getMinEnchantability(int enchantmentLevel) { public int getMinEnchantability(int enchantmentLevel) {

View file

@ -10,7 +10,7 @@ public class EnchantmentDurability extends Enchantment
protected EnchantmentDurability(int enchID, String enchName, int enchWeight) protected EnchantmentDurability(int enchID, String enchName, int enchWeight)
{ {
super(enchID, enchName, enchWeight, EnumEnchantmentType.BREAKABLE); super(enchID, enchName, enchWeight, EnumEnchantmentType.BREAKABLE);
this.setName("Haltbarkeit"); this.setDisplay("Haltbarkeit");
} }
/** /**

View file

@ -7,7 +7,7 @@ public class EnchantmentFireAspect extends Enchantment
protected EnchantmentFireAspect(int enchID, String enchName, int enchWeight) protected EnchantmentFireAspect(int enchID, String enchName, int enchWeight)
{ {
super(enchID, enchName, enchWeight, EnumEnchantmentType.WEAPON); super(enchID, enchName, enchWeight, EnumEnchantmentType.WEAPON);
this.setName("Verbrennung"); this.setDisplay("Verbrennung");
} }
/** /**

View file

@ -7,7 +7,7 @@ public class EnchantmentFishingSpeed extends Enchantment
protected EnchantmentFishingSpeed(int enchID, String enchName, int enchWeight, EnumEnchantmentType enchType) protected EnchantmentFishingSpeed(int enchID, String enchName, int enchWeight, EnumEnchantmentType enchType)
{ {
super(enchID, enchName, enchWeight, enchType); super(enchID, enchName, enchWeight, enchType);
this.setName("Köder"); this.setDisplay("Köder");
} }
/** /**

View file

@ -483,7 +483,7 @@ public class EnchantmentHelper
Map<Integer, RngEnchantment> map = null; Map<Integer, RngEnchantment> map = null;
boolean flag = p_77505_1_.getItem() == Items.book; boolean flag = p_77505_1_.getItem() == Items.book;
for (Enchantment enchantment : Enchantment.enchantmentsBookList) for (Enchantment enchantment : Enchantment.ENCHANTMENTS)
{ {
if (enchantment != null && (enchantment.type.canEnchantItem(item) || flag)) if (enchantment != null && (enchantment.type.canEnchantItem(item) || flag))
{ {

View file

@ -7,7 +7,7 @@ public class EnchantmentKnockback extends Enchantment
protected EnchantmentKnockback(int p_i45768_1_, String p_i45768_2_, int p_i45768_3_) protected EnchantmentKnockback(int p_i45768_1_, String p_i45768_2_, int p_i45768_3_)
{ {
super(p_i45768_1_, p_i45768_2_, p_i45768_3_, EnumEnchantmentType.WEAPON); super(p_i45768_1_, p_i45768_2_, p_i45768_3_, EnumEnchantmentType.WEAPON);
this.setName("Rückstoß"); this.setDisplay("Rückstoß");
} }
/** /**

View file

@ -10,15 +10,15 @@ public class EnchantmentLootBonus extends Enchantment
if (p_i45767_4_ == EnumEnchantmentType.DIGGER) if (p_i45767_4_ == EnumEnchantmentType.DIGGER)
{ {
this.setName("Glück"); this.setDisplay("Glück");
} }
else if (p_i45767_4_ == EnumEnchantmentType.FISHING_ROD) else if (p_i45767_4_ == EnumEnchantmentType.FISHING_ROD)
{ {
this.setName("Glück des Meeres"); this.setDisplay("Glück des Meeres");
} }
else else
{ {
this.setName("Plünderung"); this.setDisplay("Plünderung");
} }
} }

View file

@ -33,13 +33,8 @@ public class EnchantmentProtection extends Enchantment
public EnchantmentProtection(int id, String name, int weight, int type) public EnchantmentProtection(int id, String name, int weight, int type)
{ {
super(id, name, weight, EnumEnchantmentType.ARMOR); super(id, name, weight, type == 2 ? EnumEnchantmentType.ARMOR_FEET : EnumEnchantmentType.ARMOR);
this.protectionType = type; this.protectionType = type;
if (type == 2)
{
this.type = EnumEnchantmentType.ARMOR_FEET;
}
} }
/** /**
@ -82,7 +77,7 @@ public class EnchantmentProtection extends Enchantment
} }
} }
public String getName() public String getDisplay()
{ {
return NAMES[this.protectionType]; return NAMES[this.protectionType];
} }

View file

@ -13,7 +13,7 @@ public class EnchantmentThorns extends Enchantment
public EnchantmentThorns(int p_i45764_1_, String p_i45764_2_, int p_i45764_3_) public EnchantmentThorns(int p_i45764_1_, String p_i45764_2_, int p_i45764_3_)
{ {
super(p_i45764_1_, p_i45764_2_, p_i45764_3_, EnumEnchantmentType.ARMOR_TORSO); super(p_i45764_1_, p_i45764_2_, p_i45764_3_, EnumEnchantmentType.ARMOR_TORSO);
this.setName("Dornen"); this.setDisplay("Dornen");
} }
/** /**

View file

@ -9,7 +9,7 @@ public class EnchantmentUntouching extends Enchantment
protected EnchantmentUntouching(int p_i45763_1_, String p_i45763_2_, int p_i45763_3_) protected EnchantmentUntouching(int p_i45763_1_, String p_i45763_2_, int p_i45763_3_)
{ {
super(p_i45763_1_, p_i45763_2_, p_i45763_3_, EnumEnchantmentType.DIGGER); super(p_i45763_1_, p_i45763_2_, p_i45763_3_, EnumEnchantmentType.DIGGER);
this.setName("Behutsamkeit"); this.setDisplay("Behutsamkeit");
} }
/** /**

View file

@ -179,7 +179,7 @@ public abstract class TradeRegistry {
static class BookForGem implements ITradeList { static class BookForGem implements ITradeList {
public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random) { public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random) {
Enchantment enchantment = random.pick(Enchantment.enchantmentsBookList); Enchantment enchantment = random.pick(Enchantment.ENCHANTMENTS);
int i = random.range(enchantment.getMinLevel(), enchantment.getMaxLevel()); int i = random.range(enchantment.getMinLevel(), enchantment.getMaxLevel());
ItemStack itemstack = Items.enchanted_book.getEnchantedItemStack(new RngEnchantment(enchantment, i)); ItemStack itemstack = Items.enchanted_book.getEnchantedItemStack(new RngEnchantment(enchantment, i));
int j = 2 + random.zrange(5 + i * 10) + 3 * i; int j = 2 + random.zrange(5 + i * 10) + 3 * i;

View file

@ -34,7 +34,7 @@ public class ItemEnchantedBook extends Item
public void getSubItems(Item itemIn, CheatTab tab, List<ItemStack> subItems) public void getSubItems(Item itemIn, CheatTab tab, List<ItemStack> subItems)
{ {
for (Enchantment enchantment : Enchantment.enchantmentsBookList) { for (Enchantment enchantment : Enchantment.ENCHANTMENTS) {
if(enchantment != null && enchantment.type != null) if(enchantment != null && enchantment.type != null)
subItems.add(Items.enchanted_book.getEnchantedItemStack(new RngEnchantment(enchantment, enchantment.getMaxLevel()))); subItems.add(Items.enchanted_book.getEnchantedItemStack(new RngEnchantment(enchantment, enchantment.getMaxLevel())));
} }

View file

@ -36,7 +36,7 @@ public record ArgumentSplitter(Map<String, Argument> arguments, String command,
parse = false; parse = false;
continue; 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)); param = parameters.get(arg.substring(arg.startsWith("--") ? 2 : 1));
if(param != null && param.positional() && !args.containsKey(param.name())) { if(param != null && param.positional() && !args.containsKey(param.name())) {
for(int n = 0; n < positionals.size(); n++) { 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++) { for(int n = pos ? 0 : 1; n < nargs + (pos ? 0 : 1); n++) {
String par = inputs[n] = argv[z + n]; String par = inputs[n] = argv[z + n];
ArgumentParser parser = param.parsers().get(apos); 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())) if(nargs == 1 && param.name().equals(parser.getName()))
throw new RunException("Position %d: Argument '%s': '%s' als Parameter verwendet", z + n, param.name(), par); throw new RunException("Position %d: Argument '%s': '%s' als Parameter verwendet", z + n, param.name(), par);
else else
@ -151,7 +151,7 @@ public record ArgumentSplitter(Map<String, Argument> arguments, String command,
parse = false; parse = false;
continue; 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)); param = parameters.get(arg.substring(arg.startsWith("--") ? 2 : 1));
if(param != null && param.positional() && !args.contains(param.name())) { if(param != null && param.positional() && !args.contains(param.name())) {
for(int n = 0; n < positionals.size(); n++) { 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); return custom != null ? custom : param.parsers().get(apos).getCompletions(env);
} }
String par = argv[z + n]; 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; return null;
apos += 1; apos += 1;
} }

View file

@ -192,6 +192,14 @@ public abstract class Command implements Executable {
return this.addParameter(new PlayerEntityParser(name, defaulted, policy)); 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) { protected Command addPlayerEntityList(String name, boolean defaulted, UserPolicy policy) {
return this.addParameter(new PlayerEntityListParser(name, defaulted, policy)); return this.addParameter(new PlayerEntityListParser(name, defaulted, policy));
} }

View file

@ -262,5 +262,6 @@ public class CommandEnvironment {
this.registerExecutable(new CommandRunat()); this.registerExecutable(new CommandRunat());
this.registerExecutable(new CommandRunas()); this.registerExecutable(new CommandRunas());
this.registerExecutable(new CommandExp()); this.registerExecutable(new CommandExp());
this.registerExecutable(new CommandMagic());
} }
} }

View file

@ -4,6 +4,8 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import common.util.Identifyable;
public abstract class CompletingParser extends ArgumentParser { public abstract class CompletingParser extends ArgumentParser {
private final List<String> defCompletions; private final List<String> defCompletions;
@ -11,7 +13,7 @@ public abstract class CompletingParser extends ArgumentParser {
super(name); super(name);
this.defCompletions = new ArrayList<String>(completions.length); this.defCompletions = new ArrayList<String>(completions.length);
for(Object comp : completions) { for(Object comp : completions) {
this.defCompletions.add(String.valueOf(comp)); this.defCompletions.add(comp instanceof Identifyable id ? id.getName() : String.valueOf(comp));
} }
} }

View file

@ -3,6 +3,7 @@ package server.command;
import java.util.Map; import java.util.Map;
import common.collect.Maps; import common.collect.Maps;
import common.util.Identifyable;
public class EnumParser<T> extends DefaultingParser { public class EnumParser<T> extends DefaultingParser {
private final Class<T> clazz; private final Class<T> clazz;
@ -14,7 +15,7 @@ public class EnumParser<T> extends DefaultingParser {
for(T obj : iter) { for(T obj : iter) {
if(sb.length() > 1) if(sb.length() > 1)
sb.append("', '"); sb.append("', '");
sb.append(String.valueOf(obj)); sb.append(obj instanceof Identifyable id ? id.getName() : String.valueOf(obj));
} }
return sb.append("'").toString(); return sb.append("'").toString();
} }
@ -24,7 +25,7 @@ public class EnumParser<T> extends DefaultingParser {
this.clazz = clazz; this.clazz = clazz;
this.selections = selections; this.selections = selections;
for(T o : 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);
} }
} }

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

View file

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