-
Notifications
You must be signed in to change notification settings - Fork 40
/
CorePearlManager.java
360 lines (284 loc) · 9.81 KB
/
CorePearlManager.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
package com.devotedmc.ExilePearl.core;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import com.devotedmc.ExilePearl.ExilePearl;
import com.devotedmc.ExilePearl.ExilePearlApi;
import com.devotedmc.ExilePearl.PearlFactory;
import com.devotedmc.ExilePearl.PearlFreeReason;
import com.devotedmc.ExilePearl.PearlManager;
import com.devotedmc.ExilePearl.StorageProvider;
import com.devotedmc.ExilePearl.event.PearlDecayEvent;
import com.devotedmc.ExilePearl.event.PearlDecayEvent.DecayAction;
import com.devotedmc.ExilePearl.event.PlayerFreedEvent;
import com.devotedmc.ExilePearl.event.PlayerPearledEvent;
import com.devotedmc.ExilePearl.holder.BlockHolder;
import com.devotedmc.ExilePearl.holder.PearlHolder;
import com.devotedmc.ExilePearl.holder.PlayerHolder;
import vg.civcraft.mc.civmodcore.util.Guard;
/**
* The prison pearl manager implementation
* @author Gordon
*/
final class CorePearlManager implements PearlManager {
private final ExilePearlApi pearlApi;
private final PearlFactory pearlFactory;
private final StorageProvider storage;
private final Map<UUID, ExilePearl> pearls = new HashMap<UUID, ExilePearl>();
private final Map<UUID, ExilePearl> bcastRequests = new HashMap<UUID, ExilePearl>();
/**
* Creates a new PearlManager instance
* @param logger The logging instance
* @param factory The pearl factory
* @param storage The database storage provider
*/
public CorePearlManager(final ExilePearlApi pearlApi, final PearlFactory pearlFactory, final StorageProvider storage) {
Guard.ArgumentNotNull(pearlApi, "pearlApi");
Guard.ArgumentNotNull(pearlFactory, "pearlFactory");
Guard.ArgumentNotNull(storage, "storage");
this.pearlApi = pearlApi;
this.pearlFactory = pearlFactory;
this.storage = storage;
}
/**
* Loads all the pearls from the database
*/
public void loadPearls() {
pearls.clear();
for (ExilePearl p : storage.getStorage().loadAllPearls()) {
pearls.put(p.getPlayerId(), p);
}
pearlApi.log("Loaded %d pearls from storage.", pearls.size());
}
/**
* Gets the pearled players
* @return The collection of pearled players
*/
@Override
public Collection<ExilePearl> getPearls() {
return Collections.unmodifiableCollection(pearls.values().stream().filter(p -> !p.getFreedOffline()).collect(Collectors.toSet()));
}
@Override
public ExilePearl exilePlayer(final UUID exiledId, final UUID killerId, Location location) {
Guard.ArgumentNotNull(exiledId, "exiledId");
Guard.ArgumentNotNull(killerId, "killerId");
Guard.ArgumentNotNull(location, "location");
final Block block = location.getBlock();
final BlockState bs = block.getState();
if (bs == null || (!(bs instanceof InventoryHolder))) {
return null;
}
return exilePlayer(exiledId, killerId, new BlockHolder(block));
}
@Override
public ExilePearl exilePlayer(UUID exiledId, Player killer) {
Guard.ArgumentNotNull(exiledId, "exiledId");
Guard.ArgumentNotNull(killer, "killer");
if (!killer.isOnline()) {
return null;
}
return exilePlayer(exiledId, killer.getUniqueId(), new PlayerHolder(killer));
}
@Override
public ExilePearl exilePlayer(UUID exiledId, UUID killerId, PearlHolder holder) {
Guard.ArgumentNotNull(exiledId, "exiledId");
Guard.ArgumentNotNull(killerId, "killerId");
Guard.ArgumentNotNull(holder, "holder");
if (pearlApi.isPlayerExiled(exiledId)) {
return null;
}
final ExilePearl pearl = pearlFactory.createExilePearl(exiledId, killerId, createUniquePearlId(), holder);
PlayerPearledEvent e = new PlayerPearledEvent(pearl);
Bukkit.getPluginManager().callEvent(e);
if (e.isCancelled()) {
return null;
}
pearls.put(pearl.getPlayerId(), pearl);
storage.getStorage().pearlInsert(pearl);
pearl.setHealth(pearlApi.getPearlConfig().getPearlHealthStartValue());
return pearl;
}
/**
* Frees a pearl's player
* @param pearl The pearl to free
*/
@Override
public boolean freePearl(ExilePearl pearl, PearlFreeReason reason) {
Guard.ArgumentNotNull(pearl, "pearl");
Guard.ArgumentNotNull(reason, "reason");
// Don't call the event if the pearl was already freed while they were offline
if (!pearl.getFreedOffline()) {
PlayerFreedEvent e = new PlayerFreedEvent(pearl, reason);
Bukkit.getPluginManager().callEvent(e);
if (e.isCancelled()) {
return false; // The event was cancelled
}
}
Player player = pearlApi.getPlayer(pearl.getPlayerId());
// If the player is online, do the full remove, otherwise mark the pearl
// as free offline and it will be removed when they log in
if ((player != null && player.isOnline()) || reason == PearlFreeReason.FORCE_FREED_BY_ADMIN) {
pearls.remove(pearl.getPlayerId());
clearPearlBroadcasts(pearl);
storage.getStorage().pearlRemove(pearl);
} else {
pearl.setFreedOffline(true);
}
pearlApi.log("Player %s was freed for reason %s.", pearl.getPlayerName(), reason.toString());
return true;
}
@Override
public ExilePearl getPearl(String name) {
Guard.ArgumentNotNullOrEmpty(name, "name");
for(ExilePearl pearl :pearls.values()) {
if (pearl.getPlayerName().equalsIgnoreCase(name)) {
return pearl;
}
}
return null;
}
@Override
public ExilePearl getPearl(UUID uid) {
Guard.ArgumentNotNull(uid, "uid");
return pearls.get(uid);
}
@Override
public boolean isPlayerExiled(Player player) {
Guard.ArgumentNotNull(player, "player");
ExilePearl pearl = pearls.get(player.getUniqueId());
return pearl != null && !pearl.getFreedOffline();
}
@Override
public boolean isPlayerExiled(UUID uid) {
Guard.ArgumentNotNull(uid, "uid");
return pearls.get(uid) != null;
}
@Override
public ExilePearl getPearlFromItemStack(ItemStack is) {
ExilePearl pearl = null;
int pearlId = pearlApi.getLoreProvider().getPearlIdFromItemStack(is);
if (pearlId != 0) {
pearl = getPearlById(pearlId);
if (pearl == null || pearl.getFreedOffline()) {
return null;
}
return pearl;
}
// Check if this is a legacy pearl
UUID legacyId = pearlApi.getLoreProvider().getPlayerIdFromLegacyPearl(is);
if (legacyId == null) {
return null;
}
// If an existing pearl is found, just use that
pearl = pearls.get(legacyId);
if (pearl == null) {
pearlApi.log(Level.SEVERE, "Found legacy PrisonPearl item for player %s but no pearl was found.", legacyId.toString());
return null;
}
return pearl;
}
@Override
public void decayPearls() {
PearlDecayEvent e = new PearlDecayEvent(DecayAction.START);
Bukkit.getPluginManager().callEvent(e);
if (e.isCancelled()) {
return;
}
pearlApi.log("Performing pearl decay.");
long startTime = System.currentTimeMillis();
final Collection<ExilePearl> pearls = getPearls();
final int decayAmount = pearlApi.getPearlConfig().getPearlHealthDecayAmount();
final Set<String> disallowedWorlds = pearlApi.getPearlConfig().getDisallowedWorlds();
final HashSet<ExilePearl> pearlsToFree = new HashSet<ExilePearl>();
// Iterate through all the pearls and reduce the health
for (ExilePearl pearl : pearls) {
// Ignore freed offline pearls
if (pearl.getFreedOffline()) {
continue;
}
PearlHolder holder = pearl.getHolder();
if(holder != null && holder.isBlock() && disallowedWorlds.contains(holder.getLocation().getWorld().getName())) {
pearlApi.log("Freeing pearl for player %s because the pearl is stored in disallowed world %s.", pearl.getPlayerName(),holder.getLocation().getWorld().getName());
pearlsToFree.add(pearl);
}
pearl.setHealth(pearl.getHealth() - decayAmount);
if (pearl.getHealth() == 0) {
pearlApi.log("Freeing pearl for player %s because the health reached 0.", pearl.getPlayerName());
pearlsToFree.add(pearl);
}
if (!pearl.verifyLocation()) {
pearlApi.log("Freeing pearl for player %s because the verification failed.", pearl.getPlayerName());
pearlsToFree.add(pearl);
}
}
// Free the pending pearls
for (ExilePearl pearl : pearlsToFree) {
freePearl(pearl, PearlFreeReason.HEALTH_DECAY);
}
pearlApi.log("Pearl decay completed in %dms. Processed %d and freed %d." , System.currentTimeMillis() - startTime, pearls.size(), pearlsToFree.size());
e = new PearlDecayEvent(DecayAction.COMPLETE);
Bukkit.getPluginManager().callEvent(e);
}
/**
* Gets a unique pearlId
* @return The unique pearl ID
*/
private int createUniquePearlId() {
Random rand = new Random();
int pearlId = 0;
while(pearlId == 0) {
pearlId = rand.nextInt(Integer.MAX_VALUE >> 1);
if (getPearlById(pearlId) != null) {
pearlId = 0;
continue;
}
}
return pearlId;
}
private ExilePearl getPearlById(int pearlId) {
for(ExilePearl p : pearls.values()) {
if (p.getPearlId() == pearlId) {
return p;
}
}
return null;
}
@Override
public void addBroadcastRequest(Player player, ExilePearl pearl) {
bcastRequests.put(player.getUniqueId(), pearl);
}
@Override
public ExilePearl getBroadcastRequest(Player player) {
return bcastRequests.get(player.getUniqueId());
}
@Override
public void removeBroadcastRequest(Player player) {
bcastRequests.remove(player.getUniqueId());
}
private void clearPearlBroadcasts(ExilePearl pearl) {
Set<UUID> toRemove = new HashSet<UUID>();
for(Entry<UUID, ExilePearl> entry : bcastRequests.entrySet()) {
if (entry.getValue().equals(pearl)) {
toRemove.add(entry.getKey());
}
}
for(UUID uid : toRemove) {
bcastRequests.remove(uid);
}
}
}