Skip to content

Commit

Permalink
move common listeners into interface
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 7, 2024
1 parent cb5ff8c commit 6fb6917
Show file tree
Hide file tree
Showing 30 changed files with 839 additions and 1,973 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.moomoo.anarchyexploitfixes.enums;

public enum ItemLegality {
LEGAL,
ILLEGAL,
CONTAINS_ILLEGAL,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
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;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
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;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class BannedItemNames implements IllegalItemModule {
public class BannedItemNames implements IllegalItemModule, Listener {

private final Set<Component> coloredNames;
private final Set<String> 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",
List.of("Super Insane Mega Sussy Item")));
this.coloredNames = config.getList("illegals.banned-item-names.color-sensitive-names",
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Material> 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",
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -102,7 +134,7 @@ public ItemLegality getLegality(ItemStack itemStack) {
Iterable<ItemStack> 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;
}
}
Expand All @@ -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);
}
}
}
Loading

0 comments on commit 6fb6917

Please sign in to comment.