From 6b2b0d1c1d677b631ba5e85279851289f3a4be04 Mon Sep 17 00:00:00 2001 From: notstevy Date: Thu, 6 Aug 2026 15:51:53 +0200 Subject: [PATCH] Reduce state updates on locked "Head- Tail lights" When these lights were in the locked mode, they were updated when the driving direction did not match their configured value and immediately updated back to the old value (since they were locked). This behavior caused lag spikes because Create had to update the train twice for each misconfigured light, even though updates were unwanted. Now, the light mode checks are only run in the unlocked mode, which improves performance immensely. --- .../HeadTailLightMovementBehaviour.java | 132 +++++++++--------- 1 file changed, 64 insertions(+), 68 deletions(-) diff --git a/common/src/main/java/net/adeptstack/ctl/behaviours/movement/HeadTailLightMovementBehaviour.java b/common/src/main/java/net/adeptstack/ctl/behaviours/movement/HeadTailLightMovementBehaviour.java index 264fde8..aa0b5ec 100644 --- a/common/src/main/java/net/adeptstack/ctl/behaviours/movement/HeadTailLightMovementBehaviour.java +++ b/common/src/main/java/net/adeptstack/ctl/behaviours/movement/HeadTailLightMovementBehaviour.java @@ -40,7 +40,6 @@ public void tick(MovementContext context) { context.data.putInt("OpenTicks", ticksOpen); if (ticksOpen > 20) { BlockPos pos = context.localPos; - int oldLightMode = context.state.getValue(HeadTailLightBlockBase.LIGHT_MODE); if (context.contraption.entity instanceof CarriageContraptionEntity cce && context.contraption instanceof CarriageContraption cc) { Direction assemblyDirection = cc.getAssemblyDirection(); if (assemblyDirection == Direction.UP || assemblyDirection == Direction.DOWN) { @@ -80,80 +79,77 @@ public void tick(MovementContext context) { value = globalP.subtract(globalN); } - if (direction == Direction.NORTH) { - if (value.z > 0 && localXZ > 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.z > 0 && localXZ < 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.z < 0 && localXZ > 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.z < 0 && localXZ < 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } - } else if (direction == Direction.EAST) { - if (value.x > 0 && localXZ > 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.x > 0 && localXZ < 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.x < 0 && localXZ > 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.x < 0 && localXZ < 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } - } else if (direction == Direction.SOUTH) { - if (value.z > 0 && localXZ > 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.z > 0 && localXZ < 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.z < 0 && localXZ > 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.z < 0 && localXZ < 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } - } else if (direction == Direction.WEST) { - if (value.x > 0 && localXZ > 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.x > 0 && localXZ < 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.x < 0 && localXZ > 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } else if (value.x < 0 && localXZ < 0) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } - } - if (locked) { - if (context.state.getValue(HeadTailLightBlockBase.LIGHT_MODE) != oldLightMode) { - context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, oldLightMode); - context.state = context.state .setValue(HeadTailLightBlockBase.LIT, false); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } - else { - context.state = context.state .setValue(HeadTailLightBlockBase.LIT, true); - context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); - } + context.state = context.state .setValue(HeadTailLightBlockBase.LIT, true); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else { + tickLightMode(context, value, direction, localXZ, pos, structureBlockInfo); } } context.data.putInt("OpenTicks", 0); } } + private void tickLightMode(MovementContext context, Vec3 value, Direction direction, int localXZ, BlockPos pos, StructureTemplate.StructureBlockInfo structureBlockInfo) { + if (direction == Direction.NORTH) { + if (value.z > 0 && localXZ > 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.z > 0 && localXZ < 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.z < 0 && localXZ > 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.z < 0 && localXZ < 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } + } else if (direction == Direction.EAST) { + if (value.x > 0 && localXZ > 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.x > 0 && localXZ < 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.x < 0 && localXZ > 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.x < 0 && localXZ < 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } + } else if (direction == Direction.SOUTH) { + if (value.z > 0 && localXZ > 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.z > 0 && localXZ < 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.z < 0 && localXZ > 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.z < 0 && localXZ < 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } + } else if (direction == Direction.WEST) { + if (value.x > 0 && localXZ > 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.x > 0 && localXZ < 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.x < 0 && localXZ > 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 0); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } else if (value.x < 0 && localXZ < 0) { + context.state = context.state .setValue(HeadTailLightBlockBase.LIGHT_MODE, 1); + context.contraption.entity.setBlock(pos, new StructureTemplate.StructureBlockInfo(pos, context.state, structureBlockInfo.nbt())); + } + } + } + protected void tickLIT(MovementContext context, boolean currentlyOpen) { boolean shouldLIT = shouldLIT(context); if (!shouldUpdate(context, shouldLIT))