Skip to content

Commit

Permalink
fixed chance outputs being voided by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Aug 30, 2024
1 parent c2d586e commit 6819002
Showing 1 changed file with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import org.jetbrains.annotations.Nullable;
import tesseract.TesseractGraphWrappers;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;

import static muramasa.antimatter.machine.MachineState.*;

Expand All @@ -52,6 +50,7 @@ public class MachineRecipeHandler<T extends BlockEntityMachine<T>> implements IM
@Nullable
protected IRecipe activeRecipe;
protected boolean consumedResources;
protected List<ItemStack> outputs = null;
@Getter
protected int currentProgress,
maxProgress;
Expand Down Expand Up @@ -238,18 +237,13 @@ protected void activateRecipe(boolean reset) {
currentProgress = 0;
}
lastRecipe = activeRecipe;
outputs = null;
}

protected void addOutputs() {
if (activeRecipe.hasOutputItems()) {
tile.itemHandler.ifPresent(h -> {
//Roll the chances here. If they don't fit add flat (no chances).
ItemStack[] out = activeRecipe.getOutputItems(true);
if (h.canOutputsFit(out)) {
h.addOutputs(out);
} else {
h.addOutputs(activeRecipe.getFlatOutputItems());
}
h.addOutputs(outputs.toArray(ItemStack[]::new));
tile.onMachineEvent(MachineEvent.ITEMS_OUTPUTTED);
});
}
Expand Down Expand Up @@ -430,13 +424,21 @@ public boolean consumeInputs() {
}).orElse(true);
}
if (flag) consumedResources = true;
if (consumedResources){
if (activeRecipe.hasOutputItems()){
this.outputs = Arrays.stream(activeRecipe.getOutputItems(true)).toList();
}
}
return flag;
}

public boolean canOutput() {
//ignore chance for canOutput.
if (tile.itemHandler.isPresent() && activeRecipe.hasOutputItems() && !tile.itemHandler.map(t -> t.canOutputsFit(activeRecipe.getFlatOutputItems())).orElse(false))
return false;
if (tile.itemHandler.isPresent() && activeRecipe.hasOutputItems() && outputs != null) {
if (!tile.itemHandler.map(t -> t.canOutputsFit(outputs.toArray(ItemStack[]::new))).orElse(false)) {
return false;
}
}
return !tile.fluidHandler.isPresent() || !activeRecipe.hasOutputFluids() || tile.fluidHandler.map(t -> t.canOutputsFit(activeRecipe.getOutputFluids())).orElse(false);
}

Expand Down Expand Up @@ -520,6 +522,7 @@ protected long calculateGeneratorConsumption(IRecipe r) {

public void resetRecipe() {
this.activeRecipe = null;
outputs = null;
this.consumedResources = false;
this.currentProgress = 0;
this.overclock = 0;
Expand Down Expand Up @@ -632,6 +635,13 @@ public CompoundTag serialize() {
if (lastRecipe != null){
nbt.putString("LR", lastRecipe.getId().toString());
}
ListTag outputs = new ListTag();
if (this.outputs != null){
this.outputs.forEach(i -> outputs.add(i.save(new CompoundTag())));
}
if (!outputs.isEmpty()) {
nbt.put("O", outputs);
}
return nbt;
}

Expand All @@ -646,6 +656,14 @@ public void deserialize(CompoundTag nbt) {
this.consumedResources = nbt.getBoolean("C");
this.activeRecipe = nbt.contains("AR") ? this.tile.getMachineType().getRecipeMap(tile.getMachineTier()).findByID(new ResourceLocation(nbt.getString("AR"))) : null;
this.lastRecipe = nbt.contains("LR") ? this.tile.getMachineType().getRecipeMap(tile.getMachineTier()).findByID(new ResourceLocation(nbt.getString("LR"))) : null;
if (nbt.contains("O")) {
ListTag listTag = nbt.getList("O", 10);
List<ItemStack> stacks = new ArrayList<>();
listTag.forEach(t -> {
stacks.add(ItemStack.of((CompoundTag) t));
});
this.outputs = stacks;
}
if (this.activeRecipe != null) calculateDurations();
}

Expand Down

0 comments on commit 6819002

Please sign in to comment.