-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathContactsManager.java
More file actions
290 lines (250 loc) · 11.5 KB
/
ContactsManager.java
File metadata and controls
290 lines (250 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package net.countercraft.movecraft.features.contacts;
import jakarta.inject.Inject;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.craft.*;
import net.countercraft.movecraft.craft.datatag.CraftDataTagKey;
import net.countercraft.movecraft.craft.datatag.CraftDataTagRegistry;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.events.*;
import net.countercraft.movecraft.exception.EmptyHitBoxException;
import net.countercraft.movecraft.features.contacts.events.LostContactEvent;
import net.countercraft.movecraft.features.contacts.events.NewContactEvent;
import net.countercraft.movecraft.lifecycle.Worker;
import net.countercraft.movecraft.localisation.I18nSupport;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class ContactsManager implements Listener, Worker {
private static final CraftDataTagKey<Map<Craft, Long>> RECENT_CONTACTS = CraftDataTagRegistry.INSTANCE.registerTagKey(new NamespacedKey("movecraft", "recent-contacts"), craft -> new WeakHashMap<>());
private final @NotNull CraftManager craftManager;
@Inject
public ContactsManager(@NotNull CraftManager craftManager){
this.craftManager = craftManager;
}
@Override
public boolean isAsync() {
return true;
}
@Override
public int getPeriod() {
return 20;
}
@Override
public void run() {
runContacts();
runRecentContacts();
}
private void runContacts() {
for (World w : Bukkit.getWorlds()) {
if (w == null)
continue;
Set<Craft> craftsInWorld = craftManager.getCraftsInWorld(w);
for (Craft base : craftsInWorld) {
if (base instanceof SinkingCraft || base instanceof SubCraft)
continue;
update(base, craftsInWorld);
}
}
}
private void update(@NotNull Craft base, @NotNull Set<Craft> craftsInWorld) {
List<Craft> previousContacts = base.getDataTag(Craft.CONTACTS);
if (previousContacts == null)
previousContacts = new ArrayList<>(0);
List<Craft> futureContacts = get(base, craftsInWorld);
Set<Craft> newContacts = new HashSet<>(futureContacts);
previousContacts.forEach(newContacts::remove);
for (Craft target : newContacts) {
NewContactEvent event = new NewContactEvent(base, target);
Bukkit.getServer().getPluginManager().callEvent(event);
}
Set<Craft> oldContacts = new HashSet<>(previousContacts);
futureContacts.forEach(oldContacts::remove);
for (Craft target : oldContacts) {
LostContactEvent event = new LostContactEvent(base, target);
Bukkit.getServer().getPluginManager().callEvent(event);
}
base.setDataTag(Craft.CONTACTS, futureContacts);
}
private @NotNull List<Craft> get(Craft base, @NotNull Set<Craft> craftsInWorld) {
Map<Craft, Integer> inRangeDistanceSquared = new HashMap<>();
for (Craft target : craftsInWorld) {
if (target instanceof SubCraft)
continue;
if (base instanceof PilotedCraft && target instanceof PilotedCraft
&& ((PilotedCraft) base).getPilot() == ((PilotedCraft) target).getPilot())
continue;
MovecraftLocation baseCenter;
MovecraftLocation targetCenter;
try {
baseCenter = base.getHitBox().getMidPoint();
targetCenter = target.getHitBox().getMidPoint();
}
catch (EmptyHitBoxException e) {
continue;
}
int distanceSquared = baseCenter.distanceSquared(targetCenter);
double detectionMultiplier;
if (targetCenter.getY() > 65) { // TODO: fix the water line
detectionMultiplier = (double) target.getType().getPerWorldProperty(
CraftType.PER_WORLD_DETECTION_MULTIPLIER, target.getMovecraftWorld());
}
else {
detectionMultiplier = (double) target.getType().getPerWorldProperty(
CraftType.PER_WORLD_UNDERWATER_DETECTION_MULTIPLIER, target.getMovecraftWorld());
}
int detectionRange = (int) (target.getOrigBlockCount() * detectionMultiplier);
detectionRange = detectionRange * 10;
if (distanceSquared > detectionRange)
continue;
inRangeDistanceSquared.put(target, distanceSquared);
}
List<Craft> result = new ArrayList<>(inRangeDistanceSquared.keySet().size());
result.addAll(inRangeDistanceSquared.keySet());
result.sort(Comparator.comparingInt(inRangeDistanceSquared::get));
return result;
}
private void runRecentContacts() {
for (World w : Bukkit.getWorlds()) {
if (w == null)
continue;
for (PlayerCraft base : craftManager.getPlayerCraftsInWorld(w)) {
if (base.getHitBox().isEmpty())
continue;
for (Craft target : base.getDataTag(Craft.CONTACTS)) {
// has the craft not been seen in the last minute?
if (System.currentTimeMillis() - base.getDataTag(RECENT_CONTACTS).getOrDefault(target, 0L) <= 60000)
continue;
Component message = contactMessage(false, base, target);
if (message == null)
continue;
base.getAudience().sendMessage(message);
base.getDataTag(RECENT_CONTACTS).put(target, System.currentTimeMillis());
}
}
}
}
public static @Nullable Component contactMessage(boolean isNew, @NotNull Craft base, @NotNull Craft target) {
MovecraftLocation baseCenter, targetCenter;
try {
baseCenter = base.getHitBox().getMidPoint();
targetCenter = target.getHitBox().getMidPoint();
}
catch (EmptyHitBoxException e) {
return null;
}
int diffX = baseCenter.getX() - targetCenter.getX();
int diffZ = baseCenter.getZ() - targetCenter.getZ();
int distSquared = baseCenter.distanceSquared(targetCenter);
Component notification = Component.empty();
if (isNew) {
notification = notification.append(I18nSupport.getInternationalisedComponent("Contact - New Contact").color(NamedTextColor.RED).decorate(TextDecoration.BOLD));
}
else {
notification = notification.append(I18nSupport.getInternationalisedComponent("Contact"));
}
notification = notification.append(Component.text( ": "));
Component name = Component.empty();
if (!target.getName().isEmpty()) {
name = name.append(Component.text(target.getName() + " ("));
}
name = name.append(Component.text(target.getType().getStringProperty(CraftType.NAME)));
if (!target.getName().isEmpty()) {
name = name.append(Component.text(")"));
}
if (target instanceof SinkingCraft) {
name = name.color(NamedTextColor.RED);
}
else if (target.getDisabled()) {
name = name.color(NamedTextColor.BLUE);
}
notification = notification.append(name);
notification = notification.append(Component.text(" "))
.append(I18nSupport.getInternationalisedComponent("Contact - Commanded By"))
.append(Component.text(" "));
if (target instanceof PilotedCraft) {
notification = notification.append(((PilotedCraft) target).getPilot().displayName());
}
else {
notification = notification.append(Component.text("null"));
}
notification = notification.append(Component.text(", "))
.append(I18nSupport.getInternationalisedComponent("Contact - Size"))
.append(Component.text(": "))
.append(Component.text(target.getOrigBlockCount()))
.append(Component.text(", "))
.append(I18nSupport.getInternationalisedComponent("Contact - Range"))
.append(Component.text(": "))
.append(Component.text((int) Math.sqrt(distSquared)))
.append(Component.text(" "))
.append(I18nSupport.getInternationalisedComponent("Contact - To The"))
.append(Component.text(" "));
if (Math.abs(diffX) > Math.abs(diffZ)) {
if (diffX < 0) {
notification = notification.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - East"));
}
else {
notification = notification.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - West"));
}
}
else {
if (diffZ < 0) {
notification = notification.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - South"));
} else {
notification = notification.append(I18nSupport.getInternationalisedComponent("Contact/Subcraft Rotate - North"));
}
}
notification = notification.append(Component.text("."));
return notification;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCraftRelease(@NotNull CraftReleaseEvent e) {
remove(e.getCraft());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCraftSink(@NotNull CraftSinkEvent e) {
remove(e.getCraft());
}
private void remove(Craft base) {
for (Craft other : craftManager.getCrafts()) {
List<Craft> contacts = other.getDataTag(Craft.CONTACTS);
if (contacts.contains(base))
continue;
contacts.remove(base);
other.setDataTag(Craft.CONTACTS, contacts);
}
for (Craft other : craftManager.getCrafts()) {
Map<Craft, Long> recentContacts = other.getDataTag(RECENT_CONTACTS);
if (!recentContacts.containsKey(other))
continue;
recentContacts.remove(base);
other.setDataTag(RECENT_CONTACTS, recentContacts);
}
}
@EventHandler
public void onNewContact(@NotNull NewContactEvent e) {
Craft base = e.getCraft();
Craft target = e.getTargetCraft();
Component notification = contactMessage(true, base, target);
if (notification != null)
base.getAudience().sendMessage(notification);
Object object = base.getType().getObjectProperty(CraftType.COLLISION_SOUND);
if (!(object instanceof Sound sound))
throw new IllegalStateException("COLLISION_SOUND must be of type Sound");
base.getAudience().playSound(sound);
if (base instanceof PlayerCraft) {
Map<Craft, Long> recentContacts = base.getDataTag(RECENT_CONTACTS);
recentContacts.put(target, System.currentTimeMillis());
base.setDataTag(RECENT_CONTACTS, recentContacts);
}
}
}