Skip to content

Commit 3090d93

Browse files
committed
Merge remote-tracking branch 'origin/master-1.18' into master-1.19-lts
2 parents 0f23871 + 26b653c commit 3090d93

5 files changed

Lines changed: 42 additions & 9 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
As always, don't forget to backup your world before updating!
2+
Requires CyclopsCore version 1.13.4 or higher.
3+
4+
Fixes:
5+
* Fix duping with compound chests, Closes #33
6+

src/main/java/org/cyclops/structuredcrafting/craft/WorldCraftingMatrix.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ public boolean craft(boolean simulate) {
144144
// Determine output
145145
if(chosenPossibility != null && !itemStack.isEmpty()
146146
&& addItemStackForOutput(level, targetPos, targetSide, outputProviders, itemStack, true)) {
147-
if (!simulate) {
148-
addItemStackForOutput(level, targetPos, targetSide, outputProviders, itemStack, simulate);
147+
if (chosenPossibility.handleRemainingItems(level, inputSide, simulate)) {
148+
if (!simulate) {
149+
addItemStackForOutput(level, targetPos, targetSide, outputProviders, itemStack, simulate);
150+
}
151+
return true;
149152
}
150-
chosenPossibility.handleRemainingItems(level, inputSide, simulate);
151-
return true;
152153
}
153154
return false;
154155
}
@@ -208,15 +209,33 @@ public ItemStack getOutput(Level level) {
208209
* @param inputSide The crafting side.
209210
* @param simulate If the crafting should be simulated.
210211
*/
211-
public void handleRemainingItems(Level level, Direction inputSide, boolean simulate) {
212+
public boolean handleRemainingItems(Level level, Direction inputSide, boolean simulate) {
212213
Recipe recipe = getRecipe(level);
213214
NonNullList<ItemStack> remainingStacks = recipe.getRemainingItems(inventoryCrafting);
214215
for(int i = 0; i < remainingStacks.size(); i++) {
215216
ItemStack originalStack = inventoryCrafting.getItem(i);
216217
ItemStack remainingStack = remainingStacks.get(i);
217218
if(originalStack != null && !originalStack.isEmpty()) {
218219
if (providers[i] != null) {
219-
providers[i].reduceItemStack(level, positions[i], inputSide, simulate);
220+
// Consume one item from input
221+
boolean success = providers[i].reduceItemStack(level, positions[i], inputSide, simulate);
222+
223+
// If consumption failed, consider the whole crafting job failed
224+
if (!success) {
225+
// Restore all previous slots if not simulating
226+
if (!simulate) {
227+
for(int j = 0; j < i; j++) {
228+
ItemStack stackToRestore = inventoryCrafting.getItem(j);
229+
if(stackToRestore != null && !stackToRestore.isEmpty() && providers[j] != null) {
230+
providers[j].addItemStack(level, positions[j], inputSide, stackToRestore, false);
231+
}
232+
}
233+
}
234+
235+
return false;
236+
}
237+
238+
// Add a possibly remaining stack to the slot
220239
if (!remainingStack.isEmpty() && remainingStack.getCount() > 0) {
221240
providers[i].addItemStack(level, positions[i], inputSide, remainingStack, simulate);
222241
}
@@ -226,6 +245,7 @@ public void handleRemainingItems(Level level, Direction inputSide, boolean simul
226245
}
227246
}
228247
}
248+
return true;
229249
}
230250

231251
public CraftingPossibility clone() {

src/main/java/org/cyclops/structuredcrafting/craft/provider/IItemStackProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ public interface IItemStackProvider {
5454
* @param pos The position.
5555
* @param side The side.
5656
* @param simulate If the operation should be simulated.
57+
* @return If the item could be reduced
5758
*/
58-
public void reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate);
59+
public boolean reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate);
5960

6061
/**
6162
* Adds an itemstack.

src/main/java/org/cyclops/structuredcrafting/craft/provider/InventoryItemStackProvider.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,17 @@ public ItemStack getItemStack(Level world, BlockPos pos, Direction side) {
7474
}
7575

7676
@Override
77-
public void reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate) {
77+
public boolean reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate) {
7878
IItemHandler itemHandler = BlockEntityHelpers.getCapability(world, pos, side, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(null);
7979
if(itemHandler != null) {
80+
boolean extracted = false;
8081
for(int slot = 0; slot < itemHandler.getSlots(); slot++) {
8182
if(!itemHandler.extractItem(slot, 1, simulate).isEmpty()) {
83+
extracted = true;
8284
break;
8385
}
8486
}
87+
return extracted;
8588
} else {
8689
Container inventory = BlockEntityHelpers.get(world, pos, Container.class).orElse(null);
8790
Pair<Integer, ItemStack> result = getFirstItem(inventory, side);
@@ -93,6 +96,7 @@ public void reduceItemStack(Level world, BlockPos pos, Direction side, boolean s
9396
if(!simulate) {
9497
inventory.setItem(result.getLeft(), newItemStack);
9598
}
99+
return true;
96100
}
97101
}
98102

src/main/java/org/cyclops/structuredcrafting/craft/provider/WorldItemStackProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ public ItemStack getItemStack(Level world, BlockPos pos, Direction side) {
8383
}
8484

8585
@Override
86-
public void reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate) {
86+
public boolean reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate) {
87+
boolean wasAir = world.getBlockState(pos).isAir();
8788
if(!simulate) {
8889
world.removeBlock(pos, false);
8990
}
91+
return !wasAir;
9092
}
9193

9294
@Override

0 commit comments

Comments
 (0)