BlueGit

timfa minecart-trains

Files

minecart-trains / src / main / java / me / fridtjof / minecarttrains / events / OnVehicleUpdateEvent.java

OnVehicleUpdateEvent.java

OnVehicleUpdateEvent.java 10.0 KiB Download Raw
1package me.fridtjof.minecarttrains.events;
2
3import me.fridtjof.minecarttrains.managers.LinkageManager;
4import me.fridtjof.minecarttrains.MinecartTrains;
5import me.fridtjof.minecarttrains.utils.FurnaceCarts;
6import org.bukkit.Location;
7import org.bukkit.Material;
8import org.bukkit.World;
9import org.bukkit.block.Block;
10import org.bukkit.block.data.type.RedstoneRail;
11import org.bukkit.entity.Entity;
12import org.bukkit.entity.Minecart;
13import org.bukkit.entity.Vehicle;
14import org.bukkit.entity.minecart.PoweredMinecart;
15import org.bukkit.event.EventHandler;
16import org.bukkit.event.Listener;
17import org.bukkit.event.vehicle.VehicleUpdateEvent;
18import org.bukkit.util.Vector;
19
20public class OnVehicleUpdateEvent implements Listener
21{
22
23 static MinecartTrains plugin = MinecartTrains.getInstance();
24 LinkageManager linkageManager = plugin.linkageManager;
25
26 // config vars
27 int fuelPerTick = plugin.configManager.mainConfig.getConfig().getInt("trains.fuel.consumption_per_tick");
28
29 double maxDistance = plugin.configManager.physicsConfig.getConfig().getDouble("link.distance.max");
30 double minDistance = plugin.configManager.physicsConfig.getConfig().getDouble("link.distance.min");
31
32 double couplingPullSpeedLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.speed.pull");
33 double couplingPushSpeedLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.speed.push");
34 double aimedDistanceLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.distance.aimed");
35 double aimedDistanceToleranceLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.distance.aimed_tolerance");
36
37 double couplingPullSpeedHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.speed.pull");
38 double couplingPushSpeedHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.speed.push");
39 double aimedDistanceHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.distance.aimed");
40 double aimedDistanceToleranceHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.distance.aimed_tolerance");
41
42 double highSpeedGate = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.gate_speed");
43
44
45 @EventHandler
46 public void onVehicleUpdate(VehicleUpdateEvent event)
47 {
48 Vehicle vehicle = event.getVehicle();
49 Minecart minecart;
50 String uniqueId;
51 Entity parent;
52
53 if(!(vehicle instanceof Minecart))
54 {
55 return;
56 }
57 minecart = (Minecart) vehicle;
58
59 //DEBUG
60 /*if(!minecart.getPassengers().isEmpty())
61 {
62 Entity passenger = minecart.getPassengers().get(0);
63 if((passenger instanceof Player))
64 {
65 double shownSpeed = minecart.getVelocity().length() * 72;
66 passenger.sendMessage("Speed: " + (int)shownSpeed + " km/h");
67 }
68 }*/
69
70 linkageManager.remember(minecart);
71
72 if (minecart instanceof PoweredMinecart poweredCart)
73 {
74 handlePoweredCart(poweredCart);
75 }
76
77 // ensures the below function only works on trains, not on single minecarts
78 if(!linkageManager.hasLink((minecart)))
79 {
80 return;
81 }
82
83 parent = linkageManager.getParent(minecart);
84
85 // breaks link if parent cart is broken
86 if(parent == null)
87 {
88 linkageManager.removeLink(minecart);
89 return;
90 }
91
92 // breaks link if the parent and child are the same cart
93 if(parent == minecart)
94 {
95 plugin.getLogger().warning(plugin.configManager.mainConfig.getConfig().getString("trains.coupling_already_coupled_with_itself"));
96 linkageManager.removeLink(minecart);
97 return;
98 }
99
100
101 //check if parent is in same world
102 World childWorld = minecart.getWorld();
103 World parentWorld = parent.getWorld();
104 if(childWorld != parentWorld)
105 {
106 linkageManager.removeLink(minecart);
107 return;
108 }
109
110
111 // may cause problems
112 //check if parent is derailed
113 Location minecartLocation = minecart.getLocation();
114 Material parentTrack = parentWorld.getBlockAt(parent.getLocation()).getType();
115 Material parentTrackOnHill = parentWorld.getBlockAt(parent.getLocation().add(0, -1, 0)).getType();
116 if(!isAnyKindOfTrack(parentTrack) && !isAnyKindOfTrack(parentTrackOnHill))
117 {
118 linkageManager.removeLink(minecart);
119 return;
120 }
121
122
123 // defines the distance value as (parent's location)-(child's location)
124 double distance = minecart.getLocation().distance(parent.getLocation());
125
126 /* Sets the speed at which carts move towards their dependant. At HSRail speeds, this value must be high
127 to avoid breaking trains during acceleration.
128 Values in excess of 5 require more precision for the 'else if' statements, or else carts will overshoot
129 and collide, disrupting momentum and causing the 'jiggle' bug.*/
130 double pullSpeed;
131 double pushSpeed;
132
133 double aimedDistance;
134 double aimedDistanceTolerance;
135
136 // sets the parts pull vars based on the speed of the parent
137 if(parent.getVelocity().length() <= highSpeedGate)
138 {
139 //low speed
140 pullSpeed = couplingPullSpeedLowSpeed;
141 aimedDistance = this.aimedDistanceLowSpeed;
142 aimedDistanceTolerance = this.aimedDistanceToleranceLowSpeed;
143 }
144 else
145 {
146 //high speed
147 pullSpeed = couplingPullSpeedHighSpeed;
148 aimedDistance = this.aimedDistanceHighSpeed;
149 aimedDistanceTolerance = this.aimedDistanceToleranceHighSpeed;
150 }
151
152
153 // sets the carts push speed based on its own speed
154 if(minecart.getVelocity().length() <= highSpeedGate)
155 {
156 //low speed
157 pushSpeed = couplingPushSpeedLowSpeed;
158 }
159 else
160 {
161 //high speed
162 pushSpeed = couplingPushSpeedHighSpeed;
163 }
164
165
166 /* Sets max/min distance for links. Shorter distances are more reliable along corners, however, until the
167 linkageManager can be reworked, longer maximums are needed to handle acceleration.
168 If distance is out of bonds the links get removed and the cart stopped to prevent runaway trains. */
169 if((distance > maxDistance) || distance < minDistance)
170 {
171 linkageManager.removeLink(minecart);
172 minecart.setVelocity(new Vector(0, 0, 0));
173 parent.setVelocity(new Vector(0, 0, 0));
174 }
175
176 /* Handles gap between parent and dependant. Values set too low result in collisions between carts
177 while train is sitting still, especially on declines, causing 'jiggling' that kills momentum
178 More precise parameters helps to eliminate the 'jiggling' behavior that interferes with momentum
179 Slower 'away' speeds compared to 'toward' speeds also help to eliminate 'jiggling' */
180 else
181 {
182 Entity follower = minecart;
183 Entity anchor = parent;
184 Minecart leader = linkageManager.findLeader(minecart);
185 if (leader != null && linkageManager.isCloserToLeader(minecart, (Minecart) parent, leader))
186 {
187 follower = parent;
188 anchor = minecart;
189 }
190 if (leader != null && follower.getUniqueId().equals(leader.getUniqueId()))
191 {
192 Entity swap = follower;
193 follower = anchor;
194 anchor = swap;
195 }
196
197 // acceleration if distance to small
198 if(distance < (aimedDistance - aimedDistanceTolerance))
199 {
200 moveToward(follower, anchor, pushSpeed, (distance - aimedDistance));
201 }
202 // acceleration if distance to large
203 else if(distance > (aimedDistance + aimedDistanceTolerance))
204 {
205 moveToward(follower, anchor, pullSpeed, (distance - aimedDistance));
206 }
207 }
208 }
209
210 private void handlePoweredCart(PoweredMinecart poweredCart)
211 {
212 int fuelAfterConsumption = poweredCart.getFuel() - fuelPerTick + 1;
213 if (fuelAfterConsumption < 0)
214 {
215 fuelAfterConsumption = 0;
216 }
217 poweredCart.setFuel(fuelAfterConsumption);
218
219 FurnaceCarts.restorePushFromMotion(poweredCart);
220
221 Block block = poweredCart.getLocation().getBlock();
222 if (block.getType() != Material.POWERED_RAIL)
223 {
224 return;
225 }
226 RedstoneRail redstoneRail = (RedstoneRail) block.getBlockData();
227 if (redstoneRail.isPowered())
228 {
229 return;
230 }
231 poweredCart.setVelocity(new Vector(0, 0, 0));
232 poweredCart.setFuel(poweredCart.getFuel() + fuelPerTick);
233 }
234
235 // defines moveToward, which handles the elastic movement of dependant carts. Uses the speed and direction set earlier
236 public void moveToward(Entity child, Entity parent, double speed, double distance)
237 {
238 Location childLoc = child.getLocation();
239 Location parentLoc = parent.getLocation();
240
241 double x = childLoc.getX() - parentLoc.getX();
242 double y = childLoc.getY() - parentLoc.getY();
243 double z = childLoc.getZ() - parentLoc.getZ();
244 Vector velocity = new Vector(x, y, z).normalize().multiply(-speed);
245 child.setVelocity(velocity.multiply((distance * distance * distance)));
246 }
247
248
249 public boolean isAnyKindOfTrack(Material material)
250 {
251 if(material == Material.POWERED_RAIL)
252 {
253 return true;
254 }
255 if(material == Material.RAIL)
256 {
257 return true;
258 }
259 if(material == Material.ACTIVATOR_RAIL)
260 {
261 return true;
262 }
263 if(material == Material.DETECTOR_RAIL)
264 {
265 return true;
266 }
267 return false;
268 }
269}