From 06a851d43fc6c0e6f6b94c7f67518f54074ec65c Mon Sep 17 00:00:00 2001 From: applenick Date: Mon, 21 Mar 2022 01:12:26 -0700 Subject: [PATCH] Revert "Remove chest protection inventory check (#977)" This reverts commit 963f02a2974c471f356b782b5e0fd741a77869fc. --- .../inventory/ViewInventoryMatchModule.java | 37 +++++++++++++++++++ .../pgm/modules/EventFilterMatchModule.java | 8 ---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/inventory/ViewInventoryMatchModule.java b/core/src/main/java/tc/oc/pgm/inventory/ViewInventoryMatchModule.java index 156314ad74..f8fadc3e3c 100644 --- a/core/src/main/java/tc/oc/pgm/inventory/ViewInventoryMatchModule.java +++ b/core/src/main/java/tc/oc/pgm/inventory/ViewInventoryMatchModule.java @@ -51,6 +51,7 @@ import tc.oc.pgm.events.PlayerPartyChangeEvent; import tc.oc.pgm.kits.WalkSpeedKit; import tc.oc.pgm.spawns.events.ParticipantSpawnEvent; +import tc.oc.pgm.util.TimeUtils; import tc.oc.pgm.util.attribute.Attribute; import tc.oc.pgm.util.bukkit.BukkitUtils; import tc.oc.pgm.util.named.NameStyle; @@ -60,6 +61,12 @@ @ListenerScope(MatchScope.LOADED) public class ViewInventoryMatchModule implements MatchModule, Listener { + /** + * Amount of milliseconds after the match begins where players may not add / remove items from + * chests. + */ + public static final Duration CHEST_PROTECT_TIME = Duration.ofSeconds(2); + public static final Duration TICK = Duration.ofMillis(50); protected final HashMap monitoredInventories = new HashMap<>(); @@ -102,6 +109,26 @@ private void checkAllMonitoredInventories() { } } + @EventHandler(ignoreCancelled = true) + public void checkInventoryClick(final InventoryClickEvent event) { + if (event.getWhoClicked() instanceof Player) { + MatchPlayer player = this.match.getPlayer((Player) event.getWhoClicked()); + if (player == null) { + return; + } + // we only cancel when the view is a chest because the other views tend to crash + if (!allowedInventoryType(event.getInventory().getType())) { + // cancel the click if the player cannot interact with the world or if the match has just + // started + if (!player.canInteract() + || (player.getMatch().isRunning() + && TimeUtils.isShorterThan(player.getMatch().getDuration(), CHEST_PROTECT_TIME))) { + event.setCancelled(true); + } + } + } + } + @EventHandler public void closeMonitoredInventory(final InventoryCloseEvent event) { this.monitoredInventories.remove(event.getPlayer().getName()); @@ -266,6 +293,16 @@ public boolean canPreviewInventory(MatchPlayer viewer, MatchPlayer holder) { return viewer.isObserving() && holder.isAlive(); } + protected static boolean allowedInventoryType(InventoryType type) { + switch (type) { + case CREATIVE: + case PLAYER: + return true; + default: + return false; + } + } + protected void scheduleCheck(Player updater) { if (this.updateQueue.containsKey(updater.getName())) return; diff --git a/core/src/main/java/tc/oc/pgm/modules/EventFilterMatchModule.java b/core/src/main/java/tc/oc/pgm/modules/EventFilterMatchModule.java index 80651d9cc4..df809d62b4 100644 --- a/core/src/main/java/tc/oc/pgm/modules/EventFilterMatchModule.java +++ b/core/src/main/java/tc/oc/pgm/modules/EventFilterMatchModule.java @@ -26,7 +26,6 @@ import org.bukkit.event.hanging.HangingBreakByEntityEvent; import org.bukkit.event.hanging.HangingBreakEvent; import org.bukkit.event.inventory.ClickType; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.PlayerArmorStandManipulateEvent; import org.bukkit.event.player.PlayerBedEnterEvent; import org.bukkit.event.player.PlayerDropItemEvent; @@ -334,13 +333,6 @@ public void onPotionSplash(final PotionSplashEvent event) { // -- Player item/inventory actions -- // ----------------------------------- - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onInventoryClick(final InventoryClickEvent event) { - if (!event.getInventory().equals(event.getWhoClicked().getInventory())) { - cancelUnlessInteracting(event, event.getWhoClicked()); - } - } - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onPlayerDropItem(final PlayerDropItemEvent event) { if (match.getParticipant(event.getPlayer()) == null) {