Solved HashMap returns null

Discussion in 'Plugin Development' started by NortherKnight, Aug 2, 2015.

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

    NortherKnight

    In onCommand it works fine but when i call hashmap in event it returns null(i think it creates a new one)
    Code:
    public class GodMode implements Listener,CommandExecutor
    {
        protected HashMap<String, Boolean> hasGodMode = new HashMap<String, Boolean>();
        Main plugin;
        public GodMode(Main passedPlugin)
        {
            this.plugin = passedPlugin;
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if(!(sender instanceof Player))
            {
                sender.sendMessage(ChatColor.RED + "A non player cannot run this command!");
                return true;
            }
           
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("godmode"))
            {
                if(hasGodMode.containsKey(player.getName()))
                {
                    if(hasGodMode.get(player.getName()) == true)
                    {
                        hasGodMode.put(player.getName(), false);
                        player.sendMessage("God mode has been disabled!");
                        return true;
                    }
                    else if(hasGodMode.get(player.getName()) == false)
                    {
                        hasGodMode.put(player.getName(), true);
                        player.sendMessage("God mode has been enabled!");
                        return true;
                    }
                }
               
                hasGodMode.put(player.getName(), true);
                player.sendMessage("God mode enabled!");
                player.sendMessage("" + player.getName());
                return true;
            }
            return true;
        }
       
        @EventHandler
        public void onEntityDamage(EntityDamageEvent e)
        {
            if(e.getEntity() instanceof Player)
            {
                Player player = (Player) e.getEntity();
                player.sendMessage("" + player.getName());
                if(hasGodMode.containsKey(player.getName()))
                {
                    player.sendMessage("canceling!");
                    if(hasGodMode.get(player.getName()) == true)
                    {
                        e.setCancelled(true);
                    }
                }
            }
        }
    }
     
  2. Offline

    Larry Newman

  3. Offline

    NortherKnight

  4. Offline

    Boomer

    Are you getting an error? You say returns null - what are you getting as output, is there any result in the console/log, what is saying null, what does it look like?| Nothing immediaely comes to mind as looking wrong, without knowing exactly what is failing for you, and how you know it is failing, and or if there is an explicit error code to assist...

    You do have a fair bit of extraneous coding as well.. Since your hashmap get will return a boolean object, your one set of logic simplifies to this:


    if (thing = true) { do this }
    else if (thing = false) {do that}
    ... First off, if "thing=true" is not true, then by definition, it is false, which is the else-side of the if coin. So
    if (thing=true ) {do this}
    else {do that}
    ... requires less redundant checks

    Also, because your object return is a boolean itself, you are testing
    if (true=true) in one return case, or
    if (false=true) in the other return case
    true=true is always true, false=true is always false, which is the same as your get return, hence with a boolean thing:

    if (thing) {do this }
    else {do that}
    is optimal, simpler, and cleaner :)

    == Pleae provide us with more input/results, so that we can understand what exactly is failing, how it is reporting to you it is failing, and any errors that might help allow proper interpretation.

    @Larry - please report the question to be moved by a moderator when you see such need as well.
     
  5. Offline

    Myrathi

    Moved to more appropriate forum.
     
  6. Offline

    NortherKnight

    In game i call command /godmode so it registers he user in the hashmap then i try to take damage and it goes through.
    And when i try to get the value of boolean with the key player.getName() i get null

    PS: In my main class :
    Code:
    Bukkit.getPluginManager().registerEvents(new GodMode(this), this);
    this.getCommand("godmode").setExecutor(new GodMode(this));
     
  7. Offline

    Boomer

    "if i try to get the value of boolean"...at which point in the code? Cause yeah, it doesn't appear like it would be able to give null since you test if the name is in the map first everwhere... But if you are putting printout code to check in the wrong spots...
     
  8. Offline

    NortherKnight

    Code:
    GodMode gm = new GodMode(this);
    this.getCommand("godmode").setExecutor(gm);
    Bukkit.getPluginManager().registerEvents(gm, this);
    works :3
     
Thread Status:
Not open for further replies.

Share This Page