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

Add item replace action #1061

Merged
merged 3 commits into from
Oct 2, 2022
Merged
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
14 changes: 14 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 @@ -3,11 +3,13 @@
import com.google.common.collect.ImmutableList;
import java.lang.reflect.Method;
import java.util.Map;
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.ChatMessageAction;
import tc.oc.pgm.action.actions.KillEntitiesAction;
import tc.oc.pgm.action.actions.ReplaceItemAction;
import tc.oc.pgm.action.actions.ScopeSwitchAction;
import tc.oc.pgm.action.actions.SetVariableAction;
import tc.oc.pgm.api.feature.FeatureValidation;
Expand All @@ -22,6 +24,7 @@
import tc.oc.pgm.kits.Kit;
import tc.oc.pgm.util.MethodParser;
import tc.oc.pgm.util.MethodParsers;
import tc.oc.pgm.util.inventory.ItemMatcher;
import tc.oc.pgm.util.math.Formula;
import tc.oc.pgm.util.xml.InvalidXMLException;
import tc.oc.pgm.util.xml.Node;
Expand Down Expand Up @@ -211,4 +214,15 @@ public KillEntitiesAction parseKillEntities(Element el, Class<?> scope)
throws InvalidXMLException {
return new KillEntitiesAction(factory.getFilters().parseFilterProperty(el, "filter"));
}

@MethodParser("replace-item")
public ReplaceItemAction parseReplaceItem(Element el, Class<?> scope) throws InvalidXMLException {
ItemMatcher matcher = factory.getKits().parseItemMatcher(el, "find");
ItemStack item = factory.getKits().parseItem(el.getChild("replace"), true);

boolean keepAmount = XMLUtils.parseBoolean(el.getAttribute("keep-amount"), false);
boolean keepEnchants = XMLUtils.parseBoolean(el.getAttribute("keep-enchants"), false);

return new ReplaceItemAction(matcher, item, keepAmount, keepEnchants);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package tc.oc.pgm.action.actions;

import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.kits.tag.ItemModifier;
import tc.oc.pgm.util.inventory.ItemMatcher;

public class ReplaceItemAction extends AbstractAction<MatchPlayer> {

private final ItemMatcher matcher;
private final ItemStack item;
private final boolean keepAmount;
private final boolean keepEnchants;

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

@Override
public void trigger(MatchPlayer player) {
PlayerInventory inv = player.getInventory();

ItemStack[] armor = inv.getArmorContents();
for (int i = 0; i < armor.length; i++) {
ItemStack current = armor[i];
if (current == null || !matcher.matches(current)) continue;
armor[i] = replaceItem(current, player);
}
inv.setArmorContents(armor);

for (int i = 0; i < inv.getSize(); i++) {
ItemStack current = inv.getItem(i);
if (current == null || !matcher.matches(current)) continue;
inv.setItem(i, replaceItem(current, player));
}
}

private ItemStack replaceItem(ItemStack current, MatchPlayer player) {
ItemStack newItem = item.clone();
if (keepAmount) newItem.setAmount(current.getAmount());
if (keepEnchants) newItem.addEnchantments(current.getEnchantments());
ItemModifier.apply(newItem, player);
return newItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.time.Duration;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.bukkit.Material;
import java.util.stream.Collectors;
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;
import org.jdom2.Attribute;
Expand All @@ -16,6 +16,7 @@
import tc.oc.pgm.filters.operator.AnyFilter;
import tc.oc.pgm.filters.parse.FilterParser;
import tc.oc.pgm.goals.ShowOptions;
import tc.oc.pgm.kits.tag.ItemModifier;
import tc.oc.pgm.payload.PayloadDefinition;
import tc.oc.pgm.regions.BlockBoundedValidation;
import tc.oc.pgm.regions.EverywhereRegion;
Expand All @@ -30,11 +31,7 @@
public abstract class ControlPointParser {
private static final Filter VISUAL_MATERIALS =
AnyFilter.of(
new BlockFilter(Material.WOOL),
new BlockFilter(Material.CARPET),
new BlockFilter(Material.STAINED_CLAY),
new BlockFilter(Material.STAINED_GLASS),
new BlockFilter(Material.STAINED_GLASS_PANE));
ItemModifier.COLOR_AFFECTED.stream().map(BlockFilter::new).collect(Collectors.toList()));

public enum Type {
HILL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tc.oc.pgm.filters.operator;

import java.util.List;
import java.util.Collection;
import tc.oc.pgm.api.filter.Filter;
import tc.oc.pgm.api.filter.query.Query;

Expand Down Expand Up @@ -29,7 +29,7 @@ public static Filter of(Filter... filters) {
return MultiFilterFunction.of(AllFilter::new, filters);
}

public static Filter of(List<Filter> filters) {
public static Filter of(Collection<Filter> filters) {
return MultiFilterFunction.of(AllFilter::new, filters);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tc.oc.pgm.filters.operator;

import java.util.List;
import java.util.Collection;
import tc.oc.pgm.api.filter.Filter;
import tc.oc.pgm.api.filter.query.Query;

Expand Down Expand Up @@ -29,7 +29,7 @@ public static Filter of(Filter... filters) {
return MultiFilterFunction.of(AnyFilter::new, filters);
}

public static Filter of(List<Filter> filters) {
public static Filter of(Collection<Filter> filters) {
return MultiFilterFunction.of(AnyFilter::new, filters);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tc.oc.pgm.filters.operator;

import java.util.List;
import java.util.Collection;
import tc.oc.pgm.api.filter.Filter;
import tc.oc.pgm.api.filter.query.Query;

Expand Down Expand Up @@ -32,7 +32,7 @@ public static Filter of(Filter... filters) {
return MultiFilterFunction.of(OneFilter::new, filters);
}

public static Filter of(List<Filter> filters) {
public static Filter of(Collection<Filter> filters) {
return MultiFilterFunction.of(OneFilter::new, filters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import tc.oc.pgm.events.ListenerScope;
import tc.oc.pgm.events.PlayerPartyChangeEvent;
import tc.oc.pgm.filters.query.DamageQuery;
import tc.oc.pgm.kits.tag.ItemModifier;
import tc.oc.pgm.util.collection.DefaultMapAdapter;
import tc.oc.pgm.util.event.ItemTransferEvent;
import tc.oc.pgm.util.event.PlayerItemTransferEvent;
Expand Down Expand Up @@ -75,6 +76,7 @@ private void giveRewards(MatchPlayer killer, Collection<KillReward> rewards) {

for (ItemStack stack : items) {
ItemStack clone = stack.clone();
ItemModifier.apply(clone, killer);
PlayerItemTransferEvent event =
new PlayerItemTransferEvent(
null,
Expand Down
13 changes: 3 additions & 10 deletions core/src/main/java/tc/oc/pgm/kits/ArmorKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.kits.tag.ItemModifier;

public class ArmorKit extends AbstractKit {
public static class ArmorItem {
public final ItemStack stack;
public final boolean locked;
public final boolean teamColor;

public ArmorItem(ItemStack stack, boolean locked, boolean teamColor) {
public ArmorItem(ItemStack stack, boolean locked) {
this.stack = stack;
this.locked = locked;
this.teamColor = teamColor;
}
}

Expand All @@ -41,12 +39,7 @@ public void applyPostEvent(MatchPlayer player, boolean force, List<ItemStack> di
int slot = entry.getKey().ordinal();
if (force || wearing[slot] == null || wearing[slot].getType() == Material.AIR) {
wearing[slot] = entry.getValue().stack.clone();

if (entry.getValue().teamColor) {
LeatherArmorMeta meta = (LeatherArmorMeta) wearing[slot].getItemMeta();
meta.setColor(player.getParty().getFullColor());
wearing[slot].setItemMeta(meta);
}
ItemModifier.apply(wearing[slot], player);

KitMatchModule kitMatchModule = player.getMatch().getModule(KitMatchModule.class);
if (kitMatchModule != null) {
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/tc/oc/pgm/kits/ItemKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.kits.tag.ItemModifier;
import tc.oc.pgm.util.inventory.InventoryUtils;

public class ItemKit implements KitDefinition {
Expand Down Expand Up @@ -70,6 +71,11 @@ public void apply(MatchPlayer player, boolean force, List<ItemStack> displacedIt
final HumanEntity holder = player.getBukkit();
final PlayerInventory inv = player.getBukkit().getInventory();

// Apply all item modifications (eg: team-colors)
for (ItemStack item : event.getItems()) {
ItemModifier.apply(item, player);
}

if (force) {
for (Entry<Slot, ItemStack> kitEntry : event.getSlotItems().entrySet()) {
kitEntry.getKey().putItem(holder, kitEntry.getValue().clone());
Expand Down
18 changes: 12 additions & 6 deletions core/src/main/java/tc/oc/pgm/kits/KitParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import tc.oc.pgm.doublejump.DoubleJumpKit;
import tc.oc.pgm.filters.matcher.StaticFilter;
import tc.oc.pgm.kits.tag.Grenade;
import tc.oc.pgm.kits.tag.ItemModifier;
import tc.oc.pgm.kits.tag.ItemTags;
import tc.oc.pgm.projectile.ProjectileDefinition;
import tc.oc.pgm.shield.ShieldKit;
Expand Down Expand Up @@ -219,10 +220,7 @@ private ArmorKit.ArmorItem parseArmorItem(Element el) throws InvalidXMLException
ItemStack stack = parseItem(el, true);
boolean locked = XMLUtils.parseBoolean(el.getAttribute("locked"), false);

boolean teamColor =
stack.getItemMeta() instanceof LeatherArmorMeta
&& XMLUtils.parseBoolean(el.getAttribute("team-color"), false);
return new ArmorKit.ArmorItem(stack, locked, teamColor);
return new ArmorKit.ArmorItem(stack, locked);
}

public ArmorKit parseArmorKit(Element el) throws InvalidXMLException {
Expand Down Expand Up @@ -400,8 +398,13 @@ public ItemStack parseHead(Element el) throws InvalidXMLException {
}

public ItemMatcher parseItemMatcher(Element parent) throws InvalidXMLException {
ItemStack stack = parseItem(parent.getChild("item"), false);
if (stack == null) throw new InvalidXMLException("Item expected", parent);
return parseItemMatcher(parent, "item");
}

public ItemMatcher parseItemMatcher(Element parent, String childName) throws InvalidXMLException {
ItemStack stack = parseItem(parent.getChild(childName), false);
if (stack == null)
throw new InvalidXMLException("Child " + childName + " element expected", parent);

Range<Integer> amount =
XMLUtils.parseNumericRange(Node.fromAttr(parent, "amount"), Integer.class, null);
Expand Down Expand Up @@ -566,6 +569,9 @@ String itemFlagName(ItemFlag flag) {
}

public void parseCustomNBT(Element el, ItemStack itemStack) throws InvalidXMLException {
if (XMLUtils.parseBoolean(el.getAttribute("team-color"), false))
ItemModifier.TEAM_COLOR.set(itemStack, true);

if (XMLUtils.parseBoolean(el.getAttribute("grenade"), false)) {
Grenade.ITEM_TAG.set(
itemStack,
Expand Down
47 changes: 47 additions & 0 deletions core/src/main/java/tc/oc/pgm/kits/tag/ItemModifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tc.oc.pgm.kits.tag;

import com.google.common.collect.ImmutableSet;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.util.bukkit.BukkitUtils;
import tc.oc.pgm.util.inventory.tag.ItemTag;

public class ItemModifier {

public static final ItemTag<Boolean> TEAM_COLOR = ItemTag.newBoolean("team-color");

public static final ImmutableSet<Material> COLOR_AFFECTED =
ImmutableSet.of(
Material.WOOL,
Material.CARPET,
Material.STAINED_CLAY,
Material.STAINED_GLASS,
Material.STAINED_GLASS_PANE);

// Apply per-player customizations of items.
// This may be expanded on the future but currently only handles coloring armor & blocks.
// The method is explicitly mutate-only, if you don't want side effects pass a clone.
public static void apply(ItemStack item, MatchPlayer player) {
if (!TEAM_COLOR.has(item)) return;
TEAM_COLOR.clear(item);

ItemMeta meta = item.getItemMeta();

if (meta instanceof LeatherArmorMeta) {
LeatherArmorMeta leather = (LeatherArmorMeta) meta;
leather.setColor(player.getParty().getFullColor());
item.setItemMeta(meta);
} else if (COLOR_AFFECTED.contains(item.getType())) {
item.setDurability(getWoolColor(player.getParty().getColor()));
}
}

@SuppressWarnings("deprecation")
private static byte getWoolColor(ChatColor color) {
return BukkitUtils.chatColorToDyeColor(color).getWoolData();
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/tc/oc/pgm/shops/menu/ShopMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.kits.tag.ItemModifier;
import tc.oc.pgm.menu.InventoryMenu;
import tc.oc.pgm.shops.Shop;
import tc.oc.pgm.util.inventory.ItemBuilder;
Expand Down Expand Up @@ -221,6 +222,7 @@ private ClickableItem getPurchasableItem(Icon icon) {
String clickLore = TextTranslations.translateLegacy(click, getBukkit());

ItemStack item = icon.getItem().clone();
ItemModifier.apply(item, getViewer());
ItemMeta meta = item.getItemMeta();
List<String> lore = Lists.newArrayList();
if (meta.getLore() != null) {
Expand Down