-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathWStackedSpawner.java
More file actions
476 lines (366 loc) · 15.4 KB
/
WStackedSpawner.java
File metadata and controls
476 lines (366 loc) · 15.4 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
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
package com.bgsoftware.wildstacker.objects;
import com.bgsoftware.wildstacker.api.enums.StackCheckResult;
import com.bgsoftware.wildstacker.api.enums.StackResult;
import com.bgsoftware.wildstacker.api.enums.UnstackResult;
import com.bgsoftware.wildstacker.api.objects.StackedObject;
import com.bgsoftware.wildstacker.api.objects.StackedSpawner;
import com.bgsoftware.wildstacker.api.upgrades.SpawnerUpgrade;
import com.bgsoftware.wildstacker.database.Query;
import com.bgsoftware.wildstacker.menu.SpawnersManageMenu;
import com.bgsoftware.wildstacker.utils.GeneralUtils;
import com.bgsoftware.wildstacker.utils.entity.EntityUtils;
import com.bgsoftware.wildstacker.utils.events.EventsCaller;
import com.bgsoftware.wildstacker.utils.particles.ParticleWrapper;
import com.bgsoftware.wildstacker.utils.spawners.SyncedCreatureSpawner;
import com.bgsoftware.wildstacker.utils.threads.Executor;
import com.bgsoftware.wildstacker.utils.threads.StackService;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class WStackedSpawner extends WStackedHologramObject<CreatureSpawner> implements StackedSpawner {
private SpawnersManageMenu spawnersManageMenu;
private LivingEntity linkedEntity = null;
private int spawnerUpgradeId = -1;
private EntityType cachedEntity;
private boolean isSpawnerOverridenTick;
private boolean isNatural = false;
private boolean debug = false;
public WStackedSpawner(CreatureSpawner creatureSpawner) {
this(creatureSpawner, 1);
}
public WStackedSpawner(CreatureSpawner creatureSpawner, int stackAmount) {
super(SyncedCreatureSpawner.of(creatureSpawner), stackAmount);
cachedEntity = creatureSpawner.getSpawnedType();
}
public static StackedSpawner of(Block block) {
if (block.getState() instanceof CreatureSpawner)
return of((CreatureSpawner) block.getState());
throw new IllegalArgumentException("Only spawners can be applied to StackedSpawner object");
}
public static StackedSpawner of(CreatureSpawner creatureSpawner) {
return plugin.getSystemManager().getStackedSpawner(creatureSpawner);
}
@Override
public CreatureSpawner getSpawner() {
return object;
}
@Override
public EntityType getSpawnedType() {
if (object.getSpawnedType() == null)
object.setSpawnedType(EntityType.PIG);
return Bukkit.isPrimaryThread() ? (cachedEntity = object.getSpawnedType()) : cachedEntity;
}
/*
* StackedObject's methods
*/
@Override
public LivingEntity getLinkedEntity() {
if (linkedEntity != null && (!plugin.getSettings().linkedEntitiesEnabled || linkedEntity.isDead() || !linkedEntity.isValid() ||
linkedEntity.getLocation().distanceSquared(getLocation()) > Math.pow(plugin.getSettings().linkedEntitiesMaxDistance, 2.0)))
linkedEntity = null;
return linkedEntity;
}
@Override
public void setLinkedEntity(LivingEntity linkedEntity) {
this.linkedEntity = linkedEntity;
}
@Override
public List<StackedSpawner> getNearbySpawners() {
boolean chunkMerge = plugin.getSettings().chunkMergeSpawners;
Stream<StackedSpawner> spawnerStream;
if (chunkMerge) {
spawnerStream = plugin.getSystemManager().getStackedSpawners(getChunk()).stream();
} else {
int range = getMergeRadius();
Location location = getLocation();
int maxX = location.getBlockX() + range, maxY = location.getBlockY() + range, maxZ = location.getBlockZ() + range;
int minX = location.getBlockX() - range, minY = location.getBlockY() - range, minZ = location.getBlockZ() - range;
spawnerStream = plugin.getSystemManager().getStackedSpawners().stream()
.filter(stackedSpawner -> {
Location loc = stackedSpawner.getLocation();
return loc.getBlockX() >= minX && loc.getBlockX() <= maxX &&
loc.getBlockY() >= minY && loc.getBlockY() <= maxY &&
loc.getBlockZ() >= minZ && loc.getBlockZ() <= maxZ;
});
}
return spawnerStream.filter(this::canStackIntoNoLimit).collect(Collectors.toList());
}
@Override
public ItemStack getDropItem() {
return getDropItem(getStackAmount());
}
@Override
public ItemStack getDropItem(int amount) {
return plugin.getProviders().getSpawnersProvider().getSpawnerItem(object.getSpawnedType(), amount, getUpgrade());
}
@Override
public Location getLocation() {
return object.getLocation();
}
@Override
public int getStackLimit() {
int limit = plugin.getSettings().spawnersLimits.getOrDefault(getSpawnedType(), Integer.MAX_VALUE);
return limit < 1 ? Integer.MAX_VALUE : limit;
}
@Override
public int getMergeRadius() {
int radius = plugin.getSettings().spawnersMergeRadius.getOrDefault(getSpawnedType(), 0);
return radius < 1 ? 0 : radius;
}
@Override
public boolean isBlacklisted() {
return plugin.getSettings().blacklistedSpawners.contains(getSpawnedType());
}
@Override
public boolean isWhitelisted() {
return plugin.getSettings().whitelistedSpawners.size() == 0 ||
plugin.getSettings().whitelistedSpawners.contains(getSpawnedType());
}
@Override
public boolean isWorldDisabled() {
return plugin.getSettings().spawnersDisabledWorlds.contains(object.getWorld().getName());
}
@Override
public boolean isCached() {
return plugin.getSettings().spawnersStackingEnabled && (!isDefaultUpgrade() || super.isCached());
}
public boolean isNatural() {
return isNatural;
}
public void setNatural(boolean natural) {
this.isNatural = natural;
}
@Override
public void remove() {
if (!Bukkit.isPrimaryThread()) {
Executor.sync(this::remove);
return;
}
plugin.getSystemManager().removeStackObject(this);
Query.SPAWNER_DELETE.getStatementHolder()
.setLocation(getLocation())
.execute(true);
removeHologram();
if (spawnersManageMenu != null) {
spawnersManageMenu.getInventory().getViewers().forEach(HumanEntity::closeInventory);
spawnersManageMenu.stop();
unlinkInventory();
}
}
@Override
public void updateName() {
if (!Bukkit.isPrimaryThread()) {
Executor.sync(this::updateName);
return;
}
String customName = plugin.getSettings().spawnersCustomName;
if (customName.isEmpty())
return;
int amount = getStackAmount();
if ((amount < 1 || (amount == 1 && !plugin.getSettings().spawnersUnstackedCustomName)) && isDefaultUpgrade()) {
removeHologram();
return;
}
setCachedDisplayName(EntityUtils.getFormattedType(getSpawnedType().name()));
customName = plugin.getSettings().spawnersNameBuilder.build(this);
setHologramName(customName, !plugin.getSettings().floatingSpawnerNames);
}
@Override
public StackCheckResult runStackCheck(StackedObject stackedObject) {
if (!plugin.getSettings().spawnersStackingEnabled)
return StackCheckResult.NOT_ENABLED;
return super.runStackCheck(stackedObject);
}
@Override
public Optional<CreatureSpawner> runStack() {
if (getStackLimit() <= 1)
return Optional.empty();
Chunk chunk = getChunk();
boolean chunkMerge = plugin.getSettings().chunkMergeSpawners;
Location blockLocation = getLocation();
Stream<StackedSpawner> spawnerStream;
if (chunkMerge) {
spawnerStream = plugin.getSystemManager().getStackedSpawners(chunk).stream();
} else {
int range = getMergeRadius();
if (range <= 0)
return Optional.empty();
Location location = getLocation();
int maxX = location.getBlockX() + range, maxY = location.getBlockY() + range, maxZ = location.getBlockZ() + range;
int minX = location.getBlockX() - range, minY = location.getBlockY() - range, minZ = location.getBlockZ() - range;
spawnerStream = plugin.getSystemManager().getStackedSpawners().stream()
.filter(stackedSpawner -> {
Location loc = stackedSpawner.getLocation();
return loc.getBlockX() >= minX && loc.getBlockX() <= maxX &&
loc.getBlockY() >= minY && loc.getBlockY() <= maxY &&
loc.getBlockZ() >= minZ && loc.getBlockZ() <= maxZ;
});
}
Optional<StackedSpawner> spawnerOptional = GeneralUtils.getClosest(blockLocation, spawnerStream
.filter(stackedSpawner -> runStackCheck(stackedSpawner) == StackCheckResult.SUCCESS));
if (spawnerOptional.isPresent()) {
StackedSpawner targetSpawner = spawnerOptional.get();
StackResult stackResult = runStack(targetSpawner);
if (stackResult == StackResult.SUCCESS) {
return spawnerOptional.map(StackedSpawner::getSpawner);
}
}
return Optional.empty();
}
@Override
public StackResult runStack(StackedObject stackedObject) {
if (!StackService.canStackFromThread())
return StackResult.THREAD_CATCHER;
if (runStackCheck(stackedObject) != StackCheckResult.SUCCESS)
return StackResult.NOT_SIMILAR;
StackedSpawner targetSpawner = (StackedSpawner) stackedObject;
if (!plugin.getSettings().allowBreakVanillaSpawners && targetSpawner.isNatural()) {
return StackResult.NOT_SIMILAR;
}
if (!EventsCaller.callSpawnerStackEvent(targetSpawner, this))
return StackResult.EVENT_CANCELLED;
targetSpawner.increaseStackAmount(getStackAmount(), true);
this.remove();
spawnStackParticle(true);
return StackResult.SUCCESS;
}
@Override
public UnstackResult runUnstack(int amount, Entity entity) {
if (!EventsCaller.callSpawnerUnstackEvent(this, entity, amount))
return UnstackResult.EVENT_CANCELLED;
int newStackAmount = decreaseStackAmount(amount, true);
if (newStackAmount < 1)
remove();
return UnstackResult.SUCCESS;
}
@Override
public boolean isSimilar(StackedObject stackedObject) {
if (!(stackedObject instanceof StackedSpawner))
return false;
StackedSpawner otherSpawner = (StackedSpawner) stackedObject;
EntityType otherSpawnerType = otherSpawner.getSpawnedType();
// Spawners that are similar must spawn the same entity.
if (getSpawnedType() != otherSpawnerType)
return false;
int otherUpgradeId = ((WStackedSpawner) otherSpawner).getUpgradeId();
// Spawners that are similar must have the same upgrade, or be in the default upgrade.
if (spawnerUpgradeId == otherUpgradeId)
return true;
return isDefaultUpgrade() && otherSpawner.isDefaultUpgrade();
}
/*
* UpgradeableStackedObject's methods
*/
@Override
public void spawnStackParticle(boolean checkEnabled) {
if (!checkEnabled || plugin.getSettings().spawnersParticlesEnabled) {
Location location = getLocation();
for (ParticleWrapper particleWrapper : plugin.getSettings().spawnersParticles)
particleWrapper.spawnParticle(location);
}
}
@Override
public Chunk getChunk() {
return getLocation().getChunk();
}
@Override
public World getWorld() {
return object.getWorld();
}
private boolean canStackIntoNoLimit(StackedObject stackedObject) {
StackCheckResult stackCheckResult = runStackCheck(stackedObject);
return stackCheckResult == StackCheckResult.SUCCESS || stackCheckResult == StackCheckResult.LIMIT_EXCEEDED;
}
/*
* StackedSpawner's methods
*/
@Override
public int hashCode() {
return getLocation().hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof StackedSpawner ? getLocation().equals(((StackedSpawner) obj).getLocation()) : super.equals(obj);
}
@Override
public String toString() {
return String.format("StackedSpawner{location=%s,amount=%s,type=%s}", getLocation(), getStackAmount(), getSpawnedType());
}
@Override
public SpawnerUpgrade getUpgrade() {
SpawnerUpgrade currentUpgrade = plugin.getUpgradesManager().getUpgrade(spawnerUpgradeId);
if (currentUpgrade == null) {
SpawnerUpgrade defaultUpgrade = plugin.getUpgradesManager().getDefaultUpgrade(getSpawnedType());
spawnerUpgradeId = defaultUpgrade.getId();
return defaultUpgrade;
}
return currentUpgrade;
}
@Override
public void setUpgrade(SpawnerUpgrade spawnerUpgrade) {
setUpgrade(spawnerUpgrade, null);
}
@Override
public void setUpgrade(SpawnerUpgrade spawnerUpgrade, @Nullable Player player) {
setUpgradeId(spawnerUpgrade == null ? -1 : spawnerUpgrade.getId(), player, true);
updateName();
}
@Override
public boolean isDefaultUpgrade() {
return spawnerUpgradeId == -1 || spawnerUpgradeId == plugin.getUpgradesManager().getDefaultUpgrade(getSpawnedType()).getId();
}
public int getUpgradeId() {
return spawnerUpgradeId;
}
public boolean isSpawnerOverridenTick() {
return isSpawnerOverridenTick;
}
public void setSpawnerOverridenTick(boolean spawnerOverridenTick) {
isSpawnerOverridenTick = spawnerOverridenTick;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public boolean isDebug() {
return debug;
}
public void setUpgradeId(int spawnerUpgradeId, @Nullable Player who, boolean fireEvent) {
if (!isDefaultUpgrade() && !isCached())
plugin.getDataHandler().addStackedSpawner(this);
this.spawnerUpgradeId = spawnerUpgradeId;
if (isDefaultUpgrade() && !isCached())
plugin.getDataHandler().removeStackedSpawner(this);
SpawnerUpgrade spawnerUpgrade = getUpgrade();
if (fireEvent)
EventsCaller.callSpawnerUpgradeEvent(this, spawnerUpgrade, who);
SyncedCreatureSpawner.of(object).updateSpawner(spawnerUpgrade);
if (saveData)
plugin.getSystemManager().markToBeSaved(this);
}
public LivingEntity getRawLinkedEntity() {
return linkedEntity;
}
public void linkInventory(SpawnersManageMenu spawnersManageMenu) {
this.spawnersManageMenu = spawnersManageMenu;
}
public SpawnersManageMenu getLinkedInventory() {
return spawnersManageMenu;
}
public void unlinkInventory() {
this.spawnersManageMenu = null;
}
}