Skip to content

Commit

Permalink
Fix duping with compound chests, Closes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Nov 27, 2023
1 parent 05dd969 commit be4958f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ public boolean craft(boolean simulate) {
// Determine output
if(chosenPossibility != null && !itemStack.isEmpty()
&& addItemStackForOutput(level, targetPos, targetSide, outputProviders, itemStack, true)) {
if (!simulate) {
addItemStackForOutput(level, targetPos, targetSide, outputProviders, itemStack, simulate);
if (chosenPossibility.handleRemainingItems(level, inputSide, simulate)) {
if (!simulate) {
addItemStackForOutput(level, targetPos, targetSide, outputProviders, itemStack, simulate);
}
return true;
}
chosenPossibility.handleRemainingItems(level, inputSide, simulate);
return true;
}
return false;
}
Expand Down Expand Up @@ -208,15 +209,33 @@ public ItemStack getOutput(Level level) {
* @param inputSide The crafting side.
* @param simulate If the crafting should be simulated.
*/
public void handleRemainingItems(Level level, Direction inputSide, boolean simulate) {
public boolean handleRemainingItems(Level level, Direction inputSide, boolean simulate) {
Recipe recipe = getRecipe(level);
NonNullList<ItemStack> remainingStacks = recipe.getRemainingItems(inventoryCrafting);
for(int i = 0; i < remainingStacks.size(); i++) {
ItemStack originalStack = inventoryCrafting.getItem(i);
ItemStack remainingStack = remainingStacks.get(i);
if(originalStack != null && !originalStack.isEmpty()) {
if (providers[i] != null) {
providers[i].reduceItemStack(level, positions[i], inputSide, simulate);
// Consume one item from input
boolean success = providers[i].reduceItemStack(level, positions[i], inputSide, simulate);

// If consumption failed, consider the whole crafting job failed
if (!success) {
// Restore all previous slots if not simulating
if (!simulate) {
for(int j = 0; j < i; j++) {
ItemStack stackToRestore = inventoryCrafting.getItem(j);
if(stackToRestore != null && !stackToRestore.isEmpty() && providers[j] != null) {
providers[j].addItemStack(level, positions[j], inputSide, stackToRestore, false);
}
}
}

return false;
}

// Add a possibly remaining stack to the slot
if (!remainingStack.isEmpty() && remainingStack.getCount() > 0) {
providers[i].addItemStack(level, positions[i], inputSide, remainingStack, simulate);
}
Expand All @@ -226,6 +245,7 @@ public void handleRemainingItems(Level level, Direction inputSide, boolean simul
}
}
}
return true;
}

public CraftingPossibility clone() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public interface IItemStackProvider {
* @param pos The position.
* @param side The side.
* @param simulate If the operation should be simulated.
* @return If the item could be reduced
*/
public void reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate);
public boolean reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate);

/**
* Adds an itemstack.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ public ItemStack getItemStack(Level world, BlockPos pos, Direction side) {
}

@Override
public void reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate) {
public boolean reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate) {
IItemHandler itemHandler = BlockEntityHelpers.getCapability(world, pos, side, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(null);
if(itemHandler != null) {
boolean extracted = false;
for(int slot = 0; slot < itemHandler.getSlots(); slot++) {
if(!itemHandler.extractItem(slot, 1, simulate).isEmpty()) {
extracted = true;
break;
}
}
return extracted;
} else {
Container inventory = BlockEntityHelpers.get(world, pos, Container.class).orElse(null);
Pair<Integer, ItemStack> result = getFirstItem(inventory, side);
Expand All @@ -93,6 +96,7 @@ public void reduceItemStack(Level world, BlockPos pos, Direction side, boolean s
if(!simulate) {
inventory.setItem(result.getLeft(), newItemStack);
}
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ public ItemStack getItemStack(Level world, BlockPos pos, Direction side) {
}

@Override
public void reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate) {
public boolean reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate) {
boolean wasAir = world.getBlockState(pos).isAir();
if(!simulate) {
world.removeBlock(pos, false);
}
return !wasAir;
}

@Override
Expand Down

0 comments on commit be4958f

Please sign in to comment.