Error Type mismatch: cannot convert from HashMap<String,Integer> to int

Discussion in 'Plugin Development' started by dagen9, May 19, 2017.

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

    dagen9

    Help please! Here is my code:

    Code:
    package me.enchanted.mapvote;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MapVote extends JavaPlugin{
       
        public List<String> voted = new ArrayList<>();
        public HashMap<String, Integer> votes = new HashMap<>();
        public List<String> arenas = new ArrayList<>();
       
        public void onEnable(){
            arenas.add("BikeRace");
            arenas.add("SubmarineRace");
            arenas.add("Parachuting");
            arenas.add("PlaneRace");
            arenas.add("BroomRace");
           
            for (String all : arenas){
                votes.put(all, 0);
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("raceresult")){
                    int max = 0;
                    for(int i : votes.values()) {
                        if(i > max) {
                            max = i;
                        }
                    }
                   
                    String winner = "";
                   
                    for(String all : votes.keySet()) {
                        if(votes.get(all) == max) {
                            winner = all;
                        }
                    }
                   
                    Bukkit.broadcastMessage("§a" + winner + " §aHas Won!");
                }
                if(cmd.getName().equalsIgnoreCase("racelist")){
                    player.sendMessage("§a---- §a§lVotes §r§a----");
                    for(String all : arenas) {
                        player.sendMessage("§6" + all + " §c" + votes.get(all));
                    }
                }
                if(cmd.getName().equalsIgnoreCase("racevote")){
                    if(!voted.contains(player.getName())){
                    if(arenas.contains(args[1].toLowerCase())){
                        int votes1 = votes.get(args[1].toLowerCase());
                        votes++;
                   
                        player.sendMessage(ChatColor.GREEN + "You have Voted for " + args[1]);
                        voted.add(player.getName());
                    }else{
                    player.sendMessage("§cThis race does not exist.");   
                    }
                    }else{
                        player.sendMessage("§4You have already voted for a map! §r§aDo /maplist to see all the votes!");
                    }
                }
            }
            return true;
           
        }
    }
    
    votes++ is the one that has the error.
     
  2. Offline

    timtower Ninja on the waves Moderator

    @dagen9 votes1++
    Then set that vote again.
     
  3. Offline

    dagen9

    int votes1 = votes.get(args[1].toLowerCase());
    votes1 says its unused even tho:
    votes1++;
     
  4. Offline

    timtower Ninja on the waves Moderator

    @dagen9 Did you also set it in the hashmap again?
     
  5. Offline

    dagen9

    public List<String> voted = new ArrayList<>();
    public HashMap<String, Integer> votes = new HashMap<String, Integer>();
    public List<String> arenas = new ArrayList<>();

    Code:
    package me.enchanted.mapvote;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MapVote extends JavaPlugin{
       
        public List<String> voted = new ArrayList<>();
        public HashMap<String, Integer> votes = new HashMap<String, Integer>();
        public List<String> arenas = new ArrayList<>();
       
        public void onEnable(){
            arenas.add("BikeRace");
            arenas.add("SubmarineRace");
            arenas.add("Parachuting");
            arenas.add("PlaneRace");
            arenas.add("BroomRace");
           
            for (String all : arenas){
                votes.put(all, 0);
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("raceresult")){
                    int max = 0;
                    for(int i : votes.values()) {
                        if(i > max) {
                            max = i;
                        }
                    }
                   
                    String winner = "";
                   
                    for(String all : votes.keySet()) {
                        if(votes.get(all) == max) {
                            winner = all;
                        }
                    }
                   
                    Bukkit.broadcastMessage("§a" + winner + " §aHas Won!");
                }
                if(cmd.getName().equalsIgnoreCase("racelist")){
                    player.sendMessage("§a---- §a§lVotes §r§a----");
                    for(String all : arenas) {
                        player.sendMessage("§6" + all + " §c" + votes.get(all));
                    }
                }
                if(cmd.getName().equalsIgnoreCase("racevote")){
                    if(!voted.contains(player.getName())){
                    if(arenas.contains(args[1].toLowerCase())){
                                int votes1 = votes.get(args[1].toLowerCase());
                        votes1++;
                        player.sendMessage(ChatColor.GREEN + "You have Voted for " + args[1]);
                        voted.add(player.getName());
                    }else{
                    player.sendMessage("§cThis race does not exist.");   
                    }
                    }else{
                        player.sendMessage("§4You have already voted for a map! §r§aDo /maplist to see all the votes!");
                    }
                }
            }
            return true;
           
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2017
  6. Offline

    timtower Ninja on the waves Moderator

    @dagen9 You increase votes1, but you don't set it again in votes itself.
     
  7. Offline

    dagen9

    I quit.
     
Thread Status:
Not open for further replies.

Share This Page