aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util
diff options
context:
space:
mode:
authormiozune <miozune@gmail.com>2023-06-04 19:54:11 +0900
committerGitHub <noreply@github.com>2023-06-04 12:54:11 +0200
commitf046db94220c1b582175f858f07fd64e81e6e864 (patch)
tree217d79501c31b76db9d38a75ccaa1ee09d96117e /src/main/java/gregtech/api/util
parent9e4456e39709d815ba28064620ff0290ac14151b (diff)
downloadGT5-Unofficial-f046db94220c1b582175f858f07fd64e81e6e864.tar.gz
GT5-Unofficial-f046db94220c1b582175f858f07fd64e81e6e864.tar.bz2
GT5-Unofficial-f046db94220c1b582175f858f07fd64e81e6e864.zip
Fix void protection not working with MB with custom output hatch field (#2051)
* Fix void protection not working with MB with custom output hatch field * forgot to filter * Add util method for DT-like structure
Diffstat (limited to 'src/main/java/gregtech/api/util')
-rw-r--r--src/main/java/gregtech/api/util/GT_ParallelHelper.java4
-rw-r--r--src/main/java/gregtech/api/util/GT_Recipe.java4
-rw-r--r--src/main/java/gregtech/api/util/GT_RecipeBuilder.java39
-rw-r--r--src/main/java/gregtech/api/util/GT_Utility.java4
-rw-r--r--src/main/java/gregtech/api/util/OutputHatchWrapper.java65
-rw-r--r--src/main/java/gregtech/api/util/VoidProtectionHelper.java188
6 files changed, 122 insertions, 182 deletions
diff --git a/src/main/java/gregtech/api/util/GT_ParallelHelper.java b/src/main/java/gregtech/api/util/GT_ParallelHelper.java
index ff45f8213b..66f1bac8b8 100644
--- a/src/main/java/gregtech/api/util/GT_ParallelHelper.java
+++ b/src/main/java/gregtech/api/util/GT_ParallelHelper.java
@@ -299,9 +299,9 @@ public class GT_ParallelHelper {
if (protectExcessItem || protectExcessFluid) {
VoidProtectionHelper voidProtectionHelper = new VoidProtectionHelper();
if (mMachineMeta != null) {
- voidProtectionHelper.setController(mMachineMeta, protectExcessItem, protectExcessFluid);
+ voidProtectionHelper.setMachine(mMachineMeta, protectExcessItem, protectExcessFluid);
} else if (mMachineMulti != null) {
- voidProtectionHelper.setController(mMachineMulti, protectExcessItem, protectExcessFluid);
+ voidProtectionHelper.setMachine(mMachineMulti, protectExcessItem, protectExcessFluid);
}
voidProtectionHelper.setItemOutputs(mRecipe.mOutputs)
.setFluidOutputs(mRecipe.mFluidOutputs)
diff --git a/src/main/java/gregtech/api/util/GT_Recipe.java b/src/main/java/gregtech/api/util/GT_Recipe.java
index 6f0c7c403e..43dfd0c5b2 100644
--- a/src/main/java/gregtech/api/util/GT_Recipe.java
+++ b/src/main/java/gregtech/api/util/GT_Recipe.java
@@ -172,8 +172,8 @@ public class GT_Recipe implements Comparable<GT_Recipe> {
public List<List<StackTraceElement>> stackTraces = new ArrayList<>();
private GT_Recipe(GT_Recipe aRecipe, boolean shallow) {
- mInputs = shallow ? aRecipe.mInputs : GT_Utility.copyStackArray((Object[]) aRecipe.mInputs);
- mOutputs = shallow ? aRecipe.mOutputs : GT_Utility.copyStackArray((Object[]) aRecipe.mOutputs);
+ mInputs = shallow ? aRecipe.mInputs : GT_Utility.copyItemArray(aRecipe.mInputs);
+ mOutputs = shallow ? aRecipe.mOutputs : GT_Utility.copyItemArray(aRecipe.mOutputs);
mSpecialItems = aRecipe.mSpecialItems;
mChances = aRecipe.mChances;
mFluidInputs = shallow ? aRecipe.mFluidInputs : GT_Utility.copyFluidArray(aRecipe.mFluidInputs);
diff --git a/src/main/java/gregtech/api/util/GT_RecipeBuilder.java b/src/main/java/gregtech/api/util/GT_RecipeBuilder.java
index 41ad02e242..e05798479e 100644
--- a/src/main/java/gregtech/api/util/GT_RecipeBuilder.java
+++ b/src/main/java/gregtech/api/util/GT_RecipeBuilder.java
@@ -1,5 +1,8 @@
package gregtech.api.util;
+import static gregtech.api.util.GT_Utility.copyFluidArray;
+import static gregtech.api.util.GT_Utility.copyItemArray;
+
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -378,26 +381,6 @@ public class GT_RecipeBuilder {
return metadata(GT_RecipeConstants.LOW_GRAVITY, true);
}
- private static ItemStack[] copy(ItemStack[] arr) {
- if (arr == null) return null;
- ItemStack[] ret = new ItemStack[arr.length];
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] == null) continue;
- ret[i] = arr[i].copy();
- }
- return ret;
- }
-
- private static FluidStack[] copy(FluidStack[] arr) {
- if (arr == null) return null;
- FluidStack[] ret = new FluidStack[arr.length];
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] == null) continue;
- ret[i] = arr[i].copy();
- }
- return ret;
- }
-
private static <T> T[] copy(T[] arr) {
return arr == null ? null : arr.clone();
}
@@ -414,12 +397,12 @@ public class GT_RecipeBuilder {
*/
public GT_RecipeBuilder copy() {
return new GT_RecipeBuilder(
- copy(inputsBasic),
+ copyItemArray(inputsBasic),
copy(inputsOreDict),
- copy(outputs),
+ copyItemArray(outputs),
copy(alts),
- copy(fluidInputs),
- copy(fluidOutputs),
+ copyFluidArray(fluidInputs),
+ copyFluidArray(fluidOutputs),
copy(chances),
special,
duration,
@@ -441,12 +424,12 @@ public class GT_RecipeBuilder {
*/
public GT_RecipeBuilder copyNoMetadata() {
return new GT_RecipeBuilder(
- copy(inputsBasic),
+ copyItemArray(inputsBasic),
copy(inputsOreDict),
- copy(outputs),
+ copyItemArray(outputs),
copy(alts),
- copy(fluidInputs),
- copy(fluidOutputs),
+ copyFluidArray(fluidInputs),
+ copyFluidArray(fluidOutputs),
copy(chances),
special,
duration,
diff --git a/src/main/java/gregtech/api/util/GT_Utility.java b/src/main/java/gregtech/api/util/GT_Utility.java
index 5e8e9efbca..7f423e0c19 100644
--- a/src/main/java/gregtech/api/util/GT_Utility.java
+++ b/src/main/java/gregtech/api/util/GT_Utility.java
@@ -2816,12 +2816,14 @@ public class GT_Utility {
}
public static FluidStack[] copyFluidArray(FluidStack... aStacks) {
+ if (aStacks == null) return null;
FluidStack[] rStacks = new FluidStack[aStacks.length];
for (int i = 0; i < aStacks.length; i++) if (aStacks[i] != null) rStacks[i] = aStacks[i].copy();
return rStacks;
}
- public static ItemStack[] copyStackArray(Object... aStacks) {
+ public static ItemStack[] copyItemArray(ItemStack... aStacks) {
+ if (aStacks == null) return null;
ItemStack[] rStacks = new ItemStack[aStacks.length];
for (int i = 0; i < aStacks.length; i++) rStacks[i] = copy(aStacks[i]);
return rStacks;
diff --git a/src/main/java/gregtech/api/util/OutputHatchWrapper.java b/src/main/java/gregtech/api/util/OutputHatchWrapper.java
new file mode 100644
index 0000000000..b2e74d24cf
--- /dev/null
+++ b/src/main/java/gregtech/api/util/OutputHatchWrapper.java
@@ -0,0 +1,65 @@
+package gregtech.api.util;
+
+import java.util.function.Predicate;
+
+import net.minecraftforge.fluids.FluidStack;
+import net.minecraftforge.fluids.FluidTankInfo;
+
+import org.jetbrains.annotations.NotNull;
+
+import gregtech.api.interfaces.fluid.IFluidStore;
+import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch_Output;
+
+/**
+ * Wrapper for output hatch to allow multiblocks to apply specific filter.
+ */
+public class OutputHatchWrapper implements IFluidStore {
+
+ private final GT_MetaTileEntity_Hatch_Output outputHatch;
+ private final Predicate<FluidStack> filter;
+
+ public OutputHatchWrapper(GT_MetaTileEntity_Hatch_Output outputHatch, Predicate<FluidStack> filter) {
+ this.outputHatch = outputHatch;
+ this.filter = filter;
+ }
+
+ @Override
+ public FluidStack getFluid() {
+ return outputHatch.getFluid();
+ }
+
+ @Override
+ public int getFluidAmount() {
+ return outputHatch.getFluidAmount();
+ }
+
+ @Override
+ public int getCapacity() {
+ return outputHatch.getCapacity();
+ }
+
+ @Override
+ public FluidTankInfo getInfo() {
+ return outputHatch.getInfo();
+ }
+
+ @Override
+ public int fill(FluidStack resource, boolean doFill) {
+ return outputHatch.fill(resource, doFill);
+ }
+
+ @Override
+ public FluidStack drain(int maxDrain, boolean doDrain) {
+ return outputHatch.drain(maxDrain, doDrain);
+ }
+
+ @Override
+ public boolean isEmptyAndAcceptsAnyFluid() {
+ return outputHatch.isEmptyAndAcceptsAnyFluid();
+ }
+
+ @Override
+ public boolean canStoreFluid(@NotNull FluidStack fluidStack) {
+ return outputHatch.canStoreFluid(fluidStack) && filter.test(fluidStack);
+ }
+}
diff --git a/src/main/java/gregtech/api/util/VoidProtectionHelper.java b/src/main/java/gregtech/api/util/VoidProtectionHelper.java
index 9a66a55874..fe80bc97b4 100644
--- a/src/main/java/gregtech/api/util/VoidProtectionHelper.java
+++ b/src/main/java/gregtech/api/util/VoidProtectionHelper.java
@@ -1,28 +1,19 @@
package gregtech.api.util;
-import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
-import java.util.function.BiFunction;
-import java.util.function.Function;
-import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
-import net.minecraftforge.fluids.IFluidTank;
import com.gtnewhorizon.gtnhlib.util.map.ItemStackMap;
-import com.gtnewhorizons.modularui.api.forge.IItemHandler;
-import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch;
+import gregtech.api.interfaces.fluid.IFluidStore;
+import gregtech.api.interfaces.tileentity.IVoidable;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase;
-import gregtech.api.multitileentity.multiblock.base.Controller;
-import gregtech.common.tileentities.machines.GT_MetaTileEntity_Hatch_OutputBus_ME;
-import gregtech.common.tileentities.machines.GT_MetaTileEntity_Hatch_Output_ME;
/**
* Helper class to calculate how many parallels of items / fluids can fit in the output buses / hatches.
@@ -30,13 +21,9 @@ import gregtech.common.tileentities.machines.GT_MetaTileEntity_Hatch_Output_ME;
public class VoidProtectionHelper {
/**
- * A MetaTileEntity Controller
+ * Machine used for calculation
*/
- private GT_MetaTileEntity_MultiBlockBase machineMeta;
- /**
- * A MultiTileEntity Controller
- */
- private Controller<?> machineMulti;
+ private IVoidable machine;
/**
* Does void protection enabled for items
*/
@@ -66,37 +53,39 @@ public class VoidProtectionHelper {
/**
* Sets MetaTE controller, with current configuration for void protection mode.
+ *
+ * @deprecated Use {@link #setMachine(IVoidable)}
*/
+ @Deprecated
public VoidProtectionHelper setController(GT_MetaTileEntity_MultiBlockBase machineMeta) {
return setController(machineMeta, machineMeta.protectsExcessItem(), machineMeta.protectsExcessFluid());
}
/**
* Sets MetaTE controller, with void protection mode forcibly.
+ *
+ * @deprecated Use {@link #setMachine(IVoidable, boolean, boolean)}
*/
+ @Deprecated
public VoidProtectionHelper setController(GT_MetaTileEntity_MultiBlockBase machineMeta, boolean protectExcessItem,
boolean protectExcessFluid) {
- this.protectExcessItem = protectExcessItem;
- this.protectExcessFluid = protectExcessFluid;
- this.machineMeta = machineMeta;
- return this;
+ return setMachine(machineMeta, protectExcessItem, protectExcessFluid);
}
/**
- * Sets MuTE controller, with current configuration for void protection mode.
+ * Sets machine, with current configuration for void protection mode.
*/
- public VoidProtectionHelper setController(Controller<?> machineMulti) {
- return setController(machineMulti, machineMulti.protectsExcessItem(), machineMulti.protectsExcessFluid());
+ public VoidProtectionHelper setMachine(IVoidable machine) {
+ return setMachine(machine, machine.protectsExcessItem(), machine.protectsExcessFluid());
}
/**
- * Sets MuTE controller, with void protection mode forcibly.
+ * Sets machine, with void protection mode forcibly.
*/
- public VoidProtectionHelper setController(Controller<?> machineMulti, boolean protectExcessItem,
- boolean protectExcessFluid) {
+ public VoidProtectionHelper setMachine(IVoidable machine, boolean protectExcessItem, boolean protectExcessFluid) {
this.protectExcessItem = protectExcessItem;
this.protectExcessFluid = protectExcessFluid;
- this.machineMulti = machineMulti;
+ this.machine = machine;
return this;
}
@@ -125,6 +114,9 @@ public class VoidProtectionHelper {
if (built) {
throw new IllegalStateException("Tried to build twice");
}
+ if (machine == null) {
+ throw new IllegalStateException("Machine is not set");
+ }
built = true;
determineParallel();
return this;
@@ -151,85 +143,21 @@ public class VoidProtectionHelper {
fluidOutputs = new FluidStack[0];
}
- // Don't check ControllerWithOptionalFeatures#protectsExcessItem nor #protectsExcessFluid here,
+ // Don't check IVoidable#protectsExcessItem nor #protectsExcessFluid here,
// to allow more involved setting for void protections (see ComplexParallelProcessingLogic)
- if (machineMeta != null) {
- boolean tMEOutputBus = false;
- boolean tMEOutputHatch = false;
- for (GT_MetaTileEntity_Hatch tHatch : machineMeta.mOutputBusses) {
- if (tHatch instanceof GT_MetaTileEntity_Hatch_OutputBus_ME) {
- tMEOutputBus = true;
- break;
- }
- }
-
- for (GT_MetaTileEntity_Hatch tHatch : machineMeta.mOutputHatches) {
- if (tHatch instanceof GT_MetaTileEntity_Hatch_Output_ME) {
- tMEOutputHatch = true;
- break;
- }
- }
-
- if (protectExcessItem && itemOutputs.length > 0 && !tMEOutputBus) {
- maxParallel = Math.min(calculateMaxItemParallelsForMetaTEs(), maxParallel);
- }
- if (protectExcessFluid && fluidOutputs.length > 0 && !tMEOutputHatch) {
- maxParallel = Math.min(calculateMaxFluidParallelsForMetaTEs(), maxParallel);
- }
- } else if (machineMulti != null) {
- if (protectExcessItem && itemOutputs.length > 0) {
- maxParallel = Math.min(calculateMaxItemParallelsForMuTEs(), maxParallel);
- }
- if (protectExcessFluid && fluidOutputs.length > 0) {
- maxParallel = Math.min(calculateMaxFluidParallelsForMuTEs(), maxParallel);
- }
- }
- }
-
- /**
- * Calculates the max parallel for fluids if void protection is turned on
- */
- private int calculateMaxFluidParallelsForMuTEs() {
- if (machineMulti == null || machineMulti.getOutputTanks() == null) {
- return 0;
+ if (protectExcessItem && itemOutputs.length > 0 && !machine.canDumpItemToME()) {
+ maxParallel = Math.min(calculateMaxItemParallels(), maxParallel);
}
- return calculateMaxFluidParallels(
- Arrays.asList(machineMulti.getOutputTanks()),
- tHatch -> tHatch.getFluidAmount() == 0,
- (tHatch, fluidStack) -> true);
- }
-
- /**
- * Calculates the max parallel for fluids if void protection is turned on
- */
- private int calculateMaxFluidParallelsForMetaTEs() {
- if (machineMeta == null) {
- return 0;
+ if (protectExcessFluid && fluidOutputs.length > 0 && !machine.canDumpFluidToME()) {
+ maxParallel = Math.min(calculateMaxFluidParallels(), maxParallel);
}
- return calculateMaxFluidParallels(
- machineMeta.mOutputHatches,
- tHatch -> tHatch.mMode == 0 && tHatch.getFluidAmount() == 0,
- (tHatch, fluidStack) -> {
- if (GT_ModHandler.isSteam(fluidStack)) {
- return tHatch.outputsSteam();
- } else {
- if (!tHatch.outputsLiquids()) {
- return false;
- }
- String tLockedFluidName = tHatch.getLockedFluidName();
- return !tHatch.isFluidLocked() || tLockedFluidName == null
- || tLockedFluidName.equals(
- fluidStack.getFluid()
- .getName());
- }
- });
}
/**
* Calculates the max parallel for fluids if void protection is turned on
*/
- private <T extends IFluidTank> int calculateMaxFluidParallels(List<T> hatches, Function<T, Boolean> isEmpty,
- BiFunction<T, FluidStack, Boolean> acceptsFluid) {
+ private int calculateMaxFluidParallels() {
+ List<? extends IFluidStore> hatches = machine.getFluidOutputSlots(fluidOutputs);
if (hatches.size() < fluidOutputs.length) {
return 0;
}
@@ -257,25 +185,23 @@ public class VoidProtectionHelper {
return maxParallel;
}
- for (T tHatch : hatches) {
+ for (IFluidStore tHatch : hatches) {
int tSpaceLeft = tHatch.getCapacity() - tHatch.getFluidAmount();
// check if hatch filled
if (tSpaceLeft <= 0) continue;
// check if hatch is empty and unrestricted
- if (isEmpty.apply(tHatch)) continue;
+ if (tHatch.isEmptyAndAcceptsAnyFluid()) continue;
for (Map.Entry<FluidStack, ParallelData> entry : tParallels.entrySet()) {
FluidStack tFluidOutput = entry.getKey();
- if (!acceptsFluid.apply(tHatch, tFluidOutput)) continue;
+ if (!tHatch.canStoreFluid(tFluidOutput)) continue;
// this fluid is not prevented by restrictions on output hatch
- if (tHatch.getFluidAmount() == 0 || GT_Utility.areFluidsEqual(tHatch.getFluid(), tFluidOutput)) {
- ParallelData tParallel = entry.getValue();
- Integer tCraftSize = tFluidOutputMap.get(tFluidOutput);
- tParallel.batch += (tParallel.partial + tSpaceLeft) / tCraftSize;
- tParallel.partial = (tParallel.partial + tSpaceLeft) % tCraftSize;
- }
+ ParallelData tParallel = entry.getValue();
+ Integer tCraftSize = tFluidOutputMap.get(tFluidOutput);
+ tParallel.batch += (tParallel.partial + tSpaceLeft) / tCraftSize;
+ tParallel.partial = (tParallel.partial + tSpaceLeft) % tCraftSize;
}
}
// now that all partial/restricted hatches have been counted, create a priority queue for our outputs
@@ -287,9 +213,9 @@ public class VoidProtectionHelper {
.add(new ParallelStackInfo<>(entry.getValue().batch, entry.getValue().partial, entry.getKey()));
}
// add extra parallels for open slots as well
- for (T tHatch : hatches) {
- // partially filled or restricted hatch. done in last pass
- if (!isEmpty.apply(tHatch)) continue;
+ for (IFluidStore tHatch : hatches) {
+ // partially filled or restricted hatch. done in the last pass
+ if (!tHatch.isEmptyAndAcceptsAnyFluid()) continue;
ParallelStackInfo<FluidStack> tParallel = aParallelQueue.poll();
assert tParallel != null; // will always be true, specifying assert here to avoid IDE/compiler warnings
@@ -305,44 +231,8 @@ public class VoidProtectionHelper {
/**
* Calculates the max parallels one can do with items if void protection is on
*/
- private int calculateMaxItemParallelsForMuTEs() {
- List<ItemStack> busStacks = new ArrayList<>();
- if (machineMulti != null) {
- IItemHandler inv = machineMulti.getOutputInventory();
- if (inv != null && inv.getSlots() > 0) {
- for (int i = 0; i < inv.getSlots(); i++) {
- busStacks.add(inv.getStackInSlot(i));
- }
- }
- }
- return calculateMaxItemParallels(busStacks);
- }
-
- /**
- * Calculates the max parallels one can do with items if void protection is on
- */
- private int calculateMaxItemParallelsForMetaTEs() {
- List<ItemStack> busStacks = new ArrayList<>();
- if (machineMeta != null) {
- for (final GT_MetaTileEntity_Hatch tBus : machineMeta.mOutputBusses) {
- if (!GT_MetaTileEntity_MultiBlockBase.isValidMetaTileEntity(tBus)) {
- continue;
- }
- final IInventory tBusInv = tBus.getBaseMetaTileEntity();
- for (int i = 0; i < tBusInv.getSizeInventory(); i++) {
- busStacks.add(tBus.getStackInSlot(i));
- }
- }
- }
- return calculateMaxItemParallels(busStacks);
- }
-
- /**
- * Calculates the max parallels one can do with items if void protection is on
- *
- * @param busStacks List of itemstacks that are already stored in buses
- */
- private int calculateMaxItemParallels(List<ItemStack> busStacks) {
+ private int calculateMaxItemParallels() {
+ List<ItemStack> busStacks = machine.getItemOutputSlots(itemOutputs);
// A map to hold the items we will be 'inputting' into the output buses. These itemstacks are actually the
// recipe outputs.
Map<ItemStack, Integer> tItemOutputMap = new ItemStackMap<>();