Skip to content

Commit

Permalink
update => 1.0.66: per-echest book limit
Browse files Browse the repository at this point in the history
  • Loading branch information
crxyne committed May 18, 2024
1 parent febe968 commit 4f5645a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.zeroBzeroT</groupId>
<artifactId>AntiIllegals</artifactId>
<version>1.0.65</version>
<version>1.0.66</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>

Expand Down
25 changes: 16 additions & 9 deletions src/main/java/org/zeroBzeroT/antiillegals/helpers/BookHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.zeroBzeroT.antiillegals.AntiIllegals;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

public class BookHelper {
Expand All @@ -36,13 +37,14 @@ public static void dropBookShulkerItem(@NotNull final Location location, @NotNul
}

public static int cleanBookItems(@NotNull final Inventory inventory, @Nullable final Location location,
@NotNull final Collection<ItemStack> shulkerWithBooksItemStack) {
return cleanOversizedItems(inventory, location, shulkerWithBooksItemStack, maxBookItemsInShulker());
@NotNull final Collection<ItemStack> shulkerWithBooksItemStack,
final int initialCount) {
return cleanOversizedItems(inventory, location, shulkerWithBooksItemStack, maxBookItemsInShulker(), initialCount);
}

public static int cleanBookShulkers(@NotNull final Inventory inventory, @Nullable final Location location,
@NotNull final Collection<ItemStack> shulkerWithBooksItemStack) {
return cleanOversizedItems(inventory, location, shulkerWithBooksItemStack, maxBookShulkers());
return cleanOversizedItems(inventory, location, shulkerWithBooksItemStack, maxBookShulkers(), 0);
}

public static boolean isBookItem(@NotNull final ItemStack itemStack) {
Expand Down Expand Up @@ -96,28 +98,33 @@ public static int cleanBookShulkers(@NotNull final Inventory inventory, @Nullabl

public static int cleanOversizedItems(@NotNull final Inventory inventory, @Nullable final Location location,
@NotNull final Collection<ItemStack> shulkerWithBooksItemStack,
final int maxItems) {
if (location == null || maxItems < 0 || shulkerWithBooksItemStack.size() <= maxItems) return 0;
final int maxItems, final int initialCount) {
if (location == null || maxItems < 0) return 0;

int counter = 0;
int counter = initialCount;

for (final ItemStack shulkerItemStack : shulkerWithBooksItemStack) {
inventory.remove(shulkerItemStack);
Bukkit.getScheduler().runTask(AntiIllegals.INSTANCE, () -> dropBookShulkerItem(location, shulkerItemStack));
if (counter > maxItems) {
inventory.remove(shulkerItemStack);
Bukkit.getScheduler().runTask(AntiIllegals.INSTANCE, () -> dropBookShulkerItem(location, shulkerItemStack));
}
counter++;
}
return counter;
}

public static void checkEnderChest(@NotNull final InventoryOpenEvent inventoryOpenEvent,
@Nullable final Location location) {
final Inventory inventory = inventoryOpenEvent.getInventory();
final ItemStack[] inventoryContents = inventory.getContents();

final AtomicInteger bookCount = new AtomicInteger();
for (final ItemStack itemStack : inventoryContents) {
if (itemStack == null) continue;

InventoryHolderHelper.iterateInventory(itemStack, inv ->
BookHelper.cleanBookItems(inv, location, BookHelper.filterBooks(inv).toList())
bookCount.set(BookHelper.cleanBookItems(inv, location,
BookHelper.filterBooks(inv).toList(), bookCount.get()))
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static RevertionResult checkInventory(@NotNull final Inventory inventory,
removeItemStacks.forEach(i -> i.setAmount(0));

final int fixesIllegals = removeItemStacks.size();
final int fixesBooks = isInsideShulker ? BookHelper.cleanBookItems(inventory, location, bookItemStacks) : 0;
final int fixesBooks = isInsideShulker ? BookHelper.cleanBookItems(inventory, location, bookItemStacks, 0) : 0;
final int fixesBookShulkers = BookHelper.cleanBookShulkers(inventory, location, shulkerWithBooksItemStack);

final boolean wasReverted = wasFixed || fixesIllegals > 0;
Expand Down

0 comments on commit 4f5645a

Please sign in to comment.