Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create add attribute for Replace-Item Action #1254

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion core/src/main/java/tc/oc/pgm/action/ActionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,12 @@ public ReplaceItemAction parseReplaceItem(Element el, Class<?> scope) throws Inv

boolean keepAmount = XMLUtils.parseBoolean(el.getAttribute("keep-amount"), false);
boolean keepEnchants = XMLUtils.parseBoolean(el.getAttribute("keep-enchants"), false);
int add = XMLUtils.parseNumber(el.getAttribute("add"), Integer.class, 0);
if (add != 0 && !keepAmount) {
throw new InvalidXMLException("keep-amount must be enabled to use add", el);
}

return new ReplaceItemAction(matcher, item, keepAmount, keepEnchants);
return new ReplaceItemAction(matcher, item, keepAmount, keepEnchants, add);
}

@MethodParser("fill")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ public class ReplaceItemAction extends AbstractAction<MatchPlayer> {
private final ItemStack item;
private final boolean keepAmount;
private final boolean keepEnchants;
private final int add;

public ReplaceItemAction(
ItemMatcher matcher, ItemStack item, boolean keepAmount, boolean keepEnchants) {
ItemMatcher matcher, ItemStack item, boolean keepAmount, boolean keepEnchants, int add) {
super(MatchPlayer.class);
this.matcher = matcher;
this.item = item;
this.keepAmount = keepAmount;
this.keepEnchants = keepEnchants;
this.add = add;
}

@Override
Expand All @@ -45,6 +47,12 @@ private ItemStack replaceItem(ItemStack current, MatchPlayer player) {
ItemStack newItem = item.clone();
if (keepAmount) newItem.setAmount(current.getAmount());
if (keepEnchants) newItem.addEnchantments(current.getEnchantments());
if (add != 0) {
newItem.setAmount(newItem.getAmount() + add);
if (newItem.getAmount() < 0) {
newItem.setAmount(0);
}
}
ItemModifier.apply(newItem, player);
return newItem;
}
Expand Down