ArrayList not functioning properly

Discussion in 'Plugin Development' started by rcth, Oct 18, 2013.

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

    rcth

    Good day developers,

    I tried to work with ArrayList today, but it isn't functioning as it should. I asked another developer and he said it SHOULD work, so...

    Code:
    public class InvRailMaker implements Listener, CommandExecutor {
    
    Under that, I added one attribute/var:
    Code:
    private List<String> players = new ArrayList();
    
    In the command, the player should be added if he's not in the ArrayList or removed if he is in.
    Code:
    					if (!this.players.contains(player.getName())) {
    						this.players.add(player.getName());
    						player.sendMessage(ChatColor.AQUA + "EDITOR: " + ChatColor.GREEN + "ENABLED");
    						Bukkit.broadcastMessage("" + this.players); //DEBUG
    					} else {
    						this.players.remove(player.getName());
    						player.sendMessage(ChatColor.AQUA + "EDITOR: " + ChatColor.RED + "DISABLED");
    						Bukkit.broadcastMessage("" + this.players); //DEBUG
    					}
    
    When adding, it shows [PLAYERNAME]. When removing, it shows []. So that's fine.

    Now to the event:
    Code:
    	@EventHandler(priority = EventPriority.LOW)
    	public void onBlockBreak(BlockBreakEvent event) {
    
    In there, I check with an if-statement if the player is in the ArrayList:
    Code:
    if (this.players.contains(event.getPlayer().getName())) {
    
    But with my debug-lines (broadcast a specific message) it seems like that line doesn't work. The player is in the ArrayList, but not according that line. How can I fix the ArrayList or that line?
     
  2. Offline

    Rocoty

    Are you working with multiple instances without realising it? Post your main class please, that we can figure it out
     
  3. Offline

    rcth

  4. Offline

    Rocoty

    The main class, please
     
  5. Offline

    rcth

  6. Offline

    Rocoty

    There's your problem
    Code:java
    1. pm.registerEvents(new InvRailMaker(this), this); //Here you are instantiating the class once
    2.  
    3. BukkitTask CoinTimer = new TCCoinClass(this).runTaskTimer(this, 6000, 6000);
    4.  
    5. getCommand("shooter").setExecutor(new Commandshooter(this));
    6. getCommand("giveitem").setExecutor(new Commandgiveitem(this));
    7. getCommand("invrailmaker").setExecutor(new InvRailMaker(this)); //And here once again

    This means you are working with two different instances of InvRailMaker, and thus two different ArrayLists.
    Do the following instead
    Code:java
    1. InvRailMaker invrail = new InvRailMaker(this); //Instantiate the class only once
    2. pm.registerEvents(invrail, this); //and pass the one instance
    3.  
    4. BukkitTask CoinTimer = new TCCoinClass(this).runTaskTimer(this, 6000, 6000);
    5.  
    6. getCommand("shooter").setExecutor(new Commandshooter(this));
    7. getCommand("giveitem").setExecutor(new Commandgiveitem(this));
    8. getCommand("invrailmaker").setExecutor(invrail); //Twice
     
    rcth likes this.
  7. Offline

    rcth

    Thanks, that worked!
     
Thread Status:
Not open for further replies.

Share This Page