Problem with Hash Maps

Discussion in 'Plugin Development' started by Tehepicford, Aug 18, 2012.

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

    Tehepicford

    OK, I'm making a Plugin. A lot of trouble. First of all I don't think it likes the type of hashmap I am using...

    Well, Ill show the code and error, In hope for some help :D (This is my 2nd plugin so don't say that's a really bad way of doing so.)

    Code:

    Code:
    package com.fordnatford.Commands;
     
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
        public Map<Player, Boolean> hasAcceptedRules = new HashMap<Player, Boolean>();
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Main plugin;
        public final BukkitListner bl = new BukkitListner(this);
     
    public void onDisable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " Has been disabled!");
       
    }
     
    public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " Has been Enabled!");
        PluginManager pm = this.getServer().getPluginManager();
        pm.registerEvents(bl, this);
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        Player player = (Player) sender;
        if(label.equalsIgnoreCase("accept")) {
        hasAcceptedRules.put(player, true);
        player.sendMessage("Thanks for accepting the rules!");
        }
        return false;
       
    }
     
     
    public void onPlayerJoin(PlayerJoinEvent evt) {
        Player player = evt.getPlayer(); // The player who joined
        if(hasAcceptedRules.containsKey(player)) {
            player.sendMessage("Welcome back " + ChatColor.YELLOW + player.getName());
        }
        else {player.sendMessage("Please read the rules!");}
    }
     
    }
    Error:
    Code:
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:281)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:583)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:476)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.Error: Unresolved compilation problem:
        The method put(Player, ArrayList<Block>) in the type HashMap<Player,ArrayList<Block>> is not applicable for the arguments (Player, boolean)
     
        at com.fordnatford.Commands.Main.onCommand(Main.java:36)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 15 more
    2012-08-19 00:25:37 [INFO] /173.76.16.137:54087 lost connection
    2012-08-19 00:26:02 [INFO] fordnatford lost connection: disconnect.quitting
    2012-08-19 00:26:02 [INFO] Connection reset
    2012-08-19 00:26:20 [INFO] fordnatford[/86.21.20.243:54064] logged in with entity id 518 at ([Spawn] -54.22375953084183, 66.0, 261.90559600439786)
    2012-08-19 00:26:51 [INFO] /173.76.16.137:54115 lost connection
    2012-08-19 00:27:48 [INFO] [Commands] Disabling Commands v1.0
    2012-08-19 00:27:48 [INFO] Commands Has been disabled!
    2012-08-19 00:27:48 [INFO] 195 recipes
    2012-08-19 00:27:48 [INFO] [Commands] Loading Commands v1.0
    2012-08-19 00:27:48 [INFO] [Commands] Enabling Commands v1.0
    2012-08-19 00:27:48 [INFO] CommandsVersion 1.0 Has been Enabled!
    2012-08-19 00:27:48 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2012-08-19 00:27:50 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'accept' in plugin Commands v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:281)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:583)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:476)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.Error: Unresolved compilation problem:
        The method put(Player, ArrayList<Block>) in the type HashMap<Player,ArrayList<Block>> is not applicable for the arguments (Player, boolean)
     
        at com.fordnatford.Commands.Main.onCommand(Main.java:36)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 15 more

    I'm really stuck, Help appreciated :D
     
  2. Offline

    stelar7

    try recompiling it
     
  3. Offline

    Tehepicford

    I'm not doing it that way, Derp.

    New Project > Java Project
    Properties > Add External Jars
    etc.
    HELP!
     
  4. Offline

    stelar7

    it thinks you are using a
    HashMap<Player,ArrayList<Block>>
    and you are adding a
    Player, boolean
    witch wont work, thus try to export the plugin again
     
  5. Offline

    Tehepicford

    I did, But is there a way around this?
     
  6. Offline

    stelar7

    ...

    no
     
  7. Offline

    Major

    In your onCommand method, change

    Code:
     hasAcceptedRules.put(player, true);
    to

    Code:
     hasAcceptedRules.put(player, Boolean.TRUE);
    Edit: A couple of other things I noticed:

    Code:
     PluginDescriptionFile pdfFile = this.getDescription();
    Doesn't really make sense; pdfFile would be PluginDescriptionFileFile :p

    Just call it 'file' or something.

    Also I think an ArrayList would be more appropriate for this than a HashMap.

    Edit2: To clarify: the above is not the fix for this error. The code resulting in this error is no longer present in what you have posted (unless I'm missing something). The above is only for consistency - you can see my post below as to why this is a good idea to do :)
     
  8. Any specific reason for doing that ?
     
  9. Offline

    Major

    Consistency; I only suggested it as (unless I'm missing something) the code causing the error is no longer present. An error I foresaw was very likely to occur with the way he was doing it - if the HashMap does not contain a value, it will return null. If (in the future) he tried, for example

    Code:
    boolean flag = hasAcceptedRules.get(player);
    and the key (in this case, the player instance) wasn't stored in the HashMap, it would return null - and of course, a primitive type cannot be null. Hopefully this will helped prevent this as I see it very often. However, I should have mentioned that that wasn't the fix for the error (as I've already said, the code resulting in that error appears to have gone), so I'll add that into my post.
     
Thread Status:
Not open for further replies.

Share This Page