Storing An Int,Temporarily

Discussion in 'Plugin Development' started by FOEVERWAR73, Aug 7, 2014.

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

    FOEVERWAR73

    Hello Guys, I have ran into a problem, I am trying to store an int so i can make a drop party plugin. I am stuck on when a player joins, it checks how many players are in, and if that amount is higher than 4, it will start the drop party, but I can't find a way to check how many players are in the party, anyone got anything?
     
  2. Offline

    Giraffeknee

  3. Offline

    FOEVERWAR73

    thanks..i feel dumb now..
     
  4. Offline

    Giraffeknee

    As do I, lol @ .size() fail...
     
  5. Offline

    Giraffeknee

    Isn't it an array of online players (so .length)? KingFaris11
     
  6. 1.7.10 returns a Collection, 1.7.9- returns an Array.
    Collection has .size() and Array has .length.

    (By the way - .length is not a method, it's a global public variable therefore no brackets)

    http://jd.bukkit.org/rb/apidocs/org/bukkit/Server.html#getOnlinePlayers()
    http://jd.bukkit.org/dev/apidocs/org/bukkit/Server.html#getOnlinePlayers()

    Notice how the first link (recommended build (1.7.9-)) returns Player[] but the second link (dev builds (1.7.10)) return a Collection<? extends Player>
     
  7. Offline

    FOEVERWAR73

    Code:
            if (cmd.getName().equalsIgnoreCase("dp")) {
                if(playerinparty.contains(p.getName())&&dp != 0){
                    playerinparty.remove(p.getName());
                    dp--;
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "spawn "+p.getName());
                    p.sendMessage(starter+ChatColor.RED+"You Left The Party!");
                    return true;
                }
                if(playerinparty.contains(p.getName())&&dp == 0){
                    playerinparty.remove(p.getName());
                    //Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "spawn "+p.getName());
                    p.sendMessage(starter+ChatColor.RED+"You Left The Party!");
                    return true;
                }
                playerinparty.add(p.getName());
                p.sendMessage(starter+ChatColor.GREEN+"You Joined The Party!");
                dp++;
                for(Player onp : Bukkit.getOnlinePlayers()){
                    if(playerinparty.contains(onp.getName())&& Bukkit.getOnlinePlayers().length < 4){
                        int onpi = Bukkit.getOnlinePlayers().length;
                        onp.sendMessage(starter+ChatColor.GREEN+p.getDisplayName()+" Has Just Joined The Party! "+ChatColor.GOLD+onpi+"/4");                   
                    }
                    if(playerinparty.contains(onp.getName()) && Bukkit.getOnlinePlayers().length == 4){
                        int onpi = Bukkit.getOnlinePlayers().length;
                        onp.sendMessage(starter+ChatColor.GREEN+p.getDisplayName()+" Has Just Joined The Party! All Requires Met, Ready To Rock!");
                        //TODO TP
                    }
                }
            }
            return true;
    Honestly, I have a new problem, here's my code, I want to retreive the length of players inside of the arraylist playerinparty, any ideas?
     
  8. Offline

    Giraffeknee

  9. I use this in my Utils class:
    Code:
        public static Collection<? extends Player> getOnlinePlayers() {
            Object players = Bukkit.getOnlinePlayers();
            return players instanceof Collection ? (Collection<? extends Player>) players : (players instanceof Player[] ? Arrays.asList((Player[]) players) : new ArrayList<Player>());
        }
    
    ... playerinparty.size()

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. Offline

    AoH_Ruthless

    KingFaris11
    It's not a big difference, but you could improve performance of that utility method by providing size values to your Arraylist and Array (the numerical value being Bukkit.getOnlinePlayers().length, since you have already past the first ternary check)
     
  11. Offline

    1Rogue

    http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7

    Should also mention field is final, since array size is immutable.
     
  12. Offline

    1Rogue

    I'm expanding upon your answer.
     
    KingFaris11 likes this.
  13. Offline

    xTrollxDudex

    Afaik, that fails to do its job. Have you tried it yet?
     
  14. I believe I have this method in my minigame plugin, I think I used this method and it worked, or, at least didn't throw an error. Let me check.
     
Thread Status:
Not open for further replies.

Share This Page