BlueGit

timfa minecart-trains

Files

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

LinkageManager.java

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