-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
IslandCache.java
543 lines (494 loc) · 18.1 KB
/
IslandCache.java
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package world.bentobox.bentobox.managers.island;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
/**
* This class stores the islands in memory
*
* @author tastybento
*/
public class IslandCache {
/**
* Map of all islands with island uniqueId as key
*/
@NonNull
private final Map<@NonNull String, @NonNull Island> islandsById;
/**
* Every player who is associated with an island is in this map. Key is player
* UUID, value is a set of islands
*/
@NonNull
private final Map<@NonNull UUID, Set<String>> islandsByUUID;
@NonNull
private final Map<@NonNull World, @NonNull IslandGrid> grids;
private final @NonNull Database<Island> handler;
public IslandCache(@NonNull Database<Island> handler) {
islandsById = new HashMap<>();
islandsByUUID = new HashMap<>();
grids = new HashMap<>();
this.handler = handler;
}
/**
* Replace the island we have with this one
* @param newIsland island
*/
public void updateIsland(@NonNull Island newIsland) {
if (newIsland.isDeleted()) {
this.deleteIslandFromCache(newIsland);
return;
}
// Get the old island
Island oldIsland = getIslandById(newIsland.getUniqueId());
Set<UUID> newMembers = newIsland.getMembers().keySet();
if (oldIsland != null) {
Set<UUID> oldMembers = oldIsland.getMembers().keySet();
// Remove any members who are not in the new island
for (UUID oldMember : oldMembers) {
if (!newMembers.contains(oldMember)) {
// Member has been removed - remove island
islandsByUUID.computeIfAbsent(oldMember, k -> new HashSet<>()).remove(oldIsland.getUniqueId());
}
}
}
// Update the members with the new island object
for (UUID newMember : newMembers) {
Set<String> set = islandsByUUID.computeIfAbsent(newMember, k -> new HashSet<>());
if (oldIsland != null) {
set.remove(oldIsland.getUniqueId());
}
set.add(newIsland.getUniqueId());
islandsByUUID.put(newMember, set);
}
if (setIslandById(newIsland) == null) {
BentoBox.getInstance().logError("islandsById failed to update");
}
}
/**
* Adds an island to the grid, used for new islands
* Caches island.
*
* @param island island to add, not null
* @return true if successfully added, false if not
*/
public boolean addIsland(@NonNull Island island) {
return addIsland(island, false);
}
/**
* Adds an island to the grid, used for new islands
*
* @param island island to add, not null
* @param noCache - if true, island will not be cached
* @return true if successfully added, false if not
*/
public boolean addIsland(@NonNull Island island, boolean noCache) {
if (island.getCenter() == null || island.getWorld() == null) {
return false;
}
if (addToGrid(island)) {
// Insert a null into the map as a placeholder for cache
islandsById.put(island.getUniqueId().intern(), noCache ? null : island);
// Only add islands to this map if they are owned
if (island.isOwned()) {
islandsByUUID.computeIfAbsent(island.getOwner(), k -> new HashSet<>()).add(island.getUniqueId());
island.getMemberSet().forEach(member -> addPlayer(member, island));
}
return true;
}
return false;
}
/**
* Adds a player's UUID to the look up for islands. Does no checking. The island for this player must have been added beforehand.
*
* @param uuid player's uuid
* @param island island to associate with this uuid. Only one island can be
* associated per world.
*/
public void addPlayer(@NonNull UUID uuid, @NonNull Island island) {
this.islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).add(island.getUniqueId());
}
/**
* Adds an island to the grid register
*
* @param newIsland new island
* @return true if successfully added, false if not
*/
private boolean addToGrid(@NonNull Island newIsland) {
return grids.computeIfAbsent(newIsland.getWorld(), k -> new IslandGrid(this)).addToGrid(newIsland);
}
public void clear() {
islandsById.clear();
islandsByUUID.clear();
}
/**
* Deletes an island from the cache. Does not remove blocks.
*
* @param island island to delete
*/
public void deleteIslandFromCache(@NonNull Island island) {
islandsById.remove(island.getUniqueId(), island);
removeFromIslandsByUUID(island);
// Remove from grid
if (grids.containsKey(island.getWorld())) {
grids.get(island.getWorld()).removeFromGrid(island);
}
}
private void removeFromIslandsByUUID(Island island) {
for (Set<String> set : islandsByUUID.values()) {
set.removeIf(island.getUniqueId()::equals);
}
}
/**
* Delete island from the cache by ID. Does not remove blocks.
*
* @param uniqueId - island unique ID
*/
public void deleteIslandFromCache(@NonNull String uniqueId) {
if (islandsById.containsKey(uniqueId)) {
deleteIslandFromCache(getIslandById(uniqueId));
}
}
/**
* Returns island referenced by player's UUID. Returns the island the player is
* on now, or their last known island
*
* @param world world to check. Includes nether and end worlds.
* @param uuid player's UUID
* @return island or null if none
*/
@Nullable
public Island getIsland(@NonNull World world, @NonNull UUID uuid) {
List<Island> islands = getIslands(world, uuid);
if (islands.isEmpty()) {
return null;
}
for (Island island : islands) {
if (island.isPrimary(uuid)) {
return island;
}
}
// If there is no primary set, then set one - it doesn't matter which.
Island result = islands.iterator().next();
result.setPrimary(uuid);
return result;
}
/**
* Returns all the islands referenced by player's UUID.
*
* @param world world to check. Includes nether and end worlds.
* @param uuid player's UUID
* @return list of island or empty list if none sorted from oldest to youngest
*/
public List<Island> getIslands(@NonNull World world, @NonNull UUID uuid) {
World w = Util.getWorld(world);
if (w == null) {
return new ArrayList<>();
}
return islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).stream().map(this::getIslandById)
.filter(Objects::nonNull).filter(island -> w.equals(island.getWorld()))
.sorted(Comparator.comparingLong(Island::getCreatedDate))
.collect(Collectors.toList());
}
/**
* Sets the current island for the user as their primary island
*
* @param uuid UUID of user
* @param island island to make primary
*/
public void setPrimaryIsland(@NonNull UUID uuid, @NonNull Island island) {
if (island.getPrimaries().contains(uuid)) {
return;
}
for (Island is : getIslands(island.getWorld(), uuid)) {
if (is.getPrimaries().contains(uuid)) {
is.removePrimary(uuid);
}
if (is.equals(island)) {
is.setPrimary(uuid);
}
}
}
/**
* Returns the island at the location or null if there is none. This includes
* the full island space, not just the protected area
*
* @param location the location
* @return Island object
*/
@Nullable
public Island getIslandAt(@NonNull Location location) {
World w = Util.getWorld(location.getWorld());
if (w == null || !grids.containsKey(w)) {
return null;
}
return grids.get(w).getIslandAt(location.getBlockX(), location.getBlockZ());
}
/**
* Returns an <strong>unmodifiable collection</strong> of all the islands (even
* those who may be unowned). Gets them from the cache or from the database if not
* loaded. This is a very heavy operation likely to cause lag.
*
* @return unmodifiable collection containing every island.
*/
@NonNull
public Collection<Island> getIslands() {
List<Island> result = new ArrayList<>();
for (Entry<@NonNull String, @NonNull Island> entry : islandsById.entrySet()) {
Island island = entry.getValue() != null ? entry.getValue() : loadIsland(entry.getKey());
if (island != null) {
result.add(island);
}
}
return Collections.unmodifiableCollection(result);
}
/**
* Loads the island with the uniqueId from the database. Note, this could be a blocking call
* and lag the server, so be careful using it.
* @param uniqueId unique ID of the island
* @return Island or null if that uniqueID is unknown
* @since 2.4.0
*/
public Island loadIsland(String uniqueId) {
return handler.loadObject(uniqueId);
}
/**
* Returns an <strong>unmodifiable collection</strong> of all the islands (even
* those who may be unowned) that are cached.
*
* @return unmodifiable collection containing every cached island.
*/
@NonNull
public Collection<Island> getCachedIslands() {
return islandsById.entrySet().stream().filter(en -> Objects.nonNull(en.getValue())).map(Map.Entry::getValue)
.toList();
}
/**
* Returns an <strong>unmodifiable collection</strong> of all the islands (even
* those that may be unowned) in the specified world.
* Gets islands from the cache if they have been loaded, or from the database if not
*
* @param world World of the gamemode.
* @return unmodifiable collection containing all the islands in the specified
* world.
* @since 1.7.0
*/
@NonNull
public Collection<Island> getIslands(@NonNull World world) {
World overworld = Util.getWorld(world);
if (overworld == null) {
return Collections.emptyList();
}
List<Island> result = new ArrayList<>();
for (Entry<@NonNull String, @NonNull Island> entry : islandsById.entrySet()) {
Island island = entry.getValue() != null ? entry.getValue() : loadIsland(entry.getKey());
if (island != null && overworld.equals(island.getWorld())) {
result.add(island);
}
}
return Collections.unmodifiableCollection(result);
}
/**
* Checks is a player has an island and owns it in this world. Note that players
* may have multiple islands so this means the player is an owner of ANY island.
*
* @param world the world to check
* @param uuid the player
* @return true if player has an island and owns it
*/
public boolean hasIsland(@NonNull World world, @NonNull UUID uuid) {
if (!islandsByUUID.containsKey(uuid)) {
return false;
}
return this.islandsByUUID.get(uuid).stream().map(this::getIslandById).filter(Objects::nonNull)
.filter(i -> world.equals(i.getWorld()))
.anyMatch(i -> uuid.equals(i.getOwner()));
}
/**
* Removes a player from the cache. If the player has an island, the island
* owner is removed and membership cleared.
*
* @param world world
* @param uuid player's UUID
* @return set of islands player had or empty if none
*/
public Set<Island> removePlayer(@NonNull World world, @NonNull UUID uuid) {
World resolvedWorld = Util.getWorld(world);
Set<String> playerIslandIds = islandsByUUID.get(uuid);
Set<Island> removedIslands = new HashSet<>();
if (resolvedWorld == null || playerIslandIds == null) {
return Collections.emptySet(); // Return empty set if no islands map exists for the world
}
// Iterate over the player's island IDs and process each associated island
Iterator<String> iterator = playerIslandIds.iterator();
while (iterator.hasNext()) {
Island island = this.getIslandById(iterator.next());
if (island != null && resolvedWorld.equals(island.getWorld())) {
removedIslands.add(island);
if (uuid.equals(island.getOwner())) {
// Player is the owner, so clear the whole island and clear the ownership
island.getMembers().clear();
island.setOwner(null);
} else {
island.removeMember(uuid);
}
// Remove this island from the set of islands associated with this player
iterator.remove();
}
}
return removedIslands;
}
/**
* Removes player from island and removes the cache reference
*
* @param island member's island
* @param uuid uuid of member to remove
*/
public void removePlayer(@NonNull Island island, @NonNull UUID uuid) {
Set<String> islandSet = islandsByUUID.get(uuid);
if (islandSet != null) {
islandSet.remove(island.getUniqueId());
}
island.removeMember(uuid);
island.removePrimary(uuid);
}
/**
* Get the number of islands in the cache
*
* @return the number of islands
*/
public int size() {
return islandsById.size();
}
/**
* Gets the number of islands in this world
*
* @param world world to get the number of islands in
* @return the number of islands
*/
public long size(World world) {
// Get from grids because this is where we have islands by world
return this.grids.containsKey(world) ? this.grids.get(world).getSize() : 0L;
}
/**
* Sets an island owner. Clears out any other owner.
*
* @param island island
* @param newOwnerUUID new owner
*/
public void setOwner(@NonNull Island island, @Nullable UUID newOwnerUUID) {
island.setOwner(newOwnerUUID);
if (newOwnerUUID != null) {
islandsByUUID.computeIfAbsent(newOwnerUUID, k -> new HashSet<>()).add(island.getUniqueId());
}
island.setRank(newOwnerUUID, RanksManager.OWNER_RANK);
setIslandById(island);
}
/**
* Get the island by unique id
*
* @param uniqueId unique id of the Island.
* @return island or null if none found
* @since 1.3.0
*/
@Nullable
public Island getIslandById(@NonNull String uniqueId) {
// Load from cache or database
return getIslandById(uniqueId, true);
}
/**
* Get the island by unique id
*
* @param uniqueId unique id of the Island.
* @param cache if true, then the Island will be cached if it is not already
* @return island or null if none found
* @since 2.4.0
*/
@Nullable
public Island getIslandById(@NonNull String uniqueId, boolean cache) {
Island island = islandsById.get(uniqueId);
if (island != null) {
return island;
}
island = loadIsland(uniqueId);
if (cache && island != null) {
islandsById.put(uniqueId, island);
}
return island;
}
/**
* Place the island into the cache map
* @param island island
* @return the previous value associated with island, or null if this is a new entry
*/
Island setIslandById(Island island) {
return islandsById.put(island.getUniqueId().intern(), island);
}
/**
* Resets all islands in this game mode to default flag settings.
*
* @param world - world
* @since 1.3.0
*/
public void resetAllFlags(World world) {
Bukkit.getScheduler().runTaskAsynchronously(BentoBox.getInstance(),
() -> this.getIslands(world).stream().forEach(Island::setFlagsDefaults));
}
/**
* Resets a specific flag on all game mode islands in world to default setting
*
* @param world - world
* @param flag - flag to reset
* @since 1.8.0
*/
public void resetFlag(World world, Flag flag) {
int setting = BentoBox.getInstance().getIWM().getDefaultIslandFlags(world).getOrDefault(flag,
flag.getDefaultRank());
this.getIslands(world).stream().forEach(i -> i.setFlag(flag, setting));
}
/**
* Get all the island ids
*
* @return set of ids
* @since 1.8.0
*/
public Set<String> getAllIslandIds() {
return islandsById.keySet();
}
/**
* Get a unmodifiable list of all islands this player is involved with
* @param uniqueId player's UUID
* @return list of islands
*/
public @NonNull List<Island> getIslands(UUID uniqueId) {
return islandsByUUID.getOrDefault(uniqueId, Collections.emptySet()).stream().map(this::getIslandById).toList();
}
/**
* Returns if this is a known island uniqueId. Will not load the island from the database if it is not loaded already.
* @param uniqueId - unique id of island
* @return true if this island exists
*/
public boolean isIslandId(String uniqueId) {
return this.islandsById.containsKey(uniqueId);
}
}