Solved Arena players list?

Discussion in 'Plugin Development' started by thewalkingplay, Jul 4, 2015.

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

    thewalkingplay

    Hi, I'm making a minigame plugin and I need to make a list of players in each arena, this will work: ?
    Code:
        private String name;
        private static Location spawn;
        private static Location lobby;
        private static int Nplayers;
        private static List<Arena> allArenas = new ArrayList<Arena>();
        private static List<Player> playersInTheArena;
    
        public Arena(String name, Location spawn, Location lobby) {
            this.name = name;
            Arena.spawn = spawn;
            Arena.lobby = lobby;
            Nplayers = 0;
            allArenas.add(this);
            playersInTheArena = new ArrayList<Player>();
        }
    (the list playersInTheArena) will work? I don't have anyone to test the plugin, and , if this is whrong, what can I do?

    sorry for my english, I'm brazilian
     
  2. Offline

    Synapz

    Yes that will work! It would be better though to do...
    Code:
    private List<Player> playersInTheArena = new ArrayList<Player>();
    
    So you don't have to do it in the constructer, but instead in the variables. The same way you did it to the Arena list. And remove static keywords on all of them!
     
    Last edited: Jul 4, 2015
  3. Offline

    BagduFagdu

    If you have to store two Objects (Arena & Player), then use a HashMap<Arena, Player>.
     
    MarinD99 likes this.
  4. Offline

    mythbusterma

    Actually, none of this will work, assuming he wants to create more than one Arena.


    @thewalkingplay

    If you want this to have any semblance of "working," remove all the "static" keywords from the class and store the Player's UUID rather than their Player object. Also, don't have the Arena class keep track of all of it's instances, that's just tacky, keep track of them in another location. Furthermore, since you'll have a Set of all the UUIDs of the players in the arena, there's not need to keep track of the number of players since you can do Collection#size() on the Set.

    You may want to read this: The Java™ Tutorials
     
  5. Offline

    thewalkingplay

    @mythbusterma keep the playersInTheArena in the constructor?
     
  6. Remove static modifier
     
  7. Offline

    thewalkingplay

    @MaTaMoR_
    Code:
    private String name;
        private Location spawn;
        private Location lobby;
        private List<Arena> allArenas = new ArrayList<Arena>();
        private List<UUID> playersInTheArena;
    
        public Arena(String name, Location spawn, Location lobby) {
            this.name = name;
            this.spawn = spawn;
            this.lobby = lobby;
            allArenas.add(this);
            playersInTheArena = new ArrayList<UUID>();
        }
    just remove?
     
  8. Offline

    Lolmewn

    You should probably not have a list with all arena's in your Arena class. Instead, I would make an ArenaManager which is a glorified HashMap<String, Arena>.
     
    Konato_K likes this.
  9. Offline

    thewalkingplay

    @Lolmewn I alread did an arenaManager with a HashMap<String, Arena> but, with this, I can't get the number of players
     
  10. Offline

    Lolmewn

    @thewalkingplay A player is in one arena at a time right? Just iterate over all arena's and sum the count of players up.
     
  11. Offline

    thewalkingplay

    @Lolmewn aways returns 1
    Code:
    public static int getPlayersInArena(String arena) {
            int n = 0;
            for (String s : playersInArena.keySet()) {
                if (playersInArena.get(s) == arena) {
                    n++;
                }
            }
            return n;
        }
     
  12. Offline

    Konato_K

  13. Offline

    Lolmewn

    @thewalkingplay Just return playersInArena.size(), no need to make it static either.
     
  14. Offline

    thewalkingplay

    @Lolmewn playersInArena is a hashmap with all arena and the players, (HashMap<String, String> string1 is player and string2 is the arena) I made this class before the Arena object

    anyone?

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

    mythbusterma

  16. Offline

    thewalkingplay

    @mythbusterma I know that it's a noob question but, what is the difference between static and non-static methods? I don't found any javadoc related to this, probably becuse the way that I seach .-.
     
  17. Offline

    Konato_K

  18. Offline

    thewalkingplay

    @Konato_K thx, now I need to remove all the static keys from my plugin, it will take same hours .-.

    now 98% of the classes are giving me the error: http://prntscr.com/7r7dka but none of the classes have the word "static" (I don't jnow if I whrote this correctly)

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

    Evaluations

    You should be able to figure that one out..
     
  20. Offline

    thewalkingplay

    @Evaluations I think that no ;-;

    anyone?

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

    justin_393

    Just saying. If you can't figure that error out, don't you think a mini game is a little advanced?
    @mythbusterma
    Actually, it's more efficient to store a player instead of a UUID because of the conversions. As lon as you clean up when needed you won't have any memory leaks.
    Either that or store a WeakReference.
     
  22. Offline

    mythbusterma

    What's all this talk about "efficiency?" Is there any reasonable usage case where the difference between using a reference and doing a lookup over 100 items matters in Bukkit? No, didn't think so. Nobody on these forum even thinks to clean up after themselves, that's why.
     
  23. Offline

    justin_393

    Even if you stored a UUID instead you'd still want to clean up, otherwise you're going to have useless Onjects hanging around. And yes, efficiency, why make the plugin convert say, 100 players to UUIDs and store that instead of saving the processing power to just store the Player? Much simpler.
     
  24. Offline

    mythbusterma

    @justin_393

    You still haven't shown me where it actually makes a difference, ever. The processing power it takes is so far beyond insignificant it's a moot point.

    Because, leaving a small data Object hanging around is no big deal, whereas keeping an entire world loaded because nobody on the Bukkit forums is smart enough to remove the Player object is a pretty significant performance hit. Especially when they can't figure out what the "static" modifier does.

    I do it in my own plugins as well, because with the amount of data structures I have, I'm certain to lose track of a Player somewhere. It's just a precautionary measure that doesn't really make a difference.
     
  25. Offline

    thewalkingplay

    ... I reamake all my arena system with the object arena rather than the string arena, and it works, I just used the HashMap<Player, Arena> to get the actually player arena and check if the player is in an arena. Thanks everybody
     
Thread Status:
Not open for further replies.

Share This Page