Skip to content

Commit

Permalink
Reduce essentia network lag (#47)
Browse files Browse the repository at this point in the history
* Reduce node block-entity checks
This isn't very dangerous. Might have some memory implications but I don't imagine they'd be very bad.
A connected port will have notably better perf. A disconnected port will be just as bad.

* Cleanup
  • Loading branch information
KyGost authored Oct 1, 2021
1 parent 64e9fd9 commit 87964a4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/main/java/dev/cafeteria/artofalchemy/block/BlockPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static void scheduleChunkRebuild(final World world, final BlockPos pos) {
}
}

private HashSet<NetworkNode> nodes;

public BlockPipe() {
super(
Settings.of(Material.ORGANIC_PRODUCT).strength(0.1f).nonOpaque().noCollision().sounds(BlockSoundGroup.NETHERITE)
Expand Down Expand Up @@ -170,15 +172,15 @@ private Map<Direction, IOFace> getFaces(final World world, final BlockPos pos) {

@Override
public Set<NetworkNode> getNodes(final World world, final BlockPos pos) {
final HashSet<NetworkNode> nodes = new HashSet<>();
this.nodes = new HashSet<>(); // TODO: Only reset when needed
final Map<Direction, IOFace> faces = this.getFaces(world, pos);
for (final Direction dir : faces.keySet()) {
final IOFace face = faces.get(dir);
if (face.isNode()) {
nodes.add(new NetworkNode(world, face.getType(), pos, dir));
this.nodes.add(new NetworkNode(world, face.getType(), pos, dir));
}
}
return nodes;
return this.nodes;
}

@Override
Expand Down Expand Up @@ -232,6 +234,7 @@ public void neighborUpdate(
final boolean notify
) {
super.neighborUpdate(state, world, pos, block, fromPos, notify);

for (final Direction dir : Direction.values()) {
if (fromPos.subtract(pos).equals(dir.getVector())) {
if (this.faceOpen(world, pos, dir) && this.faceOpen(world, fromPos, dir.getOpposite())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public void tick() {
return;
}
this.lastTicked = this.world.getTime();
this.nodes.forEach(NetworkNode::checkBlockEntity); // KG: This should be run as irregularly as possible. Ideally
// node would listen for nearby updates and update on demand
// there.

for (final NetworkNode pusher : this.pushers) {
for (final NetworkNode puller : this.pullers) {
Expand Down Expand Up @@ -186,20 +189,21 @@ public NbtList toTag() {
public void transfer(final NetworkNode from, final NetworkNode to) {
final BlockEntity fromBE = from.getBlockEntity();
final BlockEntity toBE = to.getBlockEntity();
if ((fromBE instanceof HasEssentia) && (toBE instanceof HasEssentia)) {
for (int i = 0; i < ((HasEssentia) fromBE).getNumContainers(); i++) {
if ((fromBE instanceof HasEssentia fromEssenceBE) && (toBE instanceof HasEssentia)) {
final HasEssentia toEssenceBE = (HasEssentia) toBE;
for (int i = 0; i < fromEssenceBE.getNumContainers(); i++) {
EssentiaContainer fromContainer;
if (from.getDirection().isPresent()) {
fromContainer = ((HasEssentia) fromBE).getContainer(from.getDirection().get().getOpposite());
fromContainer = fromEssenceBE.getContainer(from.getDirection().get().getOpposite());
} else {
fromContainer = ((HasEssentia) fromBE).getContainer();
fromContainer = fromEssenceBE.getContainer();
}
for (int j = 0; j < ((HasEssentia) toBE).getNumContainers(); j++) {
for (int j = 0; j < toEssenceBE.getNumContainers(); j++) {
EssentiaContainer toContainer;
if (to.getDirection().isPresent()) {
toContainer = ((HasEssentia) toBE).getContainer(to.getDirection().get().getOpposite());
toContainer = toEssenceBE.getContainer(to.getDirection().get().getOpposite());
} else {
toContainer = ((HasEssentia) toBE).getContainer();
toContainer = toEssenceBE.getContainer();
}
fromContainer.pushContents(toContainer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public String asString() {
private final World world;
private final NetworkNode.Type type;
private final BlockPos pos;
private BlockEntity blockEntity;

private final Direction dir;

Expand All @@ -40,16 +41,19 @@ public NetworkNode(final World world, final Type type, final BlockPos pos, final
this.type = type;
this.pos = pos;
this.dir = dir;
this.updateBlockEntity();
}

public BlockEntity getBlockEntity() {
if (this.dir != null) {
return this.world.getBlockEntity(this.pos.offset(this.dir));
} else {
return this.world.getBlockEntity(this.pos);
public void checkBlockEntity() {
if ((this.blockEntity == null) || this.blockEntity.isRemoved()) {
this.updateBlockEntity();
}
}

public BlockEntity getBlockEntity() {
return this.blockEntity;
}

public Optional<Direction> getDirection() {
return Optional.of(this.dir);
}
Expand All @@ -62,4 +66,8 @@ public NetworkNode.Type getType() {
return this.type;
}

public void updateBlockEntity() {
this.blockEntity = this.world.getBlockEntity(this.dir == null ? this.pos : this.pos.offset(this.dir));
}

}

0 comments on commit 87964a4

Please sign in to comment.