Solved Scoreboard Help!

Discussion in 'Plugin Development' started by WazzapDoc, Jul 26, 2013.

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

    WazzapDoc

    I have got a huge problem with my code, my actual scoreboard won't show, can someone please say to me what's wrong, if i'm missing an obvious.
    Code:java
    1.  
    2. @EventHandler
    3. public void onJoinEvent(PlayerJoinEvent e){
    4.  
    5.  
    6. ScoreboardManager manager = Bukkit.getScoreboardManager();
    7. Scoreboard board = manager.getNewScoreboard();
    8. Objective objective = board.registerNewObjective("test", "dummy");
    9. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    10. Score scoregryff = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Gryffindor:")); //Get a fake offline player
    11. scoregryff.setScore(getConfig().getInt("Points.Gryffindor"));
    12.  
    13. Score scoreslyth = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Slytherin:")); //Get a fake offline player
    14. scoreslyth.setScore(getConfig().getInt("Points.Slytherin"));
    15.  
    16. Score scoreraven = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Ravenclaw:")); //Get a fake offline player
    17. scoreraven.setScore(getConfig().getInt("Points.Ravenclaw"));
    18.  
    19. Score scorehuffle = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Hufflepuff:")); //Get a fake offline player
    20. scorehuffle.setScore(getConfig().getInt("Points.Hufflepuff"));
    21.  
    22.  
    23. }
     
  2. Offline

    soulofw0lf

    well that should all be in your onEnable instead of on player join and then you need to send the board to the players when they join
     
  3. Offline

    WazzapDoc

    How would i do that? Now I am getting an error in player, cause it's not found in the scoreboard, I am trying to create a variable for the scoreboard but still doesn't work.
     
  4. Offline

    NerdsWBNerds

    You have to do player.setScoreboard(board);

    and if any of those scores are = 0 they won't show up, you have to set the score to -1 then 0.
     
  5. Offline

    Vislo

  6. Offline

    WazzapDoc

    Vislo
    Yeah I know, but I am not sure where he gets the

    Code:java
    1. player.setScoreboard(board);
     
  7. In the PlayerJoinEvent you use event.getPlayer().setScoreboard(board);
    The "board" is the Scoreboard object.
    Ontop of your code, under the public class Blahblah in your main class type:
    Code:
    public Scoreboard defaultScoreboard = null;
    Then in the onEnable() method type:
    Code:
    ScoreboardManager manager = Bukkit.getScoreboardManager();
    Scoreboard board = manager.getNewScoreboard();
    Objective objective = board.registerNewObjective("test", "dummy");
    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    Score scoregryff = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Gryffindor:")); //Get a fake offline player
    scoregryff.setScore(getConfig().getInt("Points.Gryffindor"));
    Score scoreslyth = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Slytherin:")); //Get a fake offline player
    scoreslyth.setScore(getConfig().getInt("Points.Slytherin"));
    Score scoreraven = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Ravenclaw:")); //Get a fake offline player
    scoreraven.setScore(getConfig().getInt("Points.Ravenclaw"));
    Score scorehuffle = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Hufflepuff:")); //Get a fake offline player
    scorehuffle.setScore(getConfig().getInt("Points.Hufflepuff"));
     
    this.defaultScoreboard = board;
    
    In the PlayerJoinEvent add:
    Code:
    event.getPlayer().setScoreboard(plugin.defaultScoreboard);
    The object "plugin" is your plugin object, if your listener is in the main plugin class just use this.defaultScoreboard.
     
  8. Offline

    WazzapDoc

    KingFaris11

    It says that the scoreboard cannot be null which I don't really understand :s

    I have also put everything you told me to do and still doesn't work :p


    Code:
    13:01:06 [SEVERE] Could not pass event PlayerJoinEvent to WizardNetworkHousePoints vBETA 1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at net.minecraft.server.v1_6_R2.PlayerList.c(PlayerList.java:206)
        at net.minecraft.server.v1_6_R2.PlayerList.a(PlayerList.java:102)
        at net.minecraft.server.v1_6_R2.PendingConnection.e(PendingConnection.java:130)
        at net.minecraft.server.v1_6_R2.PendingConnection.d(PendingConnection.java:43)
        at net.minecraft.server.v1_6_R2.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:41)
        at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:29)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.IllegalArgumentException: Scoreboard cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.craftbukkit.v1_6_R2.entity.CraftPlayer.setScoreboard(CraftPlayer.java:990)
        at me.tymor51.WizardNetworkHousePoints.WizardNetworkHousePoints.OnJoin(WizardNetworkHousePoints.java:392)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 14 more
    
     
  9. You didn't read the end of my code for the long code, I said to put in onEnable() under your code about the Scoreboard scores,
    Code:
    this.defaultScoreboard = board;
     
  10. Offline

    WazzapDoc

    KingFaris11

    oh thanks :)

    KingFaris11
    The only thing that doesn't work is that it doesn't update by itself, when I add an amount to it, I need to reload then log out and log back in :S

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  11. Of course, it's not meant to update itself. Make a method "updateScores()" in the main class and inside of it add:
    Code:
    Objective objective = this.defaultScoreboard.getObjective("test");
    objective.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Gryffindor:")).setScore(getConfig().getInt("Points.Gryffindor"));
    objective.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GREEN + "Slytherin:")).setScore(getConfig().getInt("Points.Slytherin"));
    objective.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Ravenclaw:")).setScore(getConfig().getInt("Points.Ravenclaw"));
    objective.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Hufflepuff:")).setScore(getConfig().getInt("Points.Hufflepuff"));
    
    The under that, inside the updateScores method, add:
    Code:
    for (Player onlinePlayer : this.getServer().getOnlinePlayers()) {
        if (onlinePlayer != null) onlinePlayer.setScoreboard(this.defaultScoreboard);
    }
    
    What this does is loops through every player on the server and updates their scoreboard to the new scoreboard.
    I'm not sure if this is needed as originally the scoreboard object is the "defaultScoreboard" and it's already been set to every player. Just add it incase and if someone could reply to this comment saying if it is needed.

    Then every time you edit the scores, just call the updateScores method.
     
  12. Offline

    WazzapDoc

  13. No problem. :) I sometimes need help myself and many people do help me so I think it's only fair to help other people.
     
    akabarblake likes this.
Thread Status:
Not open for further replies.

Share This Page