diff --git a/src/main/java/slimeknights/mantle/util/ItemStackList.java b/src/main/java/slimeknights/mantle/util/ItemStackList.java index 179c356..eb6f8ea 100644 --- a/src/main/java/slimeknights/mantle/util/ItemStackList.java +++ b/src/main/java/slimeknights/mantle/util/ItemStackList.java @@ -3,13 +3,9 @@ package slimeknights.mantle.util; import net.minecraft.item.ItemStack; import net.minecraft.util.NonNullList; -import org.apache.commons.lang3.Validate; - 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 { @@ -43,6 +39,15 @@ public class ItemStackList extends NonNullList { return itemStackList; } + /** + * Create an ItemStackList from the given elements. + */ + public static ItemStackList of(NonNullList boringList) { + ItemStackList itemStackList = create(); + itemStackList.addAll(boringList); + return itemStackList; + } + /** * Checks if an Itemstack at a specific index is not empty * @@ -64,4 +69,29 @@ public class ItemStackList extends NonNullList { set(index, ItemStack.EMPTY); } } + + /** + * Creates a new list with the same content. ItemStacks are shared between lists! + * @param fixed If true the list will have fixed size + */ + public ItemStackList copy(boolean fixed) { + ItemStackList copy = fixed ? withSize(this.size()) : create(); + for(int i = 0; i < size(); i++) { + copy.set(i, get(i)); + } + return copy; + } + + /** + * Creates a new list with the same content, but Itemstacks are copied too, + * meaning changes to the copy will not affect the itemstacks in the original list. + * @param fixed If true the list will have fixed size + */ + public ItemStackList deepCopy(boolean fixed) { + ItemStackList copy = fixed ? withSize(this.size()) : create(); + for(int i = 0; i < size(); i++) { + copy.set(i, get(i).copy()); + } + return copy; + } } diff --git a/src/main/java/slimeknights/mantle/util/RecipeMatchRegistry.java b/src/main/java/slimeknights/mantle/util/RecipeMatchRegistry.java index d3077b5..151251c 100644 --- a/src/main/java/slimeknights/mantle/util/RecipeMatchRegistry.java +++ b/src/main/java/slimeknights/mantle/util/RecipeMatchRegistry.java @@ -10,6 +10,7 @@ import net.minecraft.util.NonNullList; import java.util.Collection; import java.util.Comparator; import java.util.List; +import java.util.Optional; import java.util.PriorityQueue; /** @@ -19,12 +20,8 @@ public class RecipeMatchRegistry { protected final PriorityQueue items = new PriorityQueue<>(1, RecipeComparator.INSTANCE); - public RecipeMatch.Match matches(Collection stacks) { - return matches(stacks.toArray(new ItemStack[stacks.size()])); - } - // looks for a match in the given itemstacks - public RecipeMatch.Match matches(ItemStack... stacks) { + public Optional matches(ItemStack... stacks) { NonNullList nonNullStacks = NonNullList.withSize(stacks.length, ItemStack.EMPTY); for(int i = 0; i < stacks.length; i++) { if(!stacks[i].isEmpty()) { @@ -36,26 +33,27 @@ public class RecipeMatchRegistry { } // looks for a match in the given itemstacks - public RecipeMatch.Match matches(NonNullList stacks) { + public Optional matches(NonNullList stacks) { for(RecipeMatch recipe : items) { - RecipeMatch.Match match = recipe.matches(stacks); - if(match != null) { + Optional match = recipe.matches(stacks); + if(match.isPresent()) { return match; } } - return null; + return Optional.empty(); } // looks for a match with at least the given amount in the given itemstacks - public RecipeMatch.Match matches(NonNullList stacks, int minAmount) { + public Optional matches(NonNullList stacks, int minAmount) { stacks = copyItemStackArray(stacks); // copy so we don't modify original List matches = Lists.newLinkedList(); - RecipeMatch.Match match; + Optional matchOptional; int sum = 0; - while(sum < minAmount && (match = matches(stacks)) != null) { + while(sum < minAmount && (matchOptional = matches(stacks)).isPresent()) { + RecipeMatch.Match match = matchOptional.get(); matches.add(match); RecipeMatch.removeMatch(stacks, match); @@ -64,7 +62,7 @@ public class RecipeMatchRegistry { // not enough found if(sum < minAmount) { - return null; + return Optional.empty(); } // merge all found matches into one match @@ -73,17 +71,18 @@ public class RecipeMatchRegistry { foundStacks.addAll(m.stacks); } - return new RecipeMatch.Match(foundStacks, sum); + return Optional.of(new RecipeMatch.Match(foundStacks, sum)); } - public RecipeMatch.Match matchesRecursively(NonNullList stacks) { + public Optional matchesRecursively(NonNullList stacks) { stacks = copyItemStackArray(stacks); // copy so we don't modify original List matches = Lists.newLinkedList(); - RecipeMatch.Match match; + Optional matchOptional; int sum = 0; - while((match = matches(stacks)) != null) { + while((matchOptional = matches(stacks)).isPresent()) { + RecipeMatch.Match match = matchOptional.get(); matches.add(match); RecipeMatch.removeMatch(stacks, match); @@ -96,7 +95,7 @@ public class RecipeMatchRegistry { foundStacks.addAll(m.stacks); } - return new RecipeMatch.Match(foundStacks, sum); + return Optional.of(new RecipeMatch.Match(foundStacks, sum)); } /** @@ -158,7 +157,7 @@ public class RecipeMatchRegistry { public static NonNullList copyItemStackArray(NonNullList in) { - NonNullList stacksCopy = NonNullList. withSize(in.size(), ItemStack.EMPTY); + NonNullList stacksCopy = NonNullList.withSize(in.size(), ItemStack.EMPTY); for(int i = 0; i < in.size(); i++) { if(!in.get(i).isEmpty()) { stacksCopy.set(i, in.get(i).copy());