PlayerJoinEvent cannot read ArrayList?

Discussion in 'Plugin Development' started by name123321, May 22, 2018.

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

    name123321

    Code:
    package de.Fisch.Fischplugin.commands;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class VanishCommand implements CommandExecutor, Listener {
    
      
       public ArrayList<Player> VanishList = new ArrayList<>();
    
       @Override
       public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
         if (sender instanceof Player) {
    
           Player p = (Player) sender;
           if (p.hasPermission("Fischplugin.Vanish.togglevanish")) {
             if (args.length == 0) {
               if (!(VanishList.contains(p))) {
                 VanishList.add(p);
                 for (Player players : Bukkit.getOnlinePlayers()) {
                   players.hidePlayer(p);
                 }
                 p.sendMessage("§aDu bist nun unsichtbar!");
               } else {
                   for (Player players : Bukkit.getOnlinePlayers()) {
                     players.showPlayer(p);
                   }
                   p.sendMessage("§aDu bist nun wieder sichtbar.");
                   VanishList.remove(p);
                 }
              
             } else
               p.sendMessage("§cBenutze /vanish!");
           } else
             p.sendMessage("§cDazu hast du keine Rechte!");
         }
         return false;
       }
    
       @EventHandler
       public void onPlayerVanishJoin(PlayerJoinEvent e) {
         Player p = e.getPlayer();
        
         for (Player Players : Bukkit.getOnlinePlayers()) {
          
           if(VanishList.contains(Players)) {
            
          
               p.hidePlayer(Players);
            
           }
            
          
         }
       }
    
    }

    /*I tried to write a vanishplugin and wanted to add a Method that vanishes all Players registered by my
    vanish command for every person that joines the server.
    I don't know why this doesn't work like this.
    i checked it once with the '.isEmpty' method and the result was true.
    (Eventlistener and Command are correctly registered)
    */
     
    Last edited by a moderator: May 22, 2018
  2. Offline

    timtower Moderator Moderator

    @name123321 My first guess is that you have 2 instances of the same class.
     
  3. Offline

    name123321

    Thanks for your answer,
    What does this actually mean for me.
    Im not that experienced with java and both methods are registered and giving an output.
     
  4. Offline

    PhantomUnicorns

    Adding to what @timtower said, I think you create two instances when you register the command and register the event listener. Two fixes may be to make the instance variable static (not recommended) or to use one instance (recommended).
     
  5. Offline

    timtower Moderator Moderator

  6. Offline

    name123321

    @timtower @PhantomUnicorns thanks a lot for your help:)
    It actually worked.


    @timtower
    Code:
    package de.Fisch.Fischplugin.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    import de.Fisch.Fischplugin.commands.ChatclearCommand;
    import de.Fisch.Fischplugin.commands.EnchCommand;
    import de.Fisch.Fischplugin.commands.FischOpCommand;
    import de.Fisch.Fischplugin.commands.FischnessInvCommand;
    import de.Fisch.Fischplugin.commands.FlyCommand;
    import de.Fisch.Fischplugin.commands.GmCommand;
    import de.Fisch.Fischplugin.commands.HealCommand;
    import de.Fisch.Fischplugin.commands.PiCommand;
    import de.Fisch.Fischplugin.commands.SetCommand;
    import de.Fisch.Fischplugin.commands.SetspawnCommand;
    import de.Fisch.Fischplugin.commands.SpawnCommand;
    import de.Fisch.Fischplugin.commands.SunCommand;
    import de.Fisch.Fischplugin.commands.VanishCommand;
    import listeners.AntiGrief;
    import listeners.Joinplayer;
    import listeners.KlickAxt;
    //import listeners.PlayerEatInfSteak;
    
    
    
    public class Main extends JavaPlugin{
    
       public static Main plugin;
       
      public void onEnable() {
       plugin=this;
         
       getCommand("heal").setExecutor(new HealCommand());
       getCommand("fly").setExecutor(new FlyCommand());
       getCommand("setspawn").setExecutor(new SetspawnCommand());
       getCommand("spawn").setExecutor(new SpawnCommand());
       getCommand("sun").setExecutor(new SunCommand());
       getCommand("Fisch").setExecutor(new FischOpCommand());
       getCommand("chatclear").setExecutor(new ChatclearCommand());
      getCommand("avoidlist").setExecutor(new AntiGrief());
      getCommand("pi").setExecutor(new PiCommand());
      getCommand("FischnessInv").setExecutor(new FischnessInvCommand());
      getCommand("ench").setExecutor(new EnchCommand());
      getCommand("gm").setExecutor(new GmCommand());
      getCommand("set").setExecutor(new SetCommand());
      getCommand("vanish").setExecutor(new VanishCommand());
       
      PluginManager pm= Bukkit.getPluginManager();
       pm.registerEvents(new Joinplayer(),this);
       pm.registerEvents(new KlickAxt(),this);
       pm.registerEvents(new AntiGrief(),this);
       pm.registerEvents(new VanishCommand(),this);
       //pm.registerEvents(new PlayerEatInfSteak(),this);
       System.err.println("[Fischplugin] ");
       System.err.println("[Fischplugin] Fischplugin geladen.");
       System.err.println("[Fischplugin] ");
       
    }
    public static Main getPlugin() {
     return plugin;
     }
    }
     
    Last edited by a moderator: May 22, 2018
  7. Offline

    timtower Moderator Moderator

    @name123321 Don't log your own plugin please, Bukkit does that for you.
     
  8. Offline

    PhantomUnicorns

    Code:
    getCommand("vanish").setExecutor(new VanishCommand());
    ...
    pm.registerEvents(new VanishCommand(),this);
    
    You see how you are making two new VanishCommand's? Make a variable, and use that in both of its places.
     
Thread Status:
Not open for further replies.

Share This Page