From a45607f7fc5d7db43ff3088bad0cc83dd5e723ad Mon Sep 17 00:00:00 2001 From: Bernhard Bonigl Date: Fri, 21 Apr 2017 16:43:05 +0200 Subject: [PATCH] Itemstacklist util --- .../mantle/util/ItemStackList.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/slimeknights/mantle/util/ItemStackList.java diff --git a/src/main/java/slimeknights/mantle/util/ItemStackList.java b/src/main/java/slimeknights/mantle/util/ItemStackList.java new file mode 100644 index 0000000..a943dda --- /dev/null +++ b/src/main/java/slimeknights/mantle/util/ItemStackList.java @@ -0,0 +1,63 @@ +package slimeknights.mantle.util; + +import net.minecraft.item.ItemStack; +import net.minecraft.util.NonNullList; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class ItemStackList extends NonNullList { + + protected ItemStackList() { + this(new ArrayList<>()); + } + + protected ItemStackList(List delegate) { + super(new ArrayList<>(), ItemStack.EMPTY); + } + + public static ItemStackList create() { + return new ItemStackList(); + } + + /** + * Create an empty ItemStackList with the given size + */ + public static ItemStackList withSize(int size) { + return new ItemStackList(IntStream.range(0, size).mapToObj(i -> ItemStack.EMPTY).collect(Collectors.toList())); + } + + /** + * Create an ItemStackList from the given elements. + */ + public static ItemStackList of(ItemStack... element) { + ItemStackList itemStackList = create(); + itemStackList.addAll(Arrays.asList(element)); + return itemStackList; + } + + /** + * Checks if an Itemstack at a specific index is not empty + * + * @param index The index to check + * @return true if the itemstack at index index is not empty, false otherwise or if the index is out of bounds + */ + public boolean hasItem(int index) { + return index >= 0 && index < size() && !get(index).isEmpty(); + } + + /** + * Sets the itemstack at the given index to Itemstack.EMPTY. + * Does nothing if the index is out of bounds. + * + * @param index The index to set empty + */ + public void setEmpty(int index) { + if(index >= 0 && index < size()) { + set(index, ItemStack.EMPTY); + } + } +}