I

Can u add some commands for dialog too like dialogleaderboard and in tab completion add all leaderboards and typing that directly opens dialog of that leaderboard of the top 10 like how it is in normal /leaderboard second add a dialogbaltop and dialogbalancetop that opens the money leaderboard of dialog directly of top 10 one

Kodari AI
KodariOct 3 at 11:52 am 1
  1. Add /dialogleaderboard with leaderboard tab completion and direct top-10 dialog opening.

  2. Add /dialogbaltop and /dialogbalancetop for the top-10 money dialog.

  1. /dialogleaderboard <metric> now opens that metric’s top-10 dialog; the no-argument selector remains.

  2. Next I’m adding the two money-dialog command aliases.

  • /dialogleaderboard <metric> now opens that leaderboard’s top 10 in a dialog, with tab completion for supported metrics. Running it without a metric still opens the category selector.

  • Added /dialogbaltop and /dialogbalancetop; both open the top-10 money leaderboard dialog.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
manifest.kod
settings.gradle
Code
44 2,600
src/main/java/com/kodari/leaderstats/LeaderStatsPlugin.java
package com.kodari.leaderstats;

import com.kodari.leaderstats.gui.LeaderboardMenus;
import com.cryptomorin.xseries.XSound;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.List;
import java.util.function.ToDoubleFunction;

public final class LeaderStatsPlugin extends JavaPlugin {
    private StatsStorage storage;
    private StatsService stats;
    private LeaderboardMenus menus;
    private Object dialogStatsMenu;
    private Object dialogLeaderboardMenu;
    private YamlConfiguration language;
    private Method placeholderMethod;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        saveResourceIfMissing("lang.yml");
        saveDefaultGuiFiles();
        loadLanguage();
        setupPlaceholderAPI();
        int recordCount;
        try {
            storage = new StatsStorage(this);
            List<PlayerStats> loaded = storage.loadAll();
            recordCount = loaded.size();
            stats = new StatsService(this, storage, setupEconomy(), loaded);
        } catch (SQLException exception) {
            getLogger().severe("Could not open stats.db: " + exception.getMessage());
            getServer().getPluginManager().disablePlugin(this);
            return;
        }
        menus = new LeaderboardMenus(this);
        saveDefaultDialogFiles();
        setupDialogStats();
        StatsListener listener = new StatsListener(this);
        getServer().getPluginManager().registerEvents(listener, this);
        LeaderStatsCommand command = new LeaderStatsCommand(this);
        for (String name : List.of("leaderboards", "stats", "statsadmin", "baltop", "balancetop", "dialogstats", "dialogleaderboard", "dialogbaltop", "dialogbalancetop")) {
            getCommand(name).setExecutor(command);
            getCommand(name).setTabCompleter(command);
        }
        if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
            new LeaderStatsExpansion(this).register();
        }
        Bukkit.getOnlinePlayers().forEach(stats::startSession);
        long refreshTicks = Math.max(20, getConfig().getLong("economy.vault-balance-refresh-seconds", 60) * 20);
        Bukkit.getScheduler().runTaskTimer(this, stats::flushAllPlaytime, refreshTicks, refreshTicks);
        getLogger().info("Loaded " + recordCount + " player records from stats.db.");
    }

    @Override
    public void onDisable() {
        if (stats != null) stats.flushAllPlaytime();
        if (storage != null) storage.close();
    }

    public void reloadSettings() {
        reloadConfig();
        loadLanguage();
        saveDefaultGuiFiles();
        saveDefaultDialogFiles();
        if (menus != null) menus.reload();
        reloadDialogStats();
        reloadDialogLeaderboard();
    }

    private void saveDefaultGuiFiles() {
        File directory = new File(getDataFolder(), "gui");
        if (!directory.exists()) directory.mkdirs();
        saveResourceIfMissing("gui/maingui.yml");
        saveResourceIfMissing("gui/stats.yml");
        for (StatsMetric metric : StatsMetric.values()) {
            if (metric.isLeaderboardMetric()) saveResourceIfMissing("gui/leaderboard-" + metric.key() + ".yml");
        }
    }

    private void saveDefaultDialogFiles() {
        File directory = new File(getDataFolder(), "dialogs");
        if (!directory.exists()) directory.mkdirs();
        for (String file : List.of(
                "stats.yml",
                "leaderboard-main.yml",
                "leaderboard-money-top.yml", "leaderboard-money-full.yml",
                "leaderboard-deaths-top.yml", "leaderboard-deaths-full.yml",
                "leaderboard-playtime-top.yml", "leaderboard-playtime-full.yml",
                "leaderboard-blocks-placed-top.yml", "leaderboard-blocks-placed-full.yml",
                "leaderboard-blocks-broken-top.yml", "leaderboard-blocks-broken-full.yml",
                "leaderboard-kills-top.yml", "leaderboard-kills-full.yml",
                "leaderboard-mobs-killed-top.yml", "leaderboard-mobs-killed-full.yml",
                "leaderboard-player-stats.yml")) {
            saveResourceIfMissing("dialogs/" + file);
        }
    }

    private void saveResourceIfMissing(String resourcePath) {
        if (!new File(getDataFolder(), resourcePath).exists()) saveResource(resourcePath, false);
    }

    private void setupDialogStats() {
        try {
            Class.forName("io.papermc.paper.dialog.Dialog");
            Class<?> statsMenuClass = Class.forName("com.kodari.leaderstats.dialog.DialogStatsMenu");
            dialogStatsMenu = statsMenuClass.getConstructor(LeaderStatsPlugin.class).newInstance(this);
            Class<?> leaderboardMenuClass = Class.forName("com.kodari.leaderstats.dialog.DialogLeaderboardMenu");
            dialogLeaderboardMenu = leaderboardMenuClass.getConstructor(LeaderStatsPlugin.class).newInstance(this);
        } catch (ClassNotFoundException ignored) {
            getLogger().info("Dialog commands require Paper 1.21.7 or newer; normal menus remain available.");
        } catch (ReflectiveOperationException exception) {
            getLogger().warning("Could not initialize dialog stats: " + exception.getMessage());
        }
    }

    public void openDialogStats(Player viewer, PlayerStats target) {
        if (dialogStatsMenu == null) {
            viewer.sendMessage(LeaderboardMenus.color("&cDialog menus require Paper 1.21.7 or newer."));
            return;
        }
        try {
            dialogStatsMenu.getClass().getMethod("open", Player.class, PlayerStats.class).invoke(dialogStatsMenu, viewer, target);
        } catch (ReflectiveOperationException exception) {
            getLogger().warning("Could not open dialog stats: " + exception.getMessage());
            viewer.sendMessage(LeaderboardMenus.color("&cCould not open the stats dialog."));
        }
    }

    public void openDialogStatsFromLeaderboard(Player viewer, PlayerStats target, StatsMetric metric, boolean full) {
        if (dialogStatsMenu == null) {
            openDialogStats(viewer, target);
            return;
        }
        try {
            dialogStatsMenu.getClass().getMethod("openFromLeaderboard", Player.class, PlayerStats.class, StatsMetric.class, boolean.class)
                    .invoke(dialogStatsMenu, viewer, target, metric, full);
        } catch (ReflectiveOperationException exception) {
            getLogger().warning("Could not open player stats dialog from leaderboard: " + exception.getMessage());
            openDialogStats(viewer, target);
        }
    }

    public void openDialogLeaderboard(Player viewer) {
        if (dialogLeaderboardMenu == null) {
            viewer.sendMessage(LeaderboardMenus.color("&cDialog menus require Paper 1.21.7 or newer."));
            return;
        }
        try {
            dialogLeaderboardMenu.getClass().getMethod("openMain", Player.class).invoke(dialogLeaderboardMenu, viewer);
        } catch (ReflectiveOperationException exception) {
            getLogger().warning("Could not open dialog leaderboard: " + exception.getMessage());
            viewer.sendMessage(LeaderboardMenus.color("&cCould not open the leaderboard dialog."));
        }
    }

    public void openDialogLeaderboard(Player viewer, StatsMetric metric, boolean full) {
        if (dialogLeaderboardMenu == null) {
            openDialogLeaderboard(viewer);
            return;
        }
        try {
            dialogLeaderboardMenu.getClass().getMethod("openLeaderboard", Player.class, StatsMetric.class, boolean.class)
                    .invoke(dialogLeaderboardMenu, viewer, metric, full);
        } catch (ReflectiveOperationException exception) {
            getLogger().warning("Could not open dialog leaderboard page: " + exception.getMessage());
            openDialogLeaderboard(viewer);
        }
    }

    private void reloadDialogStats() {
        if (dialogStatsMenu == null) return;
        try {
            dialogStatsMenu.getClass().getMethod("reload").invoke(dialogStatsMenu);
        } catch (ReflectiveOperationException exception) {
            getLogger().warning("Could not reload dialog stats: " + exception.getMessage());
        }
    }

    private void reloadDialogLeaderboard() {
        if (dialogLeaderboardMenu == null) return;
        try {
            dialogLeaderboardMenu.getClass().getMethod("reload").invoke(dialogLeaderboardMenu);
        } catch (ReflectiveOperationException exception) {