Solved Alternate to OfflinePlayer

Discussion in 'Plugin Development' started by kameronn, Jan 10, 2017.

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

    kameronn

    This causes a huge delaay on my server and I need it for my scoreboards

    This is the create method, it works but when mojang gets the offline player it takes a huge amount of time which is causing my server to lag quite a bit.

    Code:
    public void create(Player player) {
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(title);
    
            int index = scores.size();
    
            for (Entry<String, Integer> set : scores.entrySet()) {
                Score score = null;
    
                int number = set.getValue() != null ? set.getValue() : index;
    
                if (set.getKey().length() > 16) {
                    int mid = set.getKey().length() / 2;
                    String[] parts = { set.getKey().substring(0, mid), set.getKey().substring(mid) };
                    Team team = scoreboard.registerNewTeam(parts[1]);
                    team.setPrefix(parts[0]);
                    OfflinePlayer fake = Bukkit.getPlayer(parts[1]);
                    team.addPlayer(fake);
                    score = objective.getScore(fake.getName());
                }
    
                if (score == null) {
                    score = objective.getScore(set.getKey());
    
                }
    
                index -= 1;
                score.setScore(number);
    
            }
    
            player.setScoreboard(scoreboard);
    
        }
    }
     
    Last edited: Jan 11, 2017
  2. Offline

    Zombie_Striker

    @kameronn
    What are you going to use it for? Setting scores?
     
  3. Offline

    kameronn

    @Zombie_Striker
    Making my scoreboard support characters up to 32 characters long. I create a team and split the string and set one part of the string to the offlineplayer name and add that player to the team and use offlineplayer.getName();
     
  4. Offline

    Zombie_Striker

  5. Offline

    DoggyCode™

    What I do is that I have my own Player object, which includes a name and UUID variable and getName GETTER. I then tell my plugin to efficiently keep track of all the player names of people whom have played on my server (I don't know if you need players who have not played on your server). This includes updating the Object's name whenever they join the server, update Objects' names on intervals by comparing their UUID to their names and update if they don't belong according to Bukkit. You can easily keep track of all the objects so you can later look for players who are offline and get their name.
     
  6. Offline

    kameronn

    @Zombie_Striker
    I'm assigning an offlineplayer to a team and getting his name not getting the score.

    Code:
            OfflinePlayer fake = Bukkit.getPlayer(parts[1]);
            team.addPlayer(fake);
    
    // parts[1] is the second part of the splitstring
    @DoggyCode™
    I'm sorry I should've provided code, I'm not trying to keep track of OfflinePlayers but using them to make strings longer than 16 characters on my sidebar scoreboard. (Updated code on thread)

    EDIT: Well, that was a surprisingly easy fix, so what I had done was created a new object called ScoreboardPlayer and after i looked at the bukkit source I have seen that OfflinePlayer was an inferance so I let ScoreboardPlayer implement OfflinePlayer.

    I then added unimplemented methods, created a constructor that asked for a string name and setup a getter then made ScoreboardPlayer.getName() return the name string that was put in the constructor PROBLEM FIX!
     
    Last edited: Jan 11, 2017
  7. Offline

    DoggyCode™

    What's the point of implementing OfflinePlayer, wouldn't that create unnecessary methods which would basically just returnn a value like false or 0 on stuff that returns boolean or int?
     
  8. Offline

    kameronn

    @DoggyCode™
    Code:
    team.add();
    only accept player, entity and offlineplayer types im pretty sure

    From my knowledge Bukkit.getOfflinePlayer(""); check for the UUID to see if it exists which takes a bit of time and by doing this it cuts that down because its not needed for what im doing
     
  9. Offline

    DoggyCode™

    Alright!
     
Thread Status:
Not open for further replies.

Share This Page