From 9c1d871f3633a3ff8f318d5477ccf88a351dfa3c Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Thu, 7 Jun 2018 00:08:57 -0500 Subject: [PATCH 01/19] Team Chest mutation --- .../i18n/templates/pgm/PGMMessages.properties | 4 + .../java/tc/oc/pgm/mutation/Mutation.java | 3 +- .../mutation/types/kit/TeamChestMutation.java | 135 ++++++++++++++++++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java diff --git a/Commons/core/src/main/i18n/templates/pgm/PGMMessages.properties b/Commons/core/src/main/i18n/templates/pgm/PGMMessages.properties index cb00de6b9..1598ee6ad 100644 --- a/Commons/core/src/main/i18n/templates/pgm/PGMMessages.properties +++ b/Commons/core/src/main/i18n/templates/pgm/PGMMessages.properties @@ -255,6 +255,10 @@ mutation.type.tools = Tools mutation.type.tools.desc = enchanted diamond tools mutation.type.apocalypse = Apocalypse mutation.type.apocalypse.desc = mob spawning chaos +mutation.type.teamchest = Team Chest +mutation.type.teamchest.desc = access a shared team chest +mutation.type.teamchest.item_name = Team Chest +mutation.type.teamchest.item_lore = Right click to access an inventory shared by the team. tnt.license.info.alreadyHas = You have a TNT license. You can surrender your license by typing {0} tnt.license.info.doesNotHave = You do not have a TNT license. You can request one by typing {0} diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/Mutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/Mutation.java index 75e2a9c98..bef30b094 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/Mutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/Mutation.java @@ -42,7 +42,8 @@ public enum Mutation { BREAD (BreadMutation.class, Material.BREAD), BOAT (BoatMutation.class, Material.BOAT, false), TOOLS (ToolsMutation.class, Material.DIAMOND_PICKAXE), - APOCALYPSE (ApocalypseMutation.class, Material.NETHER_STAR); + APOCALYPSE (ApocalypseMutation.class, Material.NETHER_STAR), + TEAMCHEST (TeamChestMutation.class, Material.ENDER_CHEST); public static final String TYPE_KEY = "mutation.type."; public static final String DESCRIPTION_KEY = ".desc"; diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java new file mode 100644 index 000000000..4111bdede --- /dev/null +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -0,0 +1,135 @@ +package tc.oc.pgm.mutation.types.kit; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.block.Action; +import org.bukkit.event.inventory.InventoryAction; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import tc.oc.commons.bukkit.inventory.Slot; +import tc.oc.commons.bukkit.item.ItemBuilder; +import tc.oc.pgm.PGMTranslations; +import tc.oc.pgm.kits.ItemKit; +import tc.oc.pgm.kits.Kit; +import tc.oc.pgm.kits.SlotItemKit; +import tc.oc.pgm.match.Match; +import tc.oc.pgm.match.MatchPlayer; +import tc.oc.pgm.match.Party; +import tc.oc.pgm.mutation.types.KitMutation; +import tc.oc.pgm.wool.WoolMatchModule; + +import java.util.*; + +public class TeamChestMutation extends KitMutation { + final static int SLOT_ID = 17; // Top right + final static Material TOOL_TYPE = Material.ENDER_CHEST; + final static String ITEM_NAME_KEY = "mutation.type.teamchest.item_name"; + final static String ITEM_LORE_KEY = "mutation.type.teamchest.item_lore"; + final static int CHEST_SIZE = 27; + + final Map teamChests = new WeakHashMap<>(); + + final boolean woolsAllowed; + + public TeamChestMutation(Match match) { + super(match, false); + this.woolsAllowed = match.getMatchModule(WoolMatchModule.class) == null; + for (Party party : match().getParties()) { + if (party.isParticipatingType()) { + // Could the chest title be localized properly? + teamChests.put(party, match().getServer().createInventory(null, CHEST_SIZE)); + } + } + } + + @Override + public void kits(MatchPlayer player, List kits) { + super.kits(player, kits); + kits.add(getKitForPlayer(player)); + } + + // Open shared inventory instead of placing the chest + @EventHandler(priority = EventPriority.HIGHEST) + public void onChestUse(PlayerInteractEvent event) { + if (event.getItem() == null) return; + + if (!(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) return; + if (event.getItem().getType() != TOOL_TYPE) return; + + Player bukkitPlayer = event.getPlayer(); + if (bukkitPlayer == null) return; + Optional oTeamInventory = getTeamsInventory(bukkitPlayer); + + oTeamInventory.ifPresent(teamInventory -> { + event.setCancelled(true); + // If the item is in the off-hand slot, it wont get put back visually for the player without this. + if(event.getHand() == EquipmentSlot.OFF_HAND) event.getActor().updateInventory(); + bukkitPlayer.openInventory(teamInventory); + + }); + } + + @EventHandler(priority = EventPriority.LOWEST) + public void onInventoryClick(InventoryClickEvent event) { + if (event.getCurrentItem() == null) return; + + // No putting evil items (ender chest, possibly wool) into the chest + Optional teamChest = getTeamsInventory(event.getActor()); + if (teamChest.filter(teamInventory -> { + return teamInventory.equals(event.getView().getTopInventory()); + }).isPresent() && + isItemEvil(event.getCurrentItem())) { + event.setCancelled(true); + return; + } + + // If normal right click, in their inventory, on the chest, then open shared inventory. + getTeamsInventory(event.getActor()).ifPresent(teamInventory -> { + if (event.getInventory().getType() == InventoryType.CRAFTING && + event.getCurrentItem().getType() == TOOL_TYPE && + event.getAction() == InventoryAction.PICKUP_HALF) { + event.setCancelled(true); + // This resets their mouse position annoyingly, but without it items can get stuck in places. + event.getActor().closeInventory(); + // Prevent visual inconsistencies, specifically off-hand slot + if (event.getSlotType() == InventoryType.SlotType.QUICKBAR) { + event.getActor().updateInventory(); + } + event.getActor().openInventory(teamInventory); + } + }); + } + + private boolean isItemEvil(ItemStack item) { + if(item.getType() == TOOL_TYPE) return true; + if(!woolsAllowed && item.getType() == Material.WOOL) return true; + + return false; + } + + private Optional getTeamsInventory(Player bukkitPlayer) { + MatchPlayer player = match().getPlayer(bukkitPlayer); + Party team = player.getParty(); + + if (!team.isParticipating()) return Optional.empty(); + + return Optional.of(teamChests.get(team)); + } + + private Kit getKitForPlayer(MatchPlayer player) { + ItemStack stack = new ItemBuilder(item(TOOL_TYPE)) + .name(ChatColor.DARK_PURPLE + PGMTranslations.t(ITEM_NAME_KEY, player)) + .lore(ChatColor.DARK_AQUA + PGMTranslations.t(ITEM_LORE_KEY, player)) + .get(); + + ItemKit kit = new SlotItemKit(stack, Slot.Player.forIndex(SLOT_ID)); + return kit; + } +} From 3c40914530d511311ae144f4c675aaadb9e9e539 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 02:21:10 -0500 Subject: [PATCH 02/19] Only objective wools blocked from chest --- .../tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 6 +++--- PGM/src/main/java/tc/oc/pgm/wool/WoolMatchModule.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 4111bdede..3f68e94f9 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -36,11 +36,11 @@ public class TeamChestMutation extends KitMutation { final Map teamChests = new WeakHashMap<>(); - final boolean woolsAllowed; + final Optional oWmm; public TeamChestMutation(Match match) { super(match, false); - this.woolsAllowed = match.getMatchModule(WoolMatchModule.class) == null; + oWmm = Optional.ofNullable(match().getMatchModule(WoolMatchModule.class)); for (Party party : match().getParties()) { if (party.isParticipatingType()) { // Could the chest title be localized properly? @@ -109,7 +109,7 @@ public void onInventoryClick(InventoryClickEvent event) { private boolean isItemEvil(ItemStack item) { if(item.getType() == TOOL_TYPE) return true; - if(!woolsAllowed && item.getType() == Material.WOOL) return true; + if(oWmm.filter(wmm -> wmm.isObjectiveWool(item)).isPresent()) return true; return false; } diff --git a/PGM/src/main/java/tc/oc/pgm/wool/WoolMatchModule.java b/PGM/src/main/java/tc/oc/pgm/wool/WoolMatchModule.java index e55ae9dc9..de2ad2e71 100644 --- a/PGM/src/main/java/tc/oc/pgm/wool/WoolMatchModule.java +++ b/PGM/src/main/java/tc/oc/pgm/wool/WoolMatchModule.java @@ -63,7 +63,7 @@ public class WoolMatchModule extends MatchModule implements Listener { .collect(Collectors.toImmutableList()); } - private boolean isObjectiveWool(ItemStack stack) { + public boolean isObjectiveWool(ItemStack stack) { if(stack.getType() == Material.WOOL) { for(MonumentWool wool : this.wools) { if(wool.getDefinition().isObjectiveWool(stack)) return true; From bf6bd71431b84e1811e35a9e1114a566484ec635 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 12:48:31 -0500 Subject: [PATCH 03/19] No wildcard imports --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 3f68e94f9..0a3ab1952 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -25,7 +25,10 @@ import tc.oc.pgm.mutation.types.KitMutation; import tc.oc.pgm.wool.WoolMatchModule; -import java.util.*; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.WeakHashMap; public class TeamChestMutation extends KitMutation { final static int SLOT_ID = 17; // Top right From faad1cd3e9223c42360ce5901febe4d5cb6f497d Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 12:50:25 -0500 Subject: [PATCH 04/19] Use convenience method `module` --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 0a3ab1952..fc093352b 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -43,7 +43,7 @@ public class TeamChestMutation extends KitMutation { public TeamChestMutation(Match match) { super(match, false); - oWmm = Optional.ofNullable(match().getMatchModule(WoolMatchModule.class)); + oWmm = match().module(WoolMatchModule.class); for (Party party : match().getParties()) { if (party.isParticipatingType()) { // Could the chest title be localized properly? From 8b038f609d0afd263cc9002b03f1b0701c5924ee Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 12:54:47 -0500 Subject: [PATCH 05/19] Tidy --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 1 - 1 file changed, 1 deletion(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index fc093352b..d79703fdf 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -75,7 +75,6 @@ public void onChestUse(PlayerInteractEvent event) { // If the item is in the off-hand slot, it wont get put back visually for the player without this. if(event.getHand() == EquipmentSlot.OFF_HAND) event.getActor().updateInventory(); bukkitPlayer.openInventory(teamInventory); - }); } From d2dac49872882739ec862bfa7890d50dc07b13cc Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 12:56:02 -0500 Subject: [PATCH 06/19] Inline translation strings --- .../tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index d79703fdf..b4ec242d0 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -33,8 +33,6 @@ public class TeamChestMutation extends KitMutation { final static int SLOT_ID = 17; // Top right final static Material TOOL_TYPE = Material.ENDER_CHEST; - final static String ITEM_NAME_KEY = "mutation.type.teamchest.item_name"; - final static String ITEM_LORE_KEY = "mutation.type.teamchest.item_lore"; final static int CHEST_SIZE = 27; final Map teamChests = new WeakHashMap<>(); @@ -127,8 +125,8 @@ private Optional getTeamsInventory(Player bukkitPlayer) { private Kit getKitForPlayer(MatchPlayer player) { ItemStack stack = new ItemBuilder(item(TOOL_TYPE)) - .name(ChatColor.DARK_PURPLE + PGMTranslations.t(ITEM_NAME_KEY, player)) - .lore(ChatColor.DARK_AQUA + PGMTranslations.t(ITEM_LORE_KEY, player)) + .name(ChatColor.DARK_PURPLE + PGMTranslations.t("mutation.type.teamchest.item_name", player)) + .lore(ChatColor.DARK_AQUA + PGMTranslations.t("mutation.type.teamchest.item_lore", player)) .get(); ItemKit kit = new SlotItemKit(stack, Slot.Player.forIndex(SLOT_ID)); From 0edadff8b65c5d23f2cb1587503e1896c2e46698 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 12:57:57 -0500 Subject: [PATCH 07/19] ignoreCancelled = true --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index b4ec242d0..76967eb77 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -57,7 +57,7 @@ public void kits(MatchPlayer player, List kits) { } // Open shared inventory instead of placing the chest - @EventHandler(priority = EventPriority.HIGHEST) + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onChestUse(PlayerInteractEvent event) { if (event.getItem() == null) return; From 839b4dcb97ddaca139d4d82a084854c575989bf2 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 12:58:35 -0500 Subject: [PATCH 08/19] isBlacklistedItem --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 76967eb77..18cf71c4f 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -85,7 +85,7 @@ public void onInventoryClick(InventoryClickEvent event) { if (teamChest.filter(teamInventory -> { return teamInventory.equals(event.getView().getTopInventory()); }).isPresent() && - isItemEvil(event.getCurrentItem())) { + isBlacklistedItem(event.getCurrentItem())) { event.setCancelled(true); return; } @@ -107,7 +107,7 @@ public void onInventoryClick(InventoryClickEvent event) { }); } - private boolean isItemEvil(ItemStack item) { + private boolean isBlacklistedItem(ItemStack item) { if(item.getType() == TOOL_TYPE) return true; if(oWmm.filter(wmm -> wmm.isObjectiveWool(item)).isPresent()) return true; From f0e720b56c597bdbebfe6b6d1490e278dd6038a8 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 13:00:47 -0500 Subject: [PATCH 09/19] Single-line lambda --- .../tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 18cf71c4f..2abe011bb 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -80,11 +80,10 @@ public void onChestUse(PlayerInteractEvent event) { public void onInventoryClick(InventoryClickEvent event) { if (event.getCurrentItem() == null) return; - // No putting evil items (ender chest, possibly wool) into the chest + // No putting blacklisted items (ender chest, possibly wool) into the chest Optional teamChest = getTeamsInventory(event.getActor()); - if (teamChest.filter(teamInventory -> { - return teamInventory.equals(event.getView().getTopInventory()); - }).isPresent() && + if (teamChest.filter(teamInventory -> teamInventory.equals(event.getView().getTopInventory()) + ).isPresent() && isBlacklistedItem(event.getCurrentItem())) { event.setCancelled(true); return; From 261a032ea5afd32f737e033aac261e25910f0aef Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 13:16:32 -0500 Subject: [PATCH 10/19] Organize `onChestUse` and refactor `onInventoryClick` --- .../mutation/types/kit/TeamChestMutation.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 2abe011bb..913791ed2 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -2,6 +2,7 @@ import org.bukkit.ChatColor; import org.bukkit.Material; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -59,15 +60,16 @@ public void kits(MatchPlayer player, List kits) { // Open shared inventory instead of placing the chest @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onChestUse(PlayerInteractEvent event) { - if (event.getItem() == null) return; - - if (!(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) return; - if (event.getItem().getType() != TOOL_TYPE) return; - Player bukkitPlayer = event.getPlayer(); - if (bukkitPlayer == null) return; - Optional oTeamInventory = getTeamsInventory(bukkitPlayer); + Optional player = match().participant((Entity) bukkitPlayer); + if (!player.isPresent() || + !(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) || + event.getItem() == null || + event.getItem().getType() != TOOL_TYPE) { + return; + } + Optional oTeamInventory = getTeamsInventory(bukkitPlayer); oTeamInventory.ifPresent(teamInventory -> { event.setCancelled(true); // If the item is in the off-hand slot, it wont get put back visually for the player without this. @@ -78,30 +80,30 @@ public void onChestUse(PlayerInteractEvent event) { @EventHandler(priority = EventPriority.LOWEST) public void onInventoryClick(InventoryClickEvent event) { + Player player = event.getActor(); if (event.getCurrentItem() == null) return; // No putting blacklisted items (ender chest, possibly wool) into the chest Optional teamChest = getTeamsInventory(event.getActor()); - if (teamChest.filter(teamInventory -> teamInventory.equals(event.getView().getTopInventory()) - ).isPresent() && + if (teamChest.filter(teamInventory -> teamInventory.equals(event.getView().getTopInventory())).isPresent() && isBlacklistedItem(event.getCurrentItem())) { event.setCancelled(true); return; } // If normal right click, in their inventory, on the chest, then open shared inventory. - getTeamsInventory(event.getActor()).ifPresent(teamInventory -> { + getTeamsInventory(player).ifPresent(teamInventory -> { if (event.getInventory().getType() == InventoryType.CRAFTING && event.getCurrentItem().getType() == TOOL_TYPE && event.getAction() == InventoryAction.PICKUP_HALF) { event.setCancelled(true); // This resets their mouse position annoyingly, but without it items can get stuck in places. - event.getActor().closeInventory(); + player.closeInventory(); // Prevent visual inconsistencies, specifically off-hand slot if (event.getSlotType() == InventoryType.SlotType.QUICKBAR) { - event.getActor().updateInventory(); + player.updateInventory(); } - event.getActor().openInventory(teamInventory); + player.openInventory(teamInventory); } }); } From 8748f45be0d8bb3b826a42f57dd0a492670fc686 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 13:18:31 -0500 Subject: [PATCH 11/19] Inline SLOT_ID --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 913791ed2..a538b444e 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -32,7 +32,6 @@ import java.util.WeakHashMap; public class TeamChestMutation extends KitMutation { - final static int SLOT_ID = 17; // Top right final static Material TOOL_TYPE = Material.ENDER_CHEST; final static int CHEST_SIZE = 27; @@ -130,7 +129,7 @@ private Kit getKitForPlayer(MatchPlayer player) { .lore(ChatColor.DARK_AQUA + PGMTranslations.t("mutation.type.teamchest.item_lore", player)) .get(); - ItemKit kit = new SlotItemKit(stack, Slot.Player.forIndex(SLOT_ID)); + ItemKit kit = new SlotItemKit(stack, Slot.Player.forIndex(17)); return kit; } } From 935b209b27f0765a448077e4ab63789ab5fcd85b Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 13:20:25 -0500 Subject: [PATCH 12/19] Cleanup `isBlacklistedItem` --- .../tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index a538b444e..a76fbcda0 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -108,10 +108,8 @@ public void onInventoryClick(InventoryClickEvent event) { } private boolean isBlacklistedItem(ItemStack item) { - if(item.getType() == TOOL_TYPE) return true; - if(oWmm.filter(wmm -> wmm.isObjectiveWool(item)).isPresent()) return true; - - return false; + return item.getType() == TOOL_TYPE || + oWmm.map(w -> w.isObjectiveWool(item)).orElse(false); } private Optional getTeamsInventory(Player bukkitPlayer) { From 952e20b5e9e851b78c023212e54b654d558e48e7 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 13:22:44 -0500 Subject: [PATCH 13/19] oBlah -> optBlah --- .../pgm/mutation/types/kit/TeamChestMutation.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index a76fbcda0..e17084926 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -37,11 +37,11 @@ public class TeamChestMutation extends KitMutation { final Map teamChests = new WeakHashMap<>(); - final Optional oWmm; + final Optional optWools; public TeamChestMutation(Match match) { super(match, false); - oWmm = match().module(WoolMatchModule.class); + optWools = match().module(WoolMatchModule.class); for (Party party : match().getParties()) { if (party.isParticipatingType()) { // Could the chest title be localized properly? @@ -60,16 +60,16 @@ public void kits(MatchPlayer player, List kits) { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onChestUse(PlayerInteractEvent event) { Player bukkitPlayer = event.getPlayer(); - Optional player = match().participant((Entity) bukkitPlayer); - if (!player.isPresent() || + Optional optPlayer = match().participant((Entity) bukkitPlayer); + if (optPlayer.isPresent() || !(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) || event.getItem() == null || event.getItem().getType() != TOOL_TYPE) { return; } - Optional oTeamInventory = getTeamsInventory(bukkitPlayer); - oTeamInventory.ifPresent(teamInventory -> { + Optional optTeamInventory = getTeamsInventory(bukkitPlayer); + optTeamInventory.ifPresent(teamInventory -> { event.setCancelled(true); // If the item is in the off-hand slot, it wont get put back visually for the player without this. if(event.getHand() == EquipmentSlot.OFF_HAND) event.getActor().updateInventory(); @@ -109,7 +109,7 @@ public void onInventoryClick(InventoryClickEvent event) { private boolean isBlacklistedItem(ItemStack item) { return item.getType() == TOOL_TYPE || - oWmm.map(w -> w.isObjectiveWool(item)).orElse(false); + optWools.map(w -> w.isObjectiveWool(item)).orElse(false); } private Optional getTeamsInventory(Player bukkitPlayer) { From da422626349ae4860f2560704ec08d92bfa8a706 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 13:24:10 -0500 Subject: [PATCH 14/19] Better boolean logic with Optionals --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index e17084926..99ecfd701 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -84,7 +84,7 @@ public void onInventoryClick(InventoryClickEvent event) { // No putting blacklisted items (ender chest, possibly wool) into the chest Optional teamChest = getTeamsInventory(event.getActor()); - if (teamChest.filter(teamInventory -> teamInventory.equals(event.getView().getTopInventory())).isPresent() && + if (teamChest.map(teamInventory -> teamInventory.equals(event.getView().getTopInventory())).orElse(false) && isBlacklistedItem(event.getCurrentItem())) { event.setCancelled(true); return; From a10f8e4d547ce269440a7405a5a0a76193e85876 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 13:30:52 -0500 Subject: [PATCH 15/19] enable() + disable() --- .../oc/pgm/mutation/types/kit/TeamChestMutation.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 99ecfd701..02e3600a0 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -42,6 +42,11 @@ public class TeamChestMutation extends KitMutation { public TeamChestMutation(Match match) { super(match, false); optWools = match().module(WoolMatchModule.class); + } + + @Override + public void enable() { + super.enable(); for (Party party : match().getParties()) { if (party.isParticipatingType()) { // Could the chest title be localized properly? @@ -50,6 +55,11 @@ public TeamChestMutation(Match match) { } } + @Override + public void disable() { + teamChests.clear(); + } + @Override public void kits(MatchPlayer player, List kits) { super.kits(player, kits); From 33863c32f7cca44eb601079f77047990db1b8ba1 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 13:33:57 -0500 Subject: [PATCH 16/19] streamline `getTeamsInventory` --- .../tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 02e3600a0..60fce751b 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -123,12 +123,8 @@ private boolean isBlacklistedItem(ItemStack item) { } private Optional getTeamsInventory(Player bukkitPlayer) { - MatchPlayer player = match().getPlayer(bukkitPlayer); - Party team = player.getParty(); - - if (!team.isParticipating()) return Optional.empty(); - - return Optional.of(teamChests.get(team)); + return match().participant((Entity) bukkitPlayer) + .map(matchPlayer -> teamChests.get(matchPlayer.getParty())); } private Kit getKitForPlayer(MatchPlayer player) { From 2bf647208cbbe86a174cfa6f6244ca65b40cfffd Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 13:48:01 -0500 Subject: [PATCH 17/19] Blep --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index 60fce751b..d8fbcc805 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -71,7 +71,7 @@ public void kits(MatchPlayer player, List kits) { public void onChestUse(PlayerInteractEvent event) { Player bukkitPlayer = event.getPlayer(); Optional optPlayer = match().participant((Entity) bukkitPlayer); - if (optPlayer.isPresent() || + if (!optPlayer.isPresent() || !(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) || event.getItem() == null || event.getItem().getType() != TOOL_TYPE) { @@ -93,7 +93,7 @@ public void onInventoryClick(InventoryClickEvent event) { if (event.getCurrentItem() == null) return; // No putting blacklisted items (ender chest, possibly wool) into the chest - Optional teamChest = getTeamsInventory(event.getActor()); + Optional teamChest = getTeamsInventory(player); if (teamChest.map(teamInventory -> teamInventory.equals(event.getView().getTopInventory())).orElse(false) && isBlacklistedItem(event.getCurrentItem())) { event.setCancelled(true); From 9b27ca8611f3c8137ec897041b638a0ff1fc9fe7 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Sat, 9 Jun 2018 14:43:27 -0500 Subject: [PATCH 18/19] Last round of fixes --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index d8fbcc805..c639352d5 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -49,7 +49,7 @@ public void enable() { super.enable(); for (Party party : match().getParties()) { if (party.isParticipatingType()) { - // Could the chest title be localized properly? + // TODO: Could the chest title be localized properly? teamChests.put(party, match().getServer().createInventory(null, CHEST_SIZE)); } } @@ -57,6 +57,7 @@ public void enable() { @Override public void disable() { + super.disable(); teamChests.clear(); } @@ -67,7 +68,7 @@ public void kits(MatchPlayer player, List kits) { } // Open shared inventory instead of placing the chest - @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + @EventHandler(priority = EventPriority.HIGHEST) public void onChestUse(PlayerInteractEvent event) { Player bukkitPlayer = event.getPlayer(); Optional optPlayer = match().participant((Entity) bukkitPlayer); From dcd7c1c0a68e07f98c8124426f750dcf62a83998 Mon Sep 17 00:00:00 2001 From: Geoff Yoerger Date: Thu, 21 Jun 2018 15:59:58 -0500 Subject: [PATCH 19/19] Last fix --- .../java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java index c639352d5..b6e398c18 100644 --- a/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java +++ b/PGM/src/main/java/tc/oc/pgm/mutation/types/kit/TeamChestMutation.java @@ -57,8 +57,8 @@ public void enable() { @Override public void disable() { - super.disable(); teamChests.clear(); + super.disable(); } @Override