Solved Get player scoreboard value into a variable/string

Discussion in 'Plugin Development' started by Redstone_Pro_73, Feb 5, 2016.

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

    Redstone_Pro_73

    Hello everyone, I have been searching for about an hour trying to figure out how to get a scoreboard value into a string/variable to then use in Java for my plugin. An example of this would be like have a scoreboard called MyBoard (already created, so don't include that in your suggestions please), and then some random players in it with some scores. I want to get a persons score in the scoreboard with a certain players name. Thanks in advance!
     
  2. Offline

    Xerox262

    @Redstone_Pro_73
    Player#getScoreboard(); to get the scoreboard.
    Scoreboard#getObjective(int String); to get the objective
    Objective#getScore(String); to get the score of the objective.

    Edit: Not int, either DisplaySlot or String.
     
  3. Offline

    ElCreeperHD

    I think this should work. I am not expert, but give this a try.
    Code:
                  ScoreboardManager manager = Bukkit.getScoreboardManager();
                  Scoreboard board = manager.getNewScoreboard();
                  Objective objective = board.getObjective("objectivename");
                  Score score = objective.getScore(player);
    EDIT: This will not work...
     
    Last edited: Feb 5, 2016
  4. Offline

    Xerox262

    That only works if he is getting a number from a new scoreboard, which I don't know why you would, you still need to set it, which means there would be no point getting it since he's the one setting it (It would never be different from what he put in, in the first place), what you have will cause errors, because there's no objective called "objectivename" in an empty scoreboard. Posting code almost never helps.

    Edit: It wont throw any errors immediately to get an objective that doesn't exist, but it will return null, and you're not managing it, line 5 you're trying to get the score, which will throw the npe if the objective doesn't exist, again it is impossible to exist when you create an empty scoreboard, also you forgot a semi colon on line 4.
     
    Last edited: Feb 5, 2016
  5. Offline

    Redstone_Pro_73

    Okay guys, thanks for helping out. Here is what I currently got in my onCommand method:

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            String playername = player.getName();
        
            if(cmd.getName().equalsIgnoreCase("getscore")) {
        
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();
                Objective objective = board.getObjective("MyBoard");
                Score UsersScore = objective.getScore(playername);
            
                player.sendMessage(playername + " score is: " + UsersScore);
            
                return true;
        
            }
        
            return false;
        
        }
    But when I run the command I get this:

    Code:
    6:32:35 PM [INFO] Redstone_Pro_73 issued server command: /getscore
    6:32:35 PM [ERROR] null
    6:32:35 PM org.bukkit.command.CommandException: Unhandled exception executing command 'getscore' in plugin RankUpCMD v1.0
    6:32:35 PM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_65]
    6:32:35 PM at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_65]
    6:32:35 PM at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM at java.lang.Thread.run(Thread.java:745) [?:1.8.0_65]
    6:32:35 PM Caused by: java.lang.NullPointerException
    6:32:35 PM at me.Redstone_Pro_73.scoreboardtest.Main.onCommand(Main.java:32) ~[?:?]
    6:32:35 PM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Spigot.jar:git-Spigot-db6de12-18fbb24]
    6:32:35 PM ... 15 more
    
    So if you guys could come up with anything else that could possibly get the score of a currently existing scoreboard in game? Thanks
     
  6. Offline

    Xerox262

    @Redstone_Pro_73 I said that what @ElCreeperHD posted would cause a Null Pointer, when you create a new scoreboard it won't have any objectives, so when you call getObjective(); you will be getting back null, then you're trying to call getScore(); on null, you need to use what I said before.

    Player#getScoreboard(); To get the scoreboard, don't create a new one.

    Post your code for when you create and show the scoreboard, probably on join? That way we can tell you how to do what you're trying to do.
     
  7. Offline

    Redstone_Pro_73

    I don't create or show the scoreboard on join. I made the scoreboard in game just using vanilla commands. This way simple things like command blocks can interact with the scoreboard, while my plugin can still retrieve its data. I am currently just testing getting the score in a new project and checking it with a command. Here is what I got:

    Code:
    package me.Redstone_Pro_73.scoreboardtest;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    
    public class Main extends JavaPlugin {
       
        public void onEnable() {
            getLogger().info("ScoreboardTest has been enabled");
        }
       
        public void onDisable() {
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            String playername = player.getName();
           
            if(cmd.getName().equalsIgnoreCase("getscore")) {
               
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();
                Objective objective = board.getObjective("MyBoard");
                Score UsersScore = objective.getScore(playername);
               
                player.sendMessage(playername + " score is: " + UsersScore);
               
                return true;
           
            }
           
            return false;
           
        }
    
    }
    
     
  8. Offline

    Xerox262

    Ok, well then what you want to do is Instead of
    Code:
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();
    Do the following
    Code:
    Scoreboard board = player.getScoreboard();
    Try with that and see what it does.
     
  9. Offline

    Redstone_Pro_73

    I think we are getting somewhere, but this just shows up now: Redstone_Pro_73 score is: org.bukkit.craftbukikt.v1_8_R3.scoreboard.CraftScore@3652d413
     
  10. Offline

    Xerox262

    @Redstone_Pro_73 Which means you're getting the score object, you just need to get the score from it with getScore();

    More specifically UsersScore.getScore();
     
  11. Offline

    Redstone_Pro_73

    Thanks so much! This solved my issue!
     
Thread Status:
Not open for further replies.

Share This Page