Skip to content

Commit

Permalink
Implement enchant item action (#1420)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <pabloherrerapalacio@gmail.com>
  • Loading branch information
Pablete1234 committed Nov 30, 2024
1 parent 2c79609 commit ea6d91f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/ActionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MatchPlayer> 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(
Expand Down
45 changes: 45 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/actions/EnchantItemAction.java
Original file line number Diff line number Diff line change
@@ -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<MatchPlayer> {

private final ItemMatcher matcher;
private final Enchantment enchant;
private final Formula<MatchPlayer> level;

public EnchantItemAction(ItemMatcher matcher, Enchantment enchant, Formula<MatchPlayer> 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);
}
}
}

0 comments on commit ea6d91f

Please sign in to comment.