diff --git a/src/main/java/cam72cam/immersiverailroading/Config.java b/src/main/java/cam72cam/immersiverailroading/Config.java index 09758af3b..b534df5e6 100644 --- a/src/main/java/cam72cam/immersiverailroading/Config.java +++ b/src/main/java/cam72cam/immersiverailroading/Config.java @@ -33,4 +33,7 @@ public class Config { @Comment({"Print extra chunk loading info"}) public static boolean debugChunkLoading = true; + + @Comment({"Max length of a train"}) + public static int maxTrainLength = 100; } diff --git a/src/main/java/cam72cam/immersiverailroading/entity/EntityCoupleableRollingStock.java b/src/main/java/cam72cam/immersiverailroading/entity/EntityCoupleableRollingStock.java index c46788176..15f13dd75 100644 --- a/src/main/java/cam72cam/immersiverailroading/entity/EntityCoupleableRollingStock.java +++ b/src/main/java/cam72cam/immersiverailroading/entity/EntityCoupleableRollingStock.java @@ -182,14 +182,19 @@ public void setDead() { public void moveCoupledRollingStock(Float moveDistance) { this.moveRollingStock(moveDistance); if (Math.abs(moveDistance) > 0.01) { - recursiveMove(null); + recursiveMove(null, 0); } } // This breaks with looped rolling stock - private void recursiveMove(EntityCoupleableRollingStock prev) { + private void recursiveMove(EntityCoupleableRollingStock prev, int depth) { ChunkManager.flagEntityPos(this); + if (depth > Config.maxTrainLength) { + ImmersiveRailroading.logger.warn("TRAIN TOO LONG!"); + return; + } + for (CouplerType coupler : CouplerType.values()) { EntityCoupleableRollingStock coupled = this.getCoupled(coupler); @@ -198,6 +203,10 @@ private void recursiveMove(EntityCoupleableRollingStock prev) { continue; } + if (coupled == this) { + this.decouple(coupler); + } + // THIS UPDATES LAST KNOWN POS setCoupled(coupler, coupled); @@ -263,7 +272,7 @@ private void recursiveMove(EntityCoupleableRollingStock prev) { } coupled.moveRollingStock(distance); - coupled.recursiveMove(this); + coupled.recursiveMove(this, depth+1); } }