Skip to content

Commit

Permalink
Pipefixes (#80)
Browse files Browse the repository at this point in the history
* i forgor release()
  • Loading branch information
brachy84 authored Aug 19, 2021
1 parent aba97f0 commit c7e9f84
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 250 deletions.
23 changes: 13 additions & 10 deletions src/main/java/gregtech/api/pipenet/PipeNetWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public abstract class PipeNetWalker {
private final Set<IPipeTile<?, ?>> walked = new HashSet<>();
private final List<EnumFacing> pipes = new ArrayList<>();
private List<PipeNetWalker> walkers;
private BlockPos currentPos;
private final BlockPos.MutableBlockPos currentPos;
private int walkedBlocks;
private boolean invalid;

protected PipeNetWalker(PipeNet<?> net, World world, BlockPos sourcePipe, int walkedBlocks) {
this.world = Objects.requireNonNull(world);
this.net = Objects.requireNonNull(net);
this.walkedBlocks = walkedBlocks;
this.currentPos = Objects.requireNonNull(sourcePipe);
this.currentPos = new BlockPos.MutableBlockPos(Objects.requireNonNull(sourcePipe));
}

/**
Expand Down Expand Up @@ -100,12 +100,12 @@ public void traversePipeNet(int maxWalks) {

private boolean walk() {
if (walkers == null)
checkPos(currentPos);
checkPos();

if (pipes.size() == 0)
return true;
if (pipes.size() == 1) {
currentPos = currentPos.offset(pipes.get(0));
currentPos.move(pipes.get(0));
walkedBlocks++;
return false;
}
Expand All @@ -129,9 +129,9 @@ private boolean walk() {
return walkers.size() == 0;
}

private void checkPos(BlockPos pos) {
private void checkPos() {
pipes.clear();
TileEntity thisPipe = world.getTileEntity(pos);
TileEntity thisPipe = world.getTileEntity(currentPos);
IPipeTile<?, ?> pipeTile = (IPipeTile<?, ?>) thisPipe;
if (pipeTile == null) {
if (walkedBlocks == 1) {
Expand All @@ -141,28 +141,31 @@ private void checkPos(BlockPos pos) {
} else
throw new IllegalStateException("PipeTile was not null last walk, but now is");
}
checkPipe(pipeTile, pos);
checkPipe(pipeTile, currentPos);
pipeTile.markWalked();
walked.add(pipeTile);

BlockPos.PooledMutableBlockPos pos = BlockPos.PooledMutableBlockPos.retain();
// check for surrounding pipes and item handlers
for (EnumFacing accessSide : EnumFacing.VALUES) {
//skip sides reported as blocked by pipe network
if (!pipeTile.isConnectionOpenAny(accessSide))
continue;

TileEntity tile = world.getTileEntity(pos.offset(accessSide));
pos.setPos(currentPos).move(accessSide);
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof IPipeTile) {
IPipeTile<?, ?> otherPipe = (IPipeTile<?, ?>) tile;
if (otherPipe.isWalked())
continue;
if (isValidPipe(pipeTile, otherPipe, pos, accessSide)) {
if (isValidPipe(pipeTile, otherPipe, currentPos, accessSide)) {
pipes.add(accessSide);
continue;
}
}
checkNeighbour(pipeTile, pos, accessSide, tile);
checkNeighbour(pipeTile, currentPos, accessSide, tile);
}
pos.release();
}

public PipeNet<?> getNet() {
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/gregtech/api/util/TickingObjectHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package gregtech.api.util;

import net.minecraft.world.World;

import java.util.Optional;

public class TickingObjectHolder<T> {

private T object;
private long lastUpdateTime;
private final long defaultLifeTime;
private long lifeTime;

public TickingObjectHolder(T t, long lifeTime) {
this.object = t;
this.defaultLifeTime = lifeTime;
this.lifeTime = lifeTime;
}

private void checkState(World world) {
if(lifeTime <= 0) return;
long worldTime = world.getTotalWorldTime();
if(lastUpdateTime != worldTime) {
lifeTime -= (worldTime - lastUpdateTime);
if(lifeTime <= 0)
object = null;
lastUpdateTime = worldTime;
}
}

public Optional<T> get(World world) {
checkState(world);
return object == null ? Optional.empty() : Optional.of(object);
}

public T getNullable(World world) {
checkState(world);
return object;
}

public long getRemainingTime(World world) {
checkState(world);
return Math.max(0, lifeTime);
}

public void reset(T object, long lifeTime) {
this.object = object;
this.lifeTime = lifeTime;
}

public void reset(T object) {
this.object = object;
this.lifeTime = defaultLifeTime;
}
}
27 changes: 6 additions & 21 deletions src/main/java/gregtech/common/covers/CoverInfiniteWater.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,20 @@
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;

import java.util.Arrays;
import java.util.Optional;

import static gregtech.api.unification.material.Materials.Lava;
import static gregtech.api.unification.material.Materials.Water;

public class CoverInfiniteWater extends CoverBehavior implements ITickable {

private static final int SPEED = 4;
private long timer = 0L;
private final IFluidHandler fluidHandler;

public CoverInfiniteWater(ICoverable coverHolder, EnumFacing attachedSide) {
super(coverHolder, attachedSide);
fluidHandler = this.coverHolder.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null);
}

@Override
public boolean canAttach() {
return this.coverHolder.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null) != null;
return this.coverHolder.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, attachedSide) != null;
}

@Override
Expand All @@ -43,15 +33,10 @@ public void renderCover(CCRenderState ccRenderState, Matrix4 matrix4, IVertexOpe

@Override
public void update() {
if (timer++ % ((20 * SPEED) / (Math.pow(2, SPEED - 1))) == 0) {
FluidStack fluidStack = fluidHandler.drain(Water.getFluid(Integer.MAX_VALUE), false);
Arrays.stream(fluidHandler.getTankProperties())
.filter(tank -> Optional.ofNullable(tank.getContents())
.orElse(Lava.getFluid(1)).isFluidEqual(Water.getFluid(1)) && tank.getCapacity() > 64000)
.findFirst()
.ifPresent($ -> fluidHandler.fill(Water.getFluid(16000), true));
int amount = fluidStack != null ? Math.max(16000 - fluidStack.amount, 0) : 16000;
fluidHandler.fill(Water.getFluid(amount), true);
if (!coverHolder.getWorld().isRemote && coverHolder.getOffsetTimer() % 20 == 0) {
IFluidHandler fluidHandler = coverHolder.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, attachedSide);
if(fluidHandler != null)
fluidHandler.fill(new FluidStack(FluidRegistry.WATER, 16000), true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,6 @@ public TileEntityPipeBase<FluidPipeType, FluidPipeProperties> createNewTileEntit
return supportsTicking ? new TileEntityFluidPipeTickable() : new TileEntityFluidPipe();
}

@Override
protected void onActiveModeChange(World world, BlockPos pos, boolean isActiveNow, boolean isInitialChange) {
TileEntityFluidPipe oldTileEntity = (TileEntityFluidPipe) world.getTileEntity(pos);
if (!(oldTileEntity instanceof TileEntityFluidPipeTickable) && isActiveNow) {
TileEntityFluidPipeTickable newTileEntity = new TileEntityFluidPipeTickable();
newTileEntity.transferDataFrom(oldTileEntity);
newTileEntity.setActive(true);
world.setTileEntity(pos, newTileEntity);
} else if (oldTileEntity instanceof TileEntityFluidPipeTickable) {
((TileEntityFluidPipeTickable) oldTileEntity).setActive(isActiveNow);
}
}

@Nonnull
@SuppressWarnings("deprecation")
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import gregtech.api.unification.material.properties.FluidPipeProperties;
import gregtech.common.covers.*;
import gregtech.common.pipelike.fluidpipe.tile.TileEntityFluidPipe;
import gregtech.common.pipelike.fluidpipe.tile.TileEntityFluidPipeTickable;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
Expand All @@ -19,20 +18,18 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class FluidNetHandler implements IFluidHandler {

private final FluidPipeNet net;
private final TileEntityFluidPipeTickable pipe;
private final TileEntityFluidPipe pipe;
private final EnumFacing facing;
private int simulatedTransfers = 0;

public FluidNetHandler(FluidPipeNet net, TileEntityFluidPipe pipe, EnumFacing facing) {
this.net = net;
if (pipe instanceof TileEntityFluidPipeTickable)
this.pipe = (TileEntityFluidPipeTickable) pipe;
else
this.pipe = (TileEntityFluidPipeTickable) pipe.setSupportsTicking();
this.pipe = Objects.requireNonNull(pipe);
this.facing = facing;
}

Expand Down Expand Up @@ -163,7 +160,9 @@ private int insert(Handler handler, FluidStack stack, boolean doFill) {
}

private int insert(Handler handler, FluidStack stack, boolean doFill, int max) {
for (TileEntityFluidPipeTickable tickingPipe : handler.getTickingPipes())
if(stack == null || stack.amount <= 0 || max <= 0) return 0;

for (TileEntityFluidPipe tickingPipe : handler.getHoldingPipes())
if (!tickingPipe.findAndSetChannel(stack))
return 0;

Expand Down Expand Up @@ -193,8 +192,9 @@ private int insert(Handler handler, FluidStack stack, boolean doFill, int max) {
if (max >= stack.amount) {
int inserted = fluidHandler.fill(stack, doFill);
if (inserted > 0) {
stack.amount = inserted;
if (doFill)
for (TileEntityFluidPipeTickable tickingPipe : handler.getTickingPipes())
for (TileEntityFluidPipe tickingPipe : handler.getHoldingPipes())
tickingPipe.setContainingFluid(stack, tickingPipe.getCurrentChannel());
transfer(pipe, doFill, inserted);
}
Expand All @@ -204,8 +204,9 @@ private int insert(Handler handler, FluidStack stack, boolean doFill, int max) {
toInsert.amount = Math.min(max, stack.amount);
int inserted = fluidHandler.fill(toInsert, doFill);
if (inserted > 0) {
toInsert.amount = inserted;
if (doFill)
for (TileEntityFluidPipeTickable tickingPipe : handler.getTickingPipes())
for (TileEntityFluidPipe tickingPipe : handler.getHoldingPipes())
tickingPipe.setContainingFluid(toInsert, tickingPipe.getCurrentChannel());
transfer(pipe, doFill, inserted);
}
Expand Down Expand Up @@ -279,14 +280,14 @@ public CoverBehavior getCoverOnNeighbour(BlockPos pos, EnumFacing handlerFacing)
return null;
}

private int checkTransferable(TileEntityFluidPipeTickable pipe, int throughput, int amount, boolean doFill) {
private int checkTransferable(TileEntityFluidPipe pipe, int throughput, int amount, boolean doFill) {
if (doFill)
return Math.max(0, Math.min(throughput - pipe.getTransferredFluids(), amount));
else
return Math.max(0, Math.min(throughput - (pipe.getTransferredFluids() + simulatedTransfers), amount));
}

private void transfer(TileEntityFluidPipeTickable pipe, boolean doFill, int amount) {
private void transfer(TileEntityFluidPipe pipe, boolean doFill, int amount) {
if (doFill) {
pipe.transferFluid(amount);
} else
Expand All @@ -309,7 +310,7 @@ private static class Handler extends FluidPipeNet.Inventory {
private final IFluidHandler handler;

public Handler(IFluidHandler handler, FluidPipeNet.Inventory inv) {
super(inv.getPipePos(), inv.getFaceToHandler(), inv.getDistance(), inv.getObjectsInPath(), inv.getMinThroughput(), inv.getTickingPipes());
super(inv.getPipePos(), inv.getFaceToHandler(), inv.getDistance(), inv.getObjectsInPath(), inv.getMinThroughput(), inv.getHoldingPipes());
setLastTransferredFluid(inv.getLastTransferredFluid());
this.handler = handler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import gregtech.api.pipenet.tile.IPipeTile;
import gregtech.common.covers.CoverFluidFilter;
import gregtech.common.pipelike.fluidpipe.tile.TileEntityFluidPipe;
import gregtech.common.pipelike.fluidpipe.tile.TileEntityFluidPipeTickable;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
Expand All @@ -23,23 +22,26 @@ public class FluidNetWalker extends PipeNetWalker {

public static List<FluidPipeNet.Inventory> createNetData(FluidPipeNet net, World world, BlockPos sourcePipe) {
FluidNetWalker walker = new FluidNetWalker(net, world, sourcePipe, 1, new ArrayList<>(), new HashSet<>(), Integer.MAX_VALUE, new ArrayList<>());
TileEntity tile = world.getTileEntity(sourcePipe);
if(tile instanceof TileEntityFluidPipe)
walker.holdingPipes.add((TileEntityFluidPipe) tile);
walker.traversePipeNet();
return walker.inventories;
}

private final List<FluidPipeNet.Inventory> inventories;
private final Set<Object> pathObjects;
private final List<TileEntityFluidPipeTickable> tickingPipes;
private final List<TileEntityFluidPipe> holdingPipes;
private final Map<BlockPos, List<CoverFluidFilter>> covers = new HashMap<>();
private LinkedList<PosFace> checkedCovers = new LinkedList<>();
private int rate;

protected FluidNetWalker(PipeNet<?> net, World world, BlockPos sourcePipe, int walkedBlocks, List<FluidPipeNet.Inventory> inventories, Set<Object> pathObjects, int rate, List<TileEntityFluidPipeTickable> tickingPipes) {
protected FluidNetWalker(PipeNet<?> net, World world, BlockPos sourcePipe, int walkedBlocks, List<FluidPipeNet.Inventory> inventories, Set<Object> pathObjects, int rate, List<TileEntityFluidPipe> holdingPipes) {
super(net, world, sourcePipe, walkedBlocks);
this.inventories = inventories;
this.pathObjects = pathObjects;
this.rate = rate;
this.tickingPipes = tickingPipes;
this.holdingPipes = holdingPipes;
}

@Override
Expand All @@ -48,7 +50,7 @@ protected PipeNetWalker createSubWalker(PipeNet<?> net, World world, BlockPos ne
List<CoverFluidFilter> fluidFilter = covers.get(nextPos);
if(fluidFilter != null)
pathObjectsCopy.addAll(fluidFilter);
FluidNetWalker walker = new FluidNetWalker(net, world, nextPos, walkedBlocks, inventories, pathObjectsCopy, rate, new ArrayList<>(tickingPipes));
FluidNetWalker walker = new FluidNetWalker(net, world, nextPos, walkedBlocks, inventories, pathObjectsCopy, rate, new ArrayList<>(holdingPipes));
walker.checkedCovers = checkedCovers;
return walker;
}
Expand All @@ -67,7 +69,7 @@ protected void checkPipe(IPipeTile<?, ?> pipeTile, BlockPos pos) {
BlockPos offset = pos.offset(facing);
EnumFacing opposite = facing.getOpposite();
TileEntity tile = pipeTile.getPipeWorld().getTileEntity(offset);
if (tile instanceof IPipeTile && isValidPipe(pipeTile, (IPipeTile<?, ?>) tile, pos, facing)) {
if (tile instanceof TileEntityFluidPipe) {
if(!checkedCovers.contains(new PosFace(pos, facing))) {
CoverBehavior cover = pipeTile.getCoverableImplementation().getCoverAtSide(facing);
if(cover instanceof CoverFluidFilter)
Expand All @@ -86,7 +88,7 @@ protected void checkPipe(IPipeTile<?, ?> pipeTile, BlockPos pos) {
}
}
if (validPipes > 2) {
tickingPipes.add((TileEntityFluidPipeTickable) pipeTile.setSupportsTicking());
holdingPipes.add((TileEntityFluidPipe) pipeTile);
}
}

Expand All @@ -107,8 +109,9 @@ protected void checkNeighbour(IPipeTile<?, ?> pipeTile, BlockPos pipePos, EnumFa
List<CoverFluidFilter> fluidFilter = covers.get(pipePos.offset(faceToNeighbour));
if(fluidFilter != null)
pathObjectsCopy.addAll(fluidFilter);
inventories.add(new FluidPipeNet.Inventory(pipePos, faceToNeighbour, getWalkedBlocks(), pathObjectsCopy, rate, new ArrayList<>(tickingPipes)));
tickingPipes.add((TileEntityFluidPipeTickable) pipeTile.setSupportsTicking());
List<TileEntityFluidPipe> holders = new ArrayList<>(holdingPipes);
holders.add((TileEntityFluidPipe) pipeTile);
inventories.add(new FluidPipeNet.Inventory(new BlockPos(pipePos), faceToNeighbour, getWalkedBlocks(), pathObjectsCopy, rate, holders));
}
}

Expand Down
Loading

0 comments on commit c7e9f84

Please sign in to comment.