Files
src
build.gradle.kts
manifest.kod
settings.gradle.kts
Code
25 957
src/main/java/com/merchantcore/MerchantCorePlugin.java
package com.merchantcore;
import com.merchantcore.config.MerchantConfig;
import com.merchantcore.database.JdbcDatabase;
import com.merchantcore.gui.GUIListener;
import com.merchantcore.gui.GUIManager;
import com.merchantcore.integration.IntegrationManager;
import com.merchantcore.listeners.ProfileListener;
import com.merchantcore.managers.CacheManager;
import com.merchantcore.managers.SchedulerManager;
import com.merchantcore.services.ProfileService;
import com.merchantcore.services.RewardService;
import com.merchantcore.commands.MerchantCommand;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Method;
/** Main composition root. All long-lived services are owned and stopped here. */
public final class MerchantCorePlugin extends JavaPlugin {
private MerchantConfig merchantConfig;
private JdbcDatabase database;
private CacheManager cacheManager;
private ProfileService profileService;
private RewardService rewardService;
private SchedulerManager schedulerManager;
private GUIManager guiManager;
private IntegrationManager integrationManager;
private Object placeholderExpansion;
@Override
public void onEnable() {
saveDefaultConfig();
merchantConfig = new MerchantConfig(this);
cacheManager = new CacheManager();
database = new JdbcDatabase(this, merchantConfig);
profileService = new ProfileService(database, cacheManager);
rewardService = new RewardService(this, profileService, merchantConfig);
schedulerManager = new SchedulerManager(this);
guiManager = new GUIManager();
integrationManager = new IntegrationManager(this);
Bukkit.getPluginManager().registerEvents(new GUIListener(guiManager), this);
Bukkit.getPluginManager().registerEvents(new ProfileListener(this, profileService), this);
getCommand("merchant").setExecutor(new MerchantCommand(this));
integrationManager.detect();
registerPlaceholderExpansionIfAvailable();
database.start().whenComplete((ignored, error) -> Bukkit.getScheduler().runTask(this, () -> {
if (error == null) {
getLogger().info("Database connection initialized.");
schedulerManager.startAutosave(profileService, merchantConfig.autosaveSeconds());
} else {
getLogger().severe("Database initialization failed: " + error.getMessage());
}
}));
}
@Override
public void onDisable() {
if (schedulerManager != null) {
schedulerManager.cancelAll();
}
if (profileService != null) {
try {
profileService.saveAll().join();
} catch (Exception exception) {
getLogger().severe("Could not save profiles during shutdown: " + exception.getMessage());
}
}
unregisterPlaceholderExpansion();
if (database != null) {
database.close();
}
}
private void registerPlaceholderExpansionIfAvailable() {
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") == null) {
return;
}
try {
Class<?> expansionClass = Class.forName("com.merchantcore.integration.MerchantPlaceholderExpansion");
placeholderExpansion = expansionClass
.getConstructor(JavaPlugin.class, ProfileService.class)
.newInstance(this, profileService);
Method register = expansionClass.getMethod("register");
register.invoke(placeholderExpansion);
} catch (ReflectiveOperationException exception) {
getLogger().warning("PlaceholderAPI was found, but its expansion could not be registered: " + exception.getMessage());
}
}
private void unregisterPlaceholderExpansion() {
if (placeholderExpansion == null) {
return;
}
try {
placeholderExpansion.getClass().getMethod("unregister").invoke(placeholderExpansion);
} catch (ReflectiveOperationException ignored) {
// Optional integration may be unavailable during shutdown.
}
placeholderExpansion = null;
}
public MerchantConfig merchantConfig() { return merchantConfig; }
public ProfileService profileService() { return profileService; }
public RewardService rewardService() { return rewardService; }
public GUIManager guiManager() { return guiManager; }
public void reloadMerchantConfig() {
reloadConfig();
merchantConfig.reload();
}
}