No chat to show

This project's files are all here to explore.

Files
src
pom.xml
README.md
Code
22 4,989
src/main/java/com/maxpro/maxshopgui/MaxShopGUI.java
package com.maxpro.maxshopgui;

import com.maxpro.maxshopgui.command.AdminCommand;
import com.maxpro.maxshopgui.command.BalanceCommand;
import com.maxpro.maxshopgui.command.PayCommand;
import com.maxpro.maxshopgui.command.SellCommand;
import com.maxpro.maxshopgui.command.SpawnerGiveCommand;
import com.maxpro.maxshopgui.command.ShopCommand;
import com.maxpro.maxshopgui.economy.EconomyManager;
import com.maxpro.maxshopgui.gui.GUIListener;
import com.maxpro.maxshopgui.gui.FarmingFixListener;
import com.maxpro.maxshopgui.shop.ShopManager;
import com.maxpro.maxshopgui.spawner.MoneySpawnerManager;
import com.maxpro.maxshopgui.spawner.SpawnerListener;
import com.maxpro.maxshopgui.spawner.SpawnerLootManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;

public final class MaxShopGUI extends JavaPlugin {
    private EconomyManager economy;
    private ShopManager shop;
    private MoneySpawnerManager spawners;
    private SpawnerLootManager spawnerLoot;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        mergeMissingDefaults();
        ensureShopFile();
        economy = new EconomyManager(this);
        shop = new ShopManager(this);
        spawnerLoot = new SpawnerLootManager(this);
        spawners = new MoneySpawnerManager(this);

        getCommand("shop").setExecutor(new ShopCommand(this));
        getCommand("sell").setExecutor(new SellCommand(this));
        getCommand("balance").setExecutor(new BalanceCommand(this));
        getCommand("pay").setExecutor(new PayCommand(this));
        getCommand("maxshop").setExecutor(new AdminCommand(this));
        getCommand("give").setExecutor(new SpawnerGiveCommand(this));
        getCommand("give").setTabCompleter(new SpawnerGiveCommand(this));

        getServer().getPluginManager().registerEvents(new GUIListener(this), this);
        getServer().getPluginManager().registerEvents(new FarmingFixListener(this), this);
        getServer().getPluginManager().registerEvents(new SpawnerListener(this), this);
        getLogger().info("MaxShopGUI 1.6.8 enabled.");
    }

    private void ensureShopFile() {
        if (!getDataFolder().exists()) getDataFolder().mkdirs();
        java.io.File file = new java.io.File(getDataFolder(), "shop.yml");
        YamlConfiguration bundled = new YamlConfiguration();
        try (java.io.InputStream in = getResource("shop.yml")) {
            if (in == null) {
                getLogger().warning("Bundled shop.yml is missing.");
                return;
            }
            bundled.load(new java.io.InputStreamReader(in, java.nio.charset.StandardCharsets.UTF_8));
        } catch (Exception ex) {
            getLogger().severe("Could not load bundled shop.yml: " + ex.getMessage());
            return;
        }

        YamlConfiguration shopFile;
        if (!file.exists()) {
            shopFile = bundled;
            getLogger().info("Created fresh shop.yml from the bundled shop catalogue.");
        } else {
            shopFile = YamlConfiguration.loadConfiguration(file);
            mergeCategoryIfMissingOrBroken(shopFile, bundled, "farming", 51);
            mergeCategoryIfMissingOrBroken(shopFile, bundled, "spawners", 11);
        }
        try {
            shopFile.save(file);
        } catch (java.io.IOException ex) {
            getLogger().severe("Could not save shop.yml: " + ex.getMessage());
        }
    }

    private void mergeCategoryIfMissingOrBroken(YamlConfiguration target, YamlConfiguration bundled, String category, int minimumItems) {
        String path = "categories." + category;
        ConfigurationSection source = bundled.getConfigurationSection(path);
        ConfigurationSection existing = target.getConfigurationSection(path);
        int existingItems = existing == null ? 0 : target.getConfigurationSection(path + ".items") == null ? 0 : target.getConfigurationSection(path + ".items").getKeys(false).size();
        if (source != null && (existing == null || existingItems < minimumItems)) {
            target.set(path, source);
            getLogger().info("Repaired " + category + " shop category using the fresh bundled catalogue.");
        }
    }

    /** Adds new shop/spawner settings from the bundled config without deleting existing server prices. */
    private void mergeMissingDefaults() {
        if (getResource("config.yml") == null) return;
        YamlConfiguration defaults = YamlConfiguration.loadConfiguration(
                new java.io.InputStreamReader(getResource("config.yml"), java.nio.charset.StandardCharsets.UTF_8));
        mergeSection(defaults, defaults.getKeys(false), "");
        saveConfig();
    }

    private void mergeSection(YamlConfiguration defaults, java.util.Set<String> keys, String path) {
        for (String key : keys) {
            String child = path.isEmpty() ? key : path + "." + key;
            ConfigurationSection childSection = defaults.getConfigurationSection(child);
            if (childSection != null) {
                mergeSection(defaults, childSection.getKeys(false), child);
            } else if (!getConfig().contains(child)) {
                getConfig().set(child, defaults.get(child));
            }
        }
    }

    @Override
    public void onDisable() {
        if (spawners != null) spawners.stop();
        if (spawnerLoot != null) spawnerLoot.save();
        if (economy != null) economy.save();
    }

    public EconomyManager economy() { return economy; }
    public ShopManager shop() { return shop; }
    public MoneySpawnerManager spawners() { return spawners; }
    public SpawnerLootManager spawnerLoot() { return spawnerLoot; }
}