Skip to content

Commit 76e988a

Browse files
committed
refactor: streamline data lock acquisition and cleanup logic in SpawnerLootGenerator and SpawnerRangeChecker
1 parent 337508b commit 76e988a

2 files changed

Lines changed: 24 additions & 57 deletions

File tree

core/src/main/java/github/nighter/smartspawner/spawner/lootgen/SpawnerLootGenerator.java

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,21 @@ public void spawnLootToSpawner(SpawnerData spawner) {
4141
try {
4242
// Acquire dataLock to safely read spawn timing and configuration values
4343
// Use tryLock with short timeout to avoid blocking
44-
boolean dataLockAcquired = false;
4544
try {
46-
dataLockAcquired = spawner.getDataLock().tryLock(50, java.util.concurrent.TimeUnit.MILLISECONDS);
45+
if (!spawner.getDataLock().tryLock(50, java.util.concurrent.TimeUnit.MILLISECONDS)) {
46+
// dataLock is held (likely stack size change), skip this cycle
47+
return;
48+
}
4749
} catch (InterruptedException e) {
4850
Thread.currentThread().interrupt();
4951
return;
5052
}
51-
52-
if (!dataLockAcquired) {
53-
// dataLock is held (likely stack size change), skip this cycle
54-
return;
55-
}
56-
53+
5754
// Declare variables outside the try block so they're accessible in the async lambda
5855
final long currentTime = System.currentTimeMillis();
5956
final long spawnTime;
60-
final EntityType entityType;
6157
final int minMobs;
6258
final int maxMobs;
63-
final String spawnerId;
6459
final AtomicInteger usedSlots;
6560
final AtomicInteger maxSlots;
6661

@@ -81,10 +76,8 @@ public void spawnLootToSpawner(SpawnerData spawner) {
8176
}
8277

8378
// Important: Store the current values we need for async processing
84-
entityType = spawner.getEntityType();
8579
minMobs = spawner.getMinMobs();
8680
maxMobs = spawner.getMaxMobs();
87-
spawnerId = spawner.getSpawnerId();
8881
// Store currentTime to update lastSpawnTime after successful loot addition
8982
spawnTime = currentTime;
9083
} finally {
@@ -289,7 +282,7 @@ private List<ItemStack> limitItemsToAvailableSlots(List<ItemStack> items, Spawne
289282
int remainingSlots = maxSlots - calculateSlots(simulatedInventory);
290283
if (remainingSlots > 0) {
291284
// Maximum items we can add in the remaining slots
292-
long maxAddAmount = remainingSlots * maxStackSize - (currentAmount % maxStackSize);
285+
long maxAddAmount = (long) remainingSlots * maxStackSize - (currentAmount % maxStackSize);
293286
if (maxAddAmount > 0) {
294287
// Create a partial item
295288
ItemStack partialItem = item.clone();
@@ -348,11 +341,9 @@ private void handleGuiUpdates(SpawnerData spawner) {
348341
Location loc = spawner.getSpawnerLocation();
349342
World world = loc.getWorld();
350343
if (world != null) {
351-
Scheduler.runLocationTask(loc, () -> {
352-
world.spawnParticle(Particle.HAPPY_VILLAGER,
353-
loc.clone().add(0.5, 0.5, 0.5),
354-
10, 0.3, 0.3, 0.3, 0);
355-
});
344+
Scheduler.runLocationTask(loc, () -> world.spawnParticle(Particle.HAPPY_VILLAGER,
345+
loc.clone().add(0.5, 0.5, 0.5),
346+
10, 0.3, 0.3, 0.3, 0));
356347
}
357348
}
358349

@@ -385,19 +376,16 @@ public void preGenerateLoot(SpawnerData spawner, LootGenerationCallback callback
385376
}
386377

387378
try {
388-
boolean dataLockAcquired = false;
389379
try {
390-
dataLockAcquired = spawner.getDataLock().tryLock(50, java.util.concurrent.TimeUnit.MILLISECONDS);
380+
if (!spawner.getDataLock().tryLock(50, java.util.concurrent.TimeUnit.MILLISECONDS)) {
381+
callback.onLootGenerated(Collections.emptyList(), 0);
382+
return;
383+
}
391384
} catch (InterruptedException e) {
392385
Thread.currentThread().interrupt();
393386
callback.onLootGenerated(Collections.emptyList(), 0);
394387
return;
395388
}
396-
397-
if (!dataLockAcquired) {
398-
callback.onLootGenerated(Collections.emptyList(), 0);
399-
return;
400-
}
401389

402390
final int minMobs;
403391
final int maxMobs;
@@ -465,17 +453,14 @@ public void addPreGeneratedLoot(SpawnerData spawner, List<ItemStack> items, int
465453
}
466454

467455
try {
468-
boolean dataLockAcquired = false;
469456
try {
470-
dataLockAcquired = spawner.getDataLock().tryLock(50, java.util.concurrent.TimeUnit.MILLISECONDS);
457+
if (!spawner.getDataLock().tryLock(50, java.util.concurrent.TimeUnit.MILLISECONDS)) {
458+
return;
459+
}
471460
} catch (InterruptedException e) {
472461
Thread.currentThread().interrupt();
473462
return;
474463
}
475-
476-
if (!dataLockAcquired) {
477-
return;
478-
}
479464

480465
final long spawnTime = System.currentTimeMillis();
481466
final boolean capacityCheck;

core/src/main/java/github/nighter/smartspawner/spawner/lootgen/SpawnerRangeChecker.java

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,18 @@
1010
import org.bukkit.entity.Player;
1111

1212
import java.util.*;
13-
import java.util.concurrent.ConcurrentHashMap;
1413
import java.util.concurrent.ExecutorService;
1514
import java.util.concurrent.Executors;
1615

1716
public class SpawnerRangeChecker {
1817
private static final long CHECK_INTERVAL = 20L; // 1 second in ticks
1918
private final SmartSpawner plugin;
2019
private final SpawnerManager spawnerManager;
21-
private final SpawnerLootGenerator spawnerLootGenerator;
22-
private final Map<String, Scheduler.Task> spawnerTasks;
2320
private final ExecutorService executor;
2421

2522
public SpawnerRangeChecker(SmartSpawner plugin) {
2623
this.plugin = plugin;
2724
this.spawnerManager = plugin.getSpawnerManager();
28-
this.spawnerLootGenerator = plugin.getSpawnerLootGenerator();
29-
this.spawnerTasks = new ConcurrentHashMap<>();
3025
this.executor = Executors.newSingleThreadExecutor(r -> new Thread(r, "SmartSpawner-RangeCheck"));
3126
initializeRangeCheckTask();
3227
}
@@ -107,9 +102,10 @@ private boolean isSpawnerValid(SpawnerData spawner) {
107102
}
108103

109104
private void cleanupRemovedSpawner(String spawnerId) {
110-
Scheduler.Task task = spawnerTasks.remove(spawnerId);
111-
if (task != null) {
112-
task.cancel();
105+
// Clear any pre-generated loot when spawner is removed
106+
SpawnerData spawner = spawnerManager.getSpawnerById(spawnerId);
107+
if (spawner != null) {
108+
spawner.clearPreGeneratedLoot();
113109
}
114110
}
115111

@@ -129,7 +125,7 @@ private void handleSpawnerStateChange(SpawnerData spawner, boolean shouldStop) {
129125
public void activateSpawner(SpawnerData spawner) {
130126
deactivateSpawner(spawner);
131127

132-
// Check if spawner is actually active before starting countdown
128+
// Check if spawner is actually active before starting
133129
if (!spawner.getSpawnerActive()) {
134130
return;
135131
}
@@ -138,33 +134,18 @@ public void activateSpawner(SpawnerData spawner) {
138134
long currentTime = System.currentTimeMillis();
139135
spawner.setLastSpawnTime(currentTime);
140136

141-
// Timer manages the spawner active state - actual loot spawning triggered by SpawnerGuiViewManager
142-
Scheduler.Task task = Scheduler.runTaskTimer(() -> {
143-
// Verify spawner is still active on each tick
144-
if (!spawner.getSpawnerActive() || spawner.getSpawnerStop().get()) {
145-
return;
146-
}
147-
}, spawner.getSpawnDelay(), spawner.getSpawnDelay());
148-
149-
spawnerTasks.put(spawner.getSpawnerId(), task);
150-
151137
// Immediately update any open GUIs to show the countdown
152138
if (plugin.getSpawnerGuiViewManager().hasViewers(spawner)) {
153139
plugin.getSpawnerGuiViewManager().updateSpawnerMenuViewers(spawner);
154140
}
155141
}
156142

157143
public void deactivateSpawner(SpawnerData spawner) {
158-
Scheduler.Task task = spawnerTasks.remove(spawner.getSpawnerId());
159-
if (task != null) {
160-
task.cancel();
161-
}
144+
// Clear any pre-generated loot when deactivating
145+
spawner.clearPreGeneratedLoot();
162146
}
163147

164148
public void cleanup() {
165-
spawnerTasks.values().forEach(Scheduler.Task::cancel);
166-
spawnerTasks.clear();
167-
168149
executor.shutdown();
169150
try {
170151
if (!executor.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS)) {
@@ -176,3 +157,4 @@ public void cleanup() {
176157
}
177158
}
178159
}
160+

0 commit comments

Comments
 (0)