Simulate Players + ChatColor

Discussion in 'Plugin Development' started by 97WaterPolo, Dec 29, 2014.

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

    97WaterPolo

    Hello!

    Earlier today I was testing a small minigame, one feature was teleporting all player's to one of two spawns based on their team. I was simply looping through all online players, if they were on white teleport to white spawn, and vice versa for blue. When I only had about 4 players, it worked flawlessly, when I had about 20 player's on, the server would freeze up when it teleported players to the correct spawn.

    I started to do some debugging, and tried to find a less taxing way to teleporting 10+ players to a un-loaded chunk. My current idea is teleporting one person from each team to the spawn, then teleport the rest of the team into the chunk that is already loaded. My only issue is I have no way to test it, as its 1AM, I can't get 20 friends on at once (was a challenge to start with) and was wondering if there was a way to simulate player's.

    As for chat color, does ChatColor.{Value} count as 1 or two characters? I am trying to figure out the proper length to substring an username.

    Any help would be appreciated.
     
  2. Offline

    1Rogue

    What's your code? There might be something in there that's causing the lag. Alternatively you can get the spawn and load the chunks around the area before teleporting people there. Though I doubt that's the issue.

    ChatColor constants resolve down to two characters. A section symbol (ยง) and the respective identifier (0-9, a-f, l-o, r).
     
  3. @97WaterPolo Not sure, but I think that ChatColor doesn't even count as characters. Because you can translate the usual &{x} color codes to ChatColor. But I'm not sure and I also didn't test it.

    You can simply test it though by creating a string ChatColor.RED + "string", then printing it's length and see if it is 6, 7, or 8 characters long.
     
  4. Offline

    97WaterPolo

    @1Rogue
    I thought generating the chunk, but with no player entities there will cause it to instantly unload?

    I changed it, the gist of it is below. I started comparing System times, something I had never done before, and I started to see which methods caused the server to skip a beat. Mainly it was the generation of a scoreboard for each player. When it was called, there was a roughly 20 difference in the current time millis, I moved that to the end and it seems to work a lot better now. I still don't know how I can test it without getting a bunch of other's on.

    for (Player p : Bukkit.getOnlinePlayers())
    if (p.getTeam == Blue
    p.teleport(BlueSpawnFromConfig);
    else
    p. teleport(WHiteSpawnFromConfig);

    Now I am currently just teleporting one player a tick before to cause the chunk to generate then bringing in the rest of the team.

    @Lionhard
    Thanks! Before i was doing ChatColor.{Value} + player.getName().subString(etc); I just realized it would be smarter to combine the two THEN substring it.
     
    Lionhard likes this.
  5. Offline

    teej107

    Chat colors count as 2 characters. For each chat color you have, it adds 2 characters to the string. Minecraft just renders the text with the ChatColor characters removed.
     
  6. Offline

    TigerHix

    Why not load the chunk before you start teleporting players?
     
  7. Offline

    1Rogue

    Well for starters you can cancel the ChunkUnloadEvent.

    As for your scoreboard, why are you generating a unique board for every player? You can (and should) give the same Scoreboard instance to every player in the minigame.
     
  8. Offline

    97WaterPolo

    @TigerHix
    I was under the impression if there isn't a player within the chunk when it is loaded, it is instantly un-loaded.

    @teej107
    I was under the impression that it was always one.

    If I was trying to set a player list name, to one that is longer than 16, cause the server to freeze up when all the player's are teleported? I would think it would only throw an error, not hold up the server. Now that I think about it, the only difference was the longer names.
     
  9. Offline

    1Rogue

    Names longer than allowed would throw an exception. If you're using scoreboards and trying to add colored names, you should place people on the Scoreboard's teams and add a team prefix to them. (per my last message saying you should have a single board assigned to everyone per game).
     
  10. Offline

    97WaterPolo

    @1Rogue
    Well the issue is, the chunk wouldn't be loaded before as it is a brand new game. Join > Lobby World > Map World meaning the map world's spawn wouldn't have been loaded.

    I am also using this for scoreboards. I like it due to the simplicity, should I use a single scoreboard instance instead?

    I am using TagAPI for the names above player's head, and I didn't believe adding a player to a team would change their tab list color.
     
  11. Offline

    1Rogue

    You would simply get the chunk at the spawn (and surrounding), call #load() on them, and then cancel their unloading via event.

    Looking at the class that you linked it looks like it accomplishes a lot of its features by butchering the features in place for you to use in minigames. So yes, don't use that library for anything minigame related or whenever everyone should have the same scoreboard.

    Using scoreboard prefixes changes both the player's name in tablist and above their head.
     
    Last edited: Dec 29, 2014
    97WaterPolo likes this.
  12. Offline

    97WaterPolo

    @1Rogue
    I am assuming the player needs to have the scoreboard the teams were created from to work.

    If I get a new scoreboard for the class, will I need to set all player's existing scoreboard to null first for them to lose the old scoreboard?
     
  13. Offline

    1Rogue

    A player can only ever have a single scoreboard. When you call .setScoreboard() whatever you pass will be their new scoreboard.
     
  14. Offline

    97WaterPolo

    @1Rogue
    Would setting the player's scoreboard to null reset it, or get a new scoreboard reset it. When I say reset, I mean remove :p

    p.setScoreboard(null);
    p.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
     
  15. Offline

    1Rogue

    null would throw an IllegalArgumentException, the latter is the correct way to remove their scoreboard.
     
  16. Offline

    97WaterPolo

    @1Rogue
    Sorry for bugging you continuously, one more, I promise :p

    So I got it to work, the only issue is it is not setting the player's color in the tab list. I modify it when the player firsts join (I set it to gray) but even after I add the player to the team and update the scoreboard, the player's name in the tab list is still gray. Is there any way to force update the tab list? Or does team prefixes not work on tab list?
     
  17. Offline

    1Rogue

    Hm, you know I think this might have broken as of 1.8. I definitely remember it doing it before that update when the tablist changed...
     
  18. Offline

    97WaterPolo

    @1Rogue
    Well I am still programming in 1.7, was there an override to update the player's name?

    EDIT: If I add the player to the team, the tab doesn't update. Once the player relogs though, it does update. Any ideas?
     
  19. Offline

    1Rogue

    Seems to be working fine for me:
    [​IMG]

    This is the code I used:

    Code:java
    1. public class BukkitTest extends JavaPlugin implements Listener {
    2.  
    3. private Scoreboard board;
    4.  
    5. @Override
    6. public void onEnable() {
    7. this.board = this.getServer().getScoreboardManager().getNewScoreboard();
    8. this.board.registerNewTeam("Example").setPrefix(ChatColor.GREEN.toString());
    9. this.getServer().getPluginManager().registerEvents(this, this);
    10. }
    11.  
    12. @EventHandler
    13. public void onJoin(PlayerJoinEvent event) {
    14. event.getPlayer().setScoreboard(this.board);
    15. this.board.getTeam("Example").addPlayer(event.getPlayer());
    16. }
    17.  
    18. }
    19.  
     
    Last edited: Dec 29, 2014
  20. Offline

    97WaterPolo

    @1Rogue
    Well the issue is, I set their player list name, and once the game starts I then add them to the team.
     
  21. Offline

    1Rogue

    Why not just add them to teams beforehand? You won't see the color changes until then.
     
  22. Offline

    97WaterPolo

    @1Rogue
    Sorry, I don't think I am being Clear.
    Lobby > Team Choosing
    1. In the lobby, as soon as they join, I change their tab-list name to gray.
    2. Once they choose their team, I of course add them to the team but their name in the tab doesn't update. The player has to re-log for it to update, I have no idea why though.
     
  23. Offline

    Samthelord1

  24. Offline

    97WaterPolo

    @Samthelord1
    It has nothing at all to do with the scores? :confused: Its the team prefix not updating till the player relogs if you manually edit the playerlist name before.
     
  25. Offline

    1Rogue

    So stop manually editing the playerlist names. The scoreboard will do all the work for you, your other methods are breaking it.
     
Thread Status:
Not open for further replies.

Share This Page