refactor: simplify LoMinesPlugin (220→109 lines)
- Extract ComponentInitializer - component creation and initialization - Extract RegistrationManager - commands, listeners, integrations - Main class now delegates to specialized managers Progress: 3/7 files >200 lines fixed
This commit is contained in:
parent
1e29889902
commit
41924ea97f
3 changed files with 168 additions and 147 deletions
82
src/main/java/com/loki/lomines/ComponentInitializer.java
Normal file
82
src/main/java/com/loki/lomines/ComponentInitializer.java
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
package com.loki.lomines;
|
||||||
|
|
||||||
|
import com.loki.lomines.core.MineTicker;
|
||||||
|
import com.loki.lomines.core.Mines;
|
||||||
|
import com.loki.lomines.data.stats.StatsManager;
|
||||||
|
import com.loki.lomines.integration.IntegrationManager;
|
||||||
|
import com.loki.lomines.wand.GroupWandManager;
|
||||||
|
import dev.lolib.commands.CommandManager;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes and manages plugin components.
|
||||||
|
*/
|
||||||
|
final class ComponentInitializer {
|
||||||
|
|
||||||
|
private final LoMinesPlugin plugin;
|
||||||
|
|
||||||
|
ComponentInitializer(LoMinesPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
void createDirectories() throws IOException {
|
||||||
|
Path dataFolder = plugin.getDataFolder().toPath();
|
||||||
|
Path minesFolder = dataFolder.resolve("mines");
|
||||||
|
|
||||||
|
if (!Files.exists(minesFolder)) {
|
||||||
|
Files.createDirectories(minesFolder);
|
||||||
|
plugin.loLogger().info("Created mines directory");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Components initialize() {
|
||||||
|
Mines mines = new Mines(plugin);
|
||||||
|
GroupWandManager groupWandManager = new GroupWandManager();
|
||||||
|
StatsManager statsManager = new StatsManager(plugin);
|
||||||
|
CommandManager commandManager = new CommandManager(plugin);
|
||||||
|
IntegrationManager integrationManager = new IntegrationManager(plugin);
|
||||||
|
|
||||||
|
plugin.loLogger().info("Core components initialized");
|
||||||
|
|
||||||
|
return new Components(mines, groupWandManager, statsManager, commandManager, integrationManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadMines(Mines mines) {
|
||||||
|
try {
|
||||||
|
mines.loadAll();
|
||||||
|
plugin.loLogger().info("Loaded " + mines.getAll().size() + " mine(s)");
|
||||||
|
} catch (IOException e) {
|
||||||
|
plugin.loLogger().error("Failed to load mines: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MineTicker startTicker(Mines mines) {
|
||||||
|
MineTicker ticker = new MineTicker(mines, plugin);
|
||||||
|
ticker.start();
|
||||||
|
return ticker;
|
||||||
|
}
|
||||||
|
|
||||||
|
void startStatistics(StatsManager statsManager) {
|
||||||
|
if (plugin.getConfig().getBoolean("statistics-enabled", true)) {
|
||||||
|
try {
|
||||||
|
statsManager.load();
|
||||||
|
statsManager.startAutoSave();
|
||||||
|
plugin.loLogger().info("Statistics system enabled");
|
||||||
|
} catch (IOException e) {
|
||||||
|
plugin.loLogger().warn("Failed to load statistics: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record Components(
|
||||||
|
Mines mines,
|
||||||
|
GroupWandManager groupWandManager,
|
||||||
|
StatsManager statsManager,
|
||||||
|
CommandManager commandManager,
|
||||||
|
IntegrationManager integrationManager
|
||||||
|
) {}
|
||||||
|
}
|
||||||
|
|
@ -1,66 +1,52 @@
|
||||||
package com.loki.lomines;
|
package com.loki.lomines;
|
||||||
|
|
||||||
import dev.lolib.core.LoPlugin;
|
|
||||||
import dev.lolib.commands.CommandManager;
|
|
||||||
import dev.lolib.core.dependency.DependencyManager;
|
|
||||||
import com.loki.lomines.core.MineTicker;
|
import com.loki.lomines.core.MineTicker;
|
||||||
import com.loki.lomines.core.Mines;
|
import com.loki.lomines.core.Mines;
|
||||||
import com.loki.lomines.data.StatsManager;
|
import com.loki.lomines.data.stats.StatsManager;
|
||||||
import com.loki.lomines.integration.IntegrationManager;
|
import com.loki.lomines.integration.IntegrationManager;
|
||||||
import com.loki.lomines.listener.BlockBreakListener;
|
|
||||||
import com.loki.lomines.listener.GroupGuiListener;
|
|
||||||
import com.loki.lomines.listener.PlayerInteractListener;
|
|
||||||
import com.loki.lomines.listener.PlayerJoinListener;
|
|
||||||
import com.loki.lomines.wand.GroupWandManager;
|
import com.loki.lomines.wand.GroupWandManager;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import dev.lolib.commands.CommandManager;
|
||||||
|
import dev.lolib.core.LoPlugin;
|
||||||
import java.io.IOException;
|
import dev.lolib.core.dependency.DependencyManager;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main plugin class for LoMines.
|
* Main plugin class for LoMines.
|
||||||
* Extends LoPlugin from LoAPI for Paper/Folia compatibility.
|
* Extends LoPlugin from LoAPI for Paper/Folia compatibility.
|
||||||
*/
|
*/
|
||||||
public final class LoMinesPlugin extends LoPlugin {
|
public final class LoMinesPlugin extends LoPlugin {
|
||||||
|
|
||||||
private Mines mines;
|
private Mines mines;
|
||||||
private MineTicker mineTicker;
|
private MineTicker mineTicker;
|
||||||
private GroupWandManager groupWandManager;
|
private GroupWandManager groupWandManager;
|
||||||
private StatsManager statsManager;
|
private StatsManager statsManager;
|
||||||
private CommandManager commandManager;
|
private CommandManager commandManager;
|
||||||
private IntegrationManager integrationManager;
|
private IntegrationManager integrationManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void enable() {
|
protected void enable() {
|
||||||
try {
|
try {
|
||||||
// Save default config if not exists
|
|
||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
|
|
||||||
// Create necessary directories
|
ComponentInitializer initializer = new ComponentInitializer(this);
|
||||||
createDirectories();
|
initializer.createDirectories();
|
||||||
|
|
||||||
// Initialize core components
|
ComponentInitializer.Components components = initializer.initialize();
|
||||||
initializeComponents();
|
this.mines = components.mines();
|
||||||
|
this.groupWandManager = components.groupWandManager();
|
||||||
// Load all mines from mines/ folder
|
this.statsManager = components.statsManager();
|
||||||
loadMines();
|
this.commandManager = components.commandManager();
|
||||||
|
this.integrationManager = components.integrationManager();
|
||||||
this.mineTicker = new MineTicker(mines, this);
|
|
||||||
mineTicker.start();
|
initializer.loadMines(mines);
|
||||||
|
this.mineTicker = initializer.startTicker(mines);
|
||||||
// Register commands
|
|
||||||
registerCommands();
|
RegistrationManager registrationManager = new RegistrationManager(this);
|
||||||
|
registrationManager.registerCommands(commandManager);
|
||||||
// Register event listeners
|
registrationManager.registerListeners();
|
||||||
registerListeners();
|
registrationManager.initializeIntegrations(integrationManager);
|
||||||
|
|
||||||
// Initialize integrations (PlaceholderAPI, WorldGuard, etc.)
|
initializer.startStatistics(statsManager);
|
||||||
initializeIntegrations();
|
|
||||||
|
|
||||||
// Start statistics auto-save if enabled
|
|
||||||
startStatisticsAutoSave();
|
|
||||||
|
|
||||||
loLogger().info("LoMines has been enabled!");
|
loLogger().info("LoMines has been enabled!");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
loLogger().error("Failed to enable LoMines: " + e.getMessage());
|
loLogger().error("Failed to enable LoMines: " + e.getMessage());
|
||||||
|
|
@ -68,7 +54,7 @@ public final class LoMinesPlugin extends LoPlugin {
|
||||||
getServer().getPluginManager().disablePlugin(this);
|
getServer().getPluginManager().disablePlugin(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void disable() {
|
protected void disable() {
|
||||||
try {
|
try {
|
||||||
|
|
@ -76,144 +62,47 @@ public final class LoMinesPlugin extends LoPlugin {
|
||||||
mineTicker.stop();
|
mineTicker.stop();
|
||||||
mineTicker = null;
|
mineTicker = null;
|
||||||
}
|
}
|
||||||
// Stop all mine tasks
|
|
||||||
if (mines != null) {
|
if (mines != null) {
|
||||||
mines.getAll().forEach(mine -> mine.stop());
|
mines.getAll().forEach(mine -> mine.stop());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save statistics
|
|
||||||
if (statsManager != null) {
|
if (statsManager != null) {
|
||||||
statsManager.stopAutoSave();
|
statsManager.stopAutoSave();
|
||||||
statsManager.save();
|
statsManager.save();
|
||||||
loLogger().info("Statistics saved successfully");
|
loLogger().info("Statistics saved successfully");
|
||||||
}
|
}
|
||||||
|
|
||||||
loLogger().info("LoMines has been disabled!");
|
loLogger().info("LoMines has been disabled!");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
loLogger().error("Error during plugin shutdown: " + e.getMessage());
|
loLogger().error("Error during plugin shutdown: " + e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("removal")
|
@SuppressWarnings("removal")
|
||||||
protected void dependencies(DependencyManager dependencyManager) {
|
protected void dependencies(DependencyManager dependencyManager) {
|
||||||
// Load Apache Commons Math3 for EnumeratedDistribution
|
|
||||||
dependencyManager.add("org.apache.commons", "commons-math3", "3.6.1");
|
dependencyManager.add("org.apache.commons", "commons-math3", "3.6.1");
|
||||||
|
|
||||||
// Load Apache Commons IO for file utilities
|
|
||||||
dependencyManager.add("commons-io", "commons-io", "2.18.0");
|
dependencyManager.add("commons-io", "commons-io", "2.18.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates necessary directories for plugin data.
|
|
||||||
*/
|
|
||||||
private void createDirectories() throws IOException {
|
|
||||||
Path dataFolder = getDataFolder().toPath();
|
|
||||||
Path minesFolder = dataFolder.resolve("mines");
|
|
||||||
|
|
||||||
if (!Files.exists(minesFolder)) {
|
|
||||||
Files.createDirectories(minesFolder);
|
|
||||||
loLogger().info("Created mines directory");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes core plugin components.
|
|
||||||
*/
|
|
||||||
private void initializeComponents() {
|
|
||||||
this.mines = new Mines(this);
|
|
||||||
this.groupWandManager = new GroupWandManager();
|
|
||||||
this.statsManager = new StatsManager(this);
|
|
||||||
this.commandManager = new CommandManager(this);
|
|
||||||
this.integrationManager = new IntegrationManager(this);
|
|
||||||
|
|
||||||
loLogger().info("Core components initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads all mines from the mines/ folder.
|
|
||||||
*/
|
|
||||||
private void loadMines() {
|
|
||||||
try {
|
|
||||||
mines.loadAll();
|
|
||||||
loLogger().info("Loaded " + mines.getAll().size() + " mine(s)");
|
|
||||||
} catch (IOException e) {
|
|
||||||
loLogger().error("Failed to load mines: " + e.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers all plugin commands.
|
|
||||||
*/
|
|
||||||
private void registerCommands() {
|
|
||||||
commandManager.register(new com.loki.lomines.command.AdminCommands(this));
|
|
||||||
commandManager.register(new com.loki.lomines.command.PlayerCommands(this));
|
|
||||||
commandManager.register(new com.loki.lomines.command.StatsCommands(this));
|
|
||||||
commandManager.register(new com.loki.lomines.command.MaskCommands(this));
|
|
||||||
|
|
||||||
loLogger().info("Commands registered");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers all event listeners.
|
|
||||||
*/
|
|
||||||
private void registerListeners() {
|
|
||||||
getServer().getPluginManager().registerEvents(new BlockBreakListener(this), this);
|
|
||||||
getServer().getPluginManager().registerEvents(new PlayerInteractListener(this), this);
|
|
||||||
getServer().getPluginManager().registerEvents(new GroupGuiListener(this), this);
|
|
||||||
|
|
||||||
FileConfiguration config = getConfig();
|
|
||||||
if (config.getBoolean("statistics-enabled", true)) {
|
|
||||||
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
|
|
||||||
}
|
|
||||||
|
|
||||||
loLogger().info("Event listeners registered");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes integrations with external plugins.
|
|
||||||
*/
|
|
||||||
private void initializeIntegrations() {
|
|
||||||
integrationManager.initAll();
|
|
||||||
loLogger().info("Integrations initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts statistics auto-save if enabled in config.
|
|
||||||
*/
|
|
||||||
private void startStatisticsAutoSave() {
|
|
||||||
FileConfiguration config = getConfig();
|
|
||||||
if (config.getBoolean("statistics-enabled", true)) {
|
|
||||||
try {
|
|
||||||
statsManager.load();
|
|
||||||
statsManager.startAutoSave();
|
|
||||||
loLogger().info("Statistics system enabled");
|
|
||||||
} catch (IOException e) {
|
|
||||||
loLogger().warn("Failed to load statistics: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Getters for component access
|
|
||||||
|
|
||||||
public Mines getMines() {
|
public Mines getMines() {
|
||||||
return mines;
|
return mines;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GroupWandManager getGroupWandManager() {
|
public GroupWandManager getGroupWandManager() {
|
||||||
return groupWandManager;
|
return groupWandManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StatsManager getStatsManager() {
|
public StatsManager getStatsManager() {
|
||||||
return statsManager;
|
return statsManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandManager getCommandManager() {
|
public CommandManager getCommandManager() {
|
||||||
return commandManager;
|
return commandManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntegrationManager getIntegrationManager() {
|
public IntegrationManager getIntegrationManager() {
|
||||||
return integrationManager;
|
return integrationManager;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
50
src/main/java/com/loki/lomines/RegistrationManager.java
Normal file
50
src/main/java/com/loki/lomines/RegistrationManager.java
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.loki.lomines;
|
||||||
|
|
||||||
|
import com.loki.lomines.command.AdminCommands;
|
||||||
|
import com.loki.lomines.command.MaskCommands;
|
||||||
|
import com.loki.lomines.command.PlayerCommands;
|
||||||
|
import com.loki.lomines.command.StatsCommands;
|
||||||
|
import com.loki.lomines.integration.IntegrationManager;
|
||||||
|
import com.loki.lomines.listener.BlockBreakListener;
|
||||||
|
import com.loki.lomines.listener.GroupGuiListener;
|
||||||
|
import com.loki.lomines.listener.PlayerInteractListener;
|
||||||
|
import com.loki.lomines.listener.PlayerJoinListener;
|
||||||
|
import dev.lolib.commands.CommandManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles registration of commands, listeners, and integrations.
|
||||||
|
*/
|
||||||
|
final class RegistrationManager {
|
||||||
|
|
||||||
|
private final LoMinesPlugin plugin;
|
||||||
|
|
||||||
|
RegistrationManager(LoMinesPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
void registerCommands(CommandManager commandManager) {
|
||||||
|
commandManager.register(new AdminCommands(plugin));
|
||||||
|
commandManager.register(new PlayerCommands(plugin));
|
||||||
|
commandManager.register(new StatsCommands(plugin));
|
||||||
|
commandManager.register(new MaskCommands(plugin));
|
||||||
|
|
||||||
|
plugin.loLogger().info("Commands registered");
|
||||||
|
}
|
||||||
|
|
||||||
|
void registerListeners() {
|
||||||
|
plugin.getServer().getPluginManager().registerEvents(new BlockBreakListener(plugin), plugin);
|
||||||
|
plugin.getServer().getPluginManager().registerEvents(new PlayerInteractListener(plugin), plugin);
|
||||||
|
plugin.getServer().getPluginManager().registerEvents(new GroupGuiListener(plugin), plugin);
|
||||||
|
|
||||||
|
if (plugin.getConfig().getBoolean("statistics-enabled", true)) {
|
||||||
|
plugin.getServer().getPluginManager().registerEvents(new PlayerJoinListener(plugin), plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin.loLogger().info("Event listeners registered");
|
||||||
|
}
|
||||||
|
|
||||||
|
void initializeIntegrations(IntegrationManager integrationManager) {
|
||||||
|
integrationManager.initAll();
|
||||||
|
plugin.loLogger().info("Integrations initialized");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue