Skip to content

Commit

Permalink
some improvements to anti book ban
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 24, 2023
1 parent 1e72110 commit 5f841e3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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<ItemStack> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5f841e3

Please sign in to comment.