Untitled

 avatar
unknown
plain_text
a year ago
4.2 kB
3
Indexable
package org.lifestealsmp.duels;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

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

public class SortElo {

	// Method to get player's name by rank
    public String SortEloName(int rank) {
        List<PlayerElo> playerEloList = getPlayerEloList();
        if (rank - 1 < playerEloList.size()) {
            UUID uuid = UUID.fromString(playerEloList.get(rank - 1).getUuid());
            OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
            if (offlinePlayer.hasPlayedBefore()) {
                return offlinePlayer.getName();
            } else {
                return "Player has never played on this server";
            }
        } else {
            return "Rank not found";
        }
    }
    
    public static String TopPlayer(int rank) {
        try {
        	File directory = new File("plugins/Duels/players");
            if (!directory.exists() || !directory.isDirectory()) {
                return "N/A Player"; // Directory does not exist or is not a directory
            }

            Map<String, Integer> players = new HashMap<>();
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile() && file.getName().endsWith(".yml")) {
                        FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(file);
                        String playerName = file.getName().replace(".yml", "");
                        int playerElo = playerConfig.getInt("Elo");
                        players.put(playerName, playerElo);
                    }
                }
            }

            // Sort by ELO and get the player's name at the specified rank
            List<Map.Entry<String, Integer>> list = new ArrayList<>(players.entrySet());
            list.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));

            if (rank - 1 < list.size()) {
                return list.get(rank - 1).getKey(); // Returns the player's name
            }
            return "N/A Player";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "N/A Player"; // In case of an error or invalid rank
    }


    // Method to get player's elo amount by rank
    public int SortEloAmount(int rank) {
        List<PlayerElo> playerEloList = getPlayerEloList();
        if (rank - 1 < playerEloList.size()) {
            return playerEloList.get(rank - 1).getElo();
        } else {
            return -1; // Indicates rank not found
        }
    }

    // Helper method to load and sort player ELOs
    private List<PlayerElo> getPlayerEloList() {
        File directory = new File("plugins/Duels/players");
        File[] files = directory.listFiles();
        List<PlayerElo> playerEloList = new ArrayList<>();

        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    FileConfiguration config = YamlConfiguration.loadConfiguration(file);
                    int elo = config.getInt("Elo", 0); // Default to 0 if not found
                    String uuid = file.getName().replace(".yml", ""); // Assuming the filename is the player's UUID
                    playerEloList.add(new PlayerElo(uuid, elo));
                }
            }
            playerEloList.sort(Comparator.comparingInt(PlayerElo::getElo).reversed());
        }
        return playerEloList;
    }

    private class PlayerElo {
        private String uuid;
        private int elo;

        public PlayerElo(String uuid, int elo) {
            this.uuid = uuid;
            this.elo = elo;
        }

        public String getUuid() {
            return uuid;
        }

        public int getElo() {
            return elo;
        }
    }
}
Editor is loading...
Leave a Comment