Solved Getting highest number from config?(Will post fix when get home had to leave in a hurry)

Discussion in 'Plugin Development' started by O3Bubbles09, Jul 22, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    O3Bubbles09

    Hi there I was wondering how I could get the highest number from a config that looks like this(want to get the highest number of deaths):
    Code:
    players:
      o3bubbles09:
        exists: true
        deaths: 1
      bubbles:
        exists: true
        deaths: 0
    My failed attempt at trying to do this:
    Code:java
    1. /* Check top deaths */
    2. public String topDeaths() {
    3. ArrayList<String> players = new ArrayList<String>();
    4. ArrayList<Integer> deaths = new ArrayList<Integer>();
    5. HashMap<String, Integer> player_deaths = new HashMap<String, Integer>();
    6.  
    7. for(String s : getConfig().getConfigurationSection("players").getKeys(false)) {
    8. players.add(s);
    9. }
    10.  
    11. for(int i = 0; i > players.size(); i++) {
    12. deaths.add(getDeaths(players.get(i)));
    13. }
    14.  
    15. for(int i = 0; i > players.size(); i++) {
    16. player_deaths.put(players.get(i), deaths.get(i));
    17. }
    18.  
    19. return null;
    20. }
     
  2. Offline

    stormneo7

    Firstly, I take no credit for the MapUtil class, a friend gave it to me.
    Code:java
    1. package com.kThisIsCvpv.RankTags;
    2.  
    3. import java.util.*;
    4.  
    5. public class MapUtil{
    6. public static <K, V extends Comparable<? super V>> Map<K, V>
    7. sortByValue( Map<K, V> map ){
    8. List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>( map.entrySet() );
    9. Collections.sort( list, new Comparator<Map.Entry<K, V>>(){
    10. public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 ){
    11. return (o1.getValue()).compareTo( o2.getValue() );
    12. }
    13. });
    14.  
    15. Map<K, V> result = new LinkedHashMap<K, V>();
    16. for (Map.Entry<K, V> entry : list){
    17. result.put( entry.getKey(), entry.getValue() );
    18. }
    19. return result;
    20. }
    21. }
    22.  

    You'd want to make a new class somewhere, name it MapUtil, and paste that code in.
    That code sorts Maps by highest value.

    Next, load a Map<String, Integer>.
    This is using your format.
    Code:java
    1. Map<String, Integer> compareDeaths = new HashMap<String, Integer>();
    2. for(String s : getConfig().getConfigurationSection("players").getKeys(false))
    3. compareDeaths.put(s, getConfig().getInt("players." + s + ".deaths"));


    Next sort the HashMap.
    Code:java
    1. compareDeaths = (HashMap<String, Integer>) MapUtil.sortByValue(compareDeaths);


    Your Hashmap is now in order from Lowest to Highest.

    We'll load the HashMap's KeySet onto an ArrayList.
    Code:java
    1. ArrayList<String> KeySet = new ArrayList<String>();
    2. for(String s : compareDeaths.keySet())
    3. KeySet.add(s);


    If you want to get the last entry in the HashMap, you'd do the following...
    Code:java
    1. String playerNameMostDeaths = KeySet.get(KeySet.size() - 1);
    2. int MostDeathNumber = compareDeaths.get(playerNameMostDeaths);


    If you want the get the first entry on the HashMap, you'd do the following...
    Code:java
    1. String playerNameLeastDeaths = KeySet.get(0);
    2. int LeastNumber = compareDeaths.get(playerNameLeastDeaths);


    Please be careful however. If your HashMap is empty, there will be errors.
     
  3. O3Bubbles09
    I whipped up this quick basic example of how you can sort the values. Basically I add each player & their death count to a TreeMap. TreeMaps automatically sort values from least to greatest. Then, you can get however many of the top values you want.

    Code:java
    1. public class Test {
    2.  
    3. private Map<String, Integer> map = new TreeMap<String, Integer>(); //TreeMaps automatically sort values
    4.  
    5. public void sortValues(){
    6. Set<String> players = getConfig().getConfigurationSection("players").getKeys(false); //get all the players in the config section
    7.  
    8. for (String plyr : players){ //loop through the players in the set
    9. map.put(plyr, getConfig().getConfigurationSection("players." + plyr).getInt(".deaths")); //add the player & their deaths to the map
    10. }
    11. }
    12. }
     
  4. Offline

    fireblast709

    The Gaming Grunts TreeMaps sort key based. Use a TreeMultimap with the death as key and the name as value instead (which allows you to get two+ people if there are multiple people with the highest score)
     
  5. Offline

    fireblast709

    The Gaming Grunts do note that a TreeMap won't work well when two+ people have the same score ;)
     
  6. Offline

    O3Bubbles09

    The Gaming Grunts likes this.
  7. Offline

    O3Bubbles09

    The Gaming Grunts So I thought I could figure this out with the information you guys gave me but man was I wrong.... I do not understand this TreeMaps at all.
     
  8. O3Bubbles09
    Can you explain a bit? The example I provided should do exactly what you want. As for TreeMaps themselves, they work exactly like your standard HashMap except they automatically sort their contents.
     
  9. Offline

    O3Bubbles09

    @TheGamingGrunts

    Okay so here is what I have tried:
    Code:java
    1. /* Check top deaths */
    2. private Map<String, Integer> map = new TreeMap<String, Integer>();
    3.  
    4. public String topDeaths() {
    5. Set<String> players = getConfig().getConfigurationSection("players").getKeys(false);
    6. String list = "";
    7.  
    8. for(String s : players) {
    9. map.put(s, getDeaths(s));
    10.  
    11. list += s + ": " + map.get(s) + "\n";
    12. }
    13.  
    14. return list;
    15. }


    Yes it does get the players names and theirs deaths but it doesn't sort them from highest to lowest... Idk why.
     
Thread Status:
Not open for further replies.

Share This Page