Randomize Team of 2 on Command, give each player Wool and Compass

Discussion in 'Spigot Plugin Development' started by ThatBraxGuy, May 16, 2017.

  1. I'm developing a plugin that will, on command, randomize all online players into teams of two. I know there are a couple different ways to do this, but I'm wondering which one you guys recommend. Hashmaps, Objects, etc, and what the best way to accomplish that would be. I haven't developed with Java in at least two years now, so I'm still really rusty and a ton has changed (We were all still using Bukkit back then haha), so as specific as you can be will be great.

    All in all, the plugin needs to put each player into a scoreboard team of two, give each player a color of wool corresponding to the team color, and then the most important part, a compass that will point towards their teammate. This is the code I have for that:

    Code (Text):
    Map<Player, Player> teammates = new HashMap<Player, Player>();

    [USER=17846]@EventHandler[/USER]
    public void PlayerMoveListener(PlayerMoveEvent event){
        Player player1 = event.getPlayer(); //the player moving
        Player player2 = teammates.get(player1); //the teammate
        player2.setCompassTarget(player1.getLocation()); //setting the compass
    }
    but other than that I'm starting pretty much from scratch. Any help appreciated. Looking to learn.
     
    #1 ThatBraxGuy, May 16, 2017
    Last edited: May 16, 2017
  2. Why are you using the object Player in the hashmap. Use a UUID babe.

    For getting all the players, do something like this:
    Code (Text):
    ArrayList<UUID> tempList = new ArrayList<UUID>();
    for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    tempList.add(p.getUniqueId());
    }
    //Split them and blah blah blah using tempList
     
    #2 Fluddershy, May 16, 2017
    Last edited: May 16, 2017
  3. Using a UUID is not really needed as long as you use the Player object correctly.
     
  4. I'm almost completely new to all of this. Would a UUID be better to make sure I don't break anything?
     
  5. Using a UUID is to tell apart from players no matter what. So, if they change there name. You can never be too safe. If this is a minigame and they log out and it doesn't matter, then it should be fine using player names.
     
  6. Gotcha. Thanks a bunch for the help on that
     
  7. you can use either uuid objects or the player objects. however, overall you should perform healthy code execution and manage the storage of the objects wisely. you don't want a player object sitting around in memory even after they've logged out etc, this results in a memory leak. overall up to you, just depends if you now what you're doing in regards of object management.

    really it wouldn't matter. i doubt someone would change their name mid-game of something, not only that, you'd be logging / storing / retrieving data for the player object under the uuid, not the player name.

    if i changed my name from ChefJava to CookJava mid game, and i was still being managed by the same player object, via data storage etc, i would be remain intact with the gameprofile / player object due to the uuid.