BlueGit

timfa minecart-trains

Files

minecart-trains / src / main / java / me / fridtjof / minecarttrains / managers / LinkageManager.java

LinkageManager.java

LinkageManager.java 11.1 KiB Download Raw
1package me.fridtjof.minecarttrains.managers;
2
3import me.fridtjof.minecarttrains.MinecartTrains;
4import org.bukkit.Bukkit;
5import org.bukkit.NamespacedKey;
6import org.bukkit.entity.Entity;
7import org.bukkit.entity.Minecart;
8import org.bukkit.entity.minecart.PoweredMinecart;
9import org.bukkit.persistence.PersistentDataType;
10
11import java.util.ArrayList;
12import java.util.HashMap;
13import java.util.HashSet;
14import java.util.List;
15import java.util.Map;
16import java.util.Set;
17import java.util.UUID;
18
19public class LinkageManager
20{
21
22 public enum CoupleResult
23 {
24 SUCCESS,
25 SELF,
26 SAME_TRAIN,
27 NOT_AN_END,
28 INVALID
29 }
30
31 static MinecartTrains plugin = MinecartTrains.getInstance();
32
33 private final NamespacedKey key = new NamespacedKey(plugin, "coupler");
34 private final Map<UUID, UUID> childByParent = new HashMap<>();
35 private final Map<UUID, UUID> headByCart = new HashMap<>();
36 private final Map<UUID, List<Minecart>> trainByHead = new HashMap<>();
37 private final Map<UUID, Minecart> leaderByHead = new HashMap<>();
38 private int cacheTick = Integer.MIN_VALUE;
39
40 public void removeLink(Minecart cart)
41 {
42 if (hasLink(cart))
43 {
44 childByParent.remove(getLinkUniqueId(cart), cart.getUniqueId());
45 }
46 cart.getPersistentDataContainer().remove(key);
47 invalidateTrainCache();
48 }
49
50 public String getLinkString(Minecart cart)
51 {
52 return cart.getPersistentDataContainer().get(key, PersistentDataType.STRING);
53 }
54
55 public UUID getLinkUniqueId(Minecart cart)
56 {
57 return UUID.fromString(getLinkString(cart));
58 }
59
60 public void setLink(Minecart cart, String linkedCartUniqueId)
61 {
62 if (hasLink(cart))
63 {
64 childByParent.remove(getLinkUniqueId(cart), cart.getUniqueId());
65 }
66 cart.getPersistentDataContainer().set(key, PersistentDataType.STRING, linkedCartUniqueId);
67 childByParent.put(UUID.fromString(linkedCartUniqueId), cart.getUniqueId());
68 invalidateTrainCache();
69 }
70
71 public boolean hasLink(Minecart cart)
72 {
73 return cart.getPersistentDataContainer().has(key, PersistentDataType.STRING);
74 }
75
76 public Map<UUID, UUID> snapshotLinks()
77 {
78 return Map.copyOf(childByParent);
79 }
80
81 public void remember(Minecart cart)
82 {
83 if (hasLink(cart))
84 {
85 childByParent.put(getLinkUniqueId(cart), cart.getUniqueId());
86 }
87 }
88
89 public Minecart getParent(Minecart cart)
90 {
91 if (!hasLink(cart))
92 {
93 return null;
94 }
95 Entity parent = Bukkit.getEntity(getLinkUniqueId(cart));
96 return parent instanceof Minecart minecart ? minecart : null;
97 }
98
99 public Minecart getChild(Minecart parent)
100 {
101 UUID childId = childByParent.get(parent.getUniqueId());
102 if (childId != null)
103 {
104 Entity child = Bukkit.getEntity(childId);
105 if (child instanceof Minecart minecart)
106 {
107 return minecart;
108 }
109 }
110
111 for (Entity entity : parent.getNearbyEntities(24, 6, 24))
112 {
113 if (entity instanceof Minecart cart && hasLink(cart)
114 && getLinkUniqueId(cart).equals(parent.getUniqueId()))
115 {
116 childByParent.put(parent.getUniqueId(), cart.getUniqueId());
117 return cart;
118 }
119 }
120 return null;
121 }
122
123 public Minecart findHead(Minecart cart)
124 {
125 refreshTickCache();
126 UUID cachedHeadId = headByCart.get(cart.getUniqueId());
127 if (cachedHeadId != null)
128 {
129 Entity cachedHead = Bukkit.getEntity(cachedHeadId);
130 if (cachedHead instanceof Minecart minecart)
131 {
132 return minecart;
133 }
134 }
135
136 List<Minecart> walked = new ArrayList<>();
137 Minecart current = cart;
138 Set<UUID> seen = new HashSet<>();
139 while (hasLink(current) && seen.add(current.getUniqueId()))
140 {
141 walked.add(current);
142 Minecart parent = getParent(current);
143 if (parent == null)
144 {
145 break;
146 }
147 current = parent;
148 }
149
150 UUID headId = current.getUniqueId();
151 headByCart.put(headId, headId);
152 for (Minecart walkedCart : walked)
153 {
154 headByCart.put(walkedCart.getUniqueId(), headId);
155 }
156 return current;
157 }
158
159 public boolean isSameTrain(Minecart first, Minecart second)
160 {
161 return findHead(first).getUniqueId().equals(findHead(second).getUniqueId());
162 }
163
164 public List<Minecart> getTrain(Minecart cart)
165 {
166 refreshTickCache();
167 Minecart head = findHead(cart);
168 List<Minecart> cached = trainByHead.get(head.getUniqueId());
169 if (cached != null)
170 {
171 return cached;
172 }
173
174 List<Minecart> chain = new ArrayList<>();
175 Minecart current = head;
176 Set<UUID> seen = new HashSet<>();
177 while (current != null && seen.add(current.getUniqueId()))
178 {
179 chain.add(current);
180 headByCart.put(current.getUniqueId(), head.getUniqueId());
181 current = getChild(current);
182 }
183 trainByHead.put(head.getUniqueId(), chain);
184 return chain;
185 }
186
187 public CoupleResult couple(Minecart first, Minecart second)
188 {
189 remember(first);
190 remember(second);
191
192 if (first.getUniqueId().equals(second.getUniqueId()))
193 {
194 return CoupleResult.SELF;
195 }
196 if (isSameTrain(first, second))
197 {
198 return CoupleResult.SAME_TRAIN;
199 }
200
201 boolean firstIsHead = !hasLink(first);
202 boolean secondIsHead = !hasLink(second);
203 boolean firstIsTail = getChild(first) == null;
204 boolean secondIsTail = getChild(second) == null;
205
206 if (!(firstIsHead || firstIsTail) || !(secondIsHead || secondIsTail))
207 {
208 return CoupleResult.NOT_AN_END;
209 }
210
211 if (firstIsHead && firstIsTail && secondIsHead && secondIsTail)
212 {
213 Minecart parent = preferredParent(first, second);
214 Minecart child = parent.getUniqueId().equals(first.getUniqueId()) ? second : first;
215 setLink(child, parent.getUniqueId().toString());
216 return CoupleResult.SUCCESS;
217 }
218
219 if (firstIsHead && firstIsTail)
220 {
221 attachSingleToEnd(first, second, secondIsHead, secondIsTail);
222 return CoupleResult.SUCCESS;
223 }
224 if (secondIsHead && secondIsTail)
225 {
226 attachSingleToEnd(second, first, firstIsHead, firstIsTail);
227 return CoupleResult.SUCCESS;
228 }
229
230 if (firstIsTail && secondIsHead)
231 {
232 setLink(second, first.getUniqueId().toString());
233 return CoupleResult.SUCCESS;
234 }
235 if (secondIsTail && firstIsHead)
236 {
237 setLink(first, second.getUniqueId().toString());
238 return CoupleResult.SUCCESS;
239 }
240 if (firstIsHead && secondIsHead)
241 {
242 reverseTrain(first);
243 setLink(second, first.getUniqueId().toString());
244 return CoupleResult.SUCCESS;
245 }
246 if (firstIsTail && secondIsTail)
247 {
248 reverseTrain(second);
249 setLink(second, first.getUniqueId().toString());
250 return CoupleResult.SUCCESS;
251 }
252
253 return CoupleResult.INVALID;
254 }
255
256 private void attachSingleToEnd(Minecart single, Minecart end, boolean endIsHead, boolean endIsTail)
257 {
258 if (endIsTail && !endIsHead)
259 {
260 setLink(single, end.getUniqueId().toString());
261 return;
262 }
263 if (endIsHead && !endIsTail)
264 {
265 setLink(end, single.getUniqueId().toString());
266 return;
267 }
268 setLink(single, end.getUniqueId().toString());
269 }
270
271 private Minecart preferredParent(Minecart first, Minecart second)
272 {
273 if (first instanceof PoweredMinecart && !(second instanceof PoweredMinecart))
274 {
275 return first;
276 }
277 if (second instanceof PoweredMinecart && !(first instanceof PoweredMinecart))
278 {
279 return second;
280 }
281 return first;
282 }
283
284 public Minecart findLeader(Minecart cart)
285 {
286 refreshTickCache();
287 Minecart head = findHead(cart);
288 Minecart cachedLeader = leaderByHead.get(head.getUniqueId());
289 if (cachedLeader != null && cachedLeader.isValid())
290 {
291 return cachedLeader;
292 }
293
294 List<Minecart> train = getTrain(cart);
295 if (train.isEmpty())
296 {
297 return cart;
298 }
299
300 Minecart bestEngine = null;
301 int bestFuel = -1;
302 for (Minecart candidate : train)
303 {
304 if (candidate instanceof PoweredMinecart powered && powered.getFuel() > 0)
305 {
306 if (powered.getFuel() > bestFuel)
307 {
308 bestFuel = powered.getFuel();
309 bestEngine = candidate;
310 }
311 }
312 }
313 if (bestEngine != null)
314 {
315 leaderByHead.put(head.getUniqueId(), bestEngine);
316 return bestEngine;
317 }
318
319 Minecart fastest = train.getFirst();
320 double fastestSpeed = -1;
321 for (Minecart candidate : train)
322 {
323 double speed = candidate.getVelocity().lengthSquared();
324 if (speed > fastestSpeed)
325 {
326 fastestSpeed = speed;
327 fastest = candidate;
328 }
329 }
330 leaderByHead.put(head.getUniqueId(), fastest);
331 return fastest;
332 }
333
334 private void refreshTickCache()
335 {
336 int currentTick = Bukkit.getCurrentTick();
337 if (currentTick == cacheTick)
338 {
339 return;
340 }
341 cacheTick = currentTick;
342 headByCart.clear();
343 trainByHead.clear();
344 leaderByHead.clear();
345 }
346
347 private void invalidateTrainCache()
348 {
349 cacheTick = Integer.MIN_VALUE;
350 headByCart.clear();
351 trainByHead.clear();
352 leaderByHead.clear();
353 }
354
355 public boolean isCloserToLeader(Minecart candidate, Minecart other, Minecart leader)
356 {
357 if (leader == null)
358 {
359 return false;
360 }
361 if (candidate.getUniqueId().equals(leader.getUniqueId()))
362 {
363 return true;
364 }
365 if (other.getUniqueId().equals(leader.getUniqueId()))
366 {
367 return false;
368 }
369
370 for (Minecart trainCart : getTrain(leader))
371 {
372 if (trainCart.getUniqueId().equals(candidate.getUniqueId()))
373 {
374 return true;
375 }
376 if (trainCart.getUniqueId().equals(other.getUniqueId()))
377 {
378 return false;
379 }
380 }
381 return false;
382 }
383
384 public void reverseTrain(Minecart cart)
385 {
386 List<Minecart> chain = getTrain(cart);
387 for (Minecart linked : chain)
388 {
389 removeLink(linked);
390 }
391 for (int i = 0; i < chain.size() - 1; i++)
392 {
393 setLink(chain.get(i), chain.get(i + 1).getUniqueId().toString());
394 }
395 }
396}