Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.5 kB
2
Indexable
Never
package org.lifestealsmp.topstatistics;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;

import me.clip.placeholderapi.expansion.PlaceholderExpansion;

public class TopKills implements Listener{
	
	@EventHandler
	public void onEntityDeathEvent(PlayerDeathEvent event) {
		if(event.getEntity() instanceof Player) {
			Player deceased = (Player) event.getEntity().getPlayer();
			if(event.getEntity().getKiller() instanceof Player) {
				Player killer = (Player) event.getEntity().getKiller();
				
				String deceasedAddress = deceased.getAddress().getAddress().getHostAddress();
				String killerAddress = killer.getAddress().getAddress().getHostAddress();
				// Block same IP kill
				if (deceasedAddress.equals(killerAddress)) {
					return;
				} 
				
				int num = main.getInstance().getKills().getInt("Kills." + killer.getName());
				main.getInstance().getKills().set("Kills." + killer.getName(), num + 1);
				main.getInstance().saveKills();
			}
		}
	}
	
	public static String TopPlayer(String rankPath, int rank) {
        try {
            FileConfiguration config = main.getInstance().getKills(); // Assuming this method is in your main plugin class

            if (!config.contains(rankPath)) {
                return "N/A Player"; // Path not found in config
            }

            Map<String, Integer> players = new HashMap<>();
            for (String key : config.getConfigurationSection(rankPath).getKeys(false)) {
                players.put(key, config.getInt(rankPath + "." + key));
            }

            // Sort by value 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();
            }
            return "N/A Player";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "N/A Player"; // In case of an error or invalid rank
    }
	
	public static Integer TopPlayerAmount(String rankPath, int rank) {
        try {
            FileConfiguration config = main.getInstance().getKills(); // Assuming this method is in your main plugin class

            if (!config.contains(rankPath)) {
                return 0; // Path not found in config
            }

            Map<String, Integer> players = new HashMap<>();
            for (String key : config.getConfigurationSection(rankPath).getKeys(false)) {
                players.put(key, config.getInt(rankPath + "." + key));
            }

            // Sort by value and get the score 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).getValue();
            }
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0; // In case of an error or invalid rank
    }
}
Leave a Comment