Files
src
build.gradle
settings.gradle
Code
45 4,650
src/main/java/com/kodari/mworth/SellWorthPlugin.java
package com.kodari.mworth;

import com.kodari.mworth.command.CommandManager;
import com.kodari.mworth.config.ConfigurationManager;
import com.kodari.mworth.config.GuiConfigurationManager;
import com.kodari.mworth.data.PendingItemManager;
import com.kodari.mworth.data.PlayerDataManager;
import com.kodari.mworth.display.InventoryPriceDisplay;
import com.kodari.mworth.display.PacketInventoryPriceDisplay;
import com.kodari.mworth.economy.EconomyManager;
import com.kodari.mworth.gui.GuiManager;
import com.kodari.mworth.message.MessageManager;
import com.kodari.mworth.multiplier.MultiplierManager;
import com.kodari.mworth.placeholder.PlaceholderManager;
import com.kodari.mworth.price.CategoryManager;
import com.kodari.mworth.price.PriceManager;
import com.kodari.mworth.sale.SellManager;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Properties;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public final class SellWorthPlugin extends JavaPlugin {
    private File dataDirectory;
    private ConfigurationManager configurationManager;
    private GuiConfigurationManager guiConfigurationManager;
    private MessageManager messageManager;
    private CategoryManager categoryManager;
    private PriceManager priceManager;
    private PlayerDataManager playerDataManager;
    private PendingItemManager pendingItemManager;
    private MultiplierManager multiplierManager;
    private EconomyManager economyManager;
    private SellManager sellManager;
    private GuiManager guiManager;
    private InventoryPriceDisplay inventoryPriceDisplay;
    private PlaceholderManager placeholderManager;
    private int autoSaveTask = -1;

    @Override
    public void onEnable() {
        getLogger().info("Mworth v" + getDescription().getVersion() + " | Derleme: " + getBuildIdentifier());
        dataDirectory = getDataFolder();
        if (!migrateLegacyDataDirectory()) {
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }
        if (!dataDirectory.exists() && !dataDirectory.mkdirs()) {
            getLogger().severe("Mworth veri klasörü oluşturulamadı.");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }

        configurationManager = new ConfigurationManager(this, dataDirectory);
        guiConfigurationManager = new GuiConfigurationManager(this, dataDirectory);
        messageManager = new MessageManager(this, dataDirectory);
        if (!configurationManager.reload() || !messageManager.reload() || !guiConfigurationManager.reload()) {
            getLogger().severe("Mworth gerekli yapılandırma dosyalarını yükleyemedi.");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }
        ensureResource("players.yml");
        ensureResource("pending.yml");
        ensureResource("worthitem.yml");

        categoryManager = new CategoryManager(configurationManager);
        priceManager = new PriceManager(this, dataDirectory, categoryManager, messageManager);
        priceManager.migrateLegacyFiles();
        for (String category : CategoryManager.CATEGORY_KEYS) {
            ensureResource("price/" + category + ".yml");
        }
        ensureResource("worthitem.yml");
        priceManager.reload();
        if (!configurationManager.get().getString("storage.type", "yaml").equalsIgnoreCase("yaml")) {
            messageManager.console(java.util.logging.Level.WARNING, "console.storage-type", java.util.Map.of(
                    "type", configurationManager.get().getString("storage.type", "yaml")));
        }
        playerDataManager = new PlayerDataManager(this, dataDirectory, configurationManager, messageManager);
        pendingItemManager = new PendingItemManager(this, dataDirectory, messageManager);
        multiplierManager = new MultiplierManager(configurationManager);
        economyManager = new EconomyManager(this, configurationManager, messageManager);
        economyManager.refresh();
        sellManager = new SellManager(priceManager, categoryManager, playerDataManager, multiplierManager,
                economyManager, configurationManager, messageManager);
        guiManager = new GuiManager(this, configurationManager, guiConfigurationManager, messageManager, priceManager, categoryManager,
                playerDataManager, pendingItemManager, multiplierManager, economyManager, sellManager);
        Bukkit.getPluginManager().registerEvents(guiManager, this);
        if (Bukkit.getPluginManager().isPluginEnabled("packetevents")) {
            try {
                inventoryPriceDisplay = new PacketInventoryPriceDisplay(this, priceManager, playerDataManager,
                        sellManager, economyManager);
            } catch (LinkageError | RuntimeException exception) {
                inventoryPriceDisplay = null;
                getLogger().warning("Envanter fiyat gösterimi başlatılamadı; diğer Mworth özellikleri çalışmaya devam edecek. "
                        + "PacketEvents 2.11.1+ uyumlu sürümünü kontrol edin. ("
                        + exception.getClass().getSimpleName() + ")");
            }
        } else {
            getLogger().warning("Envanter fiyat gösterimi için PacketEvents 2.11.1+ yüklenmelidir; /worth menüsü çalışmaya devam eder.");
        }

        CommandManager commandManager = new CommandManager(this, configurationManager, messageManager,
                playerDataManager, priceManager, categoryManager, multiplierManager, economyManager,
                sellManager, guiManager, inventoryPriceDisplay);
        Objects.requireNonNull(getCommand("worth")).setExecutor(commandManager);
        Objects.requireNonNull(getCommand("sat")).setExecutor(commandManager);
        Objects.requireNonNull(getCommand("worthtoggle")).setExecutor(commandManager);
        Objects.requireNonNull(getCommand("mworth")).setExecutor(commandManager);
        Objects.requireNonNull(getCommand("sellworth")).setExecutor(commandManager);

        placeholderManager = new PlaceholderManager(this, playerDataManager, categoryManager, priceManager,
                multiplierManager, messageManager);
        boolean papiAvailable = placeholderManager.registerIfAvailable();
        scheduleAutoSave();
        messageManager.console(java.util.logging.Level.INFO, "console.startup", java.util.Map.of(
                "version", getDescription().getVersion(),
                "categories", Integer.toString(categoryManager.getCategories(priceManager.getCategories()).size()),
                "prices", Integer.toString(priceManager.getPriceCount()),
                "vault", economyManager.isAvailable() ? "available" : "unavailable",
                "papi", papiAvailable ? "available" : "unavailable"));
        if (configurationManager.get().getBoolean("debug", false)) {
            messageManager.console(java.util.logging.Level.INFO, "console.debug-entry", java.util.Map.of(
                    "prices", Integer.toString(priceManager.getPriceCount()),
                    "categories", Integer.toString(priceManager.getCategories().size())));
        }
    }

    @Override
    public void onDisable() {
        if (autoSaveTask != -1) {
            Bukkit.getScheduler().cancelTask(autoSaveTask);
        }
        if (guiManager != null) {
            guiManager.shutdownRestoreAll();
        }
        if (inventoryPriceDisplay != null) {
            inventoryPriceDisplay.shutdown();
        }
        if (placeholderManager != null) {
            placeholderManager.unregister();
        }
        if (playerDataManager != null) {
            playerDataManager.shutdownAndSave();
        }
        if (pendingItemManager != null) {
            pendingItemManager.shutdownAndSave();
        }
    }

    private void scheduleAutoSave() {
        if (autoSaveTask != -1) {
            Bukkit.getScheduler().cancelTask(autoSaveTask);
        }
        long seconds = Math.max(1L, configurationManager.get().getLong("performance.autosave-interval-seconds", 300L));
        autoSaveTask = Bukkit.getScheduler().runTaskTimer(this, () -> playerDataManager.saveAsync(),
                seconds * 20L, seconds * 20L).getTaskId();
    }

    public boolean reloadPlugin() {
        if (guiManager != null) {
            guiManager.shutdownRestoreAll();
        }
        if (!configurationManager.reload() || !messageManager.reload() || !guiConfigurationManager.reload()) {
            return false;
        }
        categoryManager.reload();
        priceManager.migrateLegacyFiles();
        for (String category : CategoryManager.CATEGORY_KEYS) {
            ensureResource("price/" + category + ".yml");
        }
        priceManager.reload();
        multiplierManager.reload();
        economyManager.refresh();
        scheduleAutoSave();
        return true;
    }

    private void ensureResource(String path) {
        File target = new File(dataDirectory, path);
        if (target.exists()) {
            return;
        }
        File parent = target.getParentFile();
        if (parent != null && !parent.exists() && !parent.mkdirs()) {
            getLogger().severe(path + " için klasör oluşturulamadı.");
            return;
        }
        try (InputStream input = getResource(path)) {
            if (input == null) {
                getLogger().severe("Eklenti paketinde " + path + " kaynağı bulunamadı.");
                return;
            }
            Files.copy(input, target.toPath());
        } catch (IOException exception) {
            getLogger().severe(path + " oluşturulamadı: " + exception.getMessage());