Skip to content

Commit

Permalink
add utf16 option to bookban module
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 4, 2024
1 parent 36b5a50 commit 4f355cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ public class BookBan implements AnarchyExploitFixesModule, Listener {
private final Cache<UUID, Integer> cachedInventorySizes;
private final MiniMessage miniMessage;
private final int maxBookSize, maxItemSize, maxInventorySize;
private final boolean useUTF16;

public BookBan() {
shouldEnable();
this.miniMessage = MiniMessage.miniMessage();
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
Charset to use to encode the result of ItemMeta#toString into a sequence of bytes.\s
The length of that sequence is then used to get the approximate Byte-size of an ItemStack.""");
this.maxBookSize = config.getInt("patches.anti-book-ban.max-book-size", 9000);
this.maxItemSize = config.getInt("patches.anti-book-ban.max-item-size", 8260);
this.maxInventorySize = config.getInt("patches.anti-book-ban.max-inventory-size", 50674);
Expand Down Expand Up @@ -99,20 +104,24 @@ private int getItemSize(@Nullable ItemStack stack) {
return byteSize;
}

byteSize += stack.getItemMeta().toString().getBytes(StandardCharsets.UTF_8).length;
// Expensive
byteSize += stack.getItemMeta().toString().getBytes(useUTF16 ? StandardCharsets.UTF_16 : StandardCharsets.UTF_8).length;
return byteSize;
}

private int getInventorySize(final @NotNull Iterable<ItemStack> iterable) {
private int getInventorySize(@NotNull Iterable<ItemStack> inventory) {
int collectiveSize = 0;
for (ItemStack stack : iterable) {
for (ItemStack stack : inventory) {
collectiveSize += getItemSize(stack);
}
return collectiveSize;
}

private int getBookSize(BookMeta bookMeta) {
return String.join("", bookMeta.pages().stream().map(miniMessage::serialize).toList()).getBytes(StandardCharsets.UTF_8).length;
// Expensive
return String.join("", bookMeta.pages().stream().map(miniMessage::serialize).toList())
.getBytes(useUTF16 ? StandardCharsets.UTF_16 : StandardCharsets.UTF_8)
.length;
}

// Prevent players from creating big books
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ 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 boolean useUTF16;

public BookBan() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
this.useUTF16 = config.getBoolean("patches.anti-book-ban.use-UTF-16", false,
"If set to false, will use UTF-8.\n" +
"Charset to use to encode the result of ItemMeta#toString into a sequence of bytes.\n" +
"The length of that sequence is then used to get the approximate Byte-size of an ItemStack.");
this.maxBookSize = config.getInt("patches.anti-book-ban.max-book-size", 8000);
this.maxItemSize = config.getInt("patches.anti-book-ban.max-item-size", 8260);
this.maxInventorySize = config.getInt("patches.anti-book-ban.max-inventory-size", 50674);
Expand Down Expand Up @@ -81,7 +86,8 @@ private int getItemSize(ItemStack stack) {
return byteSize;
}

byteSize += stack.getItemMeta().toString().getBytes(StandardCharsets.UTF_8).length;
// Expensive
byteSize += stack.getItemMeta().toString().getBytes(useUTF16 ? StandardCharsets.UTF_16 : StandardCharsets.UTF_8).length;
return byteSize;
}

Expand All @@ -94,7 +100,10 @@ private int getInventorySize(Iterable<ItemStack> inventory) {
}

private int getBookSize(BookMeta bookMeta) {
return String.join("", bookMeta.getPages()).getBytes(StandardCharsets.UTF_8).length;
// Expensive
return String.join("", bookMeta.getPages())
.getBytes(useUTF16 ? StandardCharsets.UTF_16 : StandardCharsets.UTF_8)
.length;
}

// Prevent players from creating big books
Expand Down

0 comments on commit 4f355cc

Please sign in to comment.