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 // ensures the below function only works on trains, not on single minecarts
72 if(!linkageManager.hasLink((minecart)))
73 {
74
75 // causes a full stop for Furnace Minecart on un-powered rail, and pauses Fuel Consumption.
76 if(!(minecart instanceof PoweredMinecart))
77 {
78 return;
79 }
80
81 PoweredMinecart poweredCart = (PoweredMinecart) minecart;
82
83 int fuelAfterConsumption = poweredCart.getFuel() - fuelPerTick + 1;
84
85 if(fuelAfterConsumption < 0)
86 {
87 fuelAfterConsumption = 0;
88 }
89 poweredCart.setFuel(fuelAfterConsumption);
90
91 Block block = poweredCart.getLocation().getBlock();
92 if(block.getType() != Material.POWERED_RAIL) {
93 return;
94 }
95 RedstoneRail redstoneRail = (RedstoneRail) block.getBlockData();
96 if(redstoneRail.isPowered()) {
97 return;
98 }
99 poweredCart.setVelocity(new Vector(0 ,0 ,0));
100
101 //to stop fuel consumption just re-add the consumed
102 poweredCart.setFuel(((PoweredMinecart) poweredCart).getFuel() + fuelPerTick);
103
104 return;
105 }
106
107 parent = Bukkit.getEntity(linkageManager.getLinkUniqueId(minecart));
108
109 // breaks link if parent cart is broken
110 if(parent == null)
111 {
112 linkageManager.removeLink(minecart);
113 return;
114 }
115
116 // breaks link if the parent and child are the same cart
117 if(parent == minecart)
118 {
119 plugin.getLogger().warning(plugin.configManager.mainConfig.getConfig().getString("trains.coupling_already_coupled_with_itself"));
120 linkageManager.removeLink(minecart);
121 return;
122 }
123
124
125 //check if parent is in same world
126 World childWorld = minecart.getWorld();
127 World parentWorld = parent.getWorld();
128 if(childWorld != parentWorld)
129 {
130 linkageManager.removeLink(minecart);
131 return;
132 }
133
134
135 // may cause problems
136 //check if parent is derailed
137 Location minecartLocation = minecart.getLocation();
138 Material parentTrack = parentWorld.getBlockAt(parent.getLocation()).getType();
139 Material parentTrackOnHill = parentWorld.getBlockAt(parent.getLocation().add(0, -1, 0)).getType();
140 if(!isAnyKindOfTrack(parentTrack) && !isAnyKindOfTrack(parentTrackOnHill))
141 {
142 linkageManager.removeLink(minecart);
143 return;
144 }
145
146
147 // defines the distance value as (parent's location)-(child's location)
148 double distance = minecart.getLocation().distance(parent.getLocation());
149
150 /* Sets the speed at which carts move towards their dependant. At HSRail speeds, this value must be high
151 to avoid breaking trains during acceleration.
152 Values in excess of 5 require more precision for the 'else if' statements, or else carts will overshoot
153 and collide, disrupting momentum and causing the 'jiggle' bug.*/
154 double pullSpeed;
155 double pushSpeed;
156
157 double aimedDistance;
158 double aimedDistanceTolerance;
159
160 // sets the parts pull vars based on the speed of the parent
161 if(parent.getVelocity().length() <= highSpeedGate)
162 {
163 //low speed
164 pullSpeed = couplingPullSpeedLowSpeed;
165 aimedDistance = this.aimedDistanceLowSpeed;
166 aimedDistanceTolerance = this.aimedDistanceToleranceLowSpeed;
167 }
168 else
169 {
170 //high speed
171 pullSpeed = couplingPullSpeedHighSpeed;
172 aimedDistance = this.aimedDistanceHighSpeed;
173 aimedDistanceTolerance = this.aimedDistanceToleranceHighSpeed;
174 }
175
176
177 // sets the carts push speed based on its own speed
178 if(minecart.getVelocity().length() <= highSpeedGate)
179 {
180 //low speed
181 pushSpeed = couplingPushSpeedLowSpeed;
182 }
183 else
184 {
185 //high speed
186 pushSpeed = couplingPushSpeedHighSpeed;
187 }
188
189
190 /* Sets max/min distance for links. Shorter distances are more reliable along corners, however, until the
191 linkageManager can be reworked, longer maximums are needed to handle acceleration.
192 If distance is out of bonds the links get removed and the cart stopped to prevent runaway trains. */
193 if((distance > maxDistance) || distance < minDistance)
194 {
195 linkageManager.removeLink(minecart);
196 minecart.setVelocity(new Vector(0, 0, 0));
197 parent.setVelocity(new Vector(0, 0, 0));
198 }
199
200 /* Handles gap between parent and dependant. Values set too low result in collisions between carts
201 while train is sitting still, especially on declines, causing 'jiggling' that kills momentum
202 More precise parameters helps to eliminate the 'jiggling' behavior that interferes with momentum
203 Slower 'away' speeds compared to 'toward' speeds also help to eliminate 'jiggling' */
204 // acceleration if distance to small
205 else if(distance < (aimedDistance - aimedDistanceTolerance))
206 {
207 moveToward(minecart, parent, pushSpeed, (distance - aimedDistance));
208 }
209 // acceleration if distance to large
210 else if(distance > (aimedDistance + aimedDistanceTolerance))
211 {
212 moveToward(minecart, parent, pullSpeed, (distance - aimedDistance));
213 }
214 }
215
216 // defines moveToward, which handles the elastic movement of dependant carts. Uses the speed and direction set earlier
217 public void moveToward(Entity child, Entity parent, double speed, double distance)
218 {
219 Location childLoc = child.getLocation();
220 Location parentLoc = parent.getLocation();
221
222 double x = childLoc.getX() - parentLoc.getX();
223 double y = childLoc.getY() - parentLoc.getY();
224 double z = childLoc.getZ() - parentLoc.getZ();
225 Vector velocity = new Vector(x, y, z).normalize().multiply(-speed);
226 child.setVelocity(velocity.multiply((distance * distance * distance)));
227 }
228
229
230 //TODO move to puddingapi
231 public boolean isAnyKindOfTrack(Material material)
232 {
233 if(material == Material.POWERED_RAIL)
234 {
235 return true;
236 }
237 if(material == Material.RAIL)
238 {
239 return true;
240 }
241 if(material == Material.ACTIVATOR_RAIL)
242 {
243 return true;
244 }
245 if(material == Material.DETECTOR_RAIL)
246 {
247 return true;
248 }
249 return false;
250 }
251}