Skip to content

Commit b2d9b01

Browse files
committed
refactor: streamline spawner validation and configuration loading
1 parent 903ec02 commit b2d9b01

6 files changed

Lines changed: 60 additions & 312 deletions

File tree

core/src/main/java/github/nighter/smartspawner/commands/reload/ReloadSubCommand.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void reloadAll(CommandSender sender) {
6363
plugin.setUpHopperHandler();
6464
plugin.getItemPriceManager().reload();
6565
plugin.getEntityLootRegistry().reload();
66-
reloadSpawnerLootConfigs();
66+
plugin.getSpawnerManager().reloadSpawnerDropsAndConfigs();
6767
plugin.getLanguageManager().reloadLanguages();
6868

6969
// Recheck timer placeholders after language reload to detect GUI configuration changes
@@ -96,18 +96,4 @@ private void logCacheStats() {
9696
plugin.getLogger().info(" " + entry.getKey() + ": " + entry.getValue());
9797
}
9898
}
99-
100-
private void reloadSpawnerLootConfigs() {
101-
List<SpawnerData> allSpawners = plugin.getSpawnerManager().getAllSpawners();
102-
for (SpawnerData spawner : allSpawners) {
103-
try {
104-
spawner.loadConfigurationValues();
105-
spawner.reloadLootConfig();
106-
} catch (Exception e) {
107-
plugin.getLogger().warning("Failed to reload loot config for spawner " +
108-
spawner.getSpawnerId() + ": " + e.getMessage());
109-
}
110-
}
111-
}
112-
11399
}

core/src/main/java/github/nighter/smartspawner/hooks/economy/shops/providers/economyshopgui/ESGUICompatibilityHandler.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,6 @@ public ESGUICompatibilityHandler(SmartSpawner plugin) {
1919
public void onESGUIShopItemsLoad(ShopItemsLoadEvent event) {
2020
plugin.getItemPriceManager().reloadShopIntegration();
2121
plugin.getEntityLootRegistry().loadConfigurations();
22-
reloadSpawnerLootConfigs();
23-
}
24-
25-
private void reloadSpawnerLootConfigs() {
26-
List<SpawnerData> allSpawners = plugin.getSpawnerManager().getAllSpawners();
27-
for (SpawnerData spawner : allSpawners) {
28-
try {
29-
spawner.reloadLootConfig();
30-
} catch (Exception e) {
31-
plugin.getLogger().warning("Failed to reload loot config for spawner " +
32-
spawner.getSpawnerId() + ": " + e.getMessage());
33-
}
34-
}
22+
plugin.getSpawnerManager().reloadSpawnerDrops();
3523
}
3624
}

core/src/main/java/github/nighter/smartspawner/hooks/economy/shops/providers/shopguiplus/SpawnerHook.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,10 @@ public void onShopsPostLoad(ShopsPostLoadEvent event) {
3737
Scheduler.runTaskLater(() -> {
3838
plugin.getItemPriceManager().reloadShopIntegration();
3939
plugin.getEntityLootRegistry().loadConfigurations();
40-
reloadSpawnerLootConfigs();
40+
plugin.getSpawnerManager().reloadSpawnerDrops();
4141
}, 100L); // Run after 5 second to ensure the plugin is fully loaded
4242
}
4343

44-
private void reloadSpawnerLootConfigs() {
45-
List<SpawnerData> allSpawners = plugin.getSpawnerManager().getAllSpawners();
46-
for (SpawnerData spawner : allSpawners) {
47-
try {
48-
spawner.reloadLootConfig();
49-
} catch (Exception e) {
50-
plugin.getLogger().warning("Failed to reload loot config for spawner " +
51-
spawner.getSpawnerId() + ": " + e.getMessage());
52-
}
53-
}
54-
}
55-
5644
public void unregister() {
5745
ShopGUIPlusPostEnableEvent.getHandlerList().unregister(this);
5846
ShopsPostLoadEvent.getHandlerList().unregister(this);

core/src/main/java/github/nighter/smartspawner/spawner/gui/storage/filter/FilterConfigUI.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ public void onPlayerQuit(PlayerQuitEvent event) {
9292
* Opens the filter configuration GUI for a player and spawner
9393
*/
9494
public void openFilterConfigGUI(Player player, SpawnerData spawner) {
95-
// Validate that the spawner still exists before opening the GUI
96-
if (plugin.getSpawnerManager().isGhostSpawner(spawner)) {
97-
// Spawner no longer exists, do not open the GUI
98-
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 0.8f, 0.5f);
99-
return;
100-
}
101-
10295
// Create a new inventory with title from language manager
10396
String title = languageManager.getGuiTitle("gui_title_filter_config");
10497
Inventory filterInventory = Bukkit.createInventory(
@@ -229,15 +222,6 @@ public void onFilterInventoryClick(InventoryClickEvent event) {
229222
}
230223

231224
SpawnerData spawner = holder.getSpawnerData();
232-
233-
// Validate that the spawner still exists before processing any actions
234-
if (plugin.getSpawnerManager().isGhostSpawner(spawner)) {
235-
// Spawner no longer exists, close the inventory
236-
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 0.8f, 0.5f);
237-
player.closeInventory();
238-
return;
239-
}
240-
241225
int slot = event.getRawSlot();
242226

243227
// Handle divider clicks (return to storage)

core/src/main/java/github/nighter/smartspawner/spawner/properties/SpawnerData.java

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -114,43 +114,19 @@ private void initializeDefaults() {
114114
this.spawnerStop = true;
115115
this.isAtCapacity = false;
116116
this.stackSize = 1;
117-
this.maxStackSize = plugin.getConfig().getInt("spawner_properties.default.max_stack_size", 1000);
118-
this.maxStoragePages = plugin.getConfig().getInt("spawner_properties.default.max_storage_pages", 1);
119-
this.maxSpawnerLootSlots = 45;
120-
this.lootConfig = lootRegistry.getLootConfig(entityType);
121-
// Initialize lastSpawnTime to current time to prevent timer display issues
122117
this.lastSpawnTime = System.currentTimeMillis();
123118
}
124119

125120
public void loadConfigurationValues() {
126-
int baseMaxStoredExp = plugin.getConfig().getInt("spawner_properties.default.max_stored_exp", 1000);
127-
int baseMinMobs = plugin.getConfig().getInt("spawner_properties.default.min_mobs", 1);
128-
int baseMaxMobs = plugin.getConfig().getInt("spawner_properties.default.max_mobs", 4);
129-
long baseSpawnerDelay = plugin.getTimeFromConfig("spawner_properties.default.delay", "25s");
130-
131-
validateAndSetStoragePages();
132-
calculateScaledValues(baseMaxStoredExp, baseMinMobs, baseMaxMobs);
133-
setSpawnDelay(baseSpawnerDelay);
134-
setSpawnerRange();
135-
}
136-
137-
private void validateAndSetStoragePages() {
138-
if (maxStoragePages <= 0) {
139-
logger.warning("Invalid max_storage_pages value. Setting to default: 1");
140-
maxStoragePages = 1;
141-
}
142-
this.maxSpawnerLootSlots = Math.max((45 * maxStoragePages) * stackSize, 45);
143-
}
144-
145-
private void calculateScaledValues(int baseMaxStoredExp, int baseMinMobs, int baseMaxMobs) {
146-
this.maxStoredExp = Math.max(baseMaxStoredExp * stackSize, baseMaxStoredExp);
147-
this.minMobs = Math.max(baseMinMobs * stackSize, baseMinMobs);
148-
this.maxMobs = Math.max(baseMaxMobs * stackSize, this.minMobs + stackSize);
149-
150-
if (this.maxMobs <= this.minMobs) {
151-
logger.warning("Invalid max_mobs value after scaling. Adjusting to: " + (this.minMobs + stackSize));
152-
this.maxMobs = this.minMobs + stackSize;
153-
}
121+
this.maxStackSize = plugin.getConfig().getInt("spawner_properties.default.max_stack_size", 1000);
122+
this.maxStoragePages = plugin.getConfig().getInt("spawner_properties.default.max_storage_pages", 1);
123+
this.maxSpawnerLootSlots = maxStoragePages * 45;
124+
this.maxStoredExp = plugin.getConfig().getInt("spawner_properties.default.max_stored_exp", 1000);
125+
this.minMobs = plugin.getConfig().getInt("spawner_properties.default.min_mobs", 1);
126+
this.maxMobs = plugin.getConfig().getInt("spawner_properties.default.max_mobs", 4);
127+
this.spawnDelay = plugin.getTimeFromConfig("spawner_properties.default.delay", "25s");
128+
this.spawnerRange = plugin.getConfig().getInt("spawner_properties.default.range", 16);
129+
this.lootConfig = lootRegistry.getLootConfig(entityType);
154130
}
155131

156132
public void setSpawnDelay(long baseSpawnerDelay) {
@@ -160,14 +136,6 @@ public void setSpawnDelay(long baseSpawnerDelay) {
160136
}
161137
}
162138

163-
private void setSpawnerRange() {
164-
this.spawnerRange = plugin.getConfig().getInt("spawner_properties.default.range");
165-
if (this.spawnerRange <= 0) {
166-
logger.warning("Invalid range value. Setting to default: 16");
167-
this.spawnerRange = 16;
168-
}
169-
}
170-
171139
private void initializeComponents() {
172140
this.virtualInventory = new VirtualInventory(maxSpawnerLootSlots);
173141

@@ -191,6 +159,25 @@ public void setStackSize(int stackSize) {
191159
}
192160
}
193161

162+
private void validateAndSetStoragePages() {
163+
if (maxStoragePages <= 0) {
164+
logger.warning("Invalid max_storage_pages value. Setting to default: 1");
165+
maxStoragePages = 1;
166+
}
167+
this.maxSpawnerLootSlots = Math.max((45 * maxStoragePages) * stackSize, 45);
168+
}
169+
170+
private void calculateScaledValues(int baseMaxStoredExp, int baseMinMobs, int baseMaxMobs) {
171+
this.maxStoredExp = Math.max(baseMaxStoredExp * stackSize, baseMaxStoredExp);
172+
this.minMobs = Math.max(baseMinMobs * stackSize, baseMinMobs);
173+
this.maxMobs = Math.max(baseMaxMobs * stackSize, this.minMobs + stackSize);
174+
175+
if (this.maxMobs <= this.minMobs) {
176+
logger.warning("Invalid max_mobs value after scaling. Adjusting to: " + (this.minMobs + stackSize));
177+
this.maxMobs = this.minMobs + stackSize;
178+
}
179+
}
180+
194181
private void updateStackSize(int newStackSize) {
195182
if (newStackSize <= 0) {
196183
this.stackSize = 1;
@@ -211,7 +198,9 @@ private void updateStackSize(int newStackSize) {
211198
transferItemsToNewInventory(currentItems, newInventory);
212199

213200
this.stackSize = newStackSize;
214-
loadConfigurationValues();
201+
// Recalculate dependent values
202+
validateAndSetStoragePages();
203+
calculateScaledValues(getMaxStoredExp(), getMinMobs(), getMaxMobs());
215204
this.spawnerExp = Math.min(this.spawnerExp, this.maxStoredExp);
216205
this.lastSpawnTime = System.currentTimeMillis();
217206
this.virtualInventory = newInventory;
@@ -337,7 +326,7 @@ public int getEntityExperienceValue() {
337326
return lootConfig != null ? lootConfig.getExperience() : 0;
338327
}
339328

340-
public void reloadLootConfig() {
329+
public void setLootConfig() {
341330
this.lootConfig = lootRegistry.getLootConfig(entityType);
342331
}
343332

@@ -361,11 +350,7 @@ public void markInteracted() {
361350
public void clearInteracted() {
362351
interacted.compareAndSet(true, false);
363352
}
364-
365-
/**
366-
* Updates the last interacted player and marks the spawner as modified for saving
367-
* @param playerName The name of the player who interacted with the spawner
368-
*/
353+
369354
public void updateLastInteractedPlayer(String playerName) {
370355
this.lastInteractedPlayer = playerName;
371356
markInteracted();

0 commit comments

Comments
 (0)