Array list - help needed

Discussion in 'Plugin Development' started by wannezz, Apr 7, 2012.

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

    wannezz

    Hello, my plugin is not working...
    I'm using an array list to store the players...
    Can anyone help me?

    Here is my commandexecutor...

    Code:
    public class JezusCommandExecutor implements CommandExecutor
    {
        Logger log = Logger.getLogger("Minecraft");
        ArrayList<Player> Players = new ArrayList<Player>();
       
        @SuppressWarnings("unused")
        private MyPlugin plugin;
     
        public JezusCommandExecutor(MyPlugin plugin)
        {
            this.plugin = plugin;
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(sender instanceof Player)
            {
                if(cmd.getName().equalsIgnoreCase("JezusMode"))
                {
                    toggleBlockChanger(sender);
                }
            }
            return false;
        }
     
        private void toggleJezusMode(CommandSender sender)
        {
            if (!enabled((Player) sender))
            {
                Players.add((Player) sender);
                ((Player) sender).sendMessage(ChatColor.BLUE + "You are now in JezusMode");
            }
           
            else
            {
                Players.remove((Player) sender);
                ((Player) sender).sendMessage(ChatColor.RED + "You are normal again");
            }
        }
     
        public boolean enabled(Player player)
        {
            return Players.contains(player);
        }
    }


    Heres my damage listener...

    Code:
    public class DamageListener implements Listener
    {   
        public static MyPlugin plugin;
        public DamageListener(MyPlugin instance)
        {
            plugin = instance;
        }
       
        ArrayList<Player> Players = new ArrayList<Player>();
     
        @EventHandler
        public void onPlayerDamage(EntityDamageEvent event) throws IOException
        {
            Entity entity = event.getEntity();
            Player player = (Player)entity;
           
            if(entity instanceof Player)
            {
                if(Players.contains(player))
                {
                    event.setDamage(1);
                    player.setHealth(20);
                }
            }
           
            else
            {
                //do nothing
            }
        }
    }

    Here's the main class...

    Code:
    public class MyPlugin extends JavaPlugin
    {
        Logger log = Logger.getLogger("Minecraft");
        private JezusCommandExecutor JezusExecutor;
       
        ArrayList<Player> Players = new ArrayList<Player>();
       
        public void onDisable()
        {
            log.info("[MyPlugin] has been disabled");
        }
     
        public void onEnable()
        {
            log.info("[MyPlugin] has been enabled");
            getServer().getPluginManager().registerEvents(DamageListener, this);
           
            JezusExecutor = new JezusCommandExecutor(this);
            getCommand("MyPlugin").setExecutor(JezusExecutor);
        }
    }
     
  2. Offline

    ItsHarry

    Okay so what's the problem?
    Also, variables should always start with a non-capitalized letter.
     
  3. Offline

    ferrybig

    your not using an listener to remove players from the arraylist on remove, this will cause memory leaks, use the following Set to stop this problem:
    Code:java
    1. Set<Player> list = Collections.newSetFromMap(new WeakHashMap<Player,Boolean>());

    Code:java
    1. list.add(Player); // add players to the godmode list
    2. list.contains(Player); // does player has got mode?
    3. list.remove(Player); // remove Player his gotmode

    Place this set at the main plugin class, and give referenses to it, at the contructor at your player listener and command liosteren, you can also move the listener and the on command to the main class, to decrease the memory footprint of the plugin to an even smaller format.

    WOW, I talked to mutch...
     
  4. Offline

    wannezz

    The problem is that if an entity (if I hit a cow for exmple) gets damaged, I get an error in console...
    + When i do /jezusmode the damage isn't set to 1

    So I have to change the ArrayList to "players" ?

    EDIT: this is the error I get when it gets damaged...
    Ok, I think I did everything you said, but I still get an error when an entity gets damaged...
    I think my listener isn't working properly...

    Here's the code

    Code:
    ublic class JezusMode extends JavaPlugin implements Listener
    {
        Logger log = Logger.getLogger("Minecraft");
        Set<Player> list = Collections.newSetFromMap(new WeakHashMap<Player, Boolean>());
     
        public void onDisable()
        {
            log.info("[JezusMode] has been disabled");
        }
     
        public void onEnable()
        {
            log.info("[JezusMode] has been enabled");
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(sender instanceof Player)
            {
                if(cmd.getName().equalsIgnoreCase("JezusMode"))
                {
                    toggleJezusMode(sender);
                }
            }
            return false;
        }
     
        @EventHandler
        public void onPlayerDamage(EntityDamageEvent event)
        {
            Entity entity = event.getEntity();
            Player player = (Player)entity;
         
            if(entity instanceof Player)
            {
                if(list.contains(player))
                {
                    event.setDamage(1);
                    player.setHealth(20);
                }
            }
         
            else
            {
                //do nothing
            }
        }
     
        private void toggleJezusMode(CommandSender sender)
        {
            if (!enabled((Player) sender))
            {
                list.add((Player) sender);
                ((Player) sender).sendMessage(ChatColor.BLUE + "You are now in JezusMode");
            }
         
            else
            {
                list.remove((Player) sender);
                ((Player) sender).sendMessage(ChatColor.RED + "You are normal again");
            }
        }
     
        public boolean enabled(Player player)
        {
            return list.contains(player);
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  5. Offline

    ferrybig

    can you giv ethe error, I cant detect error whit just looking at the code
     
  6. Offline

    VeryBIgCorp

    Maybe try storing the names of the players because the player's object constantly changes, thus rendering list.contains(player) useless.

    Also, in your listener, move the Player player = (Player)entity line into the if statement to check if it's an instance of a Player. That's the error.
     
    monstuhs likes this.
  7. Offline

    monstuhs

    Ditto what VeryBlgCorp said. Look at your stack trace:

    "Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.entity.CraftZomb
    ie cannot be cast to org.bukkit.entity.Player"

    Somewhere, a CraftZombie is firing this event and you're trying to cast it to a Player:
    Player player = (Player)entity;

    In addition to what VeryBlgCorp said, you should change your listener to:

    @EventHandler
    public void onPlayerDamage(EntityDamageByEntityEvent event)


    ...so that the handler isn't running when it doesn't need to.
     
Thread Status:
Not open for further replies.

Share This Page