/
DefaultIslandsContainer.java
182 lines (143 loc) · 6.77 KB
/
DefaultIslandsContainer.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
package com.bgsoftware.superiorskyblock.island.container;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.island.SortingType;
import com.bgsoftware.superiorskyblock.api.island.container.IslandsContainer;
import com.bgsoftware.superiorskyblock.core.IslandPosition;
import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
public class DefaultIslandsContainer implements IslandsContainer {
private final Map<IslandPosition, Island> islandsByPositions = new ConcurrentHashMap<>();
private final Map<UUID, Island> islandsByUUID = new ConcurrentHashMap<>();
private final Map<SortingType, Set<Island>> sortedIslands = new ConcurrentHashMap<>();
private final SuperiorSkyblockPlugin plugin;
public DefaultIslandsContainer(SuperiorSkyblockPlugin plugin) {
this.plugin = plugin;
}
@Override
public void addIsland(Island island) {
Location islandLocation = island.getCenter(plugin.getSettings().getWorlds().getDefaultWorld());
this.islandsByPositions.put(IslandPosition.of(islandLocation), island);
if (plugin.getProviders().hasCustomWorldsSupport()) {
runWithCustomWorld(islandLocation, island, World.Environment.NORMAL,
location -> this.islandsByPositions.put(IslandPosition.of(location), island));
runWithCustomWorld(islandLocation, island, World.Environment.NETHER,
location -> this.islandsByPositions.put(IslandPosition.of(location), island));
runWithCustomWorld(islandLocation, island, World.Environment.THE_END,
location -> this.islandsByPositions.put(IslandPosition.of(location), island));
}
this.islandsByUUID.put(island.getUniqueId(), island);
sortedIslands.values().forEach(sortedIslands -> sortedIslands.add(island));
}
@Override
public void removeIsland(Island island) {
Location islandLocation = island.getCenter(plugin.getSettings().getWorlds().getDefaultWorld());
islandsByUUID.remove(island.getUniqueId());
islandsByPositions.remove(IslandPosition.of(islandLocation));
sortedIslands.values().forEach(sortedIslands -> sortedIslands.remove(island));
if (plugin.getProviders().hasCustomWorldsSupport()) {
runWithCustomWorld(islandLocation, island, World.Environment.NORMAL,
location -> islandsByPositions.remove(IslandPosition.of(location)));
runWithCustomWorld(islandLocation, island, World.Environment.NETHER,
location -> islandsByPositions.remove(IslandPosition.of(location)));
runWithCustomWorld(islandLocation, island, World.Environment.THE_END,
location -> islandsByPositions.remove(IslandPosition.of(location)));
}
}
@Nullable
@Override
public Island getIslandByUUID(UUID uuid) {
return this.islandsByUUID.get(uuid);
}
@Nullable
@Override
public Island getIslandAtPosition(int position, SortingType sortingType) {
ensureSortingType(sortingType);
Set<Island> sortedIslands = this.sortedIslands.get(sortingType);
return position < 0 || position > sortedIslands.size() ? null : Iterables.get(sortedIslands, position);
}
@Override
public int getIslandPosition(Island island, SortingType sortingType) {
ensureSortingType(sortingType);
Set<Island> sortedIslands = this.sortedIslands.get(sortingType);
return Iterables.indexOf(sortedIslands, island::equals);
}
@Override
public int getIslandsAmount() {
return this.islandsByUUID.size();
}
@Nullable
@Override
public Island getIslandAt(Location location) {
Island island = this.islandsByPositions.get(IslandPosition.of(location));
return island == null || !island.isInside(location) ? null : island;
}
@Override
public void sortIslands(SortingType sortingType, Runnable onFinish) {
this.sortIslands(sortingType, false, onFinish);
}
@Override
public void sortIslands(SortingType sortingType, boolean forceSort, Runnable onFinish) {
ensureSortingType(sortingType);
Set<Island> sortedIslands = this.sortedIslands.get(sortingType);
if (!forceSort && sortedIslands.size() <= 1) {
if (onFinish != null)
onFinish.run();
return;
}
if (Bukkit.isPrimaryThread()) {
BukkitExecutor.async(() -> sortIslandsInternal(sortingType, onFinish));
} else {
sortIslandsInternal(sortingType, onFinish);
}
}
@Override
public List<Island> getSortedIslands(SortingType sortingType) {
ensureSortingType(sortingType);
return Collections.unmodifiableList(new ArrayList<>(this.sortedIslands.get(sortingType)));
}
@Override
public List<Island> getIslandsUnsorted() {
return Collections.unmodifiableList(new ArrayList<>(this.islandsByUUID.values()));
}
@Override
public void addSortingType(SortingType sortingType, boolean sort) {
Preconditions.checkArgument(!sortedIslands.containsKey(sortingType), "You cannot register an existing sorting type to the database.");
sortIslandsInternal(sortingType, null);
}
private void runWithCustomWorld(Location islandLocation, Island island, World.Environment environment, Consumer<Location> onSuccess) {
try {
Location location = island.getCenter(environment);
if (!location.getWorld().equals(islandLocation.getWorld()))
onSuccess.accept(location);
} catch (Exception ignored) {
}
}
private void ensureSortingType(SortingType sortingType) {
Preconditions.checkState(sortedIslands.containsKey(sortingType), "The sorting-type " + sortingType + " doesn't exist in the database. Please contact author!");
}
private void sortIslandsInternal(SortingType sortingType, Runnable onFinish) {
Set<Island> newSortedTree = new TreeSet<>(sortingType);
for (Island island : islandsByUUID.values()) {
if (!island.isIgnored())
newSortedTree.add(island);
}
this.sortedIslands.put(sortingType, newSortedTree);
if (onFinish != null)
onFinish.run();
}
}