Cables with different transfer rate shouldn't connect. Closes #2572

This commit is contained in:
drcrazy 2021-10-22 09:11:45 +03:00
parent 1420956167
commit 36203239e1
2 changed files with 54 additions and 59 deletions

View file

@ -145,6 +145,57 @@ public class CableBlockEntity extends BlockEntity
energyContainer.amount = energy;
}
void appendTargets(List<OfferedEnergyStorage> targetStorages) {
ServerWorld serverWorld = (ServerWorld) world;
if (serverWorld == null) { return; }
// Update our targets if necessary.
if (targets == null) {
BlockState newBlockState = getCachedState();
targets = new ArrayList<>();
for (Direction direction : Direction.values()) {
boolean foundSomething = false;
BlockPos adjPos = getPos().offset(direction);
BlockEntity adjBe = serverWorld.getBlockEntity(adjPos);
if (adjBe instanceof CableBlockEntity adjCable) {
if (adjCable.getCableType().transferRate == getCableType().transferRate) {
// Make sure cables are not used as regular targets.
foundSomething = true;
}
} else if (EnergyStorage.SIDED.find(serverWorld, adjPos, null, adjBe, direction.getOpposite()) != null) {
foundSomething = true;
targets.add(new CableTarget(
direction,
BlockApiCache.create(EnergyStorage.SIDED, serverWorld, adjPos)
));
}
newBlockState = newBlockState.with(CableBlock.PROPERTY_MAP.get(direction), foundSomething);
}
serverWorld.setBlockState(getPos(), newBlockState);
}
// Fill the list.
for (CableTarget target : targets) {
EnergyStorage storage = target.find();
if (storage == null) {
// Schedule a rebuild next tick.
// This is just a reference change, the iterator remains valid.
targets = null;
} else {
targetStorages.add(new OfferedEnergyStorage(this, target.directionTo, storage));
}
}
// Reset blocked sides.
blockedSides = 0;
}
// BlockEntity
@Override
public NbtCompound toInitialChunkDataNbt() {
@ -185,7 +236,7 @@ public class CableBlockEntity extends BlockEntity
targets = null;
}
// Tickable
// BlockEntityTicker
@Override
public void tick(World world, BlockPos pos, BlockState state, CableBlockEntity blockEntity2) {
if (world == null || world.isClient) {
@ -228,62 +279,7 @@ public class CableBlockEntity extends BlockEntity
return new ItemStack(getCableType().block);
}
void appendTargets(List<OfferedEnergyStorage> targetStorages) {
ServerWorld serverWorld = (ServerWorld) world;
// Update our targets if necessary.
if (targets == null) {
BlockState newBlockState = getCachedState();
targets = new ArrayList<>();
for (Direction direction : Direction.values()) {
boolean foundSomething = false;
BlockPos adjPos = getPos().offset(direction);
BlockEntity adjBe = serverWorld.getBlockEntity(adjPos);
if (adjBe instanceof CableBlockEntity adjCable && adjCable.getCableType() == getCableType()) {
// Make sure cables are not used as regular targets.
foundSomething = true;
} else if (EnergyStorage.SIDED.find(serverWorld, adjPos, null, adjBe, direction.getOpposite()) != null) {
foundSomething = true;
targets.add(new CableTarget(
direction,
BlockApiCache.create(EnergyStorage.SIDED, serverWorld, adjPos)
));
}
newBlockState = newBlockState.with(CableBlock.PROPERTY_MAP.get(direction), foundSomething);
}
serverWorld.setBlockState(getPos(), newBlockState);
}
// Fill the list.
for (CableTarget target : targets) {
EnergyStorage storage = target.find();
if (storage == null) {
// Schedule a rebuild next tick.
// This is just a reference change, the iterator remains valid.
targets = null;
} else {
targetStorages.add(new OfferedEnergyStorage(this, target.directionTo, storage));
}
}
// Reset blocked sides.
blockedSides = 0;
}
private static final class CableTarget {
private final Direction directionTo;
private final BlockApiCache<EnergyStorage, Direction> cache;
CableTarget(Direction directionTo, BlockApiCache<EnergyStorage, Direction> cache) {
this.directionTo = directionTo;
this.cache = cache;
}
private record CableTarget(Direction directionTo, BlockApiCache<EnergyStorage, Direction> cache) {
@Nullable
EnergyStorage find() {

View file

@ -58,7 +58,6 @@ import org.jetbrains.annotations.Nullable;
import reborncore.api.ToolManager;
import reborncore.common.blocks.BlockWrenchEventHandler;
import reborncore.common.util.WrenchUtils;
import team.reborn.energy.api.EnergyStorage;
import techreborn.api.events.CableElectrocutionEvent;
import techreborn.blockentity.cable.CableBlockEntity;
import techreborn.config.TechRebornConfig;
@ -102,7 +101,7 @@ public class CableBlock extends BlockWithEntity implements Waterloggable {
BlockWrenchEventHandler.wrenableBlocks.add(this);
}
// BlockContainer
// BlockWithEntity
@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;