From ed58c31f7c70cce07a7eb61132815aebaff363cc Mon Sep 17 00:00:00 2001 From: xGinko Date: Wed, 24 Jan 2024 19:55:47 +0100 Subject: [PATCH] improve bookban patch --- .../modules/patches/BookBan.java | 31 +++++++++++++++---- .../modules/patches/BookBan.java | 27 ++++++++++++---- 2 files changed, 46 insertions(+), 12 deletions(-) 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 8f5ae8677..d9e9b8a20 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 @@ -8,6 +8,7 @@ import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; import me.moomoo.anarchyexploitfixes.utils.MaterialUtil; import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Material; import org.bukkit.block.ShulkerBox; import org.bukkit.entity.Item; @@ -33,12 +34,14 @@ public class BookBan implements AnarchyExploitFixesModule, Listener { private final Cache cachedItemSizes; private final Cache cachedInventorySizes; private final MiniMessage miniMessage; - private final int maxBookSize, maxItemSize, maxInventorySize; + private final PlainTextComponentSerializer plainText; + private final int maxBookSize, maxItemSize, maxInventorySize, maxAuthorChars, maxTitleChars, maxPages; private final boolean useUTF16, kickOnBigBook; public BookBan() { shouldEnable(); this.miniMessage = MiniMessage.miniMessage(); + this.plainText = PlainTextComponentSerializer.plainText(); Config config = AnarchyExploitFixes.getConfiguration(); this.useUTF16 = config.getBoolean("patches.anti-book-ban.use-UTF-16", false, """ If set to false, will use UTF-8.\s @@ -47,6 +50,9 @@ public BookBan() { this.maxBookSize = config.getInt("patches.anti-book-ban.max-book-size", 9000); this.kickOnBigBook = config.getBoolean("patches.anti-book-ban.kick-on-too-large-book-edit", true, "Kicks players when they try to create a book bigger than the limit."); + this.maxAuthorChars = config.getInt("patches.anti-book-ban.max-author-chars", 32); + this.maxTitleChars = config.getInt("patches.anti-book-ban.max-title-chars", 32); + this.maxPages = config.getInt("patches.anti-book-ban.max-pages", 100); this.maxItemSize = config.getInt("patches.anti-book-ban.max-item-size", 8260); this.maxInventorySize = config.getInt("patches.anti-book-ban.max-inventory-size", 50674); this.cachedItemSizes = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis( @@ -120,16 +126,29 @@ private int getInventorySize(@NotNull Iterable inventory) { } private int getBookSize(BookMeta bookMeta) { - // Expensive - return String.join("", bookMeta.pages().stream().map(miniMessage::serialize).toList()) - .getBytes(useUTF16 ? StandardCharsets.UTF_16 : StandardCharsets.UTF_8) - .length; + String content = String.join("", bookMeta.pages().stream().map(miniMessage::serialize).toList()); + if (bookMeta.hasTitle()) + content = String.join("", content, miniMessage.serialize(bookMeta.title())); + if (bookMeta.hasAuthor()) + content = String.join("", content, miniMessage.serialize(bookMeta.author())); + return content.getBytes(useUTF16 ? StandardCharsets.UTF_16 : StandardCharsets.UTF_8).length; } // Prevent players from creating big books @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onBookEdit(PlayerEditBookEvent event) { - if (this.getBookSize(event.getNewBookMeta()) > maxBookSize) { + BookMeta meta = event.getNewBookMeta(); + + if ( + (meta.hasAuthor() && plainText.serialize(meta.author()).length() > maxAuthorChars) + || (meta.hasTitle() && plainText.serialize(meta.title()).length() > maxTitleChars) + || (meta.hasPages() && meta.pages().size() > maxPages) + ) { + event.setCancelled(true); + return; + } + + if (this.getBookSize(meta) > maxBookSize) { event.setCancelled(true); if (kickOnBigBook) { 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 c29baf0a9..6766661cc 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 @@ -26,7 +26,7 @@ public class BookBan implements AnarchyExploitFixesModule, Listener { private final Cache cachedItemSizes; private final Cache cachedInventorySizes; - private final int maxBookSize, maxItemSize, maxInventorySize; + private final int maxBookSize, maxItemSize, maxInventorySize, maxAuthorChars, maxTitleChars, maxPages; private final boolean useUTF16, kickOnBigBook; public BookBan() { @@ -39,6 +39,9 @@ public BookBan() { this.maxBookSize = config.getInt("patches.anti-book-ban.max-book-size", 8000); this.kickOnBigBook = config.getBoolean("patches.anti-book-ban.kick-on-too-large-book-edit", true, "Kicks players when they try to create a book bigger than the limit."); + this.maxAuthorChars = config.getInt("patches.anti-book-ban.max-author-chars", 32); + this.maxTitleChars = config.getInt("patches.anti-book-ban.max-title-chars", 32); + this.maxPages = config.getInt("patches.anti-book-ban.max-pages", 100); this.maxItemSize = config.getInt("patches.anti-book-ban.max-item-size", 8260); this.maxInventorySize = config.getInt("patches.anti-book-ban.max-inventory-size", 50674); this.cachedItemSizes = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis( @@ -102,18 +105,30 @@ private int getInventorySize(Iterable inventory) { } private int getBookSize(BookMeta bookMeta) { - // Expensive - return String.join("", bookMeta.getPages()) - .getBytes(useUTF16 ? StandardCharsets.UTF_16 : StandardCharsets.UTF_8) - .length; + String content = String.join("", bookMeta.getPages()); + if (bookMeta.hasTitle()) + content = String.join("", content, bookMeta.getTitle()); + if (bookMeta.hasAuthor()) + content = String.join("", content, bookMeta.getAuthor()); + return content.getBytes(useUTF16 ? StandardCharsets.UTF_16 : StandardCharsets.UTF_8).length; } // Prevent players from creating big books @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onBookEdit(PlayerEditBookEvent event) { - if (this.getBookSize(event.getNewBookMeta()) > maxBookSize) { + BookMeta meta = event.getNewBookMeta(); + + if ( + (meta.hasAuthor() && meta.getAuthor().length() > maxAuthorChars) + || (meta.hasTitle() && meta.getTitle().length() > maxTitleChars) + || (meta.hasPages() && meta.getPages().size() > maxPages) + ) { event.setCancelled(true); + return; + } + if (this.getBookSize(meta) > maxBookSize) { + event.setCancelled(true); if (kickOnBigBook) { final Player player = event.getPlayer(); player.kickPlayer(AnarchyExploitFixes.getLang(player.getLocale()).misc_MaskedKickMessage);