Skip to content

Commit

Permalink
Add wider support for team colors
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 committed Oct 2, 2022
1 parent b6d8732 commit 855a695
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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.ItemMatcher;

public class ReplaceItemAction extends AbstractAction<MatchPlayer> {
Expand All @@ -22,30 +23,29 @@ public ReplaceItemAction(
}

@Override
public void trigger(MatchPlayer matchPlayer) {
PlayerInventory inv = matchPlayer.getInventory();
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);
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;

ItemStack newItem = replaceItem(current);
inv.setItem(i, newItem);
inv.setItem(i, replaceItem(current, player));
}
}

private ItemStack replaceItem(ItemStack current) {
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
4 changes: 2 additions & 2 deletions core/src/main/java/tc/oc/pgm/filters/operator/AllFilter.java
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);
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/tc/oc/pgm/filters/operator/AnyFilter.java
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);
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/tc/oc/pgm/filters/operator/OneFilter.java
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
9 changes: 5 additions & 4 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 @@ -571,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
48 changes: 48 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,48 @@
package tc.oc.pgm.kits.tag;

import com.google.common.collect.ImmutableSet;
import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
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

0 comments on commit 855a695

Please sign in to comment.