diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BookBan.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BookBan.java index fc66d0328..6eb2f5829 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BookBan.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BookBan.java @@ -5,6 +5,7 @@ import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.block.ShulkerBox; +import org.bukkit.entity.Item; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; @@ -14,9 +15,13 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.BlockStateMeta; +import org.bukkit.inventory.meta.BundleMeta; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.nio.charset.StandardCharsets; +import java.util.List; public class BookBan implements AnarchyExploitFixesModule, Listener { @@ -60,42 +65,47 @@ public void disable() { private long getItemSize(@Nullable ItemStack stack) { long byteSize = 0L; - if (stack == null) return byteSize; + if (stack == null || !stack.hasItemMeta()) return byteSize; - if ( - stack.hasItemMeta() - && stack.getItemMeta() instanceof BlockStateMeta blockStateMeta - && blockStateMeta.getBlockState() instanceof ShulkerBox shulkerBox - ) { + final ItemMeta meta = stack.getItemMeta(); + + if (meta instanceof BlockStateMeta blockStateMeta && blockStateMeta.getBlockState() instanceof ShulkerBox shulkerBox) { byteSize += getInventorySize(shulkerBox.getInventory()); + return byteSize; } - // The serialize as bytes function takes significantly longer to run because - // it tries to compress the file. Instead, we will just use the string length, - // even though that is not a great proxy. - byteSize += stack.serialize().toString().getBytes(StandardCharsets.UTF_8).length; + if (meta instanceof BundleMeta bundleMeta) { + byteSize += getBundleSize(bundleMeta.getItems()); + return byteSize; + } + byteSize += meta.toString().getBytes(StandardCharsets.UTF_8).length; return byteSize; } - private long getInventorySize(@Nullable final Inventory inventory) { - long size = 0L; - if (inventory != null) { - for (ItemStack stack : inventory) { - size += getItemSize(stack); - } + private long getBundleSize(final @NotNull List items) { + long collectiveSize = 0L; + for (ItemStack stack : items) { + collectiveSize += getItemSize(stack); + } + return collectiveSize; + } + + private long getInventorySize(final @NotNull Inventory inventory) { + long collectiveSize = 0L; + for (ItemStack stack : inventory) { + collectiveSize += getItemSize(stack); } - return size; + return collectiveSize; } // Prevent players from creating big books @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onBookEdit(PlayerEditBookEvent event) { - // Map using MiniMessage#serialize so there are no possible bypasses using large components. - final String serializedPages = String.join("", event.getNewBookMeta().pages().stream().map(miniMessage::serialize).toList()); + final String newMeta = String.join("", event.getNewBookMeta().pages().stream().map(miniMessage::serialize).toList()); if ( - serializedPages.getBytes(StandardCharsets.UTF_8).length > maxBookSize - || serializedPages.getBytes(StandardCharsets.UTF_16).length > maxBookSize + newMeta.getBytes(StandardCharsets.UTF_8).length > maxBookSize + || newMeta.getBytes(StandardCharsets.UTF_16).length > maxBookSize ) { event.setCancelled(true); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BookBan.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BookBan.java index 9adaf69ae..675303e66 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BookBan.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BookBan.java @@ -15,6 +15,7 @@ import org.bukkit.inventory.meta.ItemMeta; import java.nio.charset.StandardCharsets; +import java.util.List; public class BookBan implements AnarchyExploitFixesModule, Listener { @@ -51,34 +52,28 @@ public boolean shouldEnable() { private long getItemSize(ItemStack stack) { long byteSize = 0L; - if (stack == null) return byteSize; - - if (stack.hasItemMeta()) { - final ItemMeta itemMeta = stack.getItemMeta(); - if (itemMeta instanceof BlockStateMeta) { - final BlockStateMeta blockStateMeta = (BlockStateMeta) itemMeta; - if (blockStateMeta.getBlockState() instanceof ShulkerBox) { - byteSize += getInventorySize(((ShulkerBox) blockStateMeta.getBlockState()).getInventory()); - } + if (stack == null || !stack.hasItemMeta()) return byteSize; + + final ItemMeta meta = stack.getItemMeta(); + + if (meta instanceof BlockStateMeta) { + final BlockStateMeta blockStateMeta = (BlockStateMeta) meta; + if (blockStateMeta.getBlockState() instanceof ShulkerBox) { + byteSize += getInventorySize(((ShulkerBox) blockStateMeta.getBlockState()).getInventory()); + return byteSize; } } - // The serialize as bytes function takes significantly longer to run because - // it tries to compress the file. Instead, we will just use the string length, - // even though that is not a great proxy. - byteSize += stack.serialize().toString().getBytes(StandardCharsets.UTF_8).length; - + byteSize += meta.toString().getBytes(StandardCharsets.UTF_8).length; return byteSize; } private long getInventorySize(final Inventory inventory) { - long size = 0L; - if (inventory != null) { - for (ItemStack stack : inventory) { - size += getItemSize(stack); - } + long collectiveSize = 0L; + for (ItemStack stack : inventory) { + collectiveSize += getItemSize(stack); } - return size; + return collectiveSize; } // Prevent players from creating big books