mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 14:52:58 +00:00
Reworked PipeUtils and GasUtils side access (#5529)
These two methods were ghost loading chunks causing issues. Rather than simply adding a is loaded check I reworked the flow to be more reusable and clearer in general. The extra code can be used in other places to improve the code base.
This commit is contained in:
1 parent
f00fe44124
commit
5d17d706c0
4 files changed
+85
-44
No files matched your search
@@ -21,6 +21,22 @@ public final class CapabilityUtils {
|
||||
return provider.getCapability(cap, side);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs an action on a capability if it exists
|
||||
*
|
||||
* @param provider - provider hosting the capability
|
||||
* @param cap - capability type
|
||||
* @param side - side to access
|
||||
* @param action - handler for completing the action on the capability
|
||||
* @param <T> - type to return
|
||||
*/
|
||||
public static <T> void runIfCap(ICapabilityProvider provider, Capability<T> cap, EnumFacing side, Consumer<T> action) {
|
||||
final T capability = getCapability(provider, cap, side);
|
||||
if (capability != null) {
|
||||
action.accept(capability);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> OptionalCapability withCapability(ICapabilityProvider provider, Capability<T> cap, EnumFacing side, Consumer<T> consumer) {
|
||||
return new OptionalCapability(provider).orElseWith(cap, side, consumer);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
package mekanism.common.util;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import mekanism.common.base.SplitInfo;
|
||||
import mekanism.common.base.SplitInfo.DoubleSplitInfo;
|
||||
import mekanism.common.base.SplitInfo.IntegerSplitInfo;
|
||||
import mekanism.common.base.target.EnergyAcceptorTarget;
|
||||
import mekanism.common.base.target.Target;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class EmitUtils {
|
||||
|
||||
@@ -70,4 +75,29 @@ public class EmitUtils {
|
||||
public static double sendToAcceptors(Set<EnergyAcceptorTarget> availableTargets, int totalTargets, double amountToSplit) {
|
||||
return sendToAcceptors(availableTargets, totalTargets, new DoubleSplitInfo(amountToSplit, totalTargets), amountToSplit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple helper to loop over each side of the block and complete an action for each tile found
|
||||
*
|
||||
* @param world - world to access
|
||||
* @param center - location to center search on
|
||||
* @param sides - sides to search
|
||||
* @param action - action to complete
|
||||
*/
|
||||
public static void forEachSide(World world, BlockPos center, Iterable<EnumFacing> sides, BiConsumer<TileEntity, EnumFacing> action) {
|
||||
if (sides != null) {
|
||||
//Loop provided sides
|
||||
for (EnumFacing side : sides) {
|
||||
//Validate we have a block loaded in world, prevents ghost chunk loading
|
||||
final BlockPos pos = center.offset(side);
|
||||
if (world.isBlockLoaded(pos)) {
|
||||
//Get tile and provide if not null
|
||||
final TileEntity tileEntity = world.getTileEntity(pos);
|
||||
if (tileEntity != null) {
|
||||
action.accept(tileEntity, side);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,13 +27,10 @@ import net.minecraft.world.World;
|
||||
public final class GasUtils {
|
||||
|
||||
public static IGasHandler[] getConnectedAcceptors(BlockPos pos, World world, Set<EnumFacing> sides) {
|
||||
IGasHandler[] acceptors = new IGasHandler[]{null, null, null, null, null, null};
|
||||
for (EnumFacing orientation : sides) {
|
||||
TileEntity acceptor = world.getTileEntity(pos.offset(orientation));
|
||||
if (CapabilityUtils.hasCapability(acceptor, Capabilities.GAS_HANDLER_CAPABILITY, orientation.getOpposite())) {
|
||||
acceptors[orientation.ordinal()] = CapabilityUtils.getCapability(acceptor, Capabilities.GAS_HANDLER_CAPABILITY, orientation.getOpposite());
|
||||
}
|
||||
}
|
||||
final IGasHandler[] acceptors = new IGasHandler[]{null, null, null, null, null, null};
|
||||
EmitUtils.forEachSide(world, pos, sides, (tile, side) -> {
|
||||
acceptors[side.ordinal()] = CapabilityUtils.getCapability(tile, Capabilities.GAS_HANDLER_CAPABILITY, side.getOpposite());
|
||||
});
|
||||
return acceptors;
|
||||
}
|
||||
|
||||
@@ -113,20 +110,21 @@ public final class GasUtils {
|
||||
|
||||
//Fake that we have one target given we know that no sides will overlap
|
||||
// This allows us to have slightly better performance
|
||||
GasHandlerTarget target = new GasHandlerTarget(stack);
|
||||
for (EnumFacing orientation : sides) {
|
||||
TileEntity acceptor = from.getWorld().getTileEntity(from.getPos().offset(orientation));
|
||||
if (acceptor == null) {
|
||||
continue;
|
||||
}
|
||||
EnumFacing opposite = orientation.getOpposite();
|
||||
if (CapabilityUtils.hasCapability(acceptor, Capabilities.GAS_HANDLER_CAPABILITY, opposite)) {
|
||||
IGasHandler handler = CapabilityUtils.getCapability(acceptor, Capabilities.GAS_HANDLER_CAPABILITY, opposite);
|
||||
if (handler != null && handler.canReceiveGas(opposite, stack.getGas())) {
|
||||
target.addHandler(opposite, handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
final GasHandlerTarget target = new GasHandlerTarget(stack);
|
||||
EmitUtils.forEachSide(from.getWorld(), from.getPos(), sides, (acceptor, side) -> {
|
||||
|
||||
//Invert to get access side
|
||||
final EnumFacing accessSide = side.getOpposite();
|
||||
|
||||
//Collect cap
|
||||
CapabilityUtils.runIfCap(acceptor, Capabilities.GAS_HANDLER_CAPABILITY, accessSide,
|
||||
(handler) -> {
|
||||
if (handler.canReceiveGas(accessSide, stack.getGas())) {
|
||||
target.addHandler(accessSide, handler);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
int curHandlers = target.getHandlers().size();
|
||||
if (curHandlers > 0) {
|
||||
Set<GasHandlerTarget> targets = new HashSet<>();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package mekanism.common.util;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import mekanism.common.base.target.FluidHandlerTarget;
|
||||
@@ -46,15 +47,10 @@ public final class PipeUtils {
|
||||
* @return array of IFluidHandlers
|
||||
*/
|
||||
public static IFluidHandler[] getConnectedAcceptors(BlockPos pos, World world) {
|
||||
IFluidHandler[] acceptors = new IFluidHandler[]{null, null, null, null, null, null};
|
||||
|
||||
for (EnumFacing orientation : EnumFacing.VALUES) {
|
||||
TileEntity acceptor = world.getTileEntity(pos.offset(orientation));
|
||||
if (CapabilityUtils.hasCapability(acceptor, CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, orientation.getOpposite())) {
|
||||
IFluidHandler handler = CapabilityUtils.getCapability(acceptor, CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, orientation.getOpposite());
|
||||
acceptors[orientation.ordinal()] = handler;
|
||||
}
|
||||
}
|
||||
final IFluidHandler[] acceptors = new IFluidHandler[]{null, null, null, null, null, null};
|
||||
EmitUtils.forEachSide(world, pos, EnumSet.allOf(EnumFacing.class), (tile, side) -> {
|
||||
acceptors[side.ordinal()] = CapabilityUtils.getCapability(tile, CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite());
|
||||
});
|
||||
return acceptors;
|
||||
}
|
||||
|
||||
@@ -73,20 +69,21 @@ public final class PipeUtils {
|
||||
}
|
||||
//Fake that we have one target given we know that no sides will overlap
|
||||
// This allows us to have slightly better performance
|
||||
FluidHandlerTarget target = new FluidHandlerTarget(stack);
|
||||
for (EnumFacing orientation : sides) {
|
||||
TileEntity acceptor = from.getWorld().getTileEntity(from.getPos().offset(orientation));
|
||||
if (acceptor == null) {
|
||||
continue;
|
||||
}
|
||||
EnumFacing opposite = orientation.getOpposite();
|
||||
if (CapabilityUtils.hasCapability(acceptor, CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opposite)) {
|
||||
IFluidHandler handler = CapabilityUtils.getCapability(acceptor, CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, opposite);
|
||||
if (handler != null && canFill(handler, stack)) {
|
||||
target.addHandler(opposite, handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
final FluidHandlerTarget target = new FluidHandlerTarget(stack);
|
||||
EmitUtils.forEachSide(from.getWorld(), from.getPos(), sides, (acceptor, side) -> {
|
||||
|
||||
//Insert to access side
|
||||
final EnumFacing accessSide = side.getOpposite();
|
||||
|
||||
//Collect cap
|
||||
CapabilityUtils.runIfCap(acceptor, CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, accessSide,
|
||||
(handler) -> {
|
||||
if (canFill(handler, stack)) {
|
||||
target.addHandler(accessSide, handler);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
int curHandlers = target.getHandlers().size();
|
||||
if (curHandlers > 0) {
|
||||
Set<FluidHandlerTarget> targets = new HashSet<>();
|
||||
|
||||
Reference in new issue
Block a user