Skip to content

Commit

Permalink
feat(utils): methods for counting matching items
Browse files Browse the repository at this point in the history
int org.auioc.mods.arnicalib.utils.game.ItemUtils.countItem()
  • Loading branch information
WakelessSloth56 committed Feb 19, 2022
1 parent 393a5bc commit 3d56bb3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/auioc/mods/arnicalib/utils/game/ItemUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.auioc.mods.arnicalib.utils.game;

import java.util.function.Predicate;
import javax.annotation.Nullable;
import com.google.gson.JsonObject;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand All @@ -8,6 +9,7 @@
import net.minecraft.nbt.TagParser;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -85,4 +87,27 @@ static ItemStack createItemStack(JsonObject json) {
return createItemStack(item, count, nbt);
}


static int countItem(ItemStack stack, Predicate<ItemStack> predicate) {
if (!stack.isEmpty() && predicate.test(stack)) {
return stack.getCount();
}
return 0;
}

static int countItem(Container container, Predicate<ItemStack> predicate) {
int r = 0;
for (int i = 0, l = container.getContainerSize(); i < l; i++) {
ItemStack stack = container.getItem(i);
if (!stack.isEmpty()) {
r += countItem(stack, predicate);
}
}
return r;
}

static int countItem(Container container) {
return countItem(container, (stack) -> true);
}

}

0 comments on commit 3d56bb3

Please # to comment.