BlueGit

timfa minecart-trains

Files

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

OnVehicleUpdateEvent.java

OnVehicleUpdateEvent.java 8.4 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.Tag;
9import org.bukkit.World;
10import org.bukkit.block.Block;
11import org.bukkit.block.BlockFace;
12import org.bukkit.block.data.type.RedstoneRail;
13import org.bukkit.entity.Entity;
14import org.bukkit.entity.Minecart;
15import org.bukkit.entity.Vehicle;
16import org.bukkit.entity.minecart.PoweredMinecart;
17import org.bukkit.event.EventHandler;
18import org.bukkit.event.Listener;
19import org.bukkit.event.vehicle.VehicleUpdateEvent;
20import org.bukkit.util.Vector;
21
22public class OnVehicleUpdateEvent implements Listener
23{
24
25 static MinecartTrains plugin = MinecartTrains.getInstance();
26 LinkageManager linkageManager = plugin.linkageManager;
27
28 // config vars
29 int fuelPerTick = plugin.configManager.mainConfig.getConfig().getInt("trains.fuel.consumption_per_tick");
30
31 double maxDistance = plugin.configManager.physicsConfig.getConfig().getDouble("link.distance.max");
32 double minDistance = plugin.configManager.physicsConfig.getConfig().getDouble("link.distance.min");
33
34 double couplingPullSpeedLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.speed.pull");
35 double couplingPushSpeedLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.speed.push");
36 double aimedDistanceLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.distance.aimed");
37 double aimedDistanceToleranceLowSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.low_speed.distance.aimed_tolerance");
38
39 double couplingPullSpeedHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.speed.pull");
40 double couplingPushSpeedHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.speed.push");
41 double aimedDistanceHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.distance.aimed");
42 double aimedDistanceToleranceHighSpeed = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.distance.aimed_tolerance");
43
44 double highSpeedGate = plugin.configManager.physicsConfig.getConfig().getDouble("link.high_speed.gate_speed");
45
46
47 @EventHandler
48 public void onVehicleUpdate(VehicleUpdateEvent event)
49 {
50 Vehicle vehicle = event.getVehicle();
51 Entity parent;
52
53 if(!(vehicle instanceof Minecart minecart))
54 {
55 return;
56 }
57
58 linkageManager.remember(minecart);
59
60 if (minecart instanceof PoweredMinecart poweredCart)
61 {
62 handlePoweredCart(poweredCart);
63 }
64
65 // ensures the below function only works on trains, not on single minecarts
66 if(!linkageManager.hasLink((minecart)))
67 {
68 return;
69 }
70
71 parent = linkageManager.getParent(minecart);
72
73 // breaks link if parent cart is broken
74 if(parent == null)
75 {
76 linkageManager.removeLink(minecart);
77 return;
78 }
79
80 // breaks link if the parent and child are the same cart
81 if(parent == minecart)
82 {
83 plugin.getLogger().warning(plugin.configManager.mainConfig.getConfig().getString("trains.coupling_already_coupled_with_itself"));
84 linkageManager.removeLink(minecart);
85 return;
86 }
87
88
89 //check if parent is in same world
90 World childWorld = minecart.getWorld();
91 World parentWorld = parent.getWorld();
92 if(childWorld != parentWorld)
93 {
94 linkageManager.removeLink(minecart);
95 return;
96 }
97
98
99 Block parentBlock = parent.getLocation().getBlock();
100 if (!isRail(parentBlock.getType()) && !isRail(parentBlock.getRelative(BlockFace.DOWN).getType()))
101 {
102 linkageManager.removeLink(minecart);
103 return;
104 }
105
106 double distanceSquared = minecart.getLocation().distanceSquared(parent.getLocation());
107 double maxDistanceSquared = maxDistance * maxDistance;
108 double minDistanceSquared = minDistance * minDistance;
109
110 /* Sets the speed at which carts move towards their dependant. At HSRail speeds, this value must be high
111 to avoid breaking trains during acceleration.
112 Values in excess of 5 require more precision for the 'else if' statements, or else carts will overshoot
113 and collide, disrupting momentum and causing the 'jiggle' bug.*/
114 double pullSpeed;
115 double pushSpeed;
116
117 double aimedDistance;
118 double aimedDistanceTolerance;
119
120 double highSpeedGateSquared = highSpeedGate * highSpeedGate;
121 if(parent.getVelocity().lengthSquared() <= highSpeedGateSquared)
122 {
123 pullSpeed = couplingPullSpeedLowSpeed;
124 aimedDistance = this.aimedDistanceLowSpeed;
125 aimedDistanceTolerance = this.aimedDistanceToleranceLowSpeed;
126 }
127 else
128 {
129 pullSpeed = couplingPullSpeedHighSpeed;
130 aimedDistance = this.aimedDistanceHighSpeed;
131 aimedDistanceTolerance = this.aimedDistanceToleranceHighSpeed;
132 }
133
134 if(minecart.getVelocity().lengthSquared() <= highSpeedGateSquared)
135 {
136 pushSpeed = couplingPushSpeedLowSpeed;
137 }
138 else
139 {
140 pushSpeed = couplingPushSpeedHighSpeed;
141 }
142
143 if((distanceSquared > maxDistanceSquared) || distanceSquared < minDistanceSquared)
144 {
145 linkageManager.removeLink(minecart);
146 minecart.setVelocity(new Vector(0, 0, 0));
147 parent.setVelocity(new Vector(0, 0, 0));
148 return;
149 }
150
151 double distance = Math.sqrt(distanceSquared);
152 double aimedLow = aimedDistance - aimedDistanceTolerance;
153 double aimedHigh = aimedDistance + aimedDistanceTolerance;
154 if (distance >= aimedLow && distance <= aimedHigh
155 && minecart.getVelocity().lengthSquared() < 0.0001
156 && parent.getVelocity().lengthSquared() < 0.0001)
157 {
158 return;
159 }
160
161 Entity follower = minecart;
162 Entity anchor = parent;
163 Minecart leader = linkageManager.findLeader(minecart);
164 if (leader != null && linkageManager.isCloserToLeader(minecart, (Minecart) parent, leader))
165 {
166 follower = parent;
167 anchor = minecart;
168 }
169 if (leader != null && follower.getUniqueId().equals(leader.getUniqueId()))
170 {
171 Entity swap = follower;
172 follower = anchor;
173 anchor = swap;
174 }
175
176 if (distance < aimedLow)
177 {
178 moveToward(follower, anchor, pushSpeed, (distance - aimedDistance));
179 }
180 else if (distance > aimedHigh)
181 {
182 moveToward(follower, anchor, pullSpeed, (distance - aimedDistance));
183 }
184 }
185
186 private void handlePoweredCart(PoweredMinecart poweredCart)
187 {
188 int fuelAfterConsumption = poweredCart.getFuel() - fuelPerTick + 1;
189 if (fuelAfterConsumption < 0)
190 {
191 fuelAfterConsumption = 0;
192 }
193 poweredCart.setFuel(fuelAfterConsumption);
194
195 FurnaceCarts.restorePushFromMotion(poweredCart);
196
197 Block block = poweredCart.getLocation().getBlock();
198 if (block.getType() != Material.POWERED_RAIL)
199 {
200 return;
201 }
202 RedstoneRail redstoneRail = (RedstoneRail) block.getBlockData();
203 if (redstoneRail.isPowered())
204 {
205 return;
206 }
207 poweredCart.setVelocity(new Vector(0, 0, 0));
208 poweredCart.setFuel(poweredCart.getFuel() + fuelPerTick);
209 }
210
211 // defines moveToward, which handles the elastic movement of dependant carts. Uses the speed and direction set earlier
212 public void moveToward(Entity child, Entity parent, double speed, double distance)
213 {
214 Location childLoc = child.getLocation();
215 Location parentLoc = parent.getLocation();
216
217 double x = childLoc.getX() - parentLoc.getX();
218 double y = childLoc.getY() - parentLoc.getY();
219 double z = childLoc.getZ() - parentLoc.getZ();
220 Vector velocity = new Vector(x, y, z).normalize().multiply(-speed);
221 child.setVelocity(velocity.multiply((distance * distance * distance)));
222 }
223
224
225 private boolean isRail(Material material)
226 {
227 return Tag.RAILS.isTagged(material);
228 }
229}