Cable optimization: use BlockApiCache to get adjacent BE. Thanks Technici4n

This commit is contained in:
Technici4n 2022-03-25 08:38:58 +01:00 committed by GitHub
parent cfff12afd6
commit 5eb8890d94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View file

@ -84,6 +84,11 @@ public class CableBlockEntity extends BlockEntity
long lastTick = 0; long lastTick = 0;
// null means that it needs to be re-queried // null means that it needs to be re-queried
List<CableTarget> targets = null; List<CableTarget> targets = null;
/**
* Adjacent caches, used to quickly query adjacent cable block entities.
*/
@SuppressWarnings("unchecked")
private final BlockApiCache<EnergyStorage, Direction>[] adjacentCaches = new BlockApiCache[6];
/** /**
* Bitmask to prevent input or output into/from the cable when the cable already transferred in the target direction. * Bitmask to prevent input or output into/from the cable when the cable already transferred in the target direction.
* This prevents double transfer rates, and back and forth between two cables. * This prevents double transfer rates, and back and forth between two cables.
@ -146,6 +151,18 @@ public class CableBlockEntity extends BlockEntity
energyContainer.amount = energy; energyContainer.amount = energy;
} }
private BlockApiCache<EnergyStorage, Direction> getAdjacentCache(Direction direction) {
if (adjacentCaches[direction.getId()] == null) {
adjacentCaches[direction.getId()] = BlockApiCache.create(EnergyStorage.SIDED, (ServerWorld) world, pos.offset(direction));
}
return adjacentCaches[direction.getId()];
}
@Nullable
BlockEntity getAdjacentBlockEntity(Direction direction) {
return getAdjacentCache(direction).getBlockEntity();
}
void appendTargets(List<OfferedEnergyStorage> targetStorages) { void appendTargets(List<OfferedEnergyStorage> targetStorages) {
ServerWorld serverWorld = (ServerWorld) world; ServerWorld serverWorld = (ServerWorld) world;
if (serverWorld == null) { return; } if (serverWorld == null) { return; }
@ -158,20 +175,16 @@ public class CableBlockEntity extends BlockEntity
for (Direction direction : Direction.values()) { for (Direction direction : Direction.values()) {
boolean foundSomething = false; boolean foundSomething = false;
BlockPos adjPos = getPos().offset(direction); BlockApiCache<EnergyStorage, Direction> adjCache = getAdjacentCache(direction);
BlockEntity adjBe = serverWorld.getBlockEntity(adjPos);
if (adjBe instanceof CableBlockEntity adjCable) { if (adjCache.getBlockEntity() instanceof CableBlockEntity adjCable) {
if (adjCable.getCableType().transferRate == getCableType().transferRate) { if (adjCable.getCableType().transferRate == getCableType().transferRate) {
// Make sure cables are not used as regular targets. // Make sure cables are not used as regular targets.
foundSomething = true; foundSomething = true;
} }
} else if (EnergyStorage.SIDED.find(serverWorld, adjPos, null, adjBe, direction.getOpposite()) != null) { } else if (adjCache.find(direction.getOpposite()) != null) {
foundSomething = true; foundSomething = true;
targets.add(new CableTarget( targets.add(new CableTarget(direction, adjCache));
direction,
BlockApiCache.create(EnergyStorage.SIDED, serverWorld, adjPos)
));
} }
newBlockState = newBlockState.with(CableBlock.PROPERTY_MAP.get(direction), foundSomething); newBlockState = newBlockState.with(CableBlock.PROPERTY_MAP.get(direction), foundSomething);

View file

@ -118,9 +118,7 @@ class CableTickManager {
CableBlockEntity current = bfsQueue.removeFirst(); CableBlockEntity current = bfsQueue.removeFirst();
for (Direction direction : Direction.values()) { for (Direction direction : Direction.values()) {
BlockPos adjPos = current.getPos().offset(direction); if (current.getAdjacentBlockEntity(direction) instanceof CableBlockEntity adjCable && current.getCableType() == adjCable.getCableType()) {
if (current.getWorld().getBlockEntity(adjPos) instanceof CableBlockEntity adjCable && current.getCableType() == adjCable.getCableType()) {
if (shouldTickCable(adjCable)) { if (shouldTickCable(adjCable)) {
bfsQueue.add(adjCable); bfsQueue.add(adjCable);
adjCable.lastTick = tickCounter; adjCable.lastTick = tickCounter;