Skip to content

Commit

Permalink
Support spaces in old enchantment functions just like before, and dep…
Browse files Browse the repository at this point in the history
…ecrate can_enchant_target() for can_enchant_item()
  • Loading branch information
PseudoKnight committed Aug 13, 2018
1 parent 90a38b5 commit fde172f
Showing 1 changed file with 57 additions and 15 deletions.
72 changes: 57 additions & 15 deletions src/main/java/com/laytonsmith/core/functions/Enchantments.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public Construct exec(Target t, Environment environment, Construct... args) thro
levelArray = (CArray) args[3 - offset]; levelArray = (CArray) args[3 - offset];
} }
for(String key : enchantArray.stringKeySet()) { for(String key : enchantArray.stringKeySet()) {
MCEnchantment e = GetEnchantment(enchantArray.get(key, t).val(), t); MCEnchantment e = GetEnchantment(enchantArray.get(key, t).val().replace(' ', '_'), t);
if(e.canEnchantItem(is)) { if(e.canEnchantItem(is)) {
int level = ConvertLevel(levelArray.get(key, t)); int level = ConvertLevel(levelArray.get(key, t));
if(e.getMaxLevel() >= level && level > 0) { if(e.getMaxLevel() >= level && level > 0) {
Expand Down Expand Up @@ -372,7 +372,7 @@ public Construct exec(Target t, Environment environment, Construct... args) thro
enchantArray = (CArray) args[2 - offset]; enchantArray = (CArray) args[2 - offset];
} }
for(String key : enchantArray.stringKeySet()) { for(String key : enchantArray.stringKeySet()) {
MCEnchantment e = GetEnchantment(enchantArray.get(key, t).val(), t); MCEnchantment e = GetEnchantment(enchantArray.get(key, t).val().replace(' ', '_'), t);
is.removeEnchantment(e); is.removeEnchantment(e);
} }
return CVoid.VOID; return CVoid.VOID;
Expand Down Expand Up @@ -585,6 +585,7 @@ public Construct exec(Target t, Environment environment, Construct... args) thro
} }


@api @api
@hide("Deprecated for can_enchant_item()")
public static class can_enchant_target extends AbstractFunction implements Optimizable { public static class can_enchant_target extends AbstractFunction implements Optimizable {


@Override @Override
Expand All @@ -599,7 +600,7 @@ public Integer[] numArgs() {


@Override @Override
public String docs() { public String docs() {
return "boolean {name, item} Given an enchantment name and an item array," return "boolean {name, item} Given an enchantment name and an item type,"
+ " returns whether or not that item can be enchanted with that enchantment." + " returns whether or not that item can be enchanted with that enchantment."
+ " Throws an EnchantmentException if the name is not a valid enchantment type."; + " Throws an EnchantmentException if the name is not a valid enchantment type.";
} }
Expand Down Expand Up @@ -627,23 +628,15 @@ public Boolean runAsync() {


@Override @Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException { public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
MCEnchantment e = GetEnchantment(args[0].val(), t); MCEnchantment e = GetEnchantment(args[0].val().replace(' ', '_'), t);
MCItemStack is; MCItemStack is = Static.ParseItemNotation(null, args[1].val(), 1, t);
if(args[1] instanceof CArray) {
is = ObjectGenerator.GetGenerator().item(args[1], t);
} else {
is = Static.ParseItemNotation(null, args[1].val(), 1, t);
}
return CBoolean.get(e.canEnchantItem(is)); return CBoolean.get(e.canEnchantItem(is));
} }


@Override @Override
public ParseTree optimizeDynamic(Target t, List<ParseTree> children, FileOptions fileOptions) public ParseTree optimizeDynamic(Target t, List<ParseTree> children, FileOptions fileOptions)
throws ConfigCompileException, ConfigRuntimeException { throws ConfigCompileException, ConfigRuntimeException {
if(children.size() == 2 CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, getName() + " is deprecated for can_enchant_item()", t);
&& (children.get(1).getData() instanceof CString || children.get(1).getData() instanceof CInt)) {
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "The string item format in " + getName() + " is deprecated.", t);
}
return null; return null;
} }


Expand All @@ -653,6 +646,55 @@ public Set<OptimizationOption> optimizationOptions() {
} }
} }


@api
public static class can_enchant_item extends AbstractFunction {

@Override
public String getName() {
return "can_enchant_item";
}

@Override
public Integer[] numArgs() {
return new Integer[]{2};
}

@Override
public String docs() {
return "boolean {name, item} Given an enchantment name and an item array,"
+ " returns whether or not that item can be enchanted with that enchantment."
+ " Throws an EnchantmentException if the name is not a valid enchantment type.";
}

@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREEnchantmentException.class, CREFormatException.class, CRECastException.class,
CRERangeException.class, CRENotFoundException.class};
}

@Override
public boolean isRestricted() {
return false;
}

@Override
public CHVersion since() {
return CHVersion.V3_3_3;
}

@Override
public Boolean runAsync() {
return false;
}

@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
MCEnchantment e = GetEnchantment(args[0].val(), t);
MCItemStack is = ObjectGenerator.GetGenerator().item(args[1], t);
return CBoolean.get(e.canEnchantItem(is));
}
}

@api @api
public static class get_enchant_max extends AbstractFunction { public static class get_enchant_max extends AbstractFunction {


Expand Down Expand Up @@ -694,7 +736,7 @@ public Boolean runAsync() {


@Override @Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException { public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
MCEnchantment e = GetEnchantment(args[0].val(), t); MCEnchantment e = GetEnchantment(args[0].val().replace(' ', '_'), t);
return new CInt(e.getMaxLevel(), t); return new CInt(e.getMaxLevel(), t);
} }
} }
Expand Down

0 comments on commit fde172f

Please sign in to comment.