From 99f57b7128620c079d43dfe1e74d48860ac0ae4e Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Mon, 23 Apr 2018 02:07:00 +0200 Subject: [PATCH] Ban certain items from the "My Items" class. This commit removes certain items from the in-arena inventories of players who choose the "My Items" class: - Ender chests allow players to smuggle stuff out of arenas - Ender pearls can be used to warp to unintended spots, and to build ender chests - Shulker boxes can carry any items - including ender chests - Shulker shells can be used to build shulker boxes At the end of the day, ender chests are the bad seed. The rest are just to prevent ender chests from making appearances. --- .../com/garbagemule/MobArena/ArenaClass.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/garbagemule/MobArena/ArenaClass.java b/src/main/java/com/garbagemule/MobArena/ArenaClass.java index c766f5a..a516d92 100644 --- a/src/main/java/com/garbagemule/MobArena/ArenaClass.java +++ b/src/main/java/com/garbagemule/MobArena/ArenaClass.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.stream.IntStream; public class ArenaClass { @@ -389,6 +390,7 @@ public class ArenaClass if (arena != null) { try { arena.getInventoryManager().restoreInv(p); + removeBannedItems(p.getInventory()); } catch (Exception e) { am.getPlugin().getLogger().severe("Failed to give " + p.getName() + " their own items: " + e.getMessage()); } @@ -399,5 +401,23 @@ public class ArenaClass public Location getClassChest() { return null; } + + private void removeBannedItems(PlayerInventory inv) { + ItemStack[] contents = inv.getContents(); + IntStream.range(0, contents.length) + .filter(i -> contents[i] != null) + .filter(i -> isBanned(contents[i].getType())) + .forEach(inv::clear); + } + + private boolean isBanned(Material type) { + switch (type) { + case ENDER_PEARL: + case ENDER_CHEST: + case SHULKER_SHELL: + return true; + } + return type.name().endsWith("_SHULKER_BOX"); + } } }