Skip to content

Commit

Permalink
fix: Correcting the ways pipes connect (#332)
Browse files Browse the repository at this point in the history
* Fixing the ways pipes connect

* Only flag update if needed
  • Loading branch information
supersimple33 authored Jun 30, 2024
1 parent 39d466d commit 305d77b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class PipeNetworkImpl extends SnapshotParticipant<PipeNetworkImpl.PipeSna
public PipeNetworkImpl(@NotNull ServerLevel level, long maxTransferRate, @NotNull BlockPos pos) {
this.level = level;
this.maxTransferRate = maxTransferRate;
this.tickId = level.getServer().getTickCount();
this.tickId = this.level.getServer().getTickCount();
this.addPipe(pos, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public long insert(long amount, @NotNull TransactionContext transaction) {
final long baseTransferred = this.transferred;

this.updateSnapshots(transaction);

requests.forEach((storage, requested) -> {
long insert = (long) (requested * ratio);
if (insert > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public void onPlace(BlockState blockState, Level level, BlockPos blockPos, Block
}
if (changed) {
wire.setChanged();
level.sendBlockUpdated(blockPos, blockState, blockState, Block.UPDATE_IMMEDIATE);
}
level.sendBlockUpdated(blockPos, blockState, blockState, Block.UPDATE_IMMEDIATE);
}
}

Expand All @@ -112,9 +112,9 @@ public void neighborChanged(BlockState blockState, Level level, BlockPos blockPo
if (direction != null) {
if (!level.isClientSide && wire.getConnections()[direction.ordinal()] != (wire.getConnections()[direction.ordinal()] = wire.canConnect(direction) && EnergyStorage.SIDED.find(level, neighborPos, direction.getOpposite()) != null)) {
level.sendBlockUpdated(blockPos, blockState, blockState, Block.UPDATE_IMMEDIATE);
wire.setChanged();
}
}
wire.setChanged();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import dev.galacticraft.mod.content.block.entity.networked.GlassFluidPipeBlockEntity;
import dev.galacticraft.mod.content.item.StandardWrenchItem;
import dev.galacticraft.mod.util.ConnectingBlockUtil;
import dev.galacticraft.mod.util.DirectionUtil;
import dev.galacticraft.mod.util.FluidUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -80,11 +81,16 @@ public void setPlacedBy(Level level, BlockPos blockPos, BlockState blockState, @
livingEntity.setItemInHand(interactionHand, copy);
}
}

// Regular Stuff
var changed = false;
for (var direction : Constant.Misc.DIRECTIONS) {
var otherBlockEntity = level.getBlockEntity(blockPos.relative(direction));
glassPipe.getConnections()[direction.ordinal()] = (otherBlockEntity instanceof Pipe pipe && pipe.canConnect(direction.getOpposite())) || FluidUtil.canAccessFluid(level, blockPos.relative(direction), direction);
changed |= glassPipe.getConnections()[direction.ordinal()] = glassPipe.canConnect(direction) && FluidUtil.canAccessFluid(level, blockPos.relative(direction), direction);
}
if (changed) {
glassPipe.setChanged();
level.sendBlockUpdated(blockPos, blockState, blockState, Block.UPDATE_IMMEDIATE);
}
level.updateNeighborsAt(blockPos, blockState.getBlock());
}
}

Expand Down Expand Up @@ -125,14 +131,20 @@ public InteractionResult use(BlockState blockState, Level level, BlockPos blockP
@Override
public void neighborChanged(BlockState blockState, Level level, BlockPos blockPos, Block block, BlockPos fromPos, boolean notify) {
super.neighborChanged(blockState, level, blockPos, block, fromPos, notify);
var neighborState = level.getBlockState(fromPos);
var delta = fromPos.subtract(blockPos);
var direction = Direction.fromDelta(delta.getX(), delta.getY(), delta.getZ());

if (direction != null && level.getBlockEntity(blockPos) instanceof GlassFluidPipeBlockEntity glassPipe) {
var otherBlockEntity = level.getBlockEntity(fromPos);
glassPipe.getConnections()[direction.ordinal()] = !neighborState.isAir() && ((otherBlockEntity instanceof Pipe pipe && pipe.canConnect(direction.getOpposite())) || FluidUtil.canAccessFluid(level, fromPos, direction));
level.sendBlockUpdated(blockPos, blockState, blockState, Block.UPDATE_IMMEDIATE);

if (level.getBlockEntity(blockPos) instanceof PipeBlockEntity glassPipe) {
var direction = DirectionUtil.fromNormal(fromPos.getX() - blockPos.getX(), fromPos.getY() - blockPos.getY(), fromPos.getZ() - blockPos.getZ());

if (direction != null) {
if (!level.isClientSide
&& glassPipe.getConnections()[direction.ordinal()]
!= (glassPipe.getConnections()[direction.ordinal()]
= glassPipe.canConnect(direction) && FluidUtil.canAccessFluid(level, fromPos, direction))
) {
level.sendBlockUpdated(blockPos, blockState, blockState, Block.UPDATE_IMMEDIATE);
glassPipe.setChanged();
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import dev.galacticraft.mod.api.pipe.Pipe;
import dev.galacticraft.mod.api.pipe.PipeNetwork;
import dev.galacticraft.mod.api.pipe.impl.PipeNetworkImpl;
import dev.galacticraft.mod.util.FluidUtil;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
Expand All @@ -36,11 +37,11 @@
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
import team.reborn.energy.api.EnergyStorage;

import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -103,7 +104,7 @@ public boolean[] getConnections() {

@Override
public void updateConnection(BlockState state, BlockPos pos, BlockPos neighborPos, Direction direction) {
boolean connected = this.canConnect(direction) && EnergyStorage.SIDED.find(this.level, neighborPos, direction.getOpposite()) != null;
boolean connected = this.canConnect(direction) && FluidUtil.canAccessFluid(this.level, neighborPos, direction);
if (this.connections[direction.get3DDataValue()] != connected) {
this.connections[direction.get3DDataValue()] = connected;
this.level.sendBlockUpdated(pos, state, state, 0);
Expand Down Expand Up @@ -138,7 +139,7 @@ public boolean supportsExtraction() {

@Override
public boolean supportsInsertion() {
return true;
return this.maxTransferRate > 0;
}

@Override
Expand All @@ -161,6 +162,10 @@ public void load(CompoundTag nbt) {
super.load(nbt);
this.readColorNbt(nbt);
this.readConnectionNbt(nbt);

if (this.level != null && this.level.isClientSide) {
this.level.sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), Block.UPDATE_IMMEDIATE);
}
}

@Override
Expand Down

0 comments on commit 305d77b

Please sign in to comment.