BlueGit

timfa minecart-trains

Let unpowered trains coast and stop with vanilla rail friction.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-22 · 2 months ago

Hash
5a030b11b9cf0497516c59d21c541241e77b3e00
Parent
65812234
4 files changed +24 −30
README.md +2 −2
@@ -72,9 +72,9 @@ You need JDK 25 and Maven. Then run:
72 72 mvn package
73 73 ```
74 74
75 Results will be in the `target` folder. With version `1.5.2` that is:
75 Results will be in the `target` folder. With version `1.5.3` that is:
76 76
77 - `MinecartTrains-1.5.2.jar`
77 - `MinecartTrains-1.5.3.jar`
78 78
79 79 Copy this file to the plugins folder of a Paper 26.2 server.
80 80
@@ -72,9 +72,9 @@ You need JDK 25 and Maven. Then run:
72mvn package
72mvn package
73```
73```
74
74
75Results will be in the `target` folder. With version `1.5.2` that is:
75Results will be in the `target` folder. With version `1.5.3` that is:
76
76
77- `MinecartTrains-1.5.2.jar`
77- `MinecartTrains-1.5.3.jar`
78
78
79Copy this file to the plugins folder of a Paper 26.2 server.
79Copy this file to the plugins folder of a Paper 26.2 server.
80
80
pom.xml +1 −1
@@ -6,7 +6,7 @@
6 6
7 7 <groupId>me.fridtjof</groupId>
8 8 <artifactId>minecarttrains</artifactId>
9 <version>1.5.2</version>
9 <version>1.5.3</version>
10 10 <packaging>jar</packaging>
11 11
12 12 <name>MinecartTrains</name>
@@ -6,7 +6,7 @@
6
6
7 <groupId>me.fridtjof</groupId>
7 <groupId>me.fridtjof</groupId>
8 <artifactId>minecarttrains</artifactId>
8 <artifactId>minecarttrains</artifactId>
9 <version>1.5.2</version>
9 <version>1.5.3</version>
10 <packaging>jar</packaging>
10 <packaging>jar</packaging>
11
11
12 <name>MinecartTrains</name>
12 <name>MinecartTrains</name>
src/main/java/me/fridtjof/minecarttrains/managers/LinkPhysics.java +19 −5
@@ -51,6 +51,15 @@ public class LinkPhysics
51 51 }
52 52
53 53 Minecart leader = linkageManager.findLeader(cart);
54 if (leader == null)
55 {
56 for (int i = 1; i < train.size(); i++)
57 {
58 constrain(train.get(i), train.get(i - 1), false);
59 }
60 return;
61 }
62
54 63 int leaderIndex = indexOf(train, leader);
55 64 if (leaderIndex < 0)
56 65 {
@@ -59,15 +68,15 @@ public class LinkPhysics
59 68
60 69 for (int i = leaderIndex + 1; i < train.size(); i++)
61 70 {
62 constrain(train.get(i), train.get(i - 1));
71 constrain(train.get(i), train.get(i - 1), true);
63 72 }
64 73 for (int i = leaderIndex - 1; i >= 0; i--)
65 74 {
66 constrain(train.get(i), train.get(i + 1));
75 constrain(train.get(i), train.get(i + 1), true);
67 76 }
68 77 }
69 78
70 private void constrain(Minecart follower, Minecart anchor)
79 private void constrain(Minecart follower, Minecart anchor, boolean driven)
71 80 {
72 81 Vector followerPos = follower.getLocation().toVector();
73 82 Vector anchorPos = anchor.getLocation().toVector();
@@ -110,14 +119,19 @@ public class LinkPhysics
110 119 double aimed = aimedDistance(anchor);
111 120 double stretch = distance - aimed;
112 121
113 double targetSpeed = anchor.getVelocity().length();
114 Vector newVelocity = rail.clone().multiply(targetSpeed);
122 Vector newVelocity = driven
123 ? rail.clone().multiply(anchor.getVelocity().length())
124 : follower.getVelocity().clone();
115 125
116 126 double correction = spacingCorrection(stretch, onCurve, follower.getVelocity(), anchor.getVelocity(), rail, linkAxis);
117 127 if (Math.abs(correction) > 1.0E-4)
118 128 {
119 129 newVelocity.add(rail.clone().multiply(correction));
120 130 }
131 else if (!driven)
132 {
133 return;
134 }
121 135
122 136 follower.setVelocity(newVelocity);
123 137 }
@@ -51,6 +51,15 @@ public class LinkPhysics
51 }
51 }
52
52
53 Minecart leader = linkageManager.findLeader(cart);
53 Minecart leader = linkageManager.findLeader(cart);
54 if (leader == null)
55 {
56 for (int i = 1; i < train.size(); i++)
57 {
58 constrain(train.get(i), train.get(i - 1), false);
59 }
60 return;
61 }
62
54 int leaderIndex = indexOf(train, leader);
63 int leaderIndex = indexOf(train, leader);
55 if (leaderIndex < 0)
64 if (leaderIndex < 0)
56 {
65 {
@@ -59,15 +68,15 @@ public class LinkPhysics
59
68
60 for (int i = leaderIndex + 1; i < train.size(); i++)
69 for (int i = leaderIndex + 1; i < train.size(); i++)
61 {
70 {
62 constrain(train.get(i), train.get(i - 1));
71 constrain(train.get(i), train.get(i - 1), true);
63 }
72 }
64 for (int i = leaderIndex - 1; i >= 0; i--)
73 for (int i = leaderIndex - 1; i >= 0; i--)
65 {
74 {
66 constrain(train.get(i), train.get(i + 1));
75 constrain(train.get(i), train.get(i + 1), true);
67 }
76 }
68 }
77 }
69
78
70 private void constrain(Minecart follower, Minecart anchor)
79 private void constrain(Minecart follower, Minecart anchor, boolean driven)
71 {
80 {
72 Vector followerPos = follower.getLocation().toVector();
81 Vector followerPos = follower.getLocation().toVector();
73 Vector anchorPos = anchor.getLocation().toVector();
82 Vector anchorPos = anchor.getLocation().toVector();
@@ -110,14 +119,19 @@ public class LinkPhysics
110 double aimed = aimedDistance(anchor);
119 double aimed = aimedDistance(anchor);
111 double stretch = distance - aimed;
120 double stretch = distance - aimed;
112
121
113 double targetSpeed = anchor.getVelocity().length();
122 Vector newVelocity = driven
114 Vector newVelocity = rail.clone().multiply(targetSpeed);
123 ? rail.clone().multiply(anchor.getVelocity().length())
124 : follower.getVelocity().clone();
115
125
116 double correction = spacingCorrection(stretch, onCurve, follower.getVelocity(), anchor.getVelocity(), rail, linkAxis);
126 double correction = spacingCorrection(stretch, onCurve, follower.getVelocity(), anchor.getVelocity(), rail, linkAxis);
117 if (Math.abs(correction) > 1.0E-4)
127 if (Math.abs(correction) > 1.0E-4)
118 {
128 {
119 newVelocity.add(rail.clone().multiply(correction));
129 newVelocity.add(rail.clone().multiply(correction));
120 }
130 }
131 else if (!driven)
132 {
133 return;
134 }
121
135
122 follower.setVelocity(newVelocity);
136 follower.setVelocity(newVelocity);
123 }
137 }
src/main/java/me/fridtjof/minecarttrains/managers/LinkageManager.java +2 −22
@@ -292,15 +292,9 @@ public class LinkageManager
292 292 return cachedLeader;
293 293 }
294 294
295 List<Minecart> train = getTrain(cart);
296 if (train.isEmpty())
297 {
298 return cart;
299 }
300
301 295 Minecart bestEngine = null;
302 296 int bestFuel = -1;
303 for (Minecart candidate : train)
297 for (Minecart candidate : getTrain(cart))
304 298 {
305 299 if (candidate instanceof PoweredMinecart powered && powered.getFuel() > 0)
306 300 {
@@ -314,22 +308,8 @@ public class LinkageManager
314 308 if (bestEngine != null)
315 309 {
316 310 leaderByHead.put(head.getUniqueId(), bestEngine);
317 return bestEngine;
318 }
319
320 Minecart fastest = train.getFirst();
321 double fastestSpeed = -1;
322 for (Minecart candidate : train)
323 {
324 double speed = candidate.getVelocity().lengthSquared();
325 if (speed > fastestSpeed)
326 {
327 fastestSpeed = speed;
328 fastest = candidate;
329 }
330 311 }
331 leaderByHead.put(head.getUniqueId(), fastest);
332 return fastest;
312 return bestEngine;
333 313 }
334 314
335 315 private void refreshTickCache()
@@ -292,15 +292,9 @@ public class LinkageManager
292 return cachedLeader;
292 return cachedLeader;
293 }
293 }
294
294
295 List<Minecart> train = getTrain(cart);
296 if (train.isEmpty())
297 {
298 return cart;
299 }
300
301 Minecart bestEngine = null;
295 Minecart bestEngine = null;
302 int bestFuel = -1;
296 int bestFuel = -1;
303 for (Minecart candidate : train)
297 for (Minecart candidate : getTrain(cart))
304 {
298 {
305 if (candidate instanceof PoweredMinecart powered && powered.getFuel() > 0)
299 if (candidate instanceof PoweredMinecart powered && powered.getFuel() > 0)
306 {
300 {
@@ -314,22 +308,8 @@ public class LinkageManager
314 if (bestEngine != null)
308 if (bestEngine != null)
315 {
309 {
316 leaderByHead.put(head.getUniqueId(), bestEngine);
310 leaderByHead.put(head.getUniqueId(), bestEngine);
317 return bestEngine;
318 }
319
320 Minecart fastest = train.getFirst();
321 double fastestSpeed = -1;
322 for (Minecart candidate : train)
323 {
324 double speed = candidate.getVelocity().lengthSquared();
325 if (speed > fastestSpeed)
326 {
327 fastestSpeed = speed;
328 fastest = candidate;
329 }
330 }
311 }
331 leaderByHead.put(head.getUniqueId(), fastest);
312 return bestEngine;
332 return fastest;
333 }
313 }
334
314
335 private void refreshTickCache()
315 private void refreshTickCache()