Hashmap Problems

Discussion in 'Plugin Development' started by Frizkie, Sep 23, 2011.

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

    Frizkie

    Okay, I have my first Bukkit plugin going here and I'm having problems with hashmaps. I'm not new with Java and I'm not experienced with it either, so don't be overcomplicated but don't baby me either.

    My plugin takes players and if they use the command "/snowball" and adds them to the (now problematic) hashmap. When a player is hit by a snowball, if they are part of the hashmap, the player dies instantly.

    The problem is that the snowballList hashmap is apparently null. You can see my source here:

    KillerSnowballs.java

    Code:
    package me.frizkie.plugins.killersnowballs;
    
    import java.util.HashMap;
    import java.util.Map;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class KillerSnowballs extends JavaPlugin
    {
        private final KillerSnowballsEntityListener entityListener = new KillerSnowballsEntityListener(this);
        private final KillerSnowballsPlayerListener playerListener = new KillerSnowballsPlayerListener(this);
        public static Map<Player, Boolean> snowballList = new HashMap<Player, Boolean>();
    
        public void onEnable()
        {
            System.out.println(this + " is now enabled!");
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Event.Priority.Normal, this);
        }
    
        public void onDisable()
        {
            System.out.println(this + " is now disabled!");
        }
    }
    
    KillerSnowballsPlayerListener.java
    Code:
    package me.frizkie.plugins.killersnowballs;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;
    
    public class KillerSnowballsPlayerListener extends PlayerListener
    {
        public static KillerSnowballs plugin;
    
        public KillerSnowballsPlayerListener(KillerSnowballs instance)
        {
            plugin = instance;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("snowball"))
            {
                Player player = (Player) sender;
                toggleSnowball(player);
                return true;
            }
    
            return false;
        }
    
        public void toggleSnowball(Player player)
        {
            if(KillerSnowballs.snowballList.containsKey(player))
            {
                if(KillerSnowballs.snowballList.get(player) == true)
                {
                    KillerSnowballs.snowballList.put(player, false);
                    player.sendMessage("Snowball death mode deactivated.");
                } else {
                    KillerSnowballs.snowballList.put(player, true);
                    player.sendMessage("Snowball death mode activated.");
                }
            } else {
                KillerSnowballs.snowballList.put(player, true);
                player.sendMessage("Snowball death mode activated.");
            }
        }
    }
    
    KillerSnowballsEntityListener.java
    Code:
    package me.frizkie.plugins.killersnowballs;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Creature;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityListener;
    
    public class KillerSnowballsEntityListener extends EntityListener
    {
    	public static KillerSnowballs plugin;
    	public KillerSnowballsEntityListener(KillerSnowballs instance)
    	{
    		plugin = instance;
    	}
    	public void onEntityDamage(EntityDamageEvent event)
    	{
    		if(event instanceof EntityDamageByEntityEvent)
    		{
    			EntityDamageByEntityEvent event0 = (EntityDamageByEntityEvent) event;
    			if(!(event0.getEntity() instanceof Creature))
    			{
    				Player recipient = (Player) event0.getEntity();
    				Entity damager = event0.getDamager();
    				if(damager instanceof Snowball && recipient instanceof Player)
    				{
    					if(KillerSnowballs.snowballList.get(recipient) == true)
    					{
    						recipient.damage(50, event0.getDamager());
    						String recipientName = recipient.getName();
    						ChatColor green = ChatColor.GREEN;
    						ChatColor white = ChatColor.WHITE;
    						if(recipient.isOp())
    						{
    							plugin.getServer().broadcastMessage(green + recipientName + white + " gets snowballed!");
    						} else {
    							plugin.getServer().broadcastMessage(recipientName + " gets snowballed!");
    						}
    					}
    				}
    			}
    		}
    	}
    }
    
    Any suggestions?
     
  2. Offline

    bleachisback

    onCommand goes in your JavaPlugin, not your PlayerListener

    Also a few suggestions to help clean up your code:
    1)If you're just storing a boolean value in the hashmap, you can just use an ArrayList instead.
    2)You can use EntityDamageByProjectileEvent instead of EntityDamageEvent and you don't have to check if the cause is a snowball
     
  3. Offline

    Frizkie

    Cool, thanks for the tips. In the event that I do want to use HashMaps in the future, do you see anything wrong with it?

    EDIT: Okay so I realize now that it works as long as you toggle the mode ON then back OFF. It no longer fires after that.
     
Thread Status:
Not open for further replies.

Share This Page