Skip to content

Commit

Permalink
Re-use direction and config suppliers throughout the various capabili…
Browse files Browse the repository at this point in the history
…ty helpers, and use singleton instances for can input and can output checks
  • Loading branch information
pupnewfster committed Aug 25, 2024
1 parent cc03d66 commit 7503eba
Show file tree
Hide file tree
Showing 60 changed files with 134 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TileEntityBioGenerator(BlockPos pos, BlockState state) {
@NotNull
@Override
protected IFluidTankHolder getInitialFluidTanks(IContentsListener listener) {
FluidTankHelper builder = FluidTankHelper.forSide(this::getDirection);
FluidTankHelper builder = FluidTankHelper.forSide(facingSupplier);
builder.addTank(bioFuelTank = VariableCapacityFluidTank.input(MekanismGeneratorsConfig.generators.bioTankCapacity,
fluidStack -> fluidStack.is(GeneratorTags.Fluids.BIOETHANOL), listener), RelativeSide.LEFT, RelativeSide.RIGHT,
RelativeSide.BACK, RelativeSide.TOP, RelativeSide.BOTTOM);
Expand All @@ -58,7 +58,7 @@ protected IFluidTankHolder getInitialFluidTanks(IContentsListener listener) {
@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
builder.addSlot(fuelSlot = FluidFuelInventorySlot.forFuel(bioFuelTank, stack -> stack.is(MekanismTags.Items.FUELS_BIO) ? 200 : stack.is(MekanismTags.Items.FUELS_BLOCK_BIO) ? 200 * 9 : 0,
GeneratorsFluids.BIOETHANOL::getFluidStack, listener, 17, 35), RelativeSide.FRONT, RelativeSide.LEFT, RelativeSide.BACK, RelativeSide.TOP,
RelativeSide.BOTTOM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public TileEntityGasGenerator(BlockPos pos, BlockState state) {
@NotNull
@Override
public IChemicalTankHolder getInitialChemicalTanks(IContentsListener listener) {
ChemicalTankHelper builder = ChemicalTankHelper.forSide(this::getDirection);
ChemicalTankHelper builder = ChemicalTankHelper.forSide(facingSupplier);
builder.addTank(fuelTank = new FuelTank(listener), RelativeSide.LEFT, RelativeSide.RIGHT, RelativeSide.BACK, RelativeSide.TOP, RelativeSide.BOTTOM);
return builder.build();
}

@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
builder.addSlot(fuelSlot = ChemicalInventorySlot.fill(fuelTank, listener, 17, 35), RelativeSide.FRONT, RelativeSide.LEFT, RelativeSide.BACK, RelativeSide.TOP,
RelativeSide.BOTTOM);
builder.addSlot(energySlot = EnergyInventorySlot.drain(getEnergyContainer(), listener, 143, 35), RelativeSide.RIGHT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected RelativeSide[] getEnergySides() {
@NotNull
@Override
protected IEnergyContainerHolder getInitialEnergyContainers(IContentsListener listener) {
EnergyContainerHelper builder = EnergyContainerHelper.forSide(this::getDirection);
EnergyContainerHelper builder = EnergyContainerHelper.forSide(facingSupplier);
builder.addContainer(energyContainer = BasicEnergyContainer.output(MachineEnergyContainer.validateBlock(this).getStorage(), listener), getEnergySides());
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public TileEntityHeatGenerator(BlockPos pos, BlockState state) {
@NotNull
@Override
protected IFluidTankHolder getInitialFluidTanks(IContentsListener listener) {
FluidTankHelper builder = FluidTankHelper.forSide(this::getDirection);
FluidTankHelper builder = FluidTankHelper.forSide(facingSupplier);
builder.addTank(lavaTank = VariableCapacityFluidTank.input(MekanismGeneratorsConfig.generators.heatTankCapacity,
fluidStack -> fluidStack.is(FluidTags.LAVA), listener), RelativeSide.LEFT, RelativeSide.RIGHT, RelativeSide.BACK,
RelativeSide.TOP, RelativeSide.BOTTOM);
Expand All @@ -92,7 +92,7 @@ protected IFluidTankHolder getInitialFluidTanks(IContentsListener listener) {
@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
//Divide the burn time by 20 as that is the ratio of how much a bucket of lava would burn for
//TODO: Eventually we may want to grab the 20 dynamically in case some mod is changing the burn time of a lava bucket
builder.addSlot(fuelSlot = FluidFuelInventorySlot.forFuel(lavaTank, stack -> stack.getBurnTime(null) / 20, size -> new FluidStack(Fluids.LAVA, size),
Expand All @@ -104,7 +104,7 @@ protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
@NotNull
@Override
protected IHeatCapacitorHolder getInitialHeatCapacitors(IContentsListener listener, CachedAmbientTemperature ambientTemperature) {
HeatCapacitorHelper builder = HeatCapacitorHelper.forSide(this::getDirection);
HeatCapacitorHelper builder = HeatCapacitorHelper.forSide(facingSupplier);
builder.addCapacitor(heatCapacitor = BasicHeatCapacitor.create(HEAT_CAPACITY, INVERSE_CONDUCTION_COEFFICIENT, INVERSE_INSULATION_COEFFICIENT, ambientTemperature, listener));
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected TileEntitySolarGenerator(IBlockProvider blockProvider, BlockPos pos, B
@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
builder.addSlot(energySlot = EnergyInventorySlot.drain(getEnergyContainer(), listener, 143, 35));
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public TileEntityWindGenerator(BlockPos pos, BlockState state) {
@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
builder.addSlot(energySlot = EnergyInventorySlot.drain(getEnergyContainer(), listener, 143, 35));
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

public abstract class ConfigHolder<TYPE> implements IHolder {

private static final Predicate<ISlotInfo> CAN_INPUT = ISlotInfo::canInput;
private static final Predicate<ISlotInfo> CAN_OUTPUT = ISlotInfo::canOutput;

/**
* Dummy ISlotInfo used for representing we have no config
*/
Expand Down Expand Up @@ -71,12 +74,12 @@ protected ConfigHolder(Supplier<Direction> facingSupplier, Supplier<TileComponen

@Override
public boolean canInsert(@Nullable Direction side) {
return canInteract(side, ISlotInfo::canInput);
return canInteract(side, CAN_INPUT);
}

@Override
public boolean canExtract(@Nullable Direction side) {
return canInteract(side, ISlotInfo::canOutput);
return canInteract(side, CAN_OUTPUT);
}

private boolean canInteract(@Nullable Direction side, @NotNull Predicate<ISlotInfo> interactPredicate) {
Expand Down Expand Up @@ -114,7 +117,8 @@ private ISlotInfo getSlotInfo(Direction side) {
//Invalid entire cache and update what direction we had as last if our last direction doesn't match the one we currently are facing
cachedSlotInfo.clear();
lastDirection = direction;
} else if (cachedSlotInfo.containsKey(side)) {
}
if (cachedSlotInfo.containsKey(side)) {
return cachedSlotInfo.get(side);
}
ISlotInfo slotInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public abstract class QuantumEntangloporterConfigHolder<TYPE> extends ConfigHold
protected final TileEntityQuantumEntangloporter entangloporter;

protected QuantumEntangloporterConfigHolder(TileEntityQuantumEntangloporter entangloporter) {
super(entangloporter::getDirection, entangloporter::getConfig);
super(entangloporter.facingSupplier, entangloporter.configSupplier);
this.entangloporter = entangloporter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public ProxyFluidHandler(ISidedFluidHandler fluidHandler, @Nullable Direction si
this.fluidHandler = fluidHandler;
}

public ISidedFluidHandler getInternalHandler() {
return fluidHandler;
}

@Override
public int getTanks() {
return fluidHandler.getTanks(side);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mekanism/common/tile/TileEntityBin.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected void presetVariables() {
@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
builder.addSlot(binSlot = BinInventorySlot.create(listener, tier));
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public TileEntityChargepad(BlockPos pos, BlockState state) {
@NotNull
@Override
protected IEnergyContainerHolder getInitialEnergyContainers(IContentsListener listener) {
EnergyContainerHelper builder = EnergyContainerHelper.forSide(this::getDirection);
EnergyContainerHelper builder = EnergyContainerHelper.forSide(facingSupplier);
builder.addContainer(energyContainer = MachineEnergyContainer.input(this, listener), RelativeSide.BACK, RelativeSide.BOTTOM);
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ protected void presetVariables() {

@Override
public @Nullable IChemicalTankHolder getInitialChemicalTanks(IContentsListener listener) {
ChemicalTankHelper builder = ChemicalTankHelper.forSideWithConfig(this::getDirection, this::getConfig);
ChemicalTankHelper builder = ChemicalTankHelper.forSideWithConfig(facingSupplier, configSupplier);
builder.addTank(chemicalTank = ChemicalTankChemicalTank.create(tier, listener));
return builder.build();
}

@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSideWithConfig(this::getDirection, this::getConfig);
InventorySlotHelper builder = InventorySlotHelper.forSideWithConfig(facingSupplier, configSupplier);
builder.addSlot(drainSlot = ChemicalInventorySlot.drain(chemicalTank, listener, 16, 16));
builder.addSlot(fillSlot = ChemicalInventorySlot.fill(chemicalTank, listener, 16, 48));
drainSlot.setSlotType(ContainerSlotType.OUTPUT);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mekanism/common/tile/TileEntityEnergyCube.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ protected void presetVariables() {
@NotNull
@Override
protected IEnergyContainerHolder getInitialEnergyContainers(IContentsListener listener) {
EnergyContainerHelper builder = EnergyContainerHelper.forSideWithConfig(this::getDirection, this::getConfig);
EnergyContainerHelper builder = EnergyContainerHelper.forSideWithConfig(facingSupplier, configSupplier);
builder.addContainer(energyContainer = EnergyCubeEnergyContainer.create(tier, listener));
return builder.build();
}

@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSideWithConfig(this::getDirection, this::getConfig);
InventorySlotHelper builder = InventorySlotHelper.forSideWithConfig(facingSupplier, configSupplier);
builder.addSlot(dischargeSlot = EnergyInventorySlot.fillOrConvert(energyContainer, this::getLevel, listener, 17, 35));
builder.addSlot(chargeSlot = EnergyInventorySlot.drain(energyContainer, listener, 143, 35));
dischargeSlot.setSlotOverlay(SlotOverlay.MINUS);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mekanism/common/tile/TileEntityFluidTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ protected void presetVariables() {
@NotNull
@Override
protected IFluidTankHolder getInitialFluidTanks(IContentsListener listener) {
FluidTankHelper builder = FluidTankHelper.forSide(this::getDirection);
FluidTankHelper builder = FluidTankHelper.forSide(facingSupplier);
builder.addTank(fluidTank = FluidTankFluidTank.create(this, listener));
return builder.build();
}

@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
builder.addSlot(inputSlot = FluidInventorySlot.input(fluidTank, listener, 146, 19));
builder.addSlot(outputSlot = OutputInventorySlot.at(listener, 146, 51));
inputSlot.setSlotOverlay(SlotOverlay.INPUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public TileEntityLogisticalSorter(BlockPos pos, BlockState state) {
@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
//TODO - 1.20.4: Re-evaluate the internal inventory slot and why do we even have a slot on the sorter
builder.addSlot(InternalInventorySlot.create(listener), RelativeSide.FRONT);
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public TileEntityModificationStation(BlockPos pos, BlockState state) {
@NotNull
@Override
protected IEnergyContainerHolder getInitialEnergyContainers(IContentsListener listener) {
EnergyContainerHelper builder = EnergyContainerHelper.forSide(this::getDirection);
EnergyContainerHelper builder = EnergyContainerHelper.forSide(facingSupplier);
builder.addContainer(energyContainer = MachineEnergyContainer.input(this, listener), RelativeSide.BACK);
return builder.build();
}
Expand All @@ -70,7 +70,7 @@ public MachineEnergyContainer<TileEntityModificationStation> getEnergyContainer(
@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
builder.addSlot(moduleSlot = InputInventorySlot.at(stack -> stack.getItem() instanceof IModuleItem, listener, 35, 118));
builder.addSlot(containerSlot = InputInventorySlot.at(IModuleHelper.INSTANCE::isModuleContainer, listener, 125, 118));
moduleSlot.setSlotType(ContainerSlotType.NORMAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected TileEntityPersonalStorage(IBlockProvider blockProvider, BlockPos pos,
@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
//Note: We always allow manual interaction (even for insertion), as if a player has the GUI open we treat that as they are allowed to interact with it
// and if the security mode changes we then boot any players who can't interact with it anymore out of the GUI
//Note: We can just directly pass ourselves as a security object as we know we are present and that we aren't just an owner item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public TileEntityRadioactiveWasteBarrel(BlockPos pos, BlockState state) {
@NotNull
@Override
public IChemicalTankHolder getInitialChemicalTanks(IContentsListener listener) {
ChemicalTankHelper builder = ChemicalTankHelper.forSide(this::getDirection);
ChemicalTankHelper builder = ChemicalTankHelper.forSide(facingSupplier);
builder.addTank(chemicalTank = StackedWasteBarrel.create(this, listener), RelativeSide.TOP, RelativeSide.BOTTOM);
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public TileEntitySecurityDesk(BlockPos pos, BlockState state) {
@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
builder.addSlot(unlockSlot = SecurityInventorySlot.unlock(this::getOwnerUUID, listener, 146, 18));
builder.addSlot(lockSlot = SecurityInventorySlot.lock(listener, 146, 97));
return builder.build();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mekanism/common/tile/TileEntityTeleporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ public TileEntityTeleporter(BlockPos pos, BlockState state) {
@NotNull
@Override
protected IEnergyContainerHolder getInitialEnergyContainers(IContentsListener listener) {
EnergyContainerHelper builder = EnergyContainerHelper.forSide(this::getDirection);
EnergyContainerHelper builder = EnergyContainerHelper.forSide(facingSupplier);
builder.addContainer(energyContainer = MachineEnergyContainer.input(this, listener));
return builder.build();
}

@NotNull
@Override
protected IInventorySlotHolder getInitialInventory(IContentsListener listener) {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
InventorySlotHelper builder = InventorySlotHelper.forSide(facingSupplier);
builder.addSlot(energySlot = EnergyInventorySlot.fillOrConvert(energyContainer, this::getLevel, listener, 153, 7));
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ public abstract class TileEntityMekanism extends CapabilityTileEntity implements
//Methods for implementing ITileDirectional
@Nullable
private Direction cachedDirection;

//TODO: Re-evaluate if we should have this be null when we are not a directional tile?
public final Supplier<Direction> facingSupplier = this::getDirection;
//End variables ITileRedstone

//Variables for handling ITileRedstone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.neoforged.neoforge.capabilities.BlockCapabilityCache;
import net.neoforged.neoforge.common.util.Lazy;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.items.IItemHandler;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -140,19 +139,18 @@ public boolean isEjecting(ConfigInfo info, TransmissionType type) {
}

public void tickServer() {
Lazy<Direction> facing = Lazy.of(tile::getDirection);
for (Map.Entry<TransmissionType, ConfigInfo> entry : configInfo.entrySet()) {
TransmissionType type = entry.getKey();
ConfigInfo info = entry.getValue();
if (isEjecting(info, type)) {
if (type == TransmissionType.ITEM) {
if (tickDelay == 0) {
outputItems(facing.get(), info);
outputItems(tile.facingSupplier.get(), info);
} else {
tickDelay--;
}
} else if (type != TransmissionType.HEAT) {
eject(type, facing.get(), info);
eject(type, tile.facingSupplier.get(), info);
}
}
}
Expand Down
Loading

0 comments on commit 7503eba

Please sign in to comment.