Help in static ArrayList

Discussion in 'Plugin Development' started by dominikremes, Jan 17, 2017.

Thread Status:
Not open for further replies.
  1. Hello. I`ve got a problem. How can I add somethink to a static ArrayList? It needs to be static, becouse that public void witch uses it is static too because it is used in an another class. Here is that part of my code:
    Class1
    Code:
        public static ArrayList<Player> playersInLobby = new ArrayList<Player>();
        
    public static void onJoin(Player p) {
            playersInLobby.add(p);
            teleportToArena(p); //This is an another public static void
            bossBarCounting(p); //This is an another public static void
            }
    And now hre is that part of the another class:
    Class2
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("command")) {
                    Class1.onJoin(p);
                    return true;
                }
       
            return true;
            }
    When I want to use that arraylist then it is allways empty and I do not have playersInLobby.remove(p);
    Thanks for help
     
  2. Offline

    Zombie_Striker

    @dominikremes
    There is no reason for it to be static, nor is there a reason for a reason for that method to be static. Just pass the instance through the other class's constructors.

    Here's a rule of thumb: if you do you know what static means or how to manage static objects, or if the object does not need to be static, do not make it static.
     
  3. Offline

    callum.thepro

    As Zombie_Striker said, you shouldn't really be using static unless you absolutely have to. You should check out some videos on Java so that you can get a decent understanding of objects and how to circumvent using static.

    For the sakes of answering the question, you'd enter

    Code:
    Class1.playersInLobby.add(p);
    
    Also, in your code, you do
    Code:
    Player p = (Player) sender;
    You need to check the type of this before casting because if the sender isn't a player (e.g. the console) sends the command then your cast will fail.
     
  4. I know what ststic means but I can not do non static it. but I trie to move those classes together and it worked. And yes I know that if I send it from consol it will fail bat I allways do first the functionality of a plugin and then I correct the bugs.
     
  5. @dominikremes
    Telling from the fact that you say you "can not non-static it", I don't believe you fully understand where and when to use static in the best way. There is no reason this should be static, so make it not be static (it can be done, I promise you).
     
  6. I know but i do not understnd how to do it without ststic. Now I put it all in 1 class and I have got an another problm with it. The List is not static but it is because of some reeason loosing the player again. Here is the code.
    Code:
    public List<Player> playersInLobby = new ArrayList<Player>();
      
        public Location mainSpawn = new Location(Bukkit.getServer().getWorld("Annihilation"), 0, 5, 0);
      
        public Plugin plugin;
     
        public Annihilation(Plugin plugin){
                this.plugin = plugin;
        }
    
        /**
         * Commands
         */
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("annihilation")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("Len pre hracov");
                }
                if (args.length == 1 ) {
                if (args[0].equalsIgnoreCase("join")) {
                    onJoin(p);
            //        playersInLobby.add(p);
                    for (int i = 0; i < 10; i++) {
                        Bukkit.getServer().broadcastMessage(playersInLobby.toString() + "command");
                    }
                  
                    return true;
                } else if (args[0].equalsIgnoreCase("createArena")) {
                    createArena(p);
                }
      
            return true;
            } else {
                p.sendMessage(Main.getInstance().prefix + ChatColor.GOLD + "" + ChatColor.BOLD + "/annihilation join");
              
                return true;
            }
        }
            return true;
        }
      
      
      
        /**
         * Public voids
         */
      
        public void createArena(Player p) {
          
        }
      
        public void onJoin(Player p) {
            playersInLobby.add(p);
            teleportToArena(p);
            bossBarCounting(p);
            for (int i = 0; i < 10; i++) {
                Bukkit.getServer().broadcastMessage(playersInLobby.toString() + "join");
            }
          
            }
      
        public void teleportToArena(Player p) {
            p.teleport(mainSpawn);
            for (int i = 0; i < 10; i++) {
                Bukkit.getServer().broadcastMessage(playersInLobby.toString() + "tp");
            }
            }
          
      
    
      
    
     
        public void Respawn(final Player player,int Time){
            Bukkit.getScheduler().runTaskLater(this.plugin, new Runnable() {
                     
                @Override
                public void run() {
                    ((CraftPlayer)player).getHandle().playerConnection.a(new PacketPlayInClientCommand(EnumClientCommand.PERFORM_RESPAWN));
                        }
                    },Time);
        }
      
      
      
      
      
        /**
         * Boss Bars
         */    
      
        public void removeBossBar(Player p) {
            try {
                BarAPI.removeBar(p);
            } catch(Exception e) {
                p.sendMessage("BarAPI plugin is not installed to this server!");
            }
        }
      
        public void showBossBar(Player p) {
            try {
                BarAPI.setMessage(p, "Annihilation Started");
            } catch(Exception e) {
                p.sendMessage("BarAPI plugin is not installed to this server!");
            }
          
        }
      
        public void bossBarCounting(Player p) {
            try {
              
                BarAPI.setMessage(p, "Čaká " + (playersInLobby.size() + 1) + " hráčov v lobby", (float)(playersInLobby.size() + 1) * 5);
            } catch(Exception e) {
                p.sendMessage("BarAPI plugin is not installed to this server!");
            }
        }
      
      
      
      
        /**
         * Events
         */
      
        @EventHandler
        public void onDeath(PlayerDeathEvent e) {
            for (int i = 0; i < 10; i++) {
                Bukkit.getServer().broadcastMessage(playersInLobby.toString() + "death");
            }
            Player p = e.getEntity();
            if (playersInLobby.contains(p)) {
            playersInLobby.remove(p);
        //    removeBossBar(p);
            Respawn(p,1);
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
                { public void run() {
                    p.chat("/spawn");
                    }
                }, 20);
            }
        }
      
        @EventHandler
        public void onLeave(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            if (playersInLobby.contains(p)) {
            playersInLobby.remove(p);
            removeBossBar(p);
            }
        }
    Player is not found in arrayList in PlayerDeathEvent. Can you help me how to fix it? Once I had it but now it is again wrong and I dont know why. If you dons=t understand omethink because of the language it is because Im creating this annihilation minigame for a slovakish server
     
  7. Offline

    ipodtouch0218

    1. Check that "sender" is a Player before casting
    2. Don't store Player's instances, as they will cause Memory Leaks. Store their UUID instead.

    What do you mean that "the player is not found in arrayList in PlayerDeathEvent"?
     
Thread Status:
Not open for further replies.

Share This Page