Getting a player's name that corresponds with something in config

Discussion in 'Spigot Plugin Development' started by stevensilvergood, Jun 1, 2018.

  1. Hi,

    I'm running a loop to check for whom in a config of names and integers has the highest votes.

    Code (Text):
    for (String section : getConfig().getConfigurationSection("").getKeys(false)){
             int largest = Collections.max(Arrays.asList(getConfig().getInt(players.getName())));
             // Get the player that corresponds with the integer. (k)
             Player newking = (Player) Bukkit.getPlayer(k);                                                      
             players.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&lSupreme &7&l>> &c&lFIGHTING PHASE!&r &k|&r &cThe king is &e" + newking.getName() + ". &cGood luck!"));

    But I can't figure out how, once I get the highest integer, to find out which name it belongs to.
    The config looks like this
    Name: integer

    An example:

    I do /vote (Im_steven)

    Im_steven: 1

    Please help me, been trying to fix this for hours.

  2. Local variable to store the player name with most votes, and vote amount, proceeded by a for loop over all of the keys in the config. Get the value of said key asInt, if it's larger than the most votes, set both local variables and proceed. Outside of the for loop, do all of the stuff you want to the person with the most votes.
  3. Okay, but to find out which player had the votes I need to find the first column of the config (player name.) I already found the highest integer(highest votes,) but how do I then find which player owns those votes?
  4. You set the player alongside with the votes..

    Don't do "max". Just do an if statement to see if the amount of votes is higher, and set the two variables from there.
  5. I believe there is some confusion because I left out a big portion of my code. Someone just messaged me a "very polite message" "helping" me so I'm good. Thanks though
  6. you could loop thru all the sections (which are called the player names) and get their value (the int), then as u go thru save the name of the highrest int, and if there's a higher one replace it.
    • Like Like x 1
  7. if its just the person with the highest amount of votes its easy.
    Do your loop, have two variables out side the loop - player and votes.
    get the section (player) and value (votes) and if value > votes set votes to value and player to section. Pretty simple (I’m shitty at explaining)
    • Like Like x 1
  8. pretty much what I just said
  9. There's no way to check who voted for who, because the votetimer is with the joining system. Basically the system checks for when a player joins, if the playersingame > 6, to start the voting phase. Then after 30 seconds i want to check who has the most votes.