Skip to content

Commit

Permalink
Fixes dupe glitch in #2358
Browse files Browse the repository at this point in the history
- Checks the input before any processing happens
- Recipe preview now updates when recipe grid changes instead of when
it's done processing
  • Loading branch information
PocketSizedWeeb committed Apr 20, 2024
1 parent a681f80 commit 6d79204
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/main/java/com/lothrazar/cyclic/block/crafter/TileCrafter.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ public void serverTick() {
if (timer < 0) {
timer = 0;
}
//timer phase
if (--timer > 0) {
return;
}
//timer is out, therefore processing
Recipe<CraftingContainer> lastValidRecipe = findMatchingRecipe(null);
if (lastValidRecipe == null) {
//reset
Expand All @@ -160,6 +155,13 @@ public void serverTick() {
setPreviewSlot(recipeOutput);
//if we have space for the output, then go ahead
if (hasFreeSpace(outHandler, recipeOutput)) {
if(!checkInput(inputHandler)) { //Checks input before processing
this.timer = TIMER_FULL;
return;
}
if (--timer > 0) { //Get recipe before processing
return;
}
if (doCraft(lastValidRecipe)) {
//reset the timer
this.timer = TIMER_FULL;
Expand Down Expand Up @@ -211,6 +213,35 @@ private boolean hasFreeSpace(IItemHandler inv, ItemStack output) {
return test.isEmpty(); //empty means all of it was allowed to go in
}

// This could be done better, but it works so ¯\_(ツ)_/¯
private boolean checkInput(IItemHandler inv) {
IItemHandler gridHandler = this.gridCap.orElse(null);
List<ItemStack> inputStacks = new ArrayList<ItemStack>();
List<ItemStack> gridStacks = new ArrayList<ItemStack>();
for(int i = 0; i < inv.getSlots(); i++) {
inputStacks.add(inv.getStackInSlot(i).copy());
}
for(int i = 0; i < gridHandler.getSlots(); i++) {
gridStacks.add(gridHandler.getStackInSlot(i).copy());
}
for(ItemStack stack : inputStacks) {
List<ItemStack> lolbit = new ArrayList<ItemStack>();
boolean match = false;
for(ItemStack grid : gridStacks) {
if(stack.getItem() == grid.getItem()) {
match = true;
lolbit.add(grid);
stack.shrink(1);
}
}
if(match) {
gridStacks.removeAll(lolbit);
}
}
if(gridStacks.isEmpty()) return true;
return false;
}

//TODO:? re-write this whole thing using ASSEMBLE?
//big change
// for (int i = 0; i < 9; i++) {
Expand Down

0 comments on commit 6d79204

Please sign in to comment.