Files
src
build.gradle
manifest.kod
Code
50 4,255
src/main/java/com/example/coinflip/CoinflipPlugin.java
package com.example.coinflip;
import com.example.coinflip.api.CoinflipAPI;
import com.example.coinflip.command.CoinflipCommand;
import com.example.coinflip.command.subcommands.*;
import com.example.coinflip.config.ConfigManager;
import com.example.coinflip.config.MessageManager;
import com.example.coinflip.database.DatabaseManager;
import com.example.coinflip.database.repository.CoinflipRepository;
import com.example.coinflip.database.repository.PlayerStatsRepository;
import com.example.coinflip.inventory.gui.GUIListener;
import com.example.coinflip.inventory.gui.GUIManager;
import com.example.coinflip.listener.PlayerListener;
import com.example.coinflip.manager.CoinflipManager;
import com.example.coinflip.manager.CooldownManager;
import com.example.coinflip.manager.EconomyManager;
import com.example.coinflip.service.CoinflipService;
import com.example.coinflip.service.ValidationService;
import com.example.coinflip.task.ExpirationTask;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public class CoinflipPlugin extends JavaPlugin {
private ConfigManager configManager;
private MessageManager messageManager;
private DatabaseManager databaseManager;
private EconomyManager economyManager;
private CoinflipManager coinflipManager;
private CooldownManager cooldownManager;
private GUIManager guiManager;
private CoinflipRepository coinflipRepository;
private PlayerStatsRepository statsRepository;
private ValidationService validationService;
private CoinflipService coinflipService;
private CoinflipAPI api;
private CreateSubcommand createSubcommand;
private JoinSubcommand joinSubcommand;
private CancelSubcommand cancelSubcommand;
private HistorySubcommand historySubcommand;
private StatsSubcommand statsSubcommand;
private AdminSubcommand adminSubcommand;
private ReloadSubcommand reloadSubcommand;
@Override
public void onEnable() {
this.configManager = new ConfigManager(this);
this.messageManager = new MessageManager(this);
this.databaseManager = new DatabaseManager(this, this.configManager);
this.databaseManager.connect();
this.economyManager = new EconomyManager(this);
if (this.configManager.isEconomyEnabled()) {
if (!this.economyManager.setupEconomy()) {
if (this.configManager.isVaultRequired()) {
this.getLogger().severe("Vault not found! Disabling plugin.");
this.getServer().getPluginManager().disablePlugin(this);
return;
}
this.getLogger().warning("Vault not found! Economy features disabled.");
}
}
this.coinflipManager = new CoinflipManager();
this.cooldownManager = new CooldownManager();
this.guiManager = new GUIManager();
this.coinflipRepository = new CoinflipRepository(this.databaseManager);
this.statsRepository = new PlayerStatsRepository(this.databaseManager);
this.validationService = new ValidationService(
this.configManager,
this.economyManager,
this.coinflipManager,
this.cooldownManager
);
this.coinflipService = new CoinflipService(
this,
this.configManager,
this.messageManager,
this.economyManager,
this.coinflipManager,
this.cooldownManager,
this.coinflipRepository,
this.statsRepository
);
this.api = new CoinflipAPI(this.coinflipManager, this.statsRepository);
this.loadActiveCoinflips();
this.createSubcommand = new CreateSubcommand(this, this.messageManager);
this.joinSubcommand = new JoinSubcommand(this, this.messageManager);
this.cancelSubcommand = new CancelSubcommand(this, this.messageManager, this.coinflipService);
this.historySubcommand = new HistorySubcommand(this, this.messageManager);
this.statsSubcommand = new StatsSubcommand(this, this.messageManager);
this.adminSubcommand = new AdminSubcommand(this, this.messageManager);
this.reloadSubcommand = new ReloadSubcommand(this, this.configManager, this.messageManager);
CoinflipCommand coinflipCommand = new CoinflipCommand(this, this.messageManager);
this.getCommand("coinflip").setExecutor(coinflipCommand);
this.getCommand("coinflip").setTabCompleter(coinflipCommand);
Bukkit.getPluginManager().registerEvents(new GUIListener(this.guiManager), this);
Bukkit.getPluginManager().registerEvents(new PlayerListener(this.configManager, this.messageManager, this.coinflipManager), this);
if (this.configManager.isExpirationEnabled()) {
int intervalTicks = this.configManager.getExpirationCheckInterval() * 20;
Bukkit.getScheduler().runTaskTimer(this, new ExpirationTask(this.coinflipManager, this.coinflipService), intervalTicks, intervalTicks);
}
this.getLogger().info("CoinflipPlugin enabled successfully!");
}
@Override
public void onDisable() {
if (this.databaseManager != null) {
this.databaseManager.disconnect();
}
this.getLogger().info("CoinflipPlugin disabled successfully!");
}
private void loadActiveCoinflips() {
this.coinflipRepository.getActiveCoinflips().thenAccept(coinflips -> {
Bukkit.getScheduler().runTask(this, () -> {
for (var coinflip : coinflips) {
if (coinflip.isExpired()) {
coinflip.setStatus(com.example.coinflip.model.Coinflip.CoinflipStatus.CANCELLED);
this.coinflipRepository.updateCoinflip(coinflip);
} else {
this.coinflipManager.addCoinflip(coinflip);
}
}
this.getLogger().info("Loaded " + coinflips.size() + " active coinflips from database.");
});
});
}
public ConfigManager getConfigManager() {
return this.configManager;
}
public MessageManager getMessageManager() {
return this.messageManager;
}
public DatabaseManager getDatabaseManager() {
return this.databaseManager;
}
public EconomyManager getEconomyManager() {
return this.economyManager;
}
public CoinflipManager getCoinflipManager() {
return this.coinflipManager;
}
public CooldownManager getCooldownManager() {
return this.cooldownManager;
}
public GUIManager getGuiManager() {
return this.guiManager;
}
public CoinflipRepository getCoinflipRepository() {
return this.coinflipRepository;
}
public PlayerStatsRepository getStatsRepository() {
return this.statsRepository;
}
public ValidationService getValidationService() {
return this.validationService;
}
public CoinflipService getCoinflipService() {
return this.coinflipService;
}
public CoinflipAPI getAPI() {
return this.api;
}
public CreateSubcommand getCreateSubcommand() {
return this.createSubcommand;
}
public JoinSubcommand getJoinSubcommand() {
return this.joinSubcommand;
}
public CancelSubcommand getCancelSubcommand() {