No chat to show

This project's files are all here to explore.

Files
src
build.gradle
manifest.kod
Code
23 2,031
src/main/java/com/niloy/discordsrvlinkembed/DiscordSRVLinkingEmbed.java
package com.niloy.discordsrvlinkembed;

import com.niloy.discordsrvlinkembed.commands.ReloadCommand;
import com.niloy.discordsrvlinkembed.config.ConfigManager;
import com.niloy.discordsrvlinkembed.config.EmbedConfig;
import com.niloy.discordsrvlinkembed.discord.DiscordListener;
import com.niloy.discordsrvlinkembed.discord.DiscordSRVEventListener;
import com.niloy.discordsrvlinkembed.license.LicenseManager;
import com.niloy.discordsrvlinkembed.storage.AOFStorage;
import com.niloy.discordsrvlinkembed.storage.MySQLStorage;
import com.niloy.discordsrvlinkembed.storage.StorageManager;
import com.niloy.discordsrvlinkembed.sync.NicknameSyncManager;
import com.niloy.discordsrvlinkembed.sync.RoleSyncManager;
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.dependencies.jda.api.JDA;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

@Getter
public class DiscordSRVLinkingEmbed extends JavaPlugin {
    
    @Getter
    private static DiscordSRVLinkingEmbed instance;
    
    private ConfigManager configManager;
    private EmbedConfig embedConfig;
    private StorageManager storageManager;
    private DiscordListener discordListener;
    private DiscordSRVEventListener discordSRVEventListener;
    private NicknameSyncManager nicknameSyncManager;
    private RoleSyncManager roleSyncManager;
    
    @Override
    public void onEnable() {
        instance = this;

        log("========================================");
        log("Loading license configuration...");
        log("========================================");

        LicenseManager licenseManager = new LicenseManager(this);
        if (!licenseManager.verifyLicense()) {
            logSevere("========================================");
            logSevere("Plugin will not load until license is valid!");
            logSevere("========================================");
            Bukkit.getScheduler().runTask(this, () -> 
                Bukkit.getPluginManager().disablePlugin(this)
            );
            return;
        }

        if (!setupDiscordSRV()) {
            logSevere("DiscordSRV not found! Disabling plugin...");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }

        log("Loading config.yml...");
        saveDefaultConfig();
        this.configManager = new ConfigManager(this);
        
        log("Loading embed configurations...");
        this.embedConfig = new EmbedConfig(this);
        
        if (!setupStorage()) {
            logSevere("Failed to setup storage! Disabling plugin...");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }
        
        if (!setupDiscordListener()) {
            logSevere("Failed to setup Discord listener! Disabling plugin...");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }
        
        getCommand("linkingaddon").setExecutor(new ReloadCommand(this));
        
        this.discordSRVEventListener = new DiscordSRVEventListener(this);
        DiscordSRV.api.subscribe(discordSRVEventListener);
        
        this.nicknameSyncManager = new NicknameSyncManager(this);
        this.roleSyncManager = new RoleSyncManager(this);
        
        log("DiscordSRVLinkingEmbed has been enabled!");
    }
    
    @Override
    public void onDisable() {
        if (nicknameSyncManager != null) {
            nicknameSyncManager.stopSyncTask();
        }
        
        if (storageManager != null) {
            storageManager.close();
        }
        
        if (discordListener != null) {
            JDA jda = DiscordSRV.getPlugin().getJda();
            if (jda != null) {
                jda.removeEventListener(discordListener);
            }
        }
        
        if (discordSRVEventListener != null) {
            DiscordSRV.api.unsubscribe(discordSRVEventListener);
        }
        
        log("DiscordSRVLinkingEmbed has been disabled!");
    }
    
    private boolean setupDiscordSRV() {
        return Bukkit.getPluginManager().getPlugin("DiscordSRV") != null;
    }
    
    private boolean setupStorage() {
        boolean useMysql = configManager.isMysqlEnabled();
        
        if (useMysql) {
            log("Attempting to use MySQL storage...");
            StorageManager mysqlStorage = new MySQLStorage(this);
            
            if (mysqlStorage.initialize()) {
                this.storageManager = mysqlStorage;
                log("Successfully connected to MySQL!");
                return true;
            } else {
                logWarning("MySQL connection failed! Falling back to AOF storage...");
            }
        }
        
        log("Using AOF file storage...");
        this.storageManager = new AOFStorage(this);
        return storageManager.initialize();
    }
    
    private boolean setupDiscordListener() {
        JDA jda = DiscordSRV.getPlugin().getJda();
        if (jda == null) {
            logWarning("JDA not ready yet, will retry in 5 seconds...");
            Bukkit.getScheduler().runTaskLater(this, () -> {
                if (!setupDiscordListener()) {
                    logSevere("Failed to setup Discord listener after retry!");
                    Bukkit.getPluginManager().disablePlugin(this);
                }
            }, 100L);
            return true;
        }

        this.discordListener = new DiscordListener(this);
        jda.addEventListener(discordListener);
        log("Discord listener registered successfully!");
        return true;
    }
    
    public void reload() {
        configManager.reload();
        embedConfig.reload();
        if (nicknameSyncManager != null) {
            nicknameSyncManager.reload();
        }
        if (roleSyncManager != null) {
            roleSyncManager.reload();
        }
        log("Configuration and embeds reloaded!");
    }

    public static void log(String message) {
        instance.getLogger().info(message);
    }

    public static void logWarning(String message) {
        instance.getLogger().warning(message);
    }

    public static void logSevere(String message) {
        instance.getLogger().severe(message);
    }
}