Solved Player won't get added to ArrayList?

Discussion in 'Plugin Development' started by hellobrad100, Jun 15, 2016.

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

    hellobrad100

    Hello,

    I've used this method many times for making toggleable commands. I am simply trying to add the player to an Arraylist when they use a command, and remove again when they perform it again.

    I've got the event so that it will send me a message of how many people are on the ArrayList, so I can eliminate everything else. It is just simply not adding them to the ArrayList, even though I have another method that does the exact same thing and works fine.

    Any light to be shed? Here is the code:

    Code:
    public ArrayList<Player> sands = new ArrayList<Player>();
    
        public boolean onCommand(CommandSender cs, Command c, String l, String[] args)
        {
            if(c.getName().equalsIgnoreCase("sand"))
            {
                if(!(cs instanceof Player)) return false;
                Player p = (Player)cs;
                if(p.hasPermission("permission.sand"))
                {
                    enableSand(p);
                    return true;
                }
                else
                {
                    p.sendMessage(ChatColor.RED + "You dont have permission for that command!");
                    return false;
                }
               
            }
            return false;
        }
    
        public void enableSand(Player p)
        {
            if(!(this.isEnabled(p)))
            {
                this.sands.add(p);
                p.sendMessage(ChatColor.GREEN + "Sand mode has been enabled!");
            }
            else
            {
                this.sands.remove(p);
                p.sendMessage(ChatColor.RED + "Sand mode has been disabled!");
            }
        }
       
        public boolean isEnabled(Player p)
        {
            return sands.contains(p);
        }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    hellobrad100

    Same situation happens.

    Code:
        public ArrayList<String> sands = new ArrayList<String>();
       
        public boolean onCommand(CommandSender cs, Command c, String l, String[] args)
        {
            if(c.getName().equalsIgnoreCase("sand"))
            {
                if(!(cs instanceof Player)) return false;
                Player p = (Player)cs;
                if(p.hasPermission("bradlkbz.sand"))
                {
                    enableSand(p);
                    return true;
                }
                else
                {
                    p.sendMessage(ChatColor.RED + "You dont have permission for that command!");
                    return false;
                }
               
            }
            return false;
        }
    
        public void enableSand(Player p)
        {
            if(!(this.isEnabled(p)))
            {
                this.sands.add(p.getName());
                p.sendMessage(ChatColor.GREEN + "Sand mode has been enabled!");
            }
            else
            {
                this.sands.remove(p.getName());
                p.sendMessage(ChatColor.RED + "Sand mode has been disabled!");
            }
        }
       
        public boolean isEnabled(Player p)
        {
            return sands.contains(p.getName());
        }
    
    
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlace(BlockPlaceEvent e)
        {
            Player p = e.getPlayer();
            Block b = e.getBlock();
            if(this.sands.contains(p.getName()))
            {
                if(b.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) return;
                b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), b.getData());
                b.setType(Material.AIR);
                p.sendMessage("yolo" + sands.size());
            }
        }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @hellobrad100 Please post your full code, if this isn't the main class: please post the onEnable as well.
     
  5. Offline

    MisterErwin

    @hellobrad100 A few hints:

    Code:
    if ( ! (a)) { foo(); } else {bar();}
    is the same as
    Code:
    if (a) { bar(); } else {foo();}
    Returning false in the onCommand method will send the "Usage /yourCommand" message to the user.
    Just return true as you are already sending an info to the user when he does not have the permissions.

    Then: Do you see Sand mode has been disabled/enabled in the chat?

    Your debug message (yolo) is being send after an early return statement.

    And then like timtower said^^
     
  6. Offline

    hellobrad100

    @MisterErwin @timtower

    It sends me the messages saying that it is enabled / disabled in chat.

    If I remove the contains(p) argument from the event, the event works fine, so its enabled correctly, it is just simply not adding to the ArrayList.

    If I have that argument there, nothing happens because they won't get added to the ArrayList at all.

    In my onEnable() method, I have the following for that class:
    Code:
      @Override
      public void onEnable()
      {
           Bukkit.getPluginManager().registerEvents(new SandCommand(this), this);
           getCommand("sand").setExecutor(new SandCommand(this));
      }
    
    Class:
    Code:
    public class SandCommand implements CommandExecutor, Listener
    {
        public static Main pl;
        public SandCommand(Main ins)
        {
            pl = ins;
        }
        public ArrayList<String> sands = new ArrayList<String>();
      
        public boolean onCommand(CommandSender cs, Command c, String l, String[] args)
        {
            if(c.getName().equalsIgnoreCase("sand"))
            {
                if(!(cs instanceof Player)) return false;
                Player p = (Player)cs;
                if(p.hasPermission("bradlkbz.sand"))
                {
                    enableSand(p);
                    return true;
                }
                else
                {
                    p.sendMessage(ChatColor.RED + "You dont have permission for that command!");
                    return true; //Changed like you suggested
                }
              
            }
            return false;
        }
      
        public void enableSand(Player p)
        {
            if(!(this.isEnabled(p)))
            {
                this.sands.add(p.getName());
                p.sendMessage(ChatColor.GREEN + "Sand mode has been enabled!");
            }
            else
            {
                this.sands.remove(p.getName());
                p.sendMessage(ChatColor.RED + "Sand mode has been disabled!");
            }
        }
      
        public boolean isEnabled(Player p)
        {
            return sands.contains(p.getName());
        }
      
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlace(BlockPlaceEvent e)
        {
            Player p = e.getPlayer();
            Block b = e.getBlock();
            if(this.sands.contains(p.getName()))
            {
                if(b.getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) return;
                b.getWorld().spawnFallingBlock(b.getLocation(), b.getType(), b.getData());
                b.setType(Material.AIR);
                p.sendMessage("yolo" + sands.size());
            }
        }
      
    
    }
    But it works fine, as the event and command work. For some reason it just refuses to add them to the ArrayList.
     
  7. Offline

    timtower Administrator Administrator Moderator

    @hellobrad100 That is what I thought.
    You have 2 instances, that isn't the same list.
     
  8. Offline

    hellobrad100

Thread Status:
Not open for further replies.

Share This Page