Files
src
build.gradle
Code
45 5,391
src/main/java/com/demonlord/relicrealm/RelicRealm.java
package com.demonlord.relicrealm;

import com.demonlord.relicrealm.commands.RelicRealmCommand;
import com.demonlord.relicrealm.commands.RelicXPCommand;
import com.demonlord.relicrealm.commands.RelicsCommand;
import com.demonlord.relicrealm.inventory.gui.GUIListener;
import com.demonlord.relicrealm.inventory.gui.GUIManager;
import com.demonlord.relicrealm.listeners.*;
import com.demonlord.relicrealm.managers.*;
import com.demonlord.relicrealm.tasks.CooldownDisplayTask;
import com.demonlord.relicrealm.tasks.VillagerRelicTask;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

import java.io.File;

@Getter
public class RelicRealm extends JavaPlugin {
    
    private DataManager dataManager;
    private EconomyManager economyManager;
    private CooldownManager cooldownManager;
    private AFKManager afkManager;
    private RelicManager relicManager;
    private SlotManager slotManager;
    private GUIManager guiManager;
    
    private FileConfiguration messagesConfig;
    private FileConfiguration relicsConfig;
    private FileConfiguration slotsConfig;
    private FileConfiguration upgradesConfig;
    private FileConfiguration guiConfig;
    
    private BukkitTask villagerTask;
    private BukkitTask cooldownTask;
    
    @Override
    public void onEnable() {
        saveDefaultConfig();
        loadConfigs();
        
        this.economyManager = new EconomyManager(this);
        if (!economyManager.isEnabled()) {
            getLogger().warning("Vault or economy plugin not found! Money features will be disabled.");
        }
        
        this.dataManager = new DataManager(this);
        this.cooldownManager = new CooldownManager();
        this.afkManager = new AFKManager(this);
        this.relicManager = new RelicManager(this);
        this.slotManager = new SlotManager(this);
        this.guiManager = new GUIManager();
        
        registerListeners();
        registerCommands();
        startTasks();
        
        getLogger().info("RelicRealm has been enabled!");
    }
    
    @Override
    public void onDisable() {
        if (afkManager != null) {
            afkManager.shutdown();
        }
        if (villagerTask != null) {
            villagerTask.cancel();
        }
        if (cooldownTask != null) {
            cooldownTask.cancel();
        }
        if (dataManager != null) {
            dataManager.saveAll();
        }
        getLogger().info("RelicRealm has been disabled!");
    }
    
    private void loadConfigs() {
        messagesConfig = loadConfig("messages.yml");
        relicsConfig = loadConfig("relics.yml");
        slotsConfig = loadConfig("slots.yml");
        upgradesConfig = loadConfig("upgrades.yml");
        guiConfig = loadConfig("guiconfig.yml");
    }
    
    private FileConfiguration loadConfig(String fileName) {
        File file = new File(getDataFolder(), fileName);
        if (!file.exists()) {
            saveResource(fileName, false);
        }
        return YamlConfiguration.loadConfiguration(file);
    }
    
    public void reloadConfigs() {
        reloadConfig();
        messagesConfig = loadConfig("messages.yml");
        relicsConfig = loadConfig("relics.yml");
        slotsConfig = loadConfig("slots.yml");
        upgradesConfig = loadConfig("upgrades.yml");
        guiConfig = loadConfig("guiconfig.yml");
        
        if (relicManager != null) {
            relicManager.reload();
        }
        if (slotManager != null) {
            slotManager.reload();
        }
    }
    
    private void registerListeners() {
        Bukkit.getPluginManager().registerEvents(new GUIListener(guiManager), this);
        Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(this), this);
        Bukkit.getPluginManager().registerEvents(new PlayerInteractListener(this), this);
        Bukkit.getPluginManager().registerEvents(new PlayerMoveListener(this), this);
        Bukkit.getPluginManager().registerEvents(new EntityDamageListener(this), this);
        Bukkit.getPluginManager().registerEvents(new PlayerDeathListener(this), this);
        Bukkit.getPluginManager().registerEvents(new BlockBreakListener(this), this);
        Bukkit.getPluginManager().registerEvents(new InventoryListener(this), this);
    }
    
    private void registerCommands() {
        getCommand("relics").setExecutor(new RelicsCommand(this));
        getCommand("relicxp").setExecutor(new RelicXPCommand(this));
        getCommand("relicrealm").setExecutor(new RelicRealmCommand(this));
    }
    
    private void startTasks() {
        int villagerInterval = relicsConfig.getInt("relics.VILLAGER.levels.1.interval", 60);
        villagerTask = Bukkit.getScheduler().runTaskTimer(this, new VillagerRelicTask(this), 20L * villagerInterval, 20L * villagerInterval);
        
        cooldownTask = Bukkit.getScheduler().runTaskTimer(this, new CooldownDisplayTask(this), 20L, 20L);
    }
    
    public String getMessage(String path) {
        String prefix = messagesConfig.getString("prefix", "");
        String message = messagesConfig.getString(path, "&cMessage not found: " + path);
        return prefix + message;
    }
    
    public String getMessageRaw(String path) {
        return messagesConfig.getString(path, "&cMessage not found: " + path);
    }
}