diff --git a/core/src/main/java/tc/oc/pgm/action/ActionParser.java b/core/src/main/java/tc/oc/pgm/action/ActionParser.java index 056887bd2d..88f29c8973 100644 --- a/core/src/main/java/tc/oc/pgm/action/ActionParser.java +++ b/core/src/main/java/tc/oc/pgm/action/ActionParser.java @@ -17,10 +17,12 @@ import net.kyori.adventure.sound.Sound; import net.kyori.adventure.text.Component; import net.kyori.adventure.title.Title; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.jdom2.Element; import org.jetbrains.annotations.Nullable; import tc.oc.pgm.action.actions.ActionNode; +import tc.oc.pgm.action.actions.EnchantItemAction; import tc.oc.pgm.action.actions.ExposedAction; import tc.oc.pgm.action.actions.FillAction; import tc.oc.pgm.action.actions.KillEntitiesAction; @@ -362,6 +364,15 @@ public ReplaceItemAction parseReplaceItem(Element el, Class scope) throws Inv return new ReplaceItemAction(matcher, item, keepAmount, keepEnchants); } + @MethodParser("enchant-item") + public EnchantItemAction parseEnchantItem(Element el, Class scope) throws InvalidXMLException { + ItemMatcher matcher = factory.getKits().parseItemMatcher(el, "find"); + Enchantment enchant = XMLUtils.parseEnchantment(Node.fromRequiredAttr(el, "enchantment")); + Formula level = parser.formula(MatchPlayer.class, el, "level").required(); + + return new EnchantItemAction(matcher, enchant, level); + } + @MethodParser("fill") public FillAction parseFill(Element el, Class scope) throws InvalidXMLException { return new FillAction( diff --git a/core/src/main/java/tc/oc/pgm/action/actions/EnchantItemAction.java b/core/src/main/java/tc/oc/pgm/action/actions/EnchantItemAction.java new file mode 100644 index 0000000000..b704c1d462 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/action/actions/EnchantItemAction.java @@ -0,0 +1,45 @@ +package tc.oc.pgm.action.actions; + +import java.util.Objects; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import tc.oc.pgm.api.player.MatchPlayer; +import tc.oc.pgm.util.inventory.ItemMatcher; +import tc.oc.pgm.util.math.Formula; + +public class EnchantItemAction extends AbstractAction { + + private final ItemMatcher matcher; + private final Enchantment enchant; + private final Formula level; + + public EnchantItemAction(ItemMatcher matcher, Enchantment enchant, Formula level) { + super(MatchPlayer.class); + this.matcher = matcher; + this.enchant = enchant; + this.level = level; + } + + @Override + public void trigger(MatchPlayer player) { + PlayerInventory inv = Objects.requireNonNull(player.getInventory()); + + int level = Math.max(0, (int) this.level.applyAsDouble(player)); + + for (ItemStack current : inv.getArmorContents()) { + if (current != null && matcher.matches(current)) enchant(current, level); + } + for (int i = 0; i < inv.getSize(); i++) { + ItemStack current = inv.getItem(i); + if (current != null && matcher.matches(current)) enchant(current, level); + } + } + + private void enchant(ItemStack current, int level) { + if (current.getEnchantmentLevel(enchant) != level) { + if (level == 0) current.removeEnchantment(enchant); + else current.addUnsafeEnchantment(enchant, level); + } + } +}