Skip to content

Commit f4b7b06

Browse files
authored
⚒ [2.7] Fix enchanting multiple items (#5934)
1 parent f32ac2c commit f4b7b06

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/main/java/ch/njol/skript/effects/EffEnchant.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,55 +53,51 @@ public class EffEnchant extends Effect {
5353
}
5454

5555
@SuppressWarnings("null")
56-
private Expression<ItemType> item;
56+
private Expression<ItemType> items;
5757
@Nullable
58-
private Expression<EnchantmentType> enchs;
58+
private Expression<EnchantmentType> enchantments;
5959

60-
@SuppressWarnings({"unchecked", "null"})
6160
@Override
62-
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
63-
item = (Expression<ItemType>) exprs[0];
64-
if (!ChangerUtils.acceptsChange(item, ChangeMode.SET, ItemStack.class)) {
65-
Skript.error(item + " cannot be changed, thus it cannot be (dis)enchanted");
61+
@SuppressWarnings("unchecked")
62+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
63+
items = (Expression<ItemType>) exprs[0];
64+
if (!ChangerUtils.acceptsChange(items, ChangeMode.SET, ItemStack.class)) {
65+
Skript.error(items + " cannot be changed, thus it cannot be (dis)enchanted");
6666
return false;
6767
}
6868
if (matchedPattern == 0)
69-
enchs = (Expression<EnchantmentType>) exprs[1];
69+
enchantments = (Expression<EnchantmentType>) exprs[1];
7070
return true;
7171
}
7272

7373
@Override
74-
protected void execute(final Event e) {
75-
final ItemType i = item.getSingle(e);
76-
if (i == null)
74+
protected void execute(Event event) {
75+
ItemType[] items = this.items.getArray(event);
76+
if (items.length == 0) // short circuit
7777
return;
78-
if (enchs != null) {
79-
final EnchantmentType[] types = enchs.getArray(e);
78+
79+
if (enchantments != null) {
80+
EnchantmentType[] types = enchantments.getArray(event);
8081
if (types.length == 0)
8182
return;
82-
83-
for (final EnchantmentType type : types) {
84-
Enchantment ench = type.getType();
85-
assert ench != null;
86-
i.addEnchantments(new EnchantmentType(ench, type.getLevel()));
83+
for (ItemType item : items) {
84+
for (EnchantmentType type : types) {
85+
Enchantment enchantment = type.getType();
86+
assert enchantment != null;
87+
item.addEnchantments(new EnchantmentType(enchantment, type.getLevel()));
88+
}
8789
}
88-
item.change(e, new ItemType[] {i}, ChangeMode.SET);
8990
} else {
90-
final EnchantmentType[] types = i.getEnchantmentTypes();
91-
if (types == null)
92-
return;
93-
94-
for (final EnchantmentType ench : types) {
95-
assert ench != null;
96-
i.removeEnchantments(ench);
91+
for (ItemType item : items) {
92+
item.clearEnchantments();
9793
}
98-
item.change(e, new ItemType[] {i}, ChangeMode.SET);
9994
}
95+
this.items.change(event, items.clone(), ChangeMode.SET);
10096
}
10197

10298
@Override
103-
public String toString(final @Nullable Event e, final boolean debug) {
104-
return enchs == null ? "disenchant " + item.toString(e, debug) : "enchant " + item.toString(e, debug) + " with " + enchs;
99+
public String toString(@Nullable Event event, boolean debug) {
100+
return enchantments == null ? "disenchant " + items.toString(event, debug) : "enchant " + items.toString(event, debug) + " with " + enchantments;
105101
}
106102

107103
}

0 commit comments

Comments
 (0)