Refactor book byte enforcement by replacing BookByteChunkLimitListener with simplified BookByteLimitListener. Adjust byte limits and improve oversized book handling logic.

This commit is contained in:
2026-03-27 20:46:09 +01:00
parent e03c51198c
commit 57b5b8fe84
6 changed files with 166 additions and 347 deletions
@@ -2,8 +2,11 @@ package com.alttd.playerutils.util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Material;
import org.bukkit.block.ShulkerBox;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.BlockStateMeta;
import java.nio.charset.StandardCharsets;
@@ -16,16 +19,36 @@ public final class BookByteUtils {
}
// 65,000 bytes per book (below CoreProtect hard limit ~65,535)
public static final int MAX_BOOK_BYTES = 65_000;
public static final int MAX_BOOK_BYTES = 30_000;
public static final int BIG_BOOK_BYTES = 10_000;
public static boolean isWrittenBook(ItemStack stack) {
public static boolean shouldCountForBookByteLimit(ItemStack stack) {
if (stack == null) {
return false;
}
if (!(stack.getItemMeta() instanceof BookMeta meta)) {
Material type = stack.getType();
if (type == Material.WRITTEN_BOOK || type == Material.WRITABLE_BOOK) {
return true;
}
if (stack.getItemMeta() instanceof BookMeta) {
return true;
}
if (!(stack.getItemMeta() instanceof BlockStateMeta bsm) || !(bsm.getBlockState() instanceof ShulkerBox shulker)) {
return false;
}
return meta.hasAuthor() || meta.hasPages() || meta.hasTitle();
for (ItemStack content : shulker.getInventory().getContents()) {
if (content == null) {
continue;
}
Material contentType = content.getType();
if (contentType == Material.WRITTEN_BOOK || contentType == Material.WRITABLE_BOOK) {
return true;
}
if (content.getItemMeta() instanceof BookMeta) {
return true;
}
}
return false;
}
/**
@@ -60,11 +83,24 @@ public final class BookByteUtils {
if (stack == null) {
return 0;
}
if (!(stack.getItemMeta() instanceof BookMeta meta)) {
return 0;
// Direct written book
if (stack.getItemMeta() instanceof BookMeta meta) {
int perBook = computeBytes(meta);
return perBook * Math.max(1, stack.getAmount());
}
// written books do not stack; still, be safe and multiply by amount if it ever changes
int perBook = computeBytes(meta);
return perBook * Math.max(1, stack.getAmount());
// Shulker box: sum bytes of contained written books
if (stack.getItemMeta() instanceof BlockStateMeta bsm && bsm.getBlockState() instanceof ShulkerBox shulker) {
int total = 0;
for (ItemStack content : shulker.getInventory().getContents()) {
if (content == null) {
continue;
}
if (content.getItemMeta() instanceof BookMeta bookMeta) {
total += computeBytes(bookMeta) * Math.max(1, content.getAmount());
}
}
return total;
}
return 0;
}
}