BlueGit

timfa minecart-trains

Files

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

LinkageManager.java

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