BlueGit

timfa minecart-trains

Files

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

LinkageManager.java

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