Skip to content

Commit

Permalink
improve book ban and fix burrow
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 4, 2024
1 parent 14c6cd7 commit ba5bd8b
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
}

// Beacon and Indestructibles
if (burrowMaterial.equals(Material.BEACON) || MaterialUtil.isIndestructible(burrowMaterial)) {
if (burrowMaterial.equals(Material.BEACON) || MaterialUtil.isSolidIndestructible(burrowMaterial)) {
player.damage(damageWhenMovingInBurrow);
if (shouldTeleportUp) teleportUpAndCenter(player, burrowBlock.getLocation());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package me.moomoo.anarchyexploitfixes.modules.patches;

import com.destroystokyo.paper.MaterialTags;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.MaterialUtil;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Material;
import org.bukkit.block.ShulkerBox;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.BundleMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.UUID;

public class BookBan implements AnarchyExploitFixesModule, Listener {

private final Cache<UUID, Long> cachedItemSizes;
private final Cache<UUID, Long> cachedInventorySizes;
private final MiniMessage miniMessage;
private final int maxBookSize, maxItemSize, maxInventorySize;

Expand All @@ -35,6 +42,14 @@ public BookBan() {
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);
this.cachedItemSizes = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(
config.getInt("patches.anti-book-ban.dropped-items-size-cache-ticks", 120,
"How long in ticks a dropped item's size should be cached after checking.") * 50L
)).build();
this.cachedInventorySizes = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(
config.getInt("patches.anti-book-ban.player-inventory-size-cache-ticks", 20,
"How long in ticks a player's inventory size should be cached after checking.") * 50L
)).build();
}

@Override
Expand Down Expand Up @@ -70,17 +85,17 @@ private long getItemSize(@Nullable ItemStack stack) {
final Material material = stack.getType();

if (MaterialTags.SHULKER_BOXES.isTagged(material)) {
byteSize += getInventorySize(((ShulkerBox) ((BlockStateMeta) stack.getItemMeta()).getBlockState()).getInventory());
byteSize += this.getInventorySize(((ShulkerBox) ((BlockStateMeta) stack.getItemMeta()).getBlockState()).getInventory());
return byteSize;
}

if (material.equals(Material.BUNDLE)) {
byteSize += getInventorySize(((BundleMeta) stack.getItemMeta()).getItems());
byteSize += this.getInventorySize(((BundleMeta) stack.getItemMeta()).getItems());
return byteSize;
}

if (MaterialUtil.isBook(material)) {
byteSize += getBookSize((BookMeta) stack.getItemMeta());
byteSize += this.getBookSize((BookMeta) stack.getItemMeta());
return byteSize;
}

Expand All @@ -103,29 +118,43 @@ private long getBookSize(BookMeta bookMeta) {
// Prevent players from creating big books
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBookEdit(PlayerEditBookEvent event) {
if (getBookSize(event.getNewBookMeta()) > maxBookSize) {
if (this.getBookSize(event.getNewBookMeta()) > maxBookSize) {
event.setCancelled(true);
}
}

// Attempt to stop players from picking up large items
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerAttemptPickupItem(PlayerAttemptPickupItemEvent event) {
final long itemSize = getItemSize(event.getItem().getItemStack());
// Check if item is generally over the size limit
final Item item = event.getItem();
final UUID itemUUID = item.getUniqueId();
Long itemSize = this.cachedItemSizes.getIfPresent(itemUUID);
if (itemSize == null)
itemSize = this.getItemSize(item.getItemStack());
this.cachedItemSizes.put(itemUUID, itemSize);
// If item is bigger than allowed, disallow player picking it up
if (itemSize > maxItemSize) {
event.setCancelled(true);
return;
}

// Check if the total inventory size would be larger than the maximum inventory size
if (itemSize + getInventorySize(event.getPlayer().getInventory()) > maxInventorySize) {
final Player player = event.getPlayer();
final UUID playerUUID = player.getUniqueId();
Long invSize = this.cachedInventorySizes.getIfPresent(playerUUID);
if (invSize == null)
invSize = this.getInventorySize(player.getInventory());
this.cachedInventorySizes.put(playerUUID, invSize);
// If picking up item would exceed max inventory size, disallow player picking it up
if (itemSize + invSize > maxInventorySize) {
event.setCancelled(true);
}
}

private void sanitizeInventory(PlayerInventory inventory) {
private void sanitizeInventory(Iterable<ItemStack> inventory) {
for (ItemStack item : inventory) {
if (getItemSize(item) > maxItemSize) {
if (this.getItemSize(item) > maxItemSize) {
item.subtract(item.getAmount());
}
}
Expand All @@ -134,16 +163,16 @@ private void sanitizeInventory(PlayerInventory inventory) {
// Attempt to clear off big items from affected players
@EventHandler(priority = EventPriority.LOW)
private void onJoin(PlayerJoinEvent event) {
sanitizeInventory(event.getPlayer().getInventory());
this.sanitizeInventory(event.getPlayer().getInventory());
}

@EventHandler(priority = EventPriority.LOW)
private void onLeave(PlayerQuitEvent event) {
sanitizeInventory(event.getPlayer().getInventory());
this.sanitizeInventory(event.getPlayer().getInventory());
}

@EventHandler(priority = EventPriority.LOW)
private void onKick(PlayerKickEvent event) {
sanitizeInventory(event.getPlayer().getInventory());
this.sanitizeInventory(event.getPlayer().getInventory());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public static boolean isIndestructible(Material material) {
};
}

public static boolean isSolidIndestructible(Material material) {
return switch (material) {
case BEDROCK,
END_PORTAL_FRAME,
REINFORCED_DEEPSLATE,
TRAPPED_CHEST,
STRUCTURE_BLOCK,
BARRIER,
COMMAND_BLOCK
-> true;
default -> false;
};
}

public static boolean isBook(ItemStack item) {
if (item == null) return false;
return isBook(item.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
}

// Beacons and Indestructibles
if (burrowMaterial.equals(BEACON) || MaterialUtil.INDESTRUCTIBLES.contains(burrowMaterial)) {
if (burrowMaterial.equals(BEACON) || MaterialUtil.SOLID_INDESTRUCTIBLES.contains(burrowMaterial)) {
player.damage(damageWhenMovingInBurrow);
if (shouldTeleportUp) teleportUpAndCenter(player, burrowBlock.getLocation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ private void removePlayerHeadItemIfPresent(ItemStack item) {

private boolean shulkerContainsIllegalHead(ShulkerBox shulkerBox) {
for (ItemStack shulkerContentItem : shulkerBox.getInventory()) {
if (shulkerContentItem != null && shulkerContentItem.getType().equals(PLAYER_HEAD.getType())) {
if (shulkerContentItem.isSimilar(PLAYER_HEAD) || ((SkullMeta) shulkerContentItem.getItemMeta()).hasOwner()) {
if (shulkerContentItem != null && MaterialUtil.PLAYER_HEADS.contains(shulkerContentItem.getType())) {
if (((SkullMeta) shulkerContentItem.getItemMeta()).hasOwner()) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package me.moomoo.anarchyexploitfixes.modules.patches;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.MaterialUtil;
import org.bukkit.Material;
import org.bukkit.block.ShulkerBox;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.BookMeta;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.UUID;

public class BookBan implements AnarchyExploitFixesModule, Listener {

private final Cache<UUID, Long> cachedItemSizes;
private final Cache<UUID, Long> cachedInventorySizes;
private final int maxBookSize, maxItemSize, maxInventorySize;

public BookBan() {
Expand All @@ -28,6 +35,14 @@ public BookBan() {
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);
this.cachedItemSizes = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(
config.getInt("patches.anti-book-ban.dropped-items-size-cache-ticks", 120,
"How long in ticks a dropped item's size should be cached after checking.") * 50L
)).build();
this.cachedInventorySizes = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(
config.getInt("patches.anti-book-ban.player-inventory-size-cache-ticks", 20,
"How long in ticks a player's inventory size should be cached after checking.") * 50L
)).build();
}

@Override
Expand Down Expand Up @@ -58,12 +73,12 @@ private long getItemSize(ItemStack stack) {
final Material material = stack.getType();

if (MaterialUtil.SHULKER_BOXES.contains(material)) {
byteSize += getInventorySize(((ShulkerBox) ((BlockStateMeta) stack.getItemMeta()).getBlockState()).getInventory());
byteSize += this.getInventorySize(((ShulkerBox) ((BlockStateMeta) stack.getItemMeta()).getBlockState()).getInventory());
return byteSize;
}

if (MaterialUtil.BOOKS.contains(material)) {
byteSize += getBookSize((BookMeta) stack.getItemMeta());
byteSize += this.getBookSize((BookMeta) stack.getItemMeta());
return byteSize;
}

Expand All @@ -86,29 +101,43 @@ private long getBookSize(BookMeta bookMeta) {
// Prevent players from creating big books
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBookEdit(PlayerEditBookEvent event) {
if (getBookSize(event.getNewBookMeta()) > maxBookSize) {
if (this.getBookSize(event.getNewBookMeta()) > maxBookSize) {
event.setCancelled(true);
}
}

// Attempt to stop players from picking up large items
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerAttemptPickupItem(PlayerAttemptPickupItemEvent event) {
final long itemSize = getItemSize(event.getItem().getItemStack());
// Check if item is generally over the size limit
final Item item = event.getItem();
final UUID itemUUID = item.getUniqueId();
Long itemSize = this.cachedItemSizes.getIfPresent(itemUUID);
if (itemSize == null)
itemSize = this.getItemSize(item.getItemStack());
this.cachedItemSizes.put(itemUUID, itemSize);
// If item is bigger than allowed, disallow player picking it up
if (itemSize > maxItemSize) {
event.setCancelled(true);
return;
}

// Check if the total inventory size would be larger than the maximum inventory size
if (itemSize + getInventorySize(event.getPlayer().getInventory()) > maxInventorySize) {
final Player player = event.getPlayer();
final UUID playerUUID = player.getUniqueId();
Long invSize = this.cachedInventorySizes.getIfPresent(playerUUID);
if (invSize == null)
invSize = this.getInventorySize(player.getInventory());
this.cachedInventorySizes.put(playerUUID, invSize);
// If picking up item would exceed max inventory size, disallow player picking it up
if (itemSize + invSize > maxInventorySize) {
event.setCancelled(true);
}
}

private void sanitizeInventory(PlayerInventory inventory) {
private void sanitizeInventory(Iterable<ItemStack> inventory) {
for (ItemStack item : inventory) {
if (getItemSize(item) > maxItemSize) {
if (this.getItemSize(item) > maxItemSize) {
item.subtract(item.getAmount());
}
}
Expand All @@ -117,16 +146,16 @@ private void sanitizeInventory(PlayerInventory inventory) {
// Attempt to clear off big items from affected players
@EventHandler(priority = EventPriority.LOW)
private void onJoin(PlayerJoinEvent event) {
sanitizeInventory(event.getPlayer().getInventory());
this.sanitizeInventory(event.getPlayer().getInventory());
}

@EventHandler(priority = EventPriority.LOW)
private void onLeave(PlayerQuitEvent event) {
sanitizeInventory(event.getPlayer().getInventory());
this.sanitizeInventory(event.getPlayer().getInventory());
}

@EventHandler(priority = EventPriority.LOW)
private void onKick(PlayerKickEvent event) {
sanitizeInventory(event.getPlayer().getInventory());
this.sanitizeInventory(event.getPlayer().getInventory());
}
}
Loading

0 comments on commit ba5bd8b

Please sign in to comment.