BlueGit

timfa minecart-trains

Files

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

LinkageManager.java

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