Skip to content

Commit

Permalink
improve bookban patch
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 24, 2024
1 parent e4d33a6 commit ed58c31
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,12 +34,14 @@ public class BookBan implements AnarchyExploitFixesModule, Listener {
private final Cache<UUID, Integer> cachedItemSizes;
private final Cache<UUID, Integer> 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
Expand All @@ -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(
Expand Down Expand Up @@ -120,16 +126,29 @@ private int getInventorySize(@NotNull Iterable<ItemStack> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class BookBan implements AnarchyExploitFixesModule, Listener {

private final Cache<UUID, Integer> cachedItemSizes;
private final Cache<UUID, Integer> cachedInventorySizes;
private final int maxBookSize, maxItemSize, maxInventorySize;
private final int maxBookSize, maxItemSize, maxInventorySize, maxAuthorChars, maxTitleChars, maxPages;
private final boolean useUTF16, kickOnBigBook;

public BookBan() {
Expand All @@ -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(
Expand Down Expand Up @@ -102,18 +105,30 @@ private int getInventorySize(Iterable<ItemStack> 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);
Expand Down

0 comments on commit ed58c31

Please sign in to comment.