Files
src
build.gradle
manifest.kod
settings.gradle
Code
36 1,899
src/main/java/dev/xd/auction/XDAuction.java
package dev.xd.auction;
import dev.xd.auction.command.AuctionCommand;
import dev.xd.auction.discord.DiscordWebhookService;
import dev.xd.auction.economy.EconomyService;
import dev.xd.auction.economy.VaultEconomyService;
import dev.xd.auction.gui.GUIListener;
import dev.xd.auction.gui.GUIManager;
import dev.xd.auction.listener.PlayerListener;
import dev.xd.auction.placeholder.AuctionPlaceholders;
import dev.xd.auction.service.AuctionService;
import dev.xd.auction.service.BlacklistService;
import dev.xd.auction.storage.AuctionRepository;
import dev.xd.auction.storage.DatabaseManager;
import dev.xd.auction.util.TaskScheduler;
import lombok.Getter;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Getter
public final class XDAuction extends JavaPlugin {
private DatabaseManager databaseManager;
private AuctionRepository auctionRepository;
private AuctionService auctionService;
private GUIManager guiManager;
private DiscordWebhookService discordWebhookService;
private ScheduledExecutorService maintenanceExecutor;
@Override
public void onEnable() {
saveDefaultConfig();
saveResource("messages.yml", false);
saveResource("menus.yml", false);
saveResource("categories.yml", false);
saveResource("database.yml", false);
saveResource("blacklist.yml", false);
saveResource("sounds.yml", false);
saveResource("discord.yml", false);
EconomyService economy = setupEconomy();
if (economy == null) {
getLogger().severe("Vault economy was not found. xDAuction has been disabled.");
getServer().getPluginManager().disablePlugin(this);
return;
}
databaseManager = new DatabaseManager(this);
auctionRepository = new AuctionRepository(databaseManager);
guiManager = new GUIManager();
discordWebhookService = new DiscordWebhookService(this);
auctionService = new AuctionService(this, auctionRepository, economy, new BlacklistService(this));
AuctionCommand command = new AuctionCommand(this, auctionService, guiManager);
if (getCommand("ah") != null) {
getCommand("ah").setExecutor(command);
getCommand("ah").setTabCompleter(command);
}
getServer().getPluginManager().registerEvents(new GUIListener(guiManager), this);
getServer().getPluginManager().registerEvents(new PlayerListener(this, auctionService), this);
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new AuctionPlaceholders(this).register();
}
maintenanceExecutor = Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread thread = new Thread(runnable, "xDAuction-Maintenance");
thread.setDaemon(true);
return thread;
});
long interval = Math.max(1, getConfig().getLong("cleanup.interval-minutes", 10));
TaskScheduler.repeat(maintenanceExecutor, () -> auctionService.expire(), interval * 60_000L, interval * 60_000L);
getLogger().info("xDAuction enabled.");
}
private EconomyService setupEconomy() {
if (!Bukkit.getPluginManager().isPluginEnabled("Vault")) {
return null;
}
RegisteredServiceProvider<Economy> registration =
getServer().getServicesManager().getRegistration(Economy.class);
if (registration == null || registration.getProvider() == null) {
return null;
}
return new VaultEconomyService(registration.getProvider(), getConfig().getString("currency.name", "$"));
}
@Override
public void onDisable() {
if (maintenanceExecutor != null) {
maintenanceExecutor.shutdownNow();
}
if (discordWebhookService != null) {
discordWebhookService.close();
}
if (databaseManager != null) {
databaseManager.close();
}
}
}