Skip to content

Commit

Permalink
initial work for illegal item module rewrite
Browse files Browse the repository at this point in the history
rename banned-blocks to banned-materials and reduce listeners
  • Loading branch information
xGinko committed Feb 17, 2024
1 parent 4c61acf commit 613a5f7
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 611 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import me.moomoo.anarchyexploitfixes.modules.illegals.items.RemoveSpecificItemNames;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.RevertOverstacked;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.RevertUnbreakables;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.bannedblocks.BanSpecificBlocks;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.bannedblocks.PreventPlacingBannedBlocks;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.BannedMaterial;
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;
Expand Down Expand Up @@ -119,8 +118,7 @@ static void reloadModules() {
modules.add(new RevertOverstacked());
modules.add(new RevertUnbreakables());
// Banned Blocks
modules.add(new BanSpecificBlocks());
modules.add(new PreventPlacingBannedBlocks());
modules.add(new BannedMaterial());
// Enchantments
modules.add(new IncompatibleEnchants());
modules.add(new HigherEnchants());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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.modules.AnarchyExploitFixesModule;
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.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

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 BannedMaterial implements AnarchyExploitFixesModule, Listener {

private final Set<Material> bannedMaterials;
private final boolean delete, checkStored;

public BannedMaterial() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
this.delete = config.getBoolean("illegals.ban-specific-materials.delete-illegals", 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",
"COMMAND_BLOCK",
"COMMAND_BLOCK_MINECART",
"REPEATING_COMMAND_BLOCK",
"BEDROCK",
"BARRIER",
"STRUCTURE_BLOCK",
"STRUCTURE_VOID",
"END_PORTAL_FRAME",
"END_PORTAL",
"NETHER_PORTAL",
"LIGHT"))
.stream()
.map(confMaterial -> {
try {
return Material.valueOf(confMaterial);
} catch (IllegalArgumentException e) {
LogUtil.materialNotRecognized(Level.WARNING, name(), confMaterial);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toCollection(HashSet::new));
}

@Override
public String name() {
return "ban-specific-materials";
}

@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-specific-materials.enable", false);
}

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

private boolean isIllegal(ItemStack itemStack) {
if (bannedMaterials.contains(itemStack.getType())) {
return true;
}

if (checkStored) {
Iterable<ItemStack> storedItems = ItemUtil.getStoredItems(itemStack);
if (storedItems == null) return false;
for (ItemStack stored : storedItems) {
if (stored != null && bannedMaterials.contains(stored.getType())) {
return true;
}
}
}

return false;
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
private void onInteract(PlayerInteractEvent event) {
ItemStack interactItem = event.getItem();

if (interactItem != null && isIllegal(interactItem)) {
event.setCancelled(true);
if (delete) interactItem.setAmount(0);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onAttack(PrePlayerAttackEntityEvent event) {
if (!event.willAttack()) return;

ItemStack attackItem = event.getPlayer().getActiveItem();

if (isIllegal(attackItem)) {
event.setCancelled(true);
if (delete) attackItem.setAmount(0);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onInteractEntity(PlayerInteractEntityEvent event) {
ItemStack interactItem = event.getPlayer().getInventory().getItem(event.getHand());

if (isIllegal(interactItem)) {
event.setCancelled(true);
if (delete) interactItem.setAmount(0);
}
}
}

This file was deleted.

Loading

0 comments on commit 613a5f7

Please sign in to comment.