From d9897191def99de482acbacd2ab4b56210d5d002 Mon Sep 17 00:00:00 2001 From: asofold Date: Sun, 1 Sep 2013 18:46:30 +0200 Subject: [PATCH] More on closing inventories: a) only close if open b) also on teleport. --- .../checks/inventory/InventoryListener.java | 56 ++++--------------- .../nocheatplus/utilities/InventoryUtil.java | 46 +++++++++++++++ 2 files changed, 58 insertions(+), 44 deletions(-) diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryListener.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryListener.java index efc5c014..31187e5b 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryListener.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryListener.java @@ -3,7 +3,6 @@ package fr.neatmonster.nocheatplus.checks.inventory; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.entity.Entity; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Item; import org.bukkit.entity.Player; @@ -21,6 +20,7 @@ import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.event.player.PlayerPortalEvent; +import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; @@ -29,8 +29,7 @@ import fr.neatmonster.nocheatplus.checks.CheckListener; import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.combined.Combined; import fr.neatmonster.nocheatplus.checks.combined.Improbable; -import fr.neatmonster.nocheatplus.config.ConfPaths; -import fr.neatmonster.nocheatplus.config.ConfigManager; +import fr.neatmonster.nocheatplus.utilities.InventoryUtil; /* * M""M dP @@ -327,60 +326,29 @@ public class InventoryListener extends CheckListener { @EventHandler(priority = EventPriority.MONITOR) public void onPlayerChangedWorld(final PlayerChangedWorldEvent event){ - if (shouldEnsureCloseInventories()){ - closePlayerInventoryRecursively(event.getPlayer()); + if (InventoryUtil.shouldEnsureCloseInventories()){ + InventoryUtil.closePlayerInventoryRecursively(event.getPlayer()); } } @EventHandler(priority = EventPriority.MONITOR) public void onPlayerPortal(final PlayerPortalEvent event){ - if (shouldEnsureCloseInventories()){ - closePlayerInventoryRecursively(event.getPlayer()); + if (InventoryUtil.shouldEnsureCloseInventories()){ + InventoryUtil.closePlayerInventoryRecursively(event.getPlayer()); } } @EventHandler(priority = EventPriority.MONITOR) public void onEntityPortal(final EntityPortalEnterEvent event){ - if (shouldEnsureCloseInventories()){ - closePlayerInventoryRecursively(event.getEntity()); + if (InventoryUtil.shouldEnsureCloseInventories()){ + InventoryUtil.closePlayerInventoryRecursively(event.getEntity()); } } - /** - * Test if global config has set the flag. - * @return - */ - public static boolean shouldEnsureCloseInventories(){ - return ConfigManager.getConfigFile().getBoolean(ConfPaths.INVENTORY_ENSURECLOSE, true); - } - - /** - * Search for players / passengers. - * @param entity - */ - public static void closePlayerInventoryRecursively(Entity entity){ - // Find a player. - while (entity != null){ - if (entity instanceof Player){ - closeInventory((Player) entity); - } - final Entity passenger = entity.getPassenger(); - if (entity.equals(passenger)){ - // Just in case :9. - break; - } - else{ - entity = passenger; - } + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerTeleport(final PlayerTeleportEvent event){ + if (InventoryUtil.shouldEnsureCloseInventories()){ + InventoryUtil.closePlayerInventoryRecursively(event.getPlayer()); } } - - /** - * Close one players inventory, if open. - * @param player - */ - public static void closeInventory(final Player player){ - // TODO: Better check if any !?. - player.closeInventory(); - } } diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/InventoryUtil.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/InventoryUtil.java index a66f0cef..2de69c5e 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/InventoryUtil.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/utilities/InventoryUtil.java @@ -1,9 +1,15 @@ package fr.neatmonster.nocheatplus.utilities; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; +import fr.neatmonster.nocheatplus.config.ConfPaths; +import fr.neatmonster.nocheatplus.config.ConfigManager; + /** * Auxiliary/convenience methods for inventories. * @author mc_dev @@ -63,4 +69,44 @@ public class InventoryUtil { return getStackCount(view.getBottomInventory(), reference) + getStackCount(view.getTopInventory(), reference); } + /** + * Test if global config has the flag set. + * @return + */ + public static boolean shouldEnsureCloseInventories(){ + return ConfigManager.getConfigFile().getBoolean(ConfPaths.INVENTORY_ENSURECLOSE, true); + } + + /** + * Search for players / passengers. + * @param entity + */ + public static void closePlayerInventoryRecursively(Entity entity){ + // Find a player. + while (entity != null){ + if (entity instanceof Player){ + closeOpenInventory((Player) entity); + } + final Entity passenger = entity.getPassenger(); + if (entity.equals(passenger)){ + // Just in case :9. + break; + } + else{ + entity = passenger; + } + } + } + + /** + * Close one players inventory, if open. This ignores InventoryType.CRAFTING. + * @param player + */ + public static void closeOpenInventory(final Player player){ + final InventoryView view = player.getOpenInventory(); + if (view != null && view.getType() != InventoryType.CRAFTING) { + player.closeInventory(); + } + } + }