Skip to content

Commit

Permalink
feat(utils): methods for clearing the specified count of items
Browse files Browse the repository at this point in the history
org.auioc.mods.arnicalib.utils.game.ItemUtils.clearItem()
  • Loading branch information
WakelessSloth56 committed Feb 19, 2022
1 parent 3d56bb3 commit 15d1752
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/java/org/auioc/mods/arnicalib/utils/game/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,51 @@ static int countItem(Container container) {
return countItem(container, (stack) -> true);
}


static int clearItem(ItemStack stack, Predicate<ItemStack> predicate, int count, boolean required) {
if (count > 0 && !stack.isEmpty() && predicate.test(stack)) {
int r = 0;
if (stack.getCount() < count) {
if (required) {
return stack.getCount() - count;
}
r = stack.getCount();
} else {
r = count;
}
stack.shrink(r);
return r;
}
return 0;
}

static int clearItem(Container container, Predicate<ItemStack> predicate, int count, boolean required) {
if (count <= 0) {
return 0;
}

{
int c = countItem(container, predicate);
if (required && c < count) {
return c - count;
}
}

int r = 0;
for (int i = 0, l = container.getContainerSize(); i < l; i++) {
ItemStack stack = container.getItem(i);
if (!stack.isEmpty()) {
int c = clearItem(stack, predicate, count - r, false);
if (c > 0 && stack.isEmpty()) {
container.setItem(i, ItemStack.EMPTY);
}
r += c;
if (r >= count) {
return r;
}
}
}
return r - count;
}

}

0 comments on commit 15d1752

Please # to comment.