Skip to content

Commit

Permalink
rewrite playerheads and spawneggs module
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 6, 2024
1 parent d3d92d0 commit 9b48be3
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 482 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.moomoo.anarchyexploitfixes.enums;

import org.bukkit.permissions.Permissible;

public enum AEFPermission {

BYPASS_ELYTRA("bypass.elytra"),
Expand All @@ -12,6 +10,8 @@ public enum AEFPermission {
BYPASS_ILLEGAL_BANNEDMATERIAL("bypass.illegal.bannedmaterial"),
BYPASS_ILLEGAL_BANNEDNAME("bypass.illegal.bannedname"),
BYPASS_ILLEGAL_UNBREAKABLE("bypass.illegal.unbreakable"),
BYPASS_ILLEGAL_SPAWNEGG("bypass.illegal.spawnegg"),
BYPASS_ILLEGAL_PLAYERHEAD("bypass.illegal.playerhead"),
SILENT_JOIN("silentJoin"),
SILENT_LEAVE("silentLeave");

Expand All @@ -24,11 +24,4 @@ public enum AEFPermission {
public String get() {
return permission;
}

public static boolean hasPermission(Iterable<? extends Permissible> toCheck, AEFPermission aefPerm) {
for (Permissible permissible : toCheck) {
if (permissible.hasPermission(aefPerm.get())) return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@
import me.moomoo.anarchyexploitfixes.modules.combat.*;
import me.moomoo.anarchyexploitfixes.modules.dupepreventions.*;
import me.moomoo.anarchyexploitfixes.modules.elytra.*;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.BannedMaterial;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.BannedItemNames;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.OverstackedItems;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.RevertUnbreakables;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.*;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments.HigherEnchants;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments.InapplicableEnchants;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments.IncompatibleEnchants;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments.SpecificHigherEnchants;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.nbt.DamageModifierTags;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.nbt.FilledStorageItemTags;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.playerheads.BanPlayerHeads;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.playerheads.PreventPlacingPlayerHeads;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.spawneggs.PreventUsingSpawnEggs;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.spawneggs.RemoveSpawnEggs;
import me.moomoo.anarchyexploitfixes.modules.illegals.placedblocks.PeriodicallyRemoveIllegalBlocks;
import me.moomoo.anarchyexploitfixes.modules.illegals.placedblocks.RemoveIllegalBlocksOnChunkload;
import me.moomoo.anarchyexploitfixes.modules.illegals.placedblocks.RemoveUnnaturalSpawners;
Expand Down Expand Up @@ -116,6 +109,8 @@ static void reloadModules() {
modules.add(new BannedItemNames());
modules.add(new OverstackedItems());
modules.add(new RevertUnbreakables());
modules.add(new PlayerHeads());
modules.add(new SpawnEggs());
// Banned Blocks
modules.add(new BannedMaterial());
// Enchantments
Expand All @@ -126,12 +121,6 @@ static void reloadModules() {
// NBT Items
modules.add(new DamageModifierTags());
modules.add(new FilledStorageItemTags());
// Player head items
modules.add(new BanPlayerHeads());
modules.add(new PreventPlacingPlayerHeads());
// Spawn Eggs
modules.add(new PreventUsingSpawnEggs());
modules.add(new RemoveSpawnEggs());
/*
Lag Preventions
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
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.utils.ItemUtil;
import me.moomoo.anarchyexploitfixes.utils.models.HumanPermHelper;
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 PlayerHeads implements IllegalItemModule {

private final boolean shouldDelete, checkStored;

public PlayerHeads() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("illegals.ban-player-heads.enable",
"Prevents usage of player heads. Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD.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 spawneggs.");
}

@Override
public String name() {
return "ban-player-heads";
}

@Override
public String category() {
return "illegals";
}

@Override
public void enable() {
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.ban-player-heads.enable", false);
}

@Override
public void disable() {
HandlerList.unregisterAll(this);
}

@Override
public ItemCheckResult checkItem(ItemStack itemStack) {
if (itemStack == null) {
return ItemCheckResult.FINE;
}

switch (itemStack.getType()) {
case PLAYER_HEAD, PLAYER_WALL_HEAD -> {
return ItemCheckResult.IS_ILLEGAL;
}
}

if (checkStored) {
Iterable<ItemStack> storedItems = ItemUtil.getStoredItems(itemStack);
if (storedItems != null) {
for (ItemStack stored : storedItems) {
if (checkItem(stored) != ItemCheckResult.FINE) {
return ItemCheckResult.CONTAINS_ILLEGAL;
}
}
}
}

return ItemCheckResult.FINE;
}

@Override
public void takeAction(ItemStack itemStack, ItemCheckResult result) {
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 (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getWhoClicked())) return;

takeAction(event.getCurrentItem(), checkItem(event.getCurrentItem()));
takeAction(event.getCursor(), checkItem(event.getCursor()));
for (ItemStack itemStack : event.getWhoClicked().getInventory()) {
takeAction(itemStack, checkItem(itemStack));
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onAttack(PrePlayerAttackEntityEvent event) {
if (!event.willAttack()) return;
ItemStack attackItem = event.getPlayer().getActiveItem();
final ItemCheckResult result = checkItem(attackItem);
if (result != ItemCheckResult.FINE) {
if (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return;

event.setCancelled(true);
takeAction(attackItem, result);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerDropItem(PlayerDropItemEvent event) {
ItemStack droppedItem = event.getItemDrop().getItemStack();
final ItemCheckResult result = checkItem(droppedItem);
if (result != ItemCheckResult.FINE) {
if (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return;

takeAction(droppedItem, result);
event.getItemDrop().setItemStack(droppedItem);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
private void onInteract(PlayerInteractEvent event) {
final ItemCheckResult result = checkItem(event.getItem());
if (result != ItemCheckResult.FINE) {
if (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return;

event.setCancelled(true);
takeAction(event.getItem(), result);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onInteractEntity(PlayerInteractEntityEvent event) {
ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand());
final ItemCheckResult result = checkItem(handItem);
if (result != ItemCheckResult.FINE) {
if (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_PLAYERHEAD, event.getPlayer())) return;

event.setCancelled(true);
takeAction(handItem, result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
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.utils.ItemUtil;
import me.moomoo.anarchyexploitfixes.utils.MaterialUtil;
import me.moomoo.anarchyexploitfixes.utils.models.HumanPermHelper;
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 SpawnEggs implements IllegalItemModule {

private final boolean shouldDelete, checkStored;

public SpawnEggs() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("illegals.ban-spawn-eggs.enable",
"Prevents usage of spawn eggs. Bypass permission: " + AEFPermission.BYPASS_ILLEGAL_SPAWNEGG.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.");
}

@Override
public String name() {
return "ban-spawn-eggs";
}

@Override
public String category() {
return "illegals";
}

@Override
public void enable() {
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.ban-spawn-eggs.enable", false);
}

@Override
public void disable() {
HandlerList.unregisterAll(this);
}

@Override
public ItemCheckResult checkItem(ItemStack itemStack) {
if (itemStack == null) {
return ItemCheckResult.FINE;
}

if (MaterialUtil.isSpawnEgg(itemStack.getType())) {
return ItemCheckResult.IS_ILLEGAL;
}

if (checkStored) {
Iterable<ItemStack> storedItems = ItemUtil.getStoredItems(itemStack);
if (storedItems != null) {
for (ItemStack stored : storedItems) {
if (checkItem(stored) != ItemCheckResult.FINE) {
return ItemCheckResult.CONTAINS_ILLEGAL;
}
}
}
}

return ItemCheckResult.FINE;
}

@Override
public void takeAction(ItemStack itemStack, ItemCheckResult result) {
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 (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getWhoClicked())) return;

takeAction(event.getCurrentItem(), checkItem(event.getCurrentItem()));
takeAction(event.getCursor(), checkItem(event.getCursor()));
for (ItemStack itemStack : event.getWhoClicked().getInventory()) {
takeAction(itemStack, checkItem(itemStack));
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onAttack(PrePlayerAttackEntityEvent event) {
if (!event.willAttack()) return;
ItemStack attackItem = event.getPlayer().getActiveItem();
final ItemCheckResult result = checkItem(attackItem);
if (result != ItemCheckResult.FINE) {
if (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return;

event.setCancelled(true);
takeAction(attackItem, result);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerDropItem(PlayerDropItemEvent event) {
ItemStack droppedItem = event.getItemDrop().getItemStack();
final ItemCheckResult result = checkItem(droppedItem);
if (result != ItemCheckResult.FINE) {
if (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return;

takeAction(droppedItem, result);
event.getItemDrop().setItemStack(droppedItem);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
private void onInteract(PlayerInteractEvent event) {
final ItemCheckResult result = checkItem(event.getItem());
if (result != ItemCheckResult.FINE) {
if (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return;

event.setCancelled(true);
takeAction(event.getItem(), result);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onInteractEntity(PlayerInteractEntityEvent event) {
ItemStack handItem = event.getPlayer().getInventory().getItem(event.getHand());
final ItemCheckResult result = checkItem(handItem);
if (result != ItemCheckResult.FINE) {
if (HumanPermHelper.hasPermission(AEFPermission.BYPASS_ILLEGAL_SPAWNEGG, event.getPlayer())) return;

event.setCancelled(true);
takeAction(handItem, result);
}
}
}
Loading

0 comments on commit 9b48be3

Please sign in to comment.