BlueGit

timfa minecart-trains

Files

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

OnVehicleUpdateEvent.java

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