Skip to content

Commit

Permalink
Fix recipematch removal for larger single stacks with smaller matches
Browse files Browse the repository at this point in the history
  • Loading branch information
bonii-xx committed Jul 6, 2016
1 parent 01f0539 commit c232c4e
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/main/java/slimeknights/mantle/util/RecipeMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,27 @@ public static RecipeMatch ofNBT(ItemStack stack, int matched) {
return new RecipeMatch.ItemCombination(matched, stack);
}

/** Removes the match from the stacks */
/** Removes the match from the stacks. Has to be ensured that the match is contained in the stacks. */
public static void removeMatch(ItemStack[] stacks, Match match) {
// ensure that the order is the same, so that even if multiple of the same type are contained it has the expected result
int[] amountsRemoved = new int[match.stacks.size()];

removeOrder(stacks, match.stacks, amountsRemoved);
removeRemaining(stacks, match.stacks, amountsRemoved);
}

private static void removeOrder(ItemStack[] stacks, List<ItemStack> toRemove, int[] amountsRemoved) {
int i = 0;
for(ItemStack stack : match.stacks) {
for(int j = 0; j < amountsRemoved.length; j++) {
ItemStack stack = toRemove.get(j);
for(; i < stacks.length; i++) {
// nbt sensitive since toolparts etc. use nbt
if(ItemStack.areItemsEqual(stack, stacks[i]) && ItemStack.areItemStackTagsEqual(stack, stacks[i])) {
if(stacks[i].stackSize < stack.stackSize) {
Mantle.logger.error("RecipeMatch has incorrect stacksize! {}", stacks[i].toString());
}
else {
if(stacks[i].stackSize >= stack.stackSize) {
stacks[i].stackSize -= stack.stackSize;
if(stacks[i].stackSize == 0) {
stacks[i] = null;
}
amountsRemoved[j] += stack.stackSize;
}
i++;
break;
Expand All @@ -133,6 +138,23 @@ public static void removeMatch(ItemStack[] stacks, Match match) {
}
}

private static void removeRemaining(ItemStack[] stacks, List<ItemStack> toRemove, int[] amountsRemoved) {
for(int j = 0; j < amountsRemoved.length; j++) {
ItemStack stack = toRemove.get(j);
int needed = stack.stackSize - amountsRemoved[j];
for(int i = 0; i < stacks.length && needed > 0; i++) {
if(ItemStack.areItemsEqual(stack, stacks[i]) && ItemStack.areItemStackTagsEqual(stack, stacks[i])) {
int change = Math.min(stacks[i].stackSize, needed);
stacks[i].stackSize -= change;
if(stacks[i].stackSize == 0) {
stacks[i] = null;
}
needed -= change;
}
}
}
}

/** A specific amount of a certain item is needed. Supports wildcard-metadata. Not NBT sensitive. */
public static class Item extends RecipeMatch {

Expand Down

0 comments on commit c232c4e

Please sign in to comment.