No chat to show

This project's files are all here to explore.

Files
src
pom.xml
Code
24 2,202
src/main/java/com/soulshard/bounty/BountyPlugin.java
package com.soulshard.bounty;

import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

public final class BountyPlugin extends JavaPlugin {
    private Msg msg;
    private Database db;
    private BountyService service;
    private Economy economy;
    private static final String[] FILES = {"gui/main.yml", "gui/confirm.yml", "gui/sign.yml", "dialogs/search.yml", "dialogs/add.yml"};
    private final Map<String, FileConfiguration> files = new ConcurrentHashMap<>();

    @Override
    public void onEnable() {
        saveDefaultConfig();
        if (!new File(getDataFolder(), "messages.yml").exists()) saveResource("messages.yml", false);
        msg = new Msg(this);
        msg.load();
        loadFiles();

        db = new Database(this);
        try {
            db.init();
        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Could not open the database, disabling.", e);
            getServer().getPluginManager().disablePlugin(this);
            return;
        }

        service = new BountyService(this);
        getServer().getPluginManager().registerEvents(new Listeners(this), this);
        getServer().getPluginManager().registerEvents(new GuiListener(), this);
        registerCommand(getConfig().getStringList("settings.commands"), List.of("bounty", "bounties"), false);
        registerCommand(getConfig().getStringList("settings.dialog-commands"), List.of("dialogbounty", "dialogbounties"), true);

        if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
            new Placeholders(this).register();
        }
        service.start();
        for (Player p : Bukkit.getOnlinePlayers()) service.onJoin(p);
    }

    @Override
    public void onDisable() {
        if (service != null) service.stop();
        if (db != null) db.close();
    }

    private void registerCommand(List<String> names, List<String> fallback, boolean dialog) {
        List<String> n = names.isEmpty() ? fallback : names;
        BountyCommand exec = new BountyCommand(this, dialog);
        Command cmd = new Command(n.get(0), "Bounty command", "/" + n.get(0), new ArrayList<>(n.subList(1, n.size()))) {
            @Override
            public boolean execute(CommandSender sender, String label, String[] args) {
                return exec.onCommand(sender, this, label, args);
            }

            @Override
            public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
                return exec.onTabComplete(sender, this, alias, args);
            }
        };
        getServer().getCommandMap().register("soulbounty", cmd);
    }

    public void reload() {
        reloadConfig();
        msg.load();
        loadFiles();
        service.refreshCache();
    }

    /** gui/ and dialogs/ folders: created from the jar on first start, missing keys fall back to the jar defaults. */
    private void loadFiles() {
        for (String path : FILES) {
            File f = new File(getDataFolder(), path);
            if (!f.exists()) saveResource(path, false);
            YamlConfiguration y = YamlConfiguration.loadConfiguration(f);
            InputStream in = getResource(path);
            if (in != null) y.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(in, StandardCharsets.UTF_8)));
            files.put(path, y);
        }
    }

    public FileConfiguration gui(String name) {
        return files.get("gui/" + name + ".yml");
    }

    public FileConfiguration dialog(String name) {
        return files.get("dialogs/" + name + ".yml");
    }

    public Economy economy() {
        if (economy == null) {
            RegisteredServiceProvider<Economy> r = getServer().getServicesManager().getRegistration(Economy.class);
            if (r != null) economy = r.getProvider();
        }
        return economy;
    }

    public Msg msg() {
        return msg;
    }

    public Database db() {
        return db;
    }

    public BountyService service() {
        return service;
    }
}