From 6fb69170b268f597f7ac02d1e9fe364df850fffe Mon Sep 17 00:00:00 2001 From: xGinko Date: Thu, 7 Mar 2024 21:39:20 +0100 Subject: [PATCH] move common listeners into interface --- .../enums/ItemLegality.java | 7 + .../modules/AnarchyExploitFixesModule.java | 9 +- .../illegals/items/BannedItemNames.java | 82 +---- .../illegals/items/BannedMaterials.java | 121 +++---- .../illegals/items/IllegalItemModule.java | 94 +++++- .../illegals/items/OverstackedItems.java | 86 +---- .../illegals/items/PlayerHeadItems.java | 95 +----- .../modules/illegals/items/SpawnEggItems.java | 89 +---- .../illegals/items/UnbreakableItems.java | 96 +----- .../items/enchantments/HigherEnchants.java | 86 +---- .../enchantments/InapplicableEnchants.java | 93 +---- .../enchantments/IncompatibleEnchants.java | 317 +++++++----------- .../illegals/items/nbt/CustomNBTFilter.java | 89 ++--- .../items/nbt/NBTFilledStorageItem.java | 107 ++---- .../enums/ItemLegality.java | 7 + .../modules/AnarchyExploitFixesModule.java | 8 +- .../illegals/items/BannedItemNames.java | 69 +--- .../illegals/items/BannedMaterials.java | 132 ++++---- .../illegals/items/IllegalItemModule.java | 77 ++++- .../illegals/items/OverstackedItems.java | 78 ++--- .../illegals/items/PlayerHeadItems.java | 79 +---- .../modules/illegals/items/SpawnEggItems.java | 73 +--- .../illegals/items/UnbreakableItems.java | 133 ++------ .../items/datavalues/CustomDataValues.java | 83 ++--- .../items/datavalues/IllegalGoldenApples.java | 71 +--- .../items/enchantments/HigherEnchants.java | 70 +--- .../enchantments/InapplicableEnchants.java | 75 ++--- .../enchantments/IncompatibleEnchants.java | 306 +++++++---------- .../illegals/items/nbt/CustomNBTFilter.java | 82 ++--- .../items/nbt/NBTFilledStorageItem.java | 98 ++---- 30 files changed, 839 insertions(+), 1973 deletions(-) create mode 100644 AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/enums/ItemLegality.java create mode 100644 AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/enums/ItemLegality.java diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/enums/ItemLegality.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/enums/ItemLegality.java new file mode 100644 index 000000000..5ce7eb88b --- /dev/null +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/enums/ItemLegality.java @@ -0,0 +1,7 @@ +package me.moomoo.anarchyexploitfixes.enums; + +public enum ItemLegality { + LEGAL, + ILLEGAL, + CONTAINS_ILLEGAL, +} diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java index 1bbdc9b50..45f4ceeca 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java @@ -110,15 +110,16 @@ static void reloadModules() { modules.add(new InapplicableEnchants()); modules.add(new IncompatibleEnchants()); // Other - modules.add(new BannedItemNames()); modules.add(new OverstackedItems()); modules.add(new UnbreakableItems()); - modules.add(new PlayerHeadItems()); - modules.add(new SpawnEggItems()); modules.add(new BannedMaterials()); + modules.add(new SpawnEggItems()); + modules.add(new PlayerHeadItems()); + modules.add(new BannedItemNames()); // NBT - modules.add(new CustomNBTFilter()); modules.add(new NBTFilledStorageItem()); + modules.add(new CustomNBTFilter()); + /* Lag Preventions */ diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedItemNames.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedItemNames.java index 392dfdcde..5ac1358de 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedItemNames.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedItemNames.java @@ -1,10 +1,10 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; import com.destroystokyo.paper.event.inventory.PrepareResultEvent; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; @@ -12,10 +12,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.Listener; import org.bukkit.inventory.ItemStack; import java.util.HashSet; @@ -23,7 +20,7 @@ import java.util.Set; import java.util.stream.Collectors; -public class BannedItemNames implements IllegalItemModule { +public class BannedItemNames implements IllegalItemModule, Listener { private final Set coloredNames; private final Set plainTextNames; @@ -31,7 +28,8 @@ public class BannedItemNames implements IllegalItemModule { public BannedItemNames() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("illegals.banned-item-names.enable", "Resets an item's name if configured in the config."); + config.addComment("illegals.banned-item-names.enable", + "Resets an item's name if configured in the config. Bypass permission: " + bypassPermission().get()); this.plainTextNames = new HashSet<>(config.getList("illegals.banned-item-names.plain-names", List.of("Super Insane Mega Sussy Item"))); this.coloredNames = config.getList("illegals.banned-item-names.color-sensitive-names", @@ -68,7 +66,12 @@ public void disable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_BANNEDNAME; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType().isAir() || !itemStack.hasItemMeta()) { return ItemLegality.LEGAL; } @@ -90,71 +93,12 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onPrepareResult(PrepareResultEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getView().getPlayer())) return; + if (CachingPermTool.hasPermission(bypassPermission(), event.getView().getPlayer())) return; ItemStack resultItem = event.getResult(); - if (getLegality(resultItem) == ItemLegality.ILLEGAL) { + if (determineLegality(resultItem) == ItemLegality.ILLEGAL) { handleItem(resultItem, ItemLegality.ILLEGAL); event.setResult(resultItem); } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedMaterials.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedMaterials.java index de4b1a028..f97817622 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedMaterials.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedMaterials.java @@ -1,20 +1,18 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.inventory.ItemStack; import org.slf4j.event.Level; @@ -24,17 +22,20 @@ import java.util.Set; import java.util.stream.Collectors; -public class BannedMaterials implements IllegalItemModule { +public class BannedMaterials implements IllegalItemModule, Listener { private final Set bannedMaterials; - private final boolean shouldDelete, checkStored; + private ScheduledTask periodicInvCheck; + private Listener hopperListener; + private final int checkPeriod; + private final boolean doSomething, checkStored, doPeriodicCheck, preventHopperBypass; public BannedMaterials() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.ban-specific-materials.enable", - "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL.get()); - this.shouldDelete = config.getBoolean("illegals.ban-specific-materials.delete-illegals", false); + "Bypass permission: " + bypassPermission().get()); + this.doSomething = config.getBoolean("illegals.ban-specific-materials.delete-item", false); this.checkStored = config.getBoolean("illegals.ban-specific-materials.check-stored-items", false); this.bannedMaterials = config.getList("illegals.ban-specific-materials.banned-materials", List.of( "CHAIN_COMMAND_BLOCK", @@ -60,6 +61,12 @@ public BannedMaterials() { }) .filter(Objects::nonNull) .collect(Collectors.toCollection(HashSet::new)); + this.preventHopperBypass = config.getBoolean("illegals.enchantments.ban-specific-materials.prevent-hopper32k-mechanic", false, """ + Prevents Hopper32k mechanic of placing a shulker containing illegals on top of a hopper and using the illegal\s + out of the hoppers inventory.\s + WARNING: Hooks into InventoryMoveItemEvent, which can become resource intense. Enable only if you need to."""); + this.doPeriodicCheck = config.getBoolean("illegals.enchantments.ban-specific-materials.periodically-check-player-inventories.enable", false); + this.checkPeriod = config.getInt("illegals.enchantments.ban-specific-materials.periodically-check-player-inventories.check-period-in-ticks", 20); } @Override @@ -74,8 +81,26 @@ public String category() { @Override public void enable() { - AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + final AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + + if (preventHopperBypass) { + this.hopperListener = new Listener() { + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onInventoryMove(InventoryMoveItemEvent event) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { + event.setCancelled(true); + } + } + }; + } + + if (doPeriodicCheck) { + this.periodicInvCheck = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, periodic_inv_check -> + plugin.getServer().getOnlinePlayers().forEach(player -> player.getScheduler().run(plugin, scheduled_inv_check -> + player.getInventory().forEach(item -> handleItem(item, determineLegality(item))), null)), checkPeriod, checkPeriod); + } } @Override @@ -85,11 +110,18 @@ public boolean shouldEnable() { @Override public void disable() { + if (periodicInvCheck != null) periodicInvCheck.cancel(); + if (hopperListener != null) HandlerList.unregisterAll(hopperListener); HandlerList.unregisterAll(this); } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null) { return ItemLegality.LEGAL; } @@ -102,7 +134,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -114,67 +146,10 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult != ItemLegality.LEGAL) { - if (shouldDelete) itemStack.setType(Material.AIR); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL, event.getWhoClicked())) return; + if (!doSomething) return; - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); + if (checkResult != ItemLegality.LEGAL) { + itemStack.setType(Material.AIR); } } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/IllegalItemModule.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/IllegalItemModule.java index c9a551617..3eee99fff 100644 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/IllegalItemModule.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/IllegalItemModule.java @@ -1,17 +1,101 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; +import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; +import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockDispenseEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.player.PlayerAttemptPickupItemEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; public interface IllegalItemModule extends AnarchyExploitFixesModule, Listener { - ItemLegality getLegality(ItemStack itemStack); + AEFPermission bypassPermission(); + ItemLegality determineLegality(ItemStack itemStack); void handleItem(ItemStack itemStack, ItemLegality checkResult); - enum ItemLegality { - LEGAL, - ILLEGAL, - CONTAINS_ILLEGAL, + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onDispense(BlockDispenseEvent event) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onInventoryClick(InventoryClickEvent event) { + ItemStack current = event.getCurrentItem(); + ItemStack cursor = event.getCursor(); + if (!CachingPermTool.hasPermission(bypassPermission(), event.getWhoClicked())) { + handleItem(current, determineLegality(current)); + handleItem(cursor, determineLegality(cursor)); + for (ItemStack invItem : event.getWhoClicked().getInventory()) { + handleItem(invItem, determineLegality(invItem)); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onAttack(PrePlayerAttackEntityEvent event) { + ItemStack mainHand = event.getPlayer().getInventory().getItemInMainHand(); + final ItemLegality mainHandLegality = determineLegality(mainHand); + if (mainHandLegality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + event.setCancelled(true); + handleItem(mainHand, mainHandLegality); + } + ItemStack offHand = event.getPlayer().getInventory().getItemInOffHand(); + final ItemLegality offHandLegality = determineLegality(offHand); + if (offHandLegality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + event.setCancelled(true); + handleItem(offHand, offHandLegality); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onPlayerPickupItem(PlayerAttemptPickupItemEvent event) { + ItemStack droppedItem = event.getItem().getItemStack(); + final ItemLegality legality = determineLegality(droppedItem); + if (legality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + event.setCancelled(true); + handleItem(droppedItem, legality); + event.getItem().setItemStack(droppedItem); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onPlayerDropItem(PlayerDropItemEvent event) { + ItemStack droppedItem = event.getItemDrop().getItemStack(); + final ItemLegality legality = determineLegality(droppedItem); + if (legality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + handleItem(droppedItem, legality); + event.getItemDrop().setItemStack(droppedItem); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) + default void onInteract(PlayerInteractEvent event) { + ItemStack interactItem = event.getItem(); + final ItemLegality legality = determineLegality(interactItem); + if (legality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + event.setCancelled(true); + handleItem(interactItem, legality); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onInteractEntity(PlayerInteractEntityEvent event) { + ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); + final ItemLegality legality = determineLegality(handItem); + if (legality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + event.setCancelled(true); + handleItem(handItem, legality); + } } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/OverstackedItems.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/OverstackedItems.java index 266f09992..95ceffcec 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/OverstackedItems.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/OverstackedItems.java @@ -1,23 +1,18 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.slf4j.event.Level; @@ -27,7 +22,7 @@ import java.util.Set; import java.util.stream.Collectors; -public class OverstackedItems implements IllegalItemModule { +public class OverstackedItems implements IllegalItemModule, Listener { private final Set whitelistedTypes; private ScheduledTask periodicInvCheck; @@ -40,7 +35,7 @@ public OverstackedItems() { Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.revert-overstacked-items.enable", "Revert illegally high stacked items.\n" + - "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_OVERSTACKED.get()); + "Bypass permission: " + bypassPermission().get()); this.useWhitelist = config.getBoolean("illegals.revert-overstacked-items.item-whitelist-enabled", false); this.blacklistMode = config.getBoolean("illegals.revert-overstacked-items.use-as-blacklist-instead", true); this.checkStored = config.getBoolean("illegals.revert-overstacked-items.check-stored-items", false); @@ -76,14 +71,15 @@ public String category() { @Override public void enable() { - AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + final AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); if (preventHopperBypass) { this.hopperListener = new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -94,7 +90,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { this.periodicInvCheck = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, periodic_inv_check -> plugin.getServer().getOnlinePlayers().forEach(player -> player.getScheduler().run(plugin, scheduled_inv_check -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item))), null)), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item))), null)), checkPeriod, checkPeriod); } } @@ -111,7 +107,12 @@ public void disable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_OVERSTACKED; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType().isAir()) { return ItemLegality.LEGAL; } @@ -126,7 +127,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -143,63 +144,4 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { case CONTAINS_ILLEGAL -> itemStack.setType(Material.AIR); } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_OVERSTACKED, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_OVERSTACKED, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_OVERSTACKED, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_OVERSTACKED, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_OVERSTACKED, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/PlayerHeadItems.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/PlayerHeadItems.java index 2c6c298a1..59a6870b9 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/PlayerHeadItems.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/PlayerHeadItems.java @@ -1,20 +1,11 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.HandlerList; -import org.bukkit.event.block.BlockDispenseEvent; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; public class PlayerHeadItems implements IllegalItemModule { @@ -25,7 +16,7 @@ public PlayerHeadItems() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.ban-player-heads.enable", - "Prevents usage of player heads. Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD.get()); + "Prevents usage of player heads. Bypass permission: " + bypassPermission().get()); this.shouldDelete = config.getBoolean("illegals.ban-player-heads.remove-items", false); this.checkStored = config.getBoolean("illegals.ban-player-heads.check-stored-items", false, "Will delete shulker/bundle if they contain any player heads."); @@ -42,10 +33,7 @@ public String category() { } @Override - public void enable() { - AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } + public void enable() {} @Override public boolean shouldEnable() { @@ -53,12 +41,15 @@ public boolean shouldEnable() { } @Override - public void disable() { - HandlerList.unregisterAll(this); + public void disable() {} + + @Override + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD; } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null) { return ItemLegality.LEGAL; } @@ -73,7 +64,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -89,70 +80,4 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { if (shouldDelete) itemStack.setType(Material.AIR); } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onDispense(BlockDispenseEvent event) { - switch (event.getItem().getType()) { - case PLAYER_HEAD, PLAYER_WALL_HEAD -> event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/SpawnEggItems.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/SpawnEggItems.java index abc2b877f..6e5124b39 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/SpawnEggItems.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/SpawnEggItems.java @@ -1,21 +1,13 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.MaterialUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; -import org.bukkit.event.block.BlockDispenseEvent; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; public class SpawnEggItems implements IllegalItemModule { @@ -59,7 +51,12 @@ public void disable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_SPAWNEGG; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null) { return ItemLegality.LEGAL; } @@ -72,7 +69,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -84,74 +81,10 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult != ItemLegality.LEGAL) { - if (shouldDelete) itemStack.setType(Material.AIR); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onDispense(BlockDispenseEvent event) { - if (MaterialUtil.isSpawnEgg(event.getItem())) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return; + if (!shouldDelete) return; - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); + if (checkResult != ItemLegality.LEGAL) { + itemStack.setType(Material.AIR); } } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/UnbreakableItems.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/UnbreakableItems.java index 9815343e7..5fb32c34b 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/UnbreakableItems.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/UnbreakableItems.java @@ -1,24 +1,18 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerAttemptPickupItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; @@ -41,8 +35,7 @@ public class UnbreakableItems implements IllegalItemModule { public UnbreakableItems() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("illegals.revert-unbreakables.enable", - "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE.get()); + config.addComment("illegals.revert-unbreakables.enable", "Bypass permission: " + bypassPermission().get()); this.useWhitelist = config.getBoolean("illegals.revert-unbreakables.item-whitelist-enabled", false); this.blacklistMode = config.getBoolean("illegals.revert-unbreakables.use-as-blacklist-instead", false); this.checkStored = config.getBoolean("illegals.revert-unbreakables.check-stored-items", false, @@ -86,7 +79,7 @@ public void enable() { this.hopperListener = new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -97,7 +90,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { this.periodicInvCheck = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, periodic_inv_check -> plugin.getServer().getOnlinePlayers().forEach(player -> player.getScheduler().run(plugin, scheduled_inv_check -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item))), null)), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item))), null)), checkPeriod, checkPeriod); } } @@ -114,7 +107,12 @@ public void disable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType().isAir()) { return ItemLegality.LEGAL; } @@ -129,7 +127,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -161,76 +159,4 @@ private boolean isUnbreakable(ItemStack item) { final int remainingDurability = itemMaxDurability - damageMeta.getDamage(); return remainingDurability > itemMaxDurability || remainingDurability < 0; } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerAttemptPickupItemEvent event) { - ItemStack droppedItem = event.getItem().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(droppedItem, result); - event.getItem().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/HigherEnchants.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/HigherEnchants.java index b4992b1c5..e57a0778d 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/HigherEnchants.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/HigherEnchants.java @@ -1,25 +1,20 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.slf4j.event.Level; @@ -41,7 +36,7 @@ public HigherEnchants() { Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.enchantments.higher-enchants.enable", "Reverts or prevents usage of 32ks / 255s"); this.checkStored = config.getBoolean("illegals.enchantments.higher-enchants.check-stored-items", false); - this.doSomething = config.getBoolean("illegals.enchantments.higher-enchants.delete-or-revert-item", false, + this.doSomething = config.getBoolean("illegals.enchantments.higher-enchants.delete-or-revert-item", true, "Bundles or shulkers will be deleted if you have check-stored-items enabled."); this.onlySomeEnchants = config.getBoolean("illegals.enchantments.higher-enchants.only-specific-enchants", false); this.specificEnchants = config.getList("illegals.enchantments.higher-enchants.specific-enchants", List.of("DIG_SPEED")) @@ -87,14 +82,15 @@ public String category() { @Override public void enable() { - AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + final AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); if (preventHopperBypass) { this.hopperListener = new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -105,7 +101,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { this.periodicInvCheck = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, periodic_inv_check -> plugin.getServer().getOnlinePlayers().forEach(player -> player.getScheduler().run(plugin, scheduled_inv_check -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item))), null)), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item))), null)), checkPeriod, checkPeriod); } } @@ -122,7 +118,12 @@ public void disable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType().isAir()) { return ItemLegality.LEGAL; } @@ -141,7 +142,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -155,6 +156,7 @@ public ItemLegality getLegality(ItemStack itemStack) { public void handleItem(ItemStack itemStack, ItemLegality checkResult) { if (!doSomething) return; switch (checkResult) { + case CONTAINS_ILLEGAL -> itemStack.setType(Material.AIR); case ILLEGAL -> { for (Map.Entry enchant : itemStack.getEnchantments().entrySet()) { if (enchant.getValue() > enchant.getKey().getMaxLevel()) { @@ -163,66 +165,6 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { } } } - case CONTAINS_ILLEGAL -> itemStack.setType(Material.AIR); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); } } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/InapplicableEnchants.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/InapplicableEnchants.java index 19d45b6ee..3b76fc2a1 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/InapplicableEnchants.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/InapplicableEnchants.java @@ -1,32 +1,27 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.slf4j.event.Level; import java.util.*; import java.util.stream.Collectors; -public class InapplicableEnchants implements IllegalItemModule { +public class InapplicableEnchants implements IllegalItemModule, Listener { private final Set whitelistedTypes; private ScheduledTask periodicInvCheck; @@ -37,7 +32,8 @@ public class InapplicableEnchants implements IllegalItemModule { public InapplicableEnchants() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - this.checkStored = config.getBoolean("illegals.enchantments.higher-enchants.check-stored-items", false); + this.doSomething = config.getBoolean("illegals.enchantments.inapplicable-enchants.delete-or-revert-item", true); + this.checkStored = config.getBoolean("illegals.enchantments.inapplicable-enchants.check-stored-items", false); this.useWhitelist = config.getBoolean("illegals.enchantments.inapplicable-enchants.item-whitelist-enabled", true); this.blacklistMode = config.getBoolean("illegals.enchantments.inapplicable-enchants.use-as-blacklist-instead", false); this.whitelistedTypes = config.getList("illegals.enchantments.inapplicable-enchants.whitelisted-items", List.of("GOLDEN_APPLE")) @@ -56,7 +52,7 @@ public InapplicableEnchants() { Prevents Hopper32k mechanic of placing a shulker containing illegals on top of a hopper and using the illegal\s out of the hoppers inventory.\s WARNING: Hooks into InventoryMoveItemEvent, which can become resource intense. Enable only if you need to."""); - this.doSomething = config.getBoolean("illegals.enchantments.inapplicable-enchants.delete-shulker-if-contains-inapplicable-enchant", false); + this.doPeriodicCheck = config.getBoolean("illegals.enchantments.inapplicable-enchants.periodically-check-player-inventories.enable", false); this.checkPeriod = config.getInt("illegals.enchantments.inapplicable-enchants.periodically-check-player-inventories.check-period-in-ticks", 20); } @@ -73,14 +69,15 @@ public String category() { @Override public void enable() { - AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + final AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); if (preventHopperBypass) { this.hopperListener = new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -91,7 +88,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { this.periodicInvCheck = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, periodic_inv_check -> plugin.getServer().getOnlinePlayers().forEach(player -> player.getScheduler().run(plugin, scheduled_inv_check -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item))), null)), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item))), null)), checkPeriod, checkPeriod); } } @@ -103,12 +100,16 @@ public boolean shouldEnable() { @Override public void disable() { if (periodicInvCheck != null) periodicInvCheck.cancel(); - if (hopperListener != null) HandlerList.unregisterAll(hopperListener); HandlerList.unregisterAll(this); } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType().isAir()) { return ItemLegality.LEGAL; } @@ -125,7 +126,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -138,7 +139,9 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { if (!doSomething) return; + switch (checkResult) { + case CONTAINS_ILLEGAL -> itemStack.setType(Material.AIR); case ILLEGAL -> { for (Map.Entry enchant : itemStack.getEnchantments().entrySet()) { if (!enchant.getKey().canEnchantItem(itemStack)) { @@ -146,66 +149,6 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { } } } - case CONTAINS_ILLEGAL -> itemStack.setType(Material.AIR); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); } } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/IncompatibleEnchants.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/IncompatibleEnchants.java index 400ecb166..8c24e5752 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/IncompatibleEnchants.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/IncompatibleEnchants.java @@ -1,41 +1,55 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments; -import com.destroystokyo.paper.MaterialTags; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; +import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import me.moomoo.anarchyexploitfixes.utils.MaterialUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.inventory.ItemStack; import org.slf4j.event.Level; -import java.util.*; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; import java.util.stream.Collectors; -public class IncompatibleEnchants implements IllegalItemModule { +public class IncompatibleEnchants implements IllegalItemModule, Listener { private final Set whitelistedTypes; private ScheduledTask periodicInvCheck; + private Listener hopperListener; private final long checkPeriod; - private final boolean useWhitelist, blacklistMode, doPeriodicCheck; + private final boolean useWhitelist, blacklistMode, doPeriodicCheck, preventHopperBypass, checkStored, doSomething; + + private static final Set DAMAGE_ENCHANTS = Set.of( + Enchantment.DAMAGE_ALL, + Enchantment.DAMAGE_UNDEAD, + Enchantment.DAMAGE_ARTHROPODS); + private static final Set PROTECT_ENCHANTS = Set.of( + Enchantment.PROTECTION_ENVIRONMENTAL, + Enchantment.PROTECTION_EXPLOSIONS, + Enchantment.PROTECTION_FIRE, + Enchantment.PROTECTION_PROJECTILE); public IncompatibleEnchants() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("illegals.enchantments.incompatible-enchants.enable", "Reverts items like Armor with all Protection enchantments."); + config.addComment("illegals.enchantments.incompatible-enchants.enable", + "Reverts items like Armor with all Protection enchantments. Bypass permission: " + bypassPermission().get()); + this.checkStored = config.getBoolean("illegals.enchantments.incompatible-enchants.check-stored-items", false); + this.doSomething = config.getBoolean("illegals.enchantments.higher-enchants.delete-or-revert-item", true, + "Bundles or shulkers will be deleted if you have check-stored-items enabled."); this.useWhitelist = config.getBoolean("illegals.enchantments.incompatible-enchants.item-whitelist-enabled", true); this.blacklistMode = config.getBoolean("illegals.enchantments.incompatible-enchants.use-as-blacklist-instead", false); this.whitelistedTypes = config.getList("illegals.enchantments.incompatible-enchants.whitelisted-items", List.of("BOW")) @@ -50,6 +64,10 @@ public IncompatibleEnchants() { }) .filter(Objects::nonNull) .collect(Collectors.toCollection(HashSet::new)); + this.preventHopperBypass = config.getBoolean("illegals.enchantments.incompatible-enchants.prevent-hopper32k-mechanic", false, """ + Prevents Hopper32k mechanic of placing a shulker containing illegals on top of a hopper and using the illegal\s + out of the hoppers inventory.\s + WARNING: Hooks into InventoryMoveItemEvent, which can become resource intense. Enable only if you need to."""); this.doPeriodicCheck = config.getBoolean("illegals.enchantments.incompatible-enchants.periodically-check-player-inventories.enable", false); this.checkPeriod = config.getInt("illegals.enchantments.incompatible-enchants.periodically-check-player-inventories.check-period-in-ticks", 20); } @@ -66,13 +84,26 @@ public String category() { @Override public void enable() { - AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + final AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + if (preventHopperBypass) { + this.hopperListener = new Listener() { + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onInventoryMove(InventoryMoveItemEvent event) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { + event.setCancelled(true); + } + } + }; + plugin.getServer().getPluginManager().registerEvents(hopperListener, plugin); + } + if (doPeriodicCheck) { this.periodicInvCheck = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, periodic_inv_check -> plugin.getServer().getOnlinePlayers().forEach(player -> player.getScheduler().run(plugin, scheduled_inv_check -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item))), null)), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item))), null)), checkPeriod, checkPeriod); } } @@ -84,212 +115,100 @@ public boolean shouldEnable() { @Override public void disable() { if (periodicInvCheck != null) periodicInvCheck.cancel(); + if (hopperListener != null) HandlerList.unregisterAll(hopperListener); HandlerList.unregisterAll(this); } @Override - public ItemLegality getLegality(ItemStack itemStack) { - if (itemStack == null || itemStack.getType().isAir()) { - return ItemLegality.LEGAL; - } - - if (!useWhitelist || blacklistMode == whitelistedTypes.contains(itemStack.getType())) { - return ItemLegality.ILLEGAL; - } - - return ItemLegality.LEGAL; + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE; } @Override - public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult == ItemLegality.LEGAL) { - return; + public ItemLegality determineLegality(ItemStack itemStack) { + if (itemStack == null || itemStack.getType().isAir()) { + return ItemLegality.LEGAL; } - if (MaterialTags.ARMOR.isTagged(itemStack)) { - filterProtectionEnchants(itemStack); - filterCurses(itemStack); - if (MaterialTags.BOOTS.isTagged(itemStack)) { - filterBoots(itemStack); + if (!useWhitelist || blacklistMode == whitelistedTypes.contains(itemStack.getType())) { + final Set enchantments = itemStack.getEnchantments().keySet(); + if (!enchantments.isEmpty()) { + if (enchantments.contains(Enchantment.BINDING_CURSE) && enchantments.contains(Enchantment.VANISHING_CURSE)) + return ItemLegality.ILLEGAL; + if (enchantments.contains(Enchantment.MULTISHOT) && enchantments.contains(Enchantment.PIERCING)) + return ItemLegality.ILLEGAL; + if (enchantments.contains(Enchantment.RIPTIDE) && (enchantments.contains(Enchantment.LOYALTY) || enchantments.contains(Enchantment.CHANNELING))) + return ItemLegality.ILLEGAL; + if (enchantments.contains(Enchantment.ARROW_INFINITE) && enchantments.contains(Enchantment.MENDING)) + return ItemLegality.ILLEGAL; + if (enchantments.contains(Enchantment.SILK_TOUCH) && enchantments.contains(Enchantment.LOOT_BONUS_BLOCKS)) + return ItemLegality.ILLEGAL; + if (enchantments.contains(Enchantment.DEPTH_STRIDER) && enchantments.contains(Enchantment.FROST_WALKER)) + return ItemLegality.ILLEGAL; + if (DAMAGE_ENCHANTS.stream().filter(enchantments::contains).count() > 1L) + return ItemLegality.ILLEGAL; + if (PROTECT_ENCHANTS.stream().filter(enchantments::contains).count() > 1L) + return ItemLegality.ILLEGAL; } - return; - } - - if (MaterialUtil.isElytra(itemStack)) { - filterCurses(itemStack); - return; } - if (MaterialUtil.isTool(itemStack)) { - filterTools(itemStack); - if (MaterialTags.AXES.isTagged(itemStack)) { - filterSharpnessEnchants(itemStack); + if (checkStored) { + Iterable storedItems = ItemUtil.getStoredItems(itemStack); + if (storedItems != null) { + for (ItemStack stored : storedItems) { + if (determineLegality(stored) != ItemLegality.LEGAL) { + return ItemLegality.CONTAINS_ILLEGAL; + } + } } - return; } - if (MaterialTags.SWORDS.isTagged(itemStack)) { - filterSharpnessEnchants(itemStack); - return; - } - - if (MaterialUtil.isBow(itemStack)) { - filterBow(itemStack); - return; - } - - if (MaterialUtil.isTrident(itemStack)) { - filterTrident(itemStack); - return; - } - - if (MaterialUtil.isCrossbow(itemStack)) { - filterCrossbow(itemStack); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE, event.getPlayer())) return; - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE, event.getPlayer())) return; - - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE, event.getPlayer())) return; - - handleItem(handItem, result); - } - } - - private void filterCurses(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(Enchantment.BINDING_CURSE) && enchantments.containsKey(Enchantment.VANISHING_CURSE)) { - item.removeEnchantment(Enchantment.BINDING_CURSE); - } - } - - private void filterCrossbow(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(Enchantment.MULTISHOT) && enchantments.containsKey(Enchantment.PIERCING)) { - item.removeEnchantment(Enchantment.MULTISHOT); - } + return ItemLegality.LEGAL; } - private void filterTrident(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(Enchantment.RIPTIDE)) { - if (enchantments.containsKey(Enchantment.LOYALTY) || enchantments.containsKey(Enchantment.CHANNELING)) { - item.removeEnchantment(Enchantment.RIPTIDE); + @Override + public void handleItem(ItemStack itemStack, ItemLegality checkResult) { + if (!doSomething) return; + + switch (checkResult) { + case CONTAINS_ILLEGAL -> itemStack.setType(Material.AIR); + case ILLEGAL -> { + final Set enchantments = itemStack.getEnchantments().keySet(); + + if (enchantments.contains(Enchantment.BINDING_CURSE) && enchantments.contains(Enchantment.VANISHING_CURSE)) + itemStack.removeEnchantment(Enchantment.BINDING_CURSE); + if (enchantments.contains(Enchantment.MULTISHOT) && enchantments.contains(Enchantment.PIERCING)) + itemStack.removeEnchantment(Enchantment.MULTISHOT); + if (enchantments.contains(Enchantment.RIPTIDE) && (enchantments.contains(Enchantment.LOYALTY) || enchantments.contains(Enchantment.CHANNELING))) + itemStack.removeEnchantment(Enchantment.RIPTIDE); + if (enchantments.contains(Enchantment.MENDING) && enchantments.contains(Enchantment.ARROW_INFINITE)) + itemStack.removeEnchantment(Enchantment.ARROW_INFINITE); + if (enchantments.contains(Enchantment.SILK_TOUCH) && enchantments.contains(Enchantment.LOOT_BONUS_BLOCKS)) + itemStack.removeEnchantment(Enchantment.LOOT_BONUS_BLOCKS); + if (enchantments.contains(Enchantment.DEPTH_STRIDER) && enchantments.contains(Enchantment.FROST_WALKER)) + itemStack.removeEnchantment(Enchantment.FROST_WALKER); + + if (enchantments.contains(Enchantment.DAMAGE_ALL)) { // Prefer keeping Sharpness enchantment if present + DAMAGE_ENCHANTS.stream().filter(enchant -> enchant != Enchantment.DAMAGE_ALL).forEach(itemStack::removeEnchantment); + } else if (enchantments.contains(Enchantment.DAMAGE_ARTHROPODS) && enchantments.contains(Enchantment.DAMAGE_UNDEAD)) { + itemStack.removeEnchantment(Enchantment.DAMAGE_ARTHROPODS); + } + + if (enchantments.contains(Enchantment.PROTECTION_ENVIRONMENTAL)) { // Prefer keeping Protection enchantment if present + PROTECT_ENCHANTS.stream().filter(enchant -> enchant != Enchantment.PROTECTION_ENVIRONMENTAL).forEach(itemStack::removeEnchantment); + } else if (enchantments.contains(Enchantment.PROTECTION_EXPLOSIONS)) { // If protection is present, prefer blast protection + PROTECT_ENCHANTS.stream().filter(enchant -> enchant != Enchantment.PROTECTION_EXPLOSIONS).forEach(itemStack::removeEnchantment); + } else if (enchantments.contains(Enchantment.PROTECTION_PROJECTILE) && enchantments.contains(Enchantment.PROTECTION_FIRE)) { + itemStack.removeEnchantment(Enchantment.PROTECTION_FIRE); // If protection and blast protection is not present, prefer projectile protection + } } } } - private void filterBow(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(Enchantment.MENDING) && enchantments.containsKey(Enchantment.ARROW_INFINITE)) { - item.removeEnchantment(Enchantment.ARROW_INFINITE); - } - } - - private void filterTools(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.isEmpty()) return; - if (enchantments.containsKey(Enchantment.SILK_TOUCH) && enchantments.containsKey(Enchantment.LOOT_BONUS_BLOCKS)) { - item.removeEnchantment(Enchantment.LOOT_BONUS_BLOCKS); - } - } - - private void filterBoots(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(Enchantment.DEPTH_STRIDER) && enchantments.containsKey(Enchantment.FROST_WALKER)) { - item.removeEnchantment(Enchantment.FROST_WALKER); - } - } - - private void filterSharpnessEnchants(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.isEmpty()) return; - - // Prefer keeping Sharpness enchantment if it is present - if (enchantments.containsKey(Enchantment.DAMAGE_ALL)) { - if (enchantments.containsKey(Enchantment.DAMAGE_ARTHROPODS)) - item.removeEnchantment(Enchantment.DAMAGE_ARTHROPODS); - if (enchantments.containsKey(Enchantment.DAMAGE_UNDEAD)) - item.removeEnchantment(Enchantment.DAMAGE_UNDEAD); - } else if ( - enchantments.containsKey(Enchantment.DAMAGE_ARTHROPODS) - && enchantments.containsKey(Enchantment.DAMAGE_UNDEAD) - ) { - item.removeEnchantment(Enchantment.DAMAGE_ARTHROPODS); - } - } - - private void filterProtectionEnchants(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.isEmpty()) return; - - // Prefer keeping Protection enchantment if it is present - if (enchantments.containsKey(Enchantment.PROTECTION_ENVIRONMENTAL)) { - if (enchantments.containsKey(Enchantment.PROTECTION_EXPLOSIONS)) - item.removeEnchantment(Enchantment.PROTECTION_EXPLOSIONS); - if (enchantments.containsKey(Enchantment.PROTECTION_FIRE)) - item.removeEnchantment(Enchantment.PROTECTION_FIRE); - if (enchantments.containsKey(Enchantment.PROTECTION_PROJECTILE)) - item.removeEnchantment(Enchantment.PROTECTION_PROJECTILE); - // If protection is not present, prefer blast protection - } else if (enchantments.containsKey(Enchantment.PROTECTION_EXPLOSIONS)) { - if (enchantments.containsKey(Enchantment.PROTECTION_FIRE)) - item.removeEnchantment(Enchantment.PROTECTION_FIRE); - if (enchantments.containsKey(Enchantment.PROTECTION_PROJECTILE)) - item.removeEnchantment(Enchantment.PROTECTION_PROJECTILE); - // If protection and blast protection is not present, prefer projectile protection - } else if ( - enchantments.containsKey(Enchantment.PROTECTION_PROJECTILE) - && enchantments.containsKey(Enchantment.PROTECTION_FIRE) - ) { - item.removeEnchantment(Enchantment.PROTECTION_FIRE); + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onHopperTransfer(InventoryMoveItemEvent event) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { + event.setCancelled(true); } } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/CustomNBTFilter.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/CustomNBTFilter.java index 2583708ea..67280c832 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/CustomNBTFilter.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/CustomNBTFilter.java @@ -1,25 +1,20 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items.nbt; import de.tr7zw.changeme.nbtapi.NBTItem; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.slf4j.event.Level; @@ -29,14 +24,14 @@ import java.util.Set; import java.util.stream.Collectors; -public class CustomNBTFilter implements IllegalItemModule { +public class CustomNBTFilter implements IllegalItemModule, Listener { private final Set illegalTags; private final Set whitelistedTypes; private ScheduledTask periodicInvCheck; private Listener hopperListener; private final long checkPeriod; - private final boolean useWhitelist, blacklistMode, doPeriodicCheck, checkStored, preventHopperBypass, delete; + private final boolean useWhitelist, blacklistMode, doPeriodicCheck, checkStored, preventHopperBypass, doSomething; public CustomNBTFilter() { shouldEnable(); @@ -44,7 +39,7 @@ public CustomNBTFilter() { config.addComment("illegals.nbt.ban-custom-tags.enable", "Deletes items that have one or more of the configured tags.\n" + "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM.get()); - this.delete = config.getBoolean("illegals.nbt.ban-custom-tags.delete-item", true); + this.doSomething = config.getBoolean("illegals.nbt.ban-custom-tags.delete-item", true); this.checkStored = config.getBoolean("illegals.nbt.ban-custom-tags.check-stored-items", false); this.illegalTags = new HashSet<>(config.getList("illegals.nbt.ban-custom-tags.tags", List.of("dmg"), "The exact, case sensitive value of the nbt tag.")); @@ -82,14 +77,15 @@ public String category() { @Override public void enable() { - AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + final AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); if (preventHopperBypass) { this.hopperListener = new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -100,7 +96,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { this.periodicInvCheck = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, periodic_inv_check -> plugin.getServer().getOnlinePlayers().forEach(player -> player.getScheduler().run(plugin, scheduled_inv_check -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item))), null)), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item))), null)), checkPeriod, checkPeriod); } } @@ -117,7 +113,12 @@ public void disable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType().isAir()) { return ItemLegality.LEGAL; } @@ -134,7 +135,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -146,67 +147,17 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult != ItemLegality.LEGAL) { - if (delete) itemStack.setType(Material.AIR); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM, event.getPlayer())) return; + if (!doSomething) return; - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); + if (checkResult != ItemLegality.LEGAL) { + itemStack.setType(Material.AIR); } } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM, event.getPlayer())) return; - + private void onInventoryMove(InventoryMoveItemEvent event) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); - handleItem(handItem, result); } } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/NBTFilledStorageItem.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/NBTFilledStorageItem.java index d60ff5f79..0b7dacfa0 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/NBTFilledStorageItem.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/NBTFilledStorageItem.java @@ -1,22 +1,14 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items.nbt; import de.tr7zw.changeme.nbtapi.NBTItem; -import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; -import org.bukkit.event.block.BlockDispenseEvent; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.slf4j.event.Level; @@ -32,21 +24,21 @@ public class NBTFilledStorageItem implements IllegalItemModule { private final Set storageTypes; private final String stored_items_tag; - private final boolean delete, checkStored; + private final boolean doSomething, checkStored; public NBTFilledStorageItem() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("illegals.nbt.revert-impossibly-stored-items.enable", + config.addComment("illegals.nbt.impossibly-stored-items.enable", "Prevents usage of prefilled storage items using NBT tags.\n" + "Commonly used to create dispensers / chests containing kit shulkers.\n" + - "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS.get()); - this.stored_items_tag = config.getString("illegals.nbt.revert-impossibly-stored-items.tag", "BlockEntityTag", + "Bypass permission: " + bypassPermission().get()); + this.stored_items_tag = config.getString("illegals.nbt.impossibly-stored-items.tag", "BlockEntityTag", "The exact name of the nbt tag that signals items are stored inside."); - this.delete = config.getBoolean("illegals.nbt.revert-impossibly-stored-items.delete-item", false, + this.doSomething = config.getBoolean("illegals.nbt.impossibly-stored-items.delete-item", true, "Whether to delete the item as well."); - this.checkStored = config.getBoolean("illegals.nbt.revert-impossibly-stored-items.delete-shulker-if-contains-storage-items", false); - this.storageTypes = config.getList("illegals.nbt.revert-impossibly-stored-items.materials-to-check", + this.checkStored = config.getBoolean("illegals.nbt.impossibly-stored-items.check-stored-items", false); + this.storageTypes = config.getList("illegals.nbt.impossibly-stored-items.materials-to-check", List.of("CHEST", "TRAPPED_CHEST", "DISPENSER", "DROPPER")) .stream() .map(configuredType -> { @@ -63,7 +55,7 @@ public NBTFilledStorageItem() { @Override public String name() { - return "nbt.revert-impossibly-stored-items"; + return "nbt.impossibly-stored-items"; } @Override @@ -79,7 +71,7 @@ public void enable() { @Override public boolean shouldEnable() { - return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.nbt.revert-impossibly-stored-items.enable", false); + return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.nbt.impossibly-stored-items.enable", false); } @Override @@ -88,7 +80,12 @@ public void disable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType().isAir()) { return ItemLegality.LEGAL; } @@ -101,7 +98,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -113,74 +110,10 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult != ItemLegality.LEGAL) { - if (delete) itemStack.setType(Material.AIR); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onDispense(BlockDispenseEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onAttack(PrePlayerAttackEntityEvent event) { - if (!event.willAttack()) return; - ItemStack attackItem = event.getPlayer().getActiveItem(); - final ItemLegality result = getLegality(attackItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS, event.getPlayer())) return; + if (!doSomething) return; - event.setCancelled(true); - handleItem(attackItem, result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); + if (checkResult != ItemLegality.LEGAL) { + itemStack.setType(Material.AIR); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/enums/ItemLegality.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/enums/ItemLegality.java new file mode 100644 index 000000000..2ce2a1798 --- /dev/null +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/enums/ItemLegality.java @@ -0,0 +1,7 @@ +package me.moomoo.anarchyexploitfixes.enums; + +public enum ItemLegality { + LEGAL, + ILLEGAL, + CONTAINS_ILLEGAL +} diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java index e20ef2e0f..0262403b4 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java @@ -119,15 +119,15 @@ static void reloadModules() { modules.add(new InapplicableEnchants()); modules.add(new IncompatibleEnchants()); // Other - modules.add(new BannedItemNames()); modules.add(new OverstackedItems()); modules.add(new UnbreakableItems()); - modules.add(new PlayerHeadItems()); - modules.add(new SpawnEggItems()); modules.add(new BannedMaterials()); + modules.add(new SpawnEggItems()); + modules.add(new PlayerHeadItems()); + modules.add(new BannedItemNames()); // NBT - modules.add(new CustomNBTFilter()); modules.add(new NBTFilledStorageItem()); + modules.add(new CustomNBTFilter()); /* Lag Preventions diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedItemNames.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedItemNames.java index 1ef0e1e9c..0c01466cd 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedItemNames.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedItemNames.java @@ -3,15 +3,10 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import org.bukkit.ChatColor; import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.Listener; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -20,14 +15,15 @@ import java.util.Set; import java.util.stream.Collectors; -public class BannedItemNames implements IllegalItemModule { +public class BannedItemNames implements IllegalItemModule, Listener { private final Set coloredNames, plainTextNames; public BannedItemNames() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("illegals.banned-item-names.enable", "Resets an item's name if configured in the config."); + config.addComment("illegals.banned-item-names.enable", + "Resets an item's name if configured in the config. Bypass permission: " + bypassPermission().get()); this.plainTextNames = new HashSet<>(config.getList("illegals.banned-item-names.plain-names", Collections.singletonList("Super Insane Mega Sussy Item"))); this.coloredNames = config.getList("illegals.banned-item-names.color-sensitive-names", @@ -59,8 +55,13 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { - if (itemStack == null || itemStack.getType() == Material.AIR) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_BANNEDNAME; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { + if (itemStack == null || itemStack.getType() == Material.AIR || !itemStack.hasItemMeta()) { return ItemLegality.LEGAL; } @@ -80,50 +81,4 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { itemStack.setItemMeta(meta); } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDNAME, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedMaterials.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedMaterials.java index a9703b717..42413acdc 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedMaterials.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/BannedMaterials.java @@ -4,19 +4,21 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; import org.bukkit.Material; +import org.bukkit.Server; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.inventory.ItemStack; -import java.util.*; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; import java.util.logging.Level; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -24,28 +26,29 @@ public class BannedMaterials implements IllegalItemModule { private final Set bannedMaterials; - private final boolean shouldDelete, checkStored; + private final int checkPeriod; + private final boolean doSomething, checkStored, doPeriodicCheck, preventHopperBypass; public BannedMaterials() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.ban-specific-materials.enable", - "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL.get()); - this.shouldDelete = config.getBoolean("illegals.ban-specific-materials.delete-illegals", false); + "Bypass permission: " + bypassPermission().get()); + this.doSomething = config.getBoolean("illegals.ban-specific-materials.delete-item", false); this.checkStored = config.getBoolean("illegals.ban-specific-materials.check-stored-items", false); List defaults = Stream.of( - XMaterial.CHAIN_COMMAND_BLOCK, - XMaterial.COMMAND_BLOCK, - XMaterial.COMMAND_BLOCK_MINECART, - XMaterial.REPEATING_COMMAND_BLOCK, - XMaterial.BEDROCK, - XMaterial.BARRIER, - XMaterial.STRUCTURE_BLOCK, - XMaterial.STRUCTURE_VOID, - XMaterial.END_PORTAL_FRAME, - XMaterial.END_PORTAL, - XMaterial.NETHER_PORTAL, - XMaterial.LIGHT) + XMaterial.CHAIN_COMMAND_BLOCK, + XMaterial.COMMAND_BLOCK, + XMaterial.COMMAND_BLOCK_MINECART, + XMaterial.REPEATING_COMMAND_BLOCK, + XMaterial.BEDROCK, + XMaterial.BARRIER, + XMaterial.STRUCTURE_BLOCK, + XMaterial.STRUCTURE_VOID, + XMaterial.END_PORTAL_FRAME, + XMaterial.END_PORTAL, + XMaterial.NETHER_PORTAL, + XMaterial.LIGHT) .map(xMaterial -> xMaterial.isSupported() ? xMaterial.parseMaterial().name() : null) .filter(Objects::nonNull) .collect(Collectors.toList()); @@ -61,6 +64,12 @@ public BannedMaterials() { }) .filter(Objects::nonNull) .collect(Collectors.toCollection(HashSet::new)); + this.preventHopperBypass = config.getBoolean("illegals.enchantments.ban-specific-materials.prevent-hopper32k-mechanic", false, + "Prevents Hopper32k mechanic of placing a shulker containing illegals on top of a hopper and using the illegal\n" + + "out of the hoppers inventory.\n" + + "WARNING: Hooks into InventoryMoveItemEvent, which can become resource intense. Enable only if you need to."); + this.doPeriodicCheck = config.getBoolean("illegals.enchantments.ban-specific-materials.periodically-check-player-inventories.enable", false); + this.checkPeriod = config.getInt("illegals.enchantments.ban-specific-materials.periodically-check-player-inventories.check-period-in-ticks", 20); } @Override @@ -75,8 +84,26 @@ public String category() { @Override public void enable() { - AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); - plugin.getServer().getPluginManager().registerEvents(this, plugin); + final AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); + final Server server = plugin.getServer(); + + server.getPluginManager().registerEvents(this, plugin); + + if (preventHopperBypass) { + server.getPluginManager().registerEvents(new Listener() { + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onInventoryMove(InventoryMoveItemEvent event) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { + event.setCancelled(true); + } + } + }, plugin); + } + + if (doPeriodicCheck) { + server.getScheduler().scheduleSyncRepeatingTask(plugin, () -> server.getOnlinePlayers().forEach(player -> + player.getInventory().forEach(item -> handleItem(item, determineLegality(item)))), checkPeriod, checkPeriod); + } } @Override @@ -85,7 +112,12 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null) { return ItemLegality.LEGAL; } @@ -98,7 +130,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -110,54 +142,10 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult != ItemLegality.LEGAL) { - if (shouldDelete) itemStack.setType(Material.AIR); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL, event.getWhoClicked())) return; + if (!doSomething) return; - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_BANNEDMATERIAL, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); + if (checkResult != ItemLegality.LEGAL) { + itemStack.setType(Material.AIR); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/IllegalItemModule.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/IllegalItemModule.java index 65b511792..76f74d5f3 100644 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/IllegalItemModule.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/IllegalItemModule.java @@ -1,17 +1,84 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; +import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockDispenseEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.player.PlayerAttemptPickupItemEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; public interface IllegalItemModule extends AnarchyExploitFixesModule, Listener { - ItemLegality getLegality(ItemStack itemStack); + AEFPermission bypassPermission(); + ItemLegality determineLegality(ItemStack itemStack); void handleItem(ItemStack itemStack, ItemLegality checkResult); - enum ItemLegality { - LEGAL, - ILLEGAL, - CONTAINS_ILLEGAL + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onDispense(BlockDispenseEvent event) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onInventoryClick(InventoryClickEvent event) { + ItemStack current = event.getCurrentItem(); + ItemStack cursor = event.getCursor(); + if (!CachingPermTool.hasPermission(bypassPermission(), event.getWhoClicked())) { + handleItem(current, determineLegality(current)); + handleItem(cursor, determineLegality(cursor)); + for (ItemStack invItem : event.getWhoClicked().getInventory()) { + handleItem(invItem, determineLegality(invItem)); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onPlayerPickupItem(PlayerAttemptPickupItemEvent event) { + ItemStack droppedItem = event.getItem().getItemStack(); + final ItemLegality legality = determineLegality(droppedItem); + if (legality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + event.setCancelled(true); + handleItem(droppedItem, legality); + event.getItem().setItemStack(droppedItem); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onPlayerDropItem(PlayerDropItemEvent event) { + ItemStack droppedItem = event.getItemDrop().getItemStack(); + final ItemLegality legality = determineLegality(droppedItem); + if (legality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + handleItem(droppedItem, legality); + event.getItemDrop().setItemStack(droppedItem); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) + default void onInteract(PlayerInteractEvent event) { + ItemStack interactItem = event.getItem(); + final ItemLegality legality = determineLegality(interactItem); + if (legality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + event.setCancelled(true); + handleItem(interactItem, legality); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + default void onInteractEntity(PlayerInteractEntityEvent event) { + ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); + final ItemLegality legality = determineLegality(handItem); + if (legality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), event.getPlayer())) { + event.setCancelled(true); + handleItem(handItem, legality); + } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/OverstackedItems.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/OverstackedItems.java index ac50ac5c3..ebc3bc0a8 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/OverstackedItems.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/OverstackedItems.java @@ -1,9 +1,10 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; +import com.cryptomorin.xseries.XMaterial; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; import org.bukkit.Material; @@ -11,18 +12,17 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; -import java.util.*; +import java.util.Collections; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; import java.util.logging.Level; import java.util.stream.Collectors; -public class OverstackedItems implements IllegalItemModule { +public class OverstackedItems implements IllegalItemModule, Listener { private final Set whitelistedTypes; private final long checkPeriod; @@ -33,11 +33,12 @@ public OverstackedItems() { Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.revert-overstacked-items.enable", "Revert illegally high stacked items.\n" + - "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_OVERSTACKED.get()); + "Bypass permission: " + bypassPermission().get()); this.useWhitelist = config.getBoolean("illegals.revert-overstacked-items.item-whitelist-enabled", false); this.blacklistMode = config.getBoolean("illegals.revert-overstacked-items.use-as-blacklist-instead", true); this.checkStored = config.getBoolean("illegals.revert-overstacked-items.check-stored-items", false); - this.whitelistedTypes = config.getList("illegals.revert-overstacked-items.whitelisted-items", Collections.singletonList("TOTEM_OF_UNDYING")) + this.whitelistedTypes = config.getList("illegals.revert-overstacked-items.whitelisted-items", + Collections.singletonList(XMaterial.TOTEM_OF_UNDYING.parseMaterial().name())) .stream() .map(configuredType -> { try { @@ -78,7 +79,7 @@ public void enable() { server.getPluginManager().registerEvents(new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -87,7 +88,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { server.getScheduler().scheduleSyncRepeatingTask(plugin, () -> server.getOnlinePlayers().forEach(player -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item)))), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item)))), checkPeriod, checkPeriod); } } @@ -97,7 +98,12 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_OVERSTACKED; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) { return ItemLegality.LEGAL; } @@ -112,7 +118,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -132,50 +138,4 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { itemStack.setType(Material.AIR); } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_OVERSTACKED, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_OVERSTACKED, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_OVERSTACKED, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_OVERSTACKED, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/PlayerHeadItems.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/PlayerHeadItems.java index 20452c780..89aa2962e 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/PlayerHeadItems.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/PlayerHeadItems.java @@ -3,17 +3,10 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.MaterialUtil; import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.block.BlockDispenseEvent; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; @@ -25,10 +18,10 @@ public PlayerHeadItems() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.ban-player-heads.enable", - "Prevents usage of player heads. Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD.get()); + "Prevents usage of player heads. Bypass permission: " + bypassPermission().get()); this.shouldDelete = config.getBoolean("illegals.ban-player-heads.remove-items", false); this.checkStored = config.getBoolean("illegals.ban-player-heads.check-stored-items", false, - "Will delete shulker/bundle if they contain any player heads with owning players."); + "Will delete shulker/bundle if they contain any player heads."); } @Override @@ -53,8 +46,13 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { - if (itemStack == null || !itemStack.hasItemMeta()) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { + if (itemStack == null) { return ItemLegality.LEGAL; } @@ -66,7 +64,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -82,59 +80,4 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { if (shouldDelete) itemStack.setType(Material.AIR); } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onDispense(BlockDispenseEvent event) { - final ItemStack itemStack = event.getItem(); - if (!itemStack.hasItemMeta()) return; - if (MaterialUtil.PLAYER_HEADS.contains(itemStack.getType()) && ((SkullMeta) itemStack.getItemMeta()).hasOwner()) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/SpawnEggItems.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/SpawnEggItems.java index fd90c6d4f..a979c8f3f 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/SpawnEggItems.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/SpawnEggItems.java @@ -3,17 +3,10 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.MaterialUtil; import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.block.BlockDispenseEvent; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; public class SpawnEggItems implements IllegalItemModule { @@ -24,7 +17,7 @@ public SpawnEggItems() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.ban-spawn-eggs.enable", - "Prevents usage of spawn eggs. Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_SPAWNEGG.get()); + "Prevents usage of spawn eggs. Bypass permission: " + bypassPermission().get()); this.shouldDelete = config.getBoolean("illegals.ban-spawn-eggs.remove-spawn-eggs", false); this.checkStored = config.getBoolean("illegals.ban-spawn-eggs.check-stored-items", false, "Will delete shulker/bundle if they contain any spawneggs."); @@ -52,7 +45,12 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_SPAWNEGG; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null) { return ItemLegality.LEGAL; } @@ -65,7 +63,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -81,57 +79,4 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { if (shouldDelete) itemStack.setType(Material.AIR); } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onDispense(BlockDispenseEvent event) { - if (MaterialUtil.isSpawnEgg(event.getItem())) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/UnbreakableItems.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/UnbreakableItems.java index 7b891d39d..302acaf52 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/UnbreakableItems.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/UnbreakableItems.java @@ -1,9 +1,10 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items; +import com.cryptomorin.xseries.XMaterial; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; import org.bukkit.Material; @@ -11,19 +12,10 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerAttemptPickupItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import java.util.Collections; -import java.util.HashSet; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.logging.Level; import java.util.stream.Collectors; @@ -31,20 +23,21 @@ public class UnbreakableItems implements IllegalItemModule { private final Set whitelistedTypes; private final long checkPeriod; - private final boolean useWhitelist, blacklistMode, doPeriodicCheck, checkStored, preventHopperBypass, skipZeroDurability; + private final boolean skipZeroDurability, useWhitelist, blacklistMode, doPeriodicCheck, checkStored, preventHopperBypass; public UnbreakableItems() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.revert-unbreakables.enable", - "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE.get()); - this.skipZeroDurability = config.getBoolean("illegals.revert-unbreakables.skip-zero-durability", AnarchyExploitFixes.getMCVersion() < 16, - "Make sure to keep enabled on 1.16+, otherwise netherite tools will mistakenly be set to max durability, due to some bug in spigot."); + "Bypass permission: " + bypassPermission().get()); this.useWhitelist = config.getBoolean("illegals.revert-unbreakables.item-whitelist-enabled", false); this.blacklistMode = config.getBoolean("illegals.revert-unbreakables.use-as-blacklist-instead", false); this.checkStored = config.getBoolean("illegals.revert-unbreakables.check-stored-items", false, "Will delete shulkers and bundles if they contain unbreakables."); - this.whitelistedTypes = config.getList("illegals.revert-unbreakables.whitelisted-items", Collections.singletonList("DIAMOND_CHESTPLATE")) + this.skipZeroDurability = config.getBoolean("illegals.revert-unbreakables.skip-zero-durability", AnarchyExploitFixes.getMCVersion() < 16, + "Make sure to keep enabled on 1.16+, otherwise netherite tools will mistakenly be set to max durability, due to some bug in spigot."); + this.whitelistedTypes = config.getList("illegals.revert-unbreakables.whitelisted-items", + Collections.singletonList(XMaterial.DIAMOND_CHESTPLATE.parseMaterial().name())) .stream() .map(configuredItem -> { try { @@ -56,7 +49,7 @@ public UnbreakableItems() { }) .filter(Objects::nonNull) .collect(Collectors.toCollection(HashSet::new)); - this.preventHopperBypass = config.getBoolean("illegals.revert-unbreakables.prevent-hopper32k-mechanic", true, + this.preventHopperBypass = config.getBoolean("illegals.revert-unbreakables.prevent-hopper32k-mechanic", false, "Prevents Hopper32k mechanic of placing a shulker containing illegals on top of a hopper and using the illegal\n" + "out of the hoppers inventory.\n" + "WARNING: Hooks into InventoryMoveItemEvent, which can become resource intense. Enable only if you need to."); @@ -85,7 +78,7 @@ public void enable() { server.getPluginManager().registerEvents(new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -94,7 +87,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { server.getScheduler().scheduleSyncRepeatingTask(plugin, () -> server.getOnlinePlayers().forEach(player -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item)))), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item)))), checkPeriod, checkPeriod); } } @@ -104,7 +97,12 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return null; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) { return ItemLegality.LEGAL; } @@ -119,7 +117,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -131,40 +129,14 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - switch (checkResult) { - case ILLEGAL: - // Remove possible unbreakable tags - final ItemMeta meta = itemStack.getItemMeta(); - if (meta.isUnbreakable()) { - meta.setUnbreakable(false); - itemStack.setItemMeta(meta); - } - - // Check for impossible durability values - final int durability = itemStack.getDurability(); - final short maxDurability = itemStack.getType().getMaxDurability(); - - if (!skipZeroDurability || maxDurability != 0) { - if (durability > maxDurability) { - itemStack.setDurability(maxDurability); - } - if (durability < 0) { - itemStack.setType(Material.AIR); - } - } else { - if (durability > 2031) { - itemStack.setDurability(maxDurability); - } - } - break; - case CONTAINS_ILLEGAL: - itemStack.setType(Material.AIR); + if (checkResult != ItemLegality.LEGAL) { + itemStack.setType(Material.AIR); } } private boolean isUnbreakable(ItemStack itemStack) { - int durability = itemStack.getDurability(); - short maxDurability = itemStack.getType().getMaxDurability(); + final int durability = itemStack.getDurability(); + final short maxDurability = itemStack.getType().getMaxDurability(); if (!skipZeroDurability || maxDurability != 0) { if (durability > maxDurability) { return true; @@ -174,63 +146,4 @@ private boolean isUnbreakable(ItemStack itemStack) { return durability > 2031; } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerAttemptPickupItemEvent event) { - ItemStack droppedItem = event.getItem().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(droppedItem, result); - event.getItem().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_UNBREAKABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/datavalues/CustomDataValues.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/datavalues/CustomDataValues.java index faa2e554b..296c8fd14 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/datavalues/CustomDataValues.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/datavalues/CustomDataValues.java @@ -3,20 +3,17 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; -import java.util.*; +import java.util.Collections; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; import java.util.logging.Level; import java.util.stream.Collectors; @@ -24,19 +21,21 @@ public class CustomDataValues implements IllegalItemModule { private final Set whitelistedTypes; private final Set forbiddenDataValues; - private final boolean useWhitelist, blacklistMode, checkStored, delete; + private final boolean useWhitelist, blacklistMode, checkStored, doSomething; public CustomDataValues() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("illegals.data-values.custom-data-values.enable", "1.12 Only"); - this.delete = config.getBoolean("illegals.data-values.custom-data-values.delete-item", true); + config.addComment("illegals.data-values.custom-data-values.enable", + "1.12 Only. Bypass permission: " + bypassPermission().get()); + this.doSomething = config.getBoolean("illegals.data-values.custom-data-values.delete-item", true); this.checkStored = config.getBoolean("illegals.data-values.custom-data-values.check-stored-items", false); this.forbiddenDataValues = new HashSet<>(config.getList("illegals.data-values.custom-data-values.data-values", Collections.singletonList("SuperIllegalItem(0)"))); this.useWhitelist = config.getBoolean("illegals.data-values.custom-data-values.item-whitelist-enabled", false); this.blacklistMode = config.getBoolean("illegals.data-values.custom-data-values.use-as-blacklist-instead", false); - this.whitelistedTypes = config.getList("illegals.data-values.custom-data-values.whitelisted-items", Collections.singletonList("GOLDEN_APPLE")) + this.whitelistedTypes = config.getList("illegals.data-values.custom-data-values.whitelisted-items", + Collections.singletonList("GOLDEN_APPLE")) .stream() .map(configuredItem -> { try { @@ -74,7 +73,12 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_DATA_CUSTOM; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) { return ItemLegality.LEGAL; } @@ -89,7 +93,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -101,55 +105,10 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult != ItemLegality.LEGAL) { - if (delete) itemStack.setType(Material.AIR); - } - } - - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_DATA_CUSTOM, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } + if (!doSomething) return; - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_DATA_CUSTOM, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_DATA_CUSTOM, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_DATA_CUSTOM, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); + if (checkResult != ItemLegality.LEGAL) { + itemStack.setType(Material.AIR); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/datavalues/IllegalGoldenApples.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/datavalues/IllegalGoldenApples.java index a00666947..acd2c1e7f 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/datavalues/IllegalGoldenApples.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/datavalues/IllegalGoldenApples.java @@ -4,16 +4,10 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; @@ -28,7 +22,8 @@ public class IllegalGoldenApples implements IllegalItemModule { public IllegalGoldenApples() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("illegals.data-values.illegal-golden-apples.enable", "1.12 only."); + config.addComment("illegals.data-values.illegal-golden-apples.enable", + "1.12 only. Bypass permission: " + bypassPermission().get()); this.doSomething = config.getBoolean("illegals.data-values.illegal-golden-apples.fix-or-delete-items", false, "Will delete shulkers if they contain illegal apples and revert apples themselves to default"); this.checkStored = config.getBoolean("illegals.data-values.illegal-golden-apples.check-stored-items", false); @@ -46,7 +41,7 @@ public IllegalGoldenApples() { @Override public String name() { - return "remove-illegal-golden-apples"; + return "illegal-golden-apples"; } @Override @@ -62,12 +57,17 @@ public void enable() { @Override public boolean shouldEnable() { - return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.data-values.revert-illegal-golden-apples", false) + return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.data-values.illegal-golden-apples", false) && AnarchyExploitFixes.getMCVersion() <= 12; } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_DATA_APPLE; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null) { return ItemLegality.LEGAL; } @@ -80,7 +80,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -97,54 +97,9 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { switch (checkResult) { case ILLEGAL: itemStack.setData(allowedAppleData.get(itemStack.getType())); + break; case CONTAINS_ILLEGAL: itemStack.setType(Material.AIR); } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_DATA_APPLE, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_DATA_APPLE, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_DATA_APPLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_DATA_APPLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/HigherEnchants.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/HigherEnchants.java index f1ff5b279..b93f4e9b8 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/HigherEnchants.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/HigherEnchants.java @@ -1,11 +1,10 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments; - import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; import org.bukkit.Material; @@ -14,11 +13,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import java.util.*; @@ -37,7 +32,7 @@ public HigherEnchants() { Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.enchantments.higher-enchants.enable", "Reverts or prevents usage of 32ks / 255s"); this.checkStored = config.getBoolean("illegals.enchantments.higher-enchants.check-stored-items", false); - this.doSomething = config.getBoolean("illegals.enchantments.higher-enchants.delete-or-revert-item", false, + this.doSomething = config.getBoolean("illegals.enchantments.higher-enchants.delete-or-revert-item", true, "Bundles or shulkers will be deleted if you have check-stored-items enabled."); this.onlySomeEnchants = config.getBoolean("illegals.enchantments.higher-enchants.only-specific-enchants", false); this.specificEnchants = config.getList("illegals.enchantments.higher-enchants.specific-enchants", Collections.singletonList("DIG_SPEED")) @@ -63,7 +58,7 @@ public HigherEnchants() { }) .filter(Objects::nonNull) .collect(Collectors.toCollection(HashSet::new)); - this.preventHopperBypass = config.getBoolean("illegals.enchantments.higher-enchants.prevent-hopper32k-mechanic", true, + this.preventHopperBypass = config.getBoolean("illegals.enchantments.higher-enchants.prevent-hopper32k-mechanic", true, "Prevents Hopper32k mechanic of placing a shulker containing illegals on top of a hopper and using the illegal\n" + "out of the hoppers inventory.\n" + "WARNING: Hooks into InventoryMoveItemEvent, which can become resource intense. Enable only if you need to."); @@ -92,7 +87,7 @@ public void enable() { server.getPluginManager().registerEvents(new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -101,7 +96,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { server.getScheduler().scheduleSyncRepeatingTask(plugin, () -> server.getOnlinePlayers().forEach(player -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item)))), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item)))), checkPeriod, checkPeriod); } } @@ -111,7 +106,12 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) { return ItemLegality.LEGAL; } @@ -130,7 +130,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -156,50 +156,4 @@ public void handleItem(ItemStack itemStack, ItemLegality checkResult) { itemStack.setType(Material.AIR); } } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_HIGHER, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); - } - } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/InapplicableEnchants.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/InapplicableEnchants.java index 73fa7505b..45a35adf7 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/InapplicableEnchants.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/InapplicableEnchants.java @@ -3,8 +3,8 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; import org.bukkit.Material; @@ -13,11 +13,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import java.util.*; @@ -33,10 +29,11 @@ public class InapplicableEnchants implements IllegalItemModule { public InapplicableEnchants() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - this.checkStored = config.getBoolean("illegals.enchantments.higher-enchants.check-stored-items", false); + this.doSomething = config.getBoolean("illegals.enchantments.inapplicable-enchants.delete-or-revert-item", true); + this.checkStored = config.getBoolean("illegals.enchantments.inapplicable-enchants.check-stored-items", false); this.useWhitelist = config.getBoolean("illegals.enchantments.inapplicable-enchants.item-whitelist-enabled", true); this.blacklistMode = config.getBoolean("illegals.enchantments.inapplicable-enchants.use-as-blacklist-instead", false); - this.whitelistedTypes = config.getList("illegals.enchantments.inapplicable-enchants.whitelisted-items", Collections.singletonList("GOLDEN_APPLE")) + this.whitelistedTypes = config.getList("illegals.enchantments.inapplicable-enchants.whitelisted-items", List.of("GOLDEN_APPLE")) .stream() .map(configuredType -> { try { @@ -52,7 +49,6 @@ public InapplicableEnchants() { "Prevents Hopper32k mechanic of placing a shulker containing illegals on top of a hopper and using the illegal\n" + "out of the hoppers inventory.\n" + "WARNING: Hooks into InventoryMoveItemEvent, which can become resource intense. Enable only if you need to."); - this.doSomething = config.getBoolean("illegals.enchantments.inapplicable-enchants.delete-shulker-if-contains-inapplicable-enchant", false); this.doPeriodicCheck = config.getBoolean("illegals.enchantments.inapplicable-enchants.periodically-check-player-inventories.enable", false); this.checkPeriod = config.getInt("illegals.enchantments.inapplicable-enchants.periodically-check-player-inventories.check-period-in-ticks", 20); } @@ -78,7 +74,7 @@ public void enable() { server.getPluginManager().registerEvents(new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -87,7 +83,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { server.getScheduler().scheduleSyncRepeatingTask(plugin, () -> server.getOnlinePlayers().forEach(player -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item)))), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item)))), checkPeriod, checkPeriod); } } @@ -97,7 +93,12 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) { return ItemLegality.LEGAL; } @@ -114,7 +115,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -127,62 +128,24 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { if (!doSomething) return; + switch (checkResult) { + case CONTAINS_ILLEGAL: + itemStack.setType(Material.AIR); + break; case ILLEGAL: for (Map.Entry enchant : itemStack.getEnchantments().entrySet()) { if (!enchant.getKey().canEnchantItem(itemStack)) { itemStack.removeEnchantment(enchant.getKey()); } } - break; - case CONTAINS_ILLEGAL: - itemStack.setType(Material.AIR); } } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INAPPLICABLE, event.getPlayer())) return; - + private void onHopperTransfer(InventoryMoveItemEvent event) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); - handleItem(handItem, result); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/IncompatibleEnchants.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/IncompatibleEnchants.java index 955597548..df5db2023 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/IncompatibleEnchants.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/enchantments/IncompatibleEnchants.java @@ -1,33 +1,35 @@ package me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments; import com.cryptomorin.xseries.XEnchantment; +import com.google.common.collect.Sets; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; +import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import me.moomoo.anarchyexploitfixes.utils.MaterialUtil; import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.enchantments.Enchantment; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.inventory.ItemStack; -import java.util.*; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; import java.util.logging.Level; import java.util.stream.Collectors; -public class IncompatibleEnchants implements IllegalItemModule { +public class IncompatibleEnchants implements IllegalItemModule, Listener { private final Set whitelistedTypes; private final long checkPeriod; - private final boolean useWhitelist, blacklistMode, doPeriodicCheck; + private final boolean useWhitelist, blacklistMode, doPeriodicCheck, preventHopperBypass, checkStored, doSomething; private final Enchantment binding_curse, vanishing_curse, crossbow_multishot, crossbow_piercing, trident_riptide, trident_loyalty, trident_channeling, mending, bow_arrow_infinite, tools_silk_touch, @@ -35,13 +37,27 @@ public class IncompatibleEnchants implements IllegalItemModule { sword_damage_undead, armor_protection_environmental, armor_protection_explosions, armor_protection_fire, armor_protection_projectile; + private static final Set DAMAGE_ENCHANTS = Sets.newHashSet( + XEnchantment.DAMAGE_ALL.getEnchant(), + XEnchantment.DAMAGE_UNDEAD.getEnchant(), + XEnchantment.DAMAGE_ARTHROPODS.getEnchant()); + private static final Set PROTECT_ENCHANTS = Sets.newHashSet( + XEnchantment.PROTECTION_ENVIRONMENTAL.getEnchant(), + XEnchantment.PROTECTION_EXPLOSIONS.getEnchant(), + XEnchantment.PROTECTION_FIRE.getEnchant(), + XEnchantment.PROTECTION_PROJECTILE.getEnchant()); + public IncompatibleEnchants() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("illegals.enchantments.incompatible-enchants.enable", "Reverts items like Armor with all Protection enchantments."); + config.addComment("illegals.enchantments.incompatible-enchants.enable", + "Reverts items like Armor with all Protection enchantments. Bypass permission: " + bypassPermission().get()); + this.checkStored = config.getBoolean("illegals.enchantments.incompatible-enchants.check-stored-items", false); + this.doSomething = config.getBoolean("illegals.enchantments.higher-enchants.delete-or-revert-item", true, + "Bundles or shulkers will be deleted if you have check-stored-items enabled."); this.useWhitelist = config.getBoolean("illegals.enchantments.incompatible-enchants.item-whitelist-enabled", true); this.blacklistMode = config.getBoolean("illegals.enchantments.incompatible-enchants.use-as-blacklist-instead", false); - this.whitelistedTypes = config.getList("illegals.enchantments.incompatible-enchants.whitelisted-items", Collections.singletonList("BOW")) + this.whitelistedTypes = config.getList("illegals.enchantments.incompatible-enchants.whitelisted-items", List.of("BOW")) .stream() .map(configuredType -> { try { @@ -53,6 +69,10 @@ public IncompatibleEnchants() { }) .filter(Objects::nonNull) .collect(Collectors.toCollection(HashSet::new)); + this.preventHopperBypass = config.getBoolean("illegals.enchantments.incompatible-enchants.prevent-hopper32k-mechanic", false, + "Prevents Hopper32k mechanic of placing a shulker containing illegals on top of a hopper and using the illegal\n" + + "out of the hoppers inventory.\n" + + "WARNING: Hooks into InventoryMoveItemEvent, which can become resource intense. Enable only if you need to."); this.doPeriodicCheck = config.getBoolean("illegals.enchantments.incompatible-enchants.periodically-check-player-inventories.enable", false); this.checkPeriod = config.getInt("illegals.enchantments.incompatible-enchants.periodically-check-player-inventories.check-period-in-ticks", 20); @@ -93,11 +113,22 @@ public void enable() { final AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); final Server server = plugin.getServer(); - server.getPluginManager().registerEvents(this, plugin); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + + if (preventHopperBypass) { + server.getPluginManager().registerEvents(new Listener() { + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onInventoryMove(InventoryMoveItemEvent event) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { + event.setCancelled(true); + } + } + }, plugin); + } if (doPeriodicCheck) { server.getScheduler().scheduleSyncRepeatingTask(plugin, () -> server.getOnlinePlayers().forEach(player -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item)))), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item)))), checkPeriod, checkPeriod); } } @@ -107,198 +138,89 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { - if (itemStack == null || itemStack.getType() == Material.AIR) { - return ItemLegality.LEGAL; - } - - if (!useWhitelist || blacklistMode == whitelistedTypes.contains(itemStack.getType())) { - return ItemLegality.ILLEGAL; - } - - return ItemLegality.LEGAL; + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE; } @Override - public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult == ItemLegality.LEGAL) { - return; - } - - final Material material = itemStack.getType(); - - if (MaterialUtil.isArmor(material)) { - filterProtectionEnchants(itemStack); - filterCurses(itemStack); - if (MaterialUtil.BOOTS.contains(material)) { - filterBoots(itemStack); - } - return; - } - - if (MaterialUtil.isElytra(itemStack)) { - filterCurses(itemStack); - return; + public ItemLegality determineLegality(ItemStack itemStack) { + if (itemStack == null || itemStack.getType() == Material.AIR) { + return ItemLegality.LEGAL; } - if (MaterialUtil.isTool(itemStack)) { - filterTools(itemStack); - if (MaterialUtil.AXES.contains(material)) { - filterSharpnessEnchants(itemStack); + if (!useWhitelist || blacklistMode == whitelistedTypes.contains(itemStack.getType())) { + final Set enchantments = itemStack.getEnchantments().keySet(); + if (!enchantments.isEmpty()) { + if (enchantments.contains(binding_curse) && enchantments.contains(vanishing_curse)) + return ItemLegality.ILLEGAL; + if (enchantments.contains(bow_arrow_infinite) && enchantments.contains(mending)) + return ItemLegality.ILLEGAL; + if (enchantments.contains(tools_silk_touch) && enchantments.contains(tools_loot_bonus_blocks)) + return ItemLegality.ILLEGAL; + if (enchantments.contains(boots_depth_strider) && enchantments.contains(boots_frost_walker)) + return ItemLegality.ILLEGAL; + if (DAMAGE_ENCHANTS.stream().filter(enchantments::contains).count() > 1L) + return ItemLegality.ILLEGAL; + if (PROTECT_ENCHANTS.stream().filter(enchantments::contains).count() > 1L) + return ItemLegality.ILLEGAL; + if (enchantments.contains(crossbow_multishot) && enchantments.contains(crossbow_piercing)) + return ItemLegality.ILLEGAL; + if (enchantments.contains(trident_riptide) && (enchantments.contains(trident_loyalty) || enchantments.contains(trident_channeling))) + return ItemLegality.ILLEGAL; } - return; - } - - if (MaterialUtil.SWORDS.contains(material)) { - filterSharpnessEnchants(itemStack); - return; - } - - if (MaterialUtil.isBow(itemStack)) { - filterBow(itemStack); - return; - } - - if (MaterialUtil.isTrident(itemStack)) { - filterTrident(itemStack); - return; - } - - if (MaterialUtil.isCrossbow(itemStack)) { - filterCrossbow(itemStack); } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE, event.getPlayer())) return; - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE, event.getPlayer())) return; - - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_ENCHANT_INCOMPATIBLE, event.getPlayer())) return; - - handleItem(handItem, result); - } - } - - private void filterCurses(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(binding_curse) && enchantments.containsKey(vanishing_curse)) { - item.removeEnchantment(binding_curse); - } - } - - private void filterCrossbow(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(crossbow_multishot) && enchantments.containsKey(crossbow_piercing)) { - item.removeEnchantment(crossbow_multishot); - } - } - - private void filterTrident(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(trident_riptide)) { - if (enchantments.containsKey(trident_loyalty) || enchantments.containsKey(trident_channeling)) { - item.removeEnchantment(trident_riptide); + if (checkStored) { + Iterable storedItems = ItemUtil.getStoredItems(itemStack); + if (storedItems != null) { + for (ItemStack stored : storedItems) { + if (determineLegality(stored) != ItemLegality.LEGAL) { + return ItemLegality.CONTAINS_ILLEGAL; + } + } } } - } - private void filterBow(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.isEmpty()) return; - if (enchantments.containsKey(mending) && enchantments.containsKey(bow_arrow_infinite)) { - item.removeEnchantment(bow_arrow_infinite); - } - } - - private void filterTools(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(tools_silk_touch) && enchantments.containsKey(tools_loot_bonus_blocks)) { - item.removeEnchantment(tools_loot_bonus_blocks); - } - } - - private void filterBoots(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.containsKey(boots_depth_strider) && enchantments.containsKey(boots_frost_walker)) { - item.removeEnchantment(boots_frost_walker); - } - } - - private void filterSharpnessEnchants(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.isEmpty()) return; - - // Prefer keeping Sharpness enchantment if it is present - if (enchantments.containsKey(sword_damage_all)) { - if (enchantments.containsKey(sword_damage_arthropods)) - item.removeEnchantment(sword_damage_arthropods); - if (enchantments.containsKey(sword_damage_undead)) - item.removeEnchantment(sword_damage_undead); - return; - } - - if (enchantments.containsKey(sword_damage_arthropods) && enchantments.containsKey(sword_damage_undead)) { - item.removeEnchantment(sword_damage_arthropods); - } + return ItemLegality.LEGAL; } - private void filterProtectionEnchants(ItemStack item) { - Map enchantments = item.getEnchantments(); - if (enchantments.isEmpty()) return; - // Prefer keeping Protection enchantment if it is present - if (enchantments.containsKey(armor_protection_environmental)) { - if (enchantments.containsKey(armor_protection_explosions)) - item.removeEnchantment(armor_protection_explosions); - if (enchantments.containsKey(armor_protection_fire)) - item.removeEnchantment(armor_protection_fire); - if (enchantments.containsKey(armor_protection_projectile)) - item.removeEnchantment(armor_protection_projectile); - // If protection is not present, prefer blast protection - } else if (enchantments.containsKey(armor_protection_explosions)) { - if (enchantments.containsKey(armor_protection_fire)) - item.removeEnchantment(armor_protection_fire); - if (enchantments.containsKey(armor_protection_projectile)) - item.removeEnchantment(armor_protection_projectile); - // If protection and blast protection is not present, prefer projectile protection - } else if ( - enchantments.containsKey(armor_protection_projectile) - && enchantments.containsKey(armor_protection_fire) - ) { - item.removeEnchantment(armor_protection_fire); + @Override + public void handleItem(ItemStack itemStack, ItemLegality checkResult) { + if (!doSomething) return; + + switch (checkResult) { + case CONTAINS_ILLEGAL: + itemStack.setType(Material.AIR); + break; + case ILLEGAL: + final Set enchantments = itemStack.getEnchantments().keySet(); + + if (enchantments.contains(binding_curse) && enchantments.contains(vanishing_curse)) + itemStack.removeEnchantment(binding_curse); + if (enchantments.contains(crossbow_multishot) && enchantments.contains(crossbow_piercing)) + itemStack.removeEnchantment(crossbow_multishot); + if (enchantments.contains(trident_riptide) && (enchantments.contains(trident_loyalty) || enchantments.contains(trident_channeling))) + itemStack.removeEnchantment(trident_riptide); + if (enchantments.contains(bow_arrow_infinite) && enchantments.contains(mending)) + itemStack.removeEnchantment(bow_arrow_infinite); + if (enchantments.contains(tools_silk_touch) && enchantments.contains(tools_loot_bonus_blocks)) + itemStack.removeEnchantment(tools_loot_bonus_blocks); + if (enchantments.contains(boots_depth_strider) && enchantments.contains(boots_frost_walker)) + itemStack.removeEnchantment(boots_frost_walker); + + if (enchantments.contains(sword_damage_all)) { // Prefer keeping Sharpness enchantment if present + DAMAGE_ENCHANTS.stream().filter(enchant -> enchant != sword_damage_all).forEach(itemStack::removeEnchantment); + } else if (enchantments.contains(sword_damage_arthropods) && enchantments.contains(sword_damage_undead)) { + itemStack.removeEnchantment(sword_damage_arthropods); + } + + if (enchantments.contains(armor_protection_environmental)) { // Prefer keeping Protection enchantment if present + PROTECT_ENCHANTS.stream().filter(enchant -> enchant != armor_protection_environmental).forEach(itemStack::removeEnchantment); + } else if (enchantments.contains(armor_protection_explosions)) { // If protection is present, prefer blast protection + PROTECT_ENCHANTS.stream().filter(enchant -> enchant != armor_protection_explosions).forEach(itemStack::removeEnchantment); + } else if (enchantments.contains(armor_protection_projectile) && enchantments.contains(armor_protection_fire)) { + itemStack.removeEnchantment(armor_protection_fire); // If protection and blast protection is not present, prefer projectile protection + } } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/CustomNBTFilter.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/CustomNBTFilter.java index 991d08ef7..45de02c14 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/CustomNBTFilter.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/CustomNBTFilter.java @@ -4,8 +4,8 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import me.moomoo.anarchyexploitfixes.utils.LogUtil; import org.bukkit.Material; @@ -13,14 +13,13 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; -import java.util.*; +import java.util.Collections; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; import java.util.logging.Level; import java.util.stream.Collectors; @@ -29,15 +28,15 @@ public class CustomNBTFilter implements IllegalItemModule { private final Set illegalTags; private final Set whitelistedTypes; private final long checkPeriod; - private final boolean useWhitelist, blacklistMode, doPeriodicCheck, checkStored, preventHopperBypass, delete; + private final boolean useWhitelist, blacklistMode, doPeriodicCheck, checkStored, preventHopperBypass, doSomething; public CustomNBTFilter() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("illegals.nbt.ban-custom-tags.enable", "Deletes items that have one or more of the configured tags.\n" + - "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM.get()); - this.delete = config.getBoolean("illegals.nbt.ban-custom-tags.delete-item", true); + "Bypass permission: " + bypassPermission().get()); + this.doSomething = config.getBoolean("illegals.nbt.ban-custom-tags.delete-item", true); this.checkStored = config.getBoolean("illegals.nbt.ban-custom-tags.check-stored-items", false); this.illegalTags = new HashSet<>(config.getList("illegals.nbt.ban-custom-tags.tags", Collections.singletonList("dmg"), "The exact, case sensitive value of the nbt tag.")); @@ -78,13 +77,13 @@ public void enable() { final AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); final Server server = plugin.getServer(); - server.getPluginManager().registerEvents(this, plugin); + plugin.getServer().getPluginManager().registerEvents(this, plugin); if (preventHopperBypass) { server.getPluginManager().registerEvents(new Listener() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onInventoryMove(InventoryMoveItemEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { + if (determineLegality(event.getItem()) != ItemLegality.LEGAL) { event.setCancelled(true); } } @@ -93,7 +92,7 @@ private void onInventoryMove(InventoryMoveItemEvent event) { if (doPeriodicCheck) { server.getScheduler().scheduleSyncRepeatingTask(plugin, () -> server.getOnlinePlayers().forEach(player -> - player.getInventory().forEach(item -> handleItem(item, getLegality(item)))), checkPeriod, checkPeriod); + player.getInventory().forEach(item -> handleItem(item, determineLegality(item)))), checkPeriod, checkPeriod); } } @@ -103,7 +102,12 @@ public boolean shouldEnable() { } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) { return ItemLegality.LEGAL; } @@ -120,7 +124,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -132,54 +136,10 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult != ItemLegality.LEGAL) { - if (delete) itemStack.setType(Material.AIR); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } + if (!doSomething) return; - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_CUSTOM, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); + if (checkResult != ItemLegality.LEGAL) { + itemStack.setType(Material.AIR); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/NBTFilledStorageItem.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/NBTFilledStorageItem.java index 6085dba68..6ad6356bd 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/NBTFilledStorageItem.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/nbt/NBTFilledStorageItem.java @@ -5,20 +5,16 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.AEFPermission; +import me.moomoo.anarchyexploitfixes.enums.ItemLegality; import me.moomoo.anarchyexploitfixes.modules.illegals.items.IllegalItemModule; -import me.moomoo.anarchyexploitfixes.utils.CachingPermTool; import me.moomoo.anarchyexploitfixes.utils.ItemUtil; import org.bukkit.Material; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.block.BlockDispenseEvent; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; -import java.util.*; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; import java.util.logging.Level; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -29,20 +25,20 @@ public class NBTFilledStorageItem implements IllegalItemModule { private final Set storageTypes; private final String stored_items_tag; - private final boolean delete, checkStored; + private final boolean doSomething, checkStored; public NBTFilledStorageItem() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); - config.addComment("illegals.nbt.revert-impossibly-stored-items.enable", + config.addComment("illegals.nbt.impossibly-stored-items.enable", "Prevents usage of prefilled storage items using NBT tags.\n" + "Commonly used to create dispensers / chests containing kit shulkers.\n" + - "Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS.get()); - this.stored_items_tag = config.getString("illegals.nbt.revert-impossibly-stored-items.tag", "BlockEntityTag", + "Bypass permission: " + bypassPermission().get()); + this.stored_items_tag = config.getString("illegals.nbt.impossibly-stored-items.tag", "BlockEntityTag", "The exact name of the nbt tag that signals items are stored inside."); - this.delete = config.getBoolean("illegals.nbt.revert-impossibly-stored-items.delete-item", false, + this.doSomething = config.getBoolean("illegals.nbt.impossibly-stored-items.delete-item", true, "Whether to delete the item as well."); - this.checkStored = config.getBoolean("illegals.nbt.revert-impossibly-stored-items.delete-shulker-if-contains-storage-items", false); + this.checkStored = config.getBoolean("illegals.nbt.impossibly-stored-items.check-stored-items", false); List defaults = Stream.of( XMaterial.BARREL, XMaterial.CHEST, @@ -52,7 +48,7 @@ public NBTFilledStorageItem() { .map(xMaterial -> xMaterial.isSupported() ? xMaterial.parseMaterial().name() : null) .filter(Objects::nonNull) .collect(Collectors.toList()); - this.storageTypes = config.getList("illegals.nbt.revert-impossibly-stored-items.materials-to-check", defaults) + this.storageTypes = config.getList("illegals.nbt.impossibly-stored-items.materials-to-check", defaults) .stream() .map(configuredType -> { try { @@ -68,7 +64,7 @@ public NBTFilledStorageItem() { @Override public String name() { - return "nbt.revert-impossibly-stored-items"; + return "nbt.impossibly-stored-items"; } @Override @@ -84,11 +80,16 @@ public void enable() { @Override public boolean shouldEnable() { - return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.nbt.revert-impossibly-stored-items.enable", false); + return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.nbt.impossibly-stored-items.enable", false); } @Override - public ItemLegality getLegality(ItemStack itemStack) { + public AEFPermission bypassPermission() { + return AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS; + } + + @Override + public ItemLegality determineLegality(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) { return ItemLegality.LEGAL; } @@ -101,7 +102,7 @@ public ItemLegality getLegality(ItemStack itemStack) { Iterable storedItems = ItemUtil.getStoredItems(itemStack); if (storedItems != null) { for (ItemStack stored : storedItems) { - if (getLegality(stored) != ItemLegality.LEGAL) { + if (determineLegality(stored) != ItemLegality.LEGAL) { return ItemLegality.CONTAINS_ILLEGAL; } } @@ -113,61 +114,10 @@ public ItemLegality getLegality(ItemStack itemStack) { @Override public void handleItem(ItemStack itemStack, ItemLegality checkResult) { - if (checkResult != ItemLegality.LEGAL) { - if (delete) itemStack.setType(Material.AIR); - } - } + if (!doSomething) return; - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onDispense(BlockDispenseEvent event) { - if (getLegality(event.getItem()) != ItemLegality.LEGAL) { - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInventoryClick(InventoryClickEvent event) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS, event.getWhoClicked())) return; - - handleItem(event.getCurrentItem(), getLegality(event.getCurrentItem())); - handleItem(event.getCursor(), getLegality(event.getCursor())); - for (ItemStack itemStack : event.getWhoClicked().getInventory()) { - handleItem(itemStack, getLegality(itemStack)); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerDropItem(PlayerDropItemEvent event) { - ItemStack droppedItem = event.getItemDrop().getItemStack(); - final ItemLegality result = getLegality(droppedItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS, event.getPlayer())) return; - - handleItem(droppedItem, result); - event.getItemDrop().setItemStack(droppedItem); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) - private void onInteract(PlayerInteractEvent event) { - final ItemLegality result = getLegality(event.getItem()); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(event.getItem(), result); - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onInteractEntity(PlayerInteractEntityEvent event) { - ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand()); - final ItemLegality result = getLegality(handItem); - if (result != ItemLegality.LEGAL) { - if (CachingPermTool.hasPermission(AEFPermission.BYPASS_ILLEGAL_NBT_STOREDITEMS, event.getPlayer())) return; - - event.setCancelled(true); - handleItem(handItem, result); + if (checkResult != ItemLegality.LEGAL) { + itemStack.setType(Material.AIR); } } }