Skip to content

Commit

Permalink
Reduce capturing lambdas and cleanup logic for chemical tank wrappers…
Browse files Browse the repository at this point in the history
… to make profiling data cleaner
  • Loading branch information
pupnewfster committed Apr 2, 2024
1 parent eb36b37 commit 381c303
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/api/java/mekanism/api/MekanismAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private MekanismAPI() {
/**
* The version of the api classes - may not always match the mod's version
*/
public static final String API_VERSION = "10.5.13";
public static final String API_VERSION = "10.5.14";
/**
* Mekanism's Mod ID
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mekanism.api.chemical.merged;

import java.util.List;
import java.util.function.BooleanSupplier;
import mekanism.api.Action;
import mekanism.api.AutomationType;
Expand All @@ -9,6 +10,7 @@
import mekanism.api.chemical.IChemicalTank;
import mekanism.api.chemical.attribute.ChemicalAttributeValidator;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.Nullable;

/**
* Helper class for wrapping a chemical tank for use in a multi chemical type. Disallowing interacting with various tanks if other tanks have contents. For example only
Expand All @@ -18,13 +20,22 @@
public abstract class ChemicalTankWrapper<CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>> implements IChemicalTank<CHEMICAL, STACK> {

private final IChemicalTank<CHEMICAL, STACK> internal;
private final List<IChemicalTank<?, ?>> otherTanks;
@Nullable
private final BooleanSupplier insertCheck;
private final MergedChemicalTank mergedTank;

@Deprecated(forRemoval = true, since = "10.5.14")
protected ChemicalTankWrapper(MergedChemicalTank mergedTank, IChemicalTank<CHEMICAL, STACK> internal, BooleanSupplier insertCheck) {
this(mergedTank, internal, List.of(), insertCheck);
}

protected ChemicalTankWrapper(MergedChemicalTank mergedTank, IChemicalTank<CHEMICAL, STACK> internal, List<IChemicalTank<?, ?>> otherTanks,
@Nullable BooleanSupplier insertCheck) {
//TODO: Do we want to short circuit it so that if we are not empty it allows for inserting before checking the insertCheck
this.mergedTank = mergedTank;
this.internal = internal;
this.otherTanks = otherTanks;
this.insertCheck = insertCheck;
}

Expand All @@ -50,10 +61,22 @@ public void setStackUnchecked(STACK stack) {
internal.setStackUnchecked(stack);
}

private boolean canInsert() {
if (insertCheck == null || insertCheck.getAsBoolean()) {
for (IChemicalTank<?, ?> otherTank : otherTanks) {
if (!otherTank.isEmpty()) {
return false;
}
}
return true;
}
return false;
}

@Override
public STACK insert(STACK stack, Action action, AutomationType automationType) {
//Only allow inserting if we pass the check
return insertCheck.getAsBoolean() ? internal.insert(stack, action, automationType) : stack;
return canInsert() ? internal.insert(stack, action, automationType) : stack;
}

@Override
Expand Down
46 changes: 36 additions & 10 deletions src/api/java/mekanism/api/chemical/merged/MergedChemicalTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ protected MergedChemicalTank(@Nullable BooleanSupplier extraCheck, IChemicalTank
if (type.canHandle(tank)) {
//TODO: Improve this so it doesn't have to loop nearly as much?
List<IChemicalTank<?, ?>> otherTanks = Arrays.stream(allTanks).filter(otherTank -> tank != otherTank).toList();
BooleanSupplier insertionCheck;
if (extraCheck == null) {
insertionCheck = () -> otherTanks.stream().allMatch(IChemicalTank::isEmpty);
} else {
insertionCheck = () -> extraCheck.getAsBoolean() && otherTanks.stream().allMatch(IChemicalTank::isEmpty);
}
tankMap.put(type, type.createWrapper(this, tank, insertionCheck));
tankMap.put(type, type.createWrapper(this, tank, otherTanks, extraCheck));
handled = true;
break;
}
Expand Down Expand Up @@ -177,7 +171,7 @@ public enum Current {
@FunctionalInterface
private interface IWrapperCreator<CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>, TANK extends IChemicalTank<CHEMICAL, STACK>> {

TANK create(MergedChemicalTank mergedTank, TANK tank, BooleanSupplier insertCheck);
TANK create(MergedChemicalTank mergedTank, TANK tank, List<IChemicalTank<?, ?>> otherTanks, @Nullable BooleanSupplier insertCheck);
}

private record ChemicalTankType<CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>, TANK extends IChemicalTank<CHEMICAL, STACK>>(
Expand All @@ -200,9 +194,21 @@ private boolean canHandle(IChemicalTank<?, ?> tank) {

/**
* It is assumed that {@link #canHandle(IChemicalTank)} is called before this method
*
* @deprecated Use {@link #createWrapper(MergedChemicalTank, IChemicalTank, List, BooleanSupplier)}
*/
@Deprecated(forRemoval = true, since = "10.5.14")
public TANK createWrapper(MergedChemicalTank mergedTank, IChemicalTank<?, ?> tank, BooleanSupplier insertCheck) {
return tankWrapper.create(mergedTank, (TANK) tank, insertCheck);
return createWrapper(mergedTank, tank, List.of(), insertCheck);
}

/**
* It is assumed that {@link #canHandle(IChemicalTank)} is called before this method
*
* @since 10.5.14
*/
public TANK createWrapper(MergedChemicalTank mergedTank, IChemicalTank<?, ?> tank, List<IChemicalTank<?, ?>> otherTanks, @Nullable BooleanSupplier insertCheck) {
return tankWrapper.create(mergedTank, (TANK) tank, otherTanks, insertCheck);
}

@Override
Expand All @@ -213,29 +219,49 @@ public String toString() {

private static class GasTankWrapper extends ChemicalTankWrapper<Gas, GasStack> implements IGasTank {

@Deprecated(forRemoval = true, since = "10.5.14")
public GasTankWrapper(MergedChemicalTank mergedTank, IGasTank internal, BooleanSupplier insertCheck) {
super(mergedTank, internal, insertCheck);
this(mergedTank, internal, List.of(), insertCheck);
}

public GasTankWrapper(MergedChemicalTank mergedTank, IGasTank internal, List<IChemicalTank<?, ?>> otherTanks, @Nullable BooleanSupplier insertCheck) {
super(mergedTank, internal, otherTanks, insertCheck);
}
}

private static class InfusionTankWrapper extends ChemicalTankWrapper<InfuseType, InfusionStack> implements IInfusionTank {

@Deprecated(forRemoval = true, since = "10.5.14")
public InfusionTankWrapper(MergedChemicalTank mergedTank, IInfusionTank internal, BooleanSupplier insertCheck) {
super(mergedTank, internal, insertCheck);
}

public InfusionTankWrapper(MergedChemicalTank mergedTank, IInfusionTank internal, List<IChemicalTank<?, ?>> otherTanks, @Nullable BooleanSupplier insertCheck) {
super(mergedTank, internal, otherTanks, insertCheck);
}
}

private static class PigmentTankWrapper extends ChemicalTankWrapper<Pigment, PigmentStack> implements IPigmentTank {

@Deprecated(forRemoval = true, since = "10.5.14")
public PigmentTankWrapper(MergedChemicalTank mergedTank, IPigmentTank internal, BooleanSupplier insertCheck) {
super(mergedTank, internal, insertCheck);
}

public PigmentTankWrapper(MergedChemicalTank mergedTank, IPigmentTank internal, List<IChemicalTank<?, ?>> otherTanks, @Nullable BooleanSupplier insertCheck) {
super(mergedTank, internal, otherTanks, insertCheck);
}
}

private static class SlurryTankWrapper extends ChemicalTankWrapper<Slurry, SlurryStack> implements ISlurryTank {

@Deprecated(forRemoval = true, since = "10.5.14")
public SlurryTankWrapper(MergedChemicalTank mergedTank, ISlurryTank internal, BooleanSupplier insertCheck) {
super(mergedTank, internal, insertCheck);
}

public SlurryTankWrapper(MergedChemicalTank mergedTank, ISlurryTank internal, List<IChemicalTank<?, ?>> otherTanks, @Nullable BooleanSupplier insertCheck) {
super(mergedTank, internal, otherTanks, insertCheck);
}
}
}

0 comments on commit 381c303

Please sign in to comment.