How to interact with a pre existing vanilla scoreboard

Discussion in 'Plugin Development' started by ryguy2k4, Oct 25, 2018.

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

    ryguy2k4

    I am trying to make a plugin that when a command block runs my command it will add a player's score on a scoreboard to their EssentialsX balance. How would I go about doing this, and is it even possible?

    I am sorry for so many questions, this is my first plugin and I am not very familiar with coding java. Thanks for any help
     
    Last edited: Oct 27, 2018
  2. So at first, if you want to access the vanilla scoreboard you have to use

    Code:
    Scoreboard s = Bukkit.getScoreboardManager().getMainScoreboard();
    BUT if I understand you right you want to increase the players money in EssentialsX. EssentialsX is not using a scoreboard to save the players balance. So the easiest way to add money to the players balance is to let your plugin perform the command provided by essentialsX:

    Code:
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "eco give <Name> <Amount>");
    Hope this helped you ;)
     
  3. Offline

    ryguy2k4

    Thanks a lot! Below is what I made and it works perfect from a player, but I'm trying to make it work from a command block too in the Else If statement. I'm having trouble figuring out how to change
    Code:Java
    1. Scoreboard board = player.getScoreboard();
    to
    Code:Java
    1. Scoreboard board = CraftBlockCommandSender.getScoreboard();
    Any ideas?
    The syntax is /rewardmoney <scoreboard player> <scoreboard objective> <player receiving money>

    Code:Java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    3.  
    4. Player player = (Player) sender;
    5. if (sender instanceof Player) {
    6. int rewardMoney;
    7. String Obj = args[1];
    8. String User = args[0];
    9. Scoreboard board = player.getScoreboard();
    10. Objective obj = board.getObjective(Obj);
    11. Score UsersScore = obj.getScore(User);
    12. rewardMoney = UsersScore.getScore();
    13.  
    14. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "execute @p ~ ~ ~ /eco give " + args[2] + " " + rewardMoney);
    15.  
    16. }
    17.  
    18. else if(sender instanceof CraftBlockCommandSender) {
    19. int rewardMoney;
    20. String Obj = args[1];
    21. String User = args[0];
    22. Scoreboard board = player.getScoreboard();
    23. Objective obj = board.getObjective(Obj);
    24. Score UsersScore = obj.getScore(User);
    25. rewardMoney = UsersScore.getScore();
    26.  
    27. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "execute @p ~ ~ ~ /eco give " + args[2] + " " + rewardMoney);
    28. }
    29.  
    30. else {
    31. //put action here
    32. }
    33. return true;
    34. }
     
    Last edited: Nov 21, 2018
  4. So to make it work you have to do
    Code:Java
    1. Player player = Bukkit.getPlayer(user);

    After the String user = args[0];

    Also write
    Code:Java
    1. Player player = (Player) sender;

    Inside the if block and not above it. Otherwise you'll get an error when triggering a command block (always if you get any errors in the console mention them here)

    But I still not understand how this should work because you would have to edit the content of the command block everytime another player triggers it.

    Please explain that more exactly
     
    Last edited: Nov 4, 2018
  5. Offline

    ryguy2k4

    I added your suggestions, here is my current code:




    Code:Java
    1. package com.ryguy2k4.ecoAddon.commands;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.craftbukkit.v1_12_R1.command.CraftBlockCommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.scoreboard.Objective;
    10. import org.bukkit.scoreboard.Score;
    11. import org.bukkit.scoreboard.Scoreboard;
    12.  
    13. public class RewardMoneyCommand implements CommandExecutor {
    14.  
    15.  
    16. @Override
    17. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    18.  
    19. if (sender instanceof Player) {
    20. int rewardMoney;
    21. Player player = (Player) sender;
    22. String Obj = args[1];
    23. String User = args[0];
    24. Scoreboard board = player.getScoreboard();
    25. Objective obj = board.getObjective(Obj);
    26. Score UsersScore = obj.getScore(User);
    27. rewardMoney = UsersScore.getScore();
    28.  
    29. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "execute @p ~ ~ ~ /eco give " + args[2] + " " + rewardMoney);
    30.  
    31. }
    32.  
    33. else if(sender instanceof CraftBlockCommandSender) {
    34. int rewardMoney;
    35. String Obj = args[1];
    36. String User = args[0];
    37. @SuppressWarnings("deprecation")
    38. Player player = Bukkit.getPlayer(User);
    39. Scoreboard board = player.getScoreboard();
    40. Objective obj = board.getObjective(Obj);
    41. Score UsersScore = obj.getScore(User);
    42. rewardMoney = UsersScore.getScore();
    43.  
    44. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "execute @p ~ ~ ~ /eco give " + args[2] + " " + rewardMoney);
    45. }
    46.  
    47. else {
    48. //put action here
    49. }
    50. return true;
    51. }
    52. }





    But, it still doesn't work, here is the console error
    Console Error (open)

    [16:37:10] [Server thread/WARN]: CommandBlock at (-23,81,29) failed to handle command
    org.bukkit.command.CommandException: Unhandled exception executing command 'rewardmoney' in plugin EcoAddon v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.CommandBlockListenerAbstract.executeCommand(CommandBlockListenerAbstract.java:267) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.CommandBlockListenerAbstract.executeSafely(CommandBlockListenerAbstract.java:166) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.CommandBlockListenerAbstract.a(CommandBlockListenerAbstract.java:121) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.BlockCommand.a(BlockCommand.java:95) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.BlockCommand.b(BlockCommand.java:81) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.WorldServer.a(WorldServer.java:750) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.WorldServer.doTick(WorldServer.java:304) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:742) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:371) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:651) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:555) [craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
    Caused by: java.lang.NullPointerException
    at com.ryguy2k4.ecoAddon.commands.RewardMoneyCommand.onCommand(RewardMoneyCommand.java:39) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.12.2.jar:git-Bukkit-809c399]
    ... 13 more


    Here's how it is supposed to work: I am making a minigame where when the player kills a zombie, their score goes up on a scoreboard. At the end of the game, a command block gets triggered, adding the value of their score to their balance.
     
    Last edited: Nov 21, 2018
  6. @ryguy2k4

    Something at line 39 has a value of null, could you copy that line and post it?
    (We can't see line numbers)
     
  7. ok so now i understand what you are trying to do :D
    It looks like the minigame is created in vanilla, otherwise it wouldn't make sense to trigger a command block.
    If you would control the minigame using your plugin it would be a lot more easy. But I'll try to help you the way you want it to do.
    So first of all I am not able to detect the error in your code. Next time do
    [.syntax=Java]<code here>[./syntax] (without the dots)
    and post the code from the beginning.
    But now to your code:

    I changed a few things to make it work:

    now the command block just has rewardmoney @p inside it.
    and the code would be the following:

    Code:Java
    1. if(cmd.getName().equalsIgnoreCase("rewardmoney")) {
    2. if(args.length == 1) {
    3. if(sender instanceof CraftBlockCommandSender) {
    4. Player p = Bukkit.getPlayer(args[0]);
    5. int reward = p.getScoreboard().getObjective("kills").getScore(((OfflinePlayer) p)).getScore();
    6.  
    7. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "eco give " + p.getName() + " " + reward);
    8. }
    9. }
    10. }


    this works perfectly but I don't know it it works with the mechanics of your minigame.

    Just try it out and tell if there is anything to change.
     
  8. Offline

    ryguy2k4

    Thank you so much! It worked perfectly but I just made a few minor changes to make it more customizable. I have been busy the past few weeks but I got on today and tried out your suggestion. Also I changed all my code in this thread to the Java format.

    I changed the syntax to be /rewardmoney <player receiving reward> <Scoreboard objective> <Scoreboard player that score is located on> and it worked out with a bit of troubleshooting.

    In case you're interested here's the code...

    Code:java
    1. package com.ryguy2k4.ecoAddon.commands;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.craftbukkit.v1_12_R1.command.CraftBlockCommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class RewardMoneyCommand implements CommandExecutor {
    11.  
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    15.  
    16. if(cmd.getName().equalsIgnoreCase("rewardmoney")) {
    17. if(args.length == 3) {
    18. if(sender instanceof CraftBlockCommandSender) {
    19. @SuppressWarnings("deprecation")
    20. Player p = Bukkit.getPlayer(args[0]);
    21. int reward = p.getScoreboard().getObjective(args[1]).getScore(args[2]).getScore();
    22.  
    23. Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "eco give " + p.getName() + " " + reward);
    24. }
    25. }
    26. }
    27. else {
    28. //put action here
    29. }
    30. return true;
    31. }
    32. }
     
    Last edited: Nov 23, 2018
Thread Status:
Not open for further replies.

Share This Page