Score as String

Discussion in 'Plugin Development' started by cockatoo2, Aug 23, 2014.

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

    cockatoo2

    I am making a scoreboard and need to set the score of a scoreboard as text. The scoreboard is for ranks and it won't let me set the score as a string, so I can enter the players ranks. How would I fix this/do this?
     
  2. Offline

    Dragonphase

    cockatoo2

    You can't currently set the score of a scoreboard objective to a String.
     
  3. Offline

    cockatoo2

    Dragonphase Ok then would I be able to set the display name to the rank of each player by setting that to a string?
     
  4. Offline

    Dragonphase

    cockatoo2

    The display name of an objective is a String, so yes.
     
  5. Offline

    cockatoo2

    @Dragonphase ok but when I try to set the display name as a string I get this error on the player join event (that is where I put the setup scoreboard):
    and my code is (THE ERROR IS MAINLY CAUSED AT line 65 the rank.setDisplayName):
    Code:java
    1. package me.Cockatoo2X.Main;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.scoreboard.DisplaySlot;
    16. import org.bukkit.scoreboard.Objective;
    17. import org.bukkit.scoreboard.Scoreboard;
    18.  
    19. public class Main extends JavaPlugin implements Listener{
    20.  
    21. File folder = new File(this.getDataFolder() + File.separator);
    22. File playersdata = new File(this.getDataFolder() + File.separator + "Players.yml");
    23. YamlConfiguration playerConfig = YamlConfiguration.loadConfiguration(playersdata);
    24.  
    25. private Scoreboard sb;
    26.  
    27. public void onEnable() {
    28. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    29. getLogger().info("Mage Academy Enabled!");
    30.  
    31. if(!folder.exists()){
    32. folder.mkdir();
    33. if (!(playersdata.exists())) {
    34. try {
    35. playersdata.createNewFile();
    36.  
    37. } catch (IOException e) {
    38.  
    39. }
    40. }
    41. }
    42. }
    43.  
    44. public void onDisable() {
    45. getLogger().info("Mage Academy Disabled!");
    46. pcSave();
    47. }
    48.  
    49. @EventHandler
    50. public void onPlayerJoin (final PlayerJoinEvent e){
    51. Player p = e.getPlayer();
    52. UUID pID = p.getUniqueId();
    53.  
    54. playerConfig.set("Players." + pID + ".Current Username:.", p.getName());
    55.  
    56. pcSave();
    57.  
    58. if (!(playerConfig.contains("Players." + pID + ".Rank"))){
    59. playerConfig.set("Players." + pID + ".Rank", "Apprentice");
    60. pcSave();
    61. }
    62.  
    63. String pRank = playerConfig.getString("Players." + pID + ".Rank");
    64.  
    65. pcSave();
    66.  
    67. //CREATE ALL SCOREBOARD STUFF
    68. sb = Bukkit.getScoreboardManager().getNewScoreboard();
    69. Objective rank = sb.registerNewObjective("rank", "dummy");
    70. rank.setDisplayName(pRank);
    71. rank.setDisplaySlot(DisplaySlot.BELOW_NAME);
    72.  
    73. p.setScoreboard(getScoreboard());
    74.  
    75. p.sendMessage(ChatColor.RED + "WELCOME TO THE MAGE ACADEMY!");
    76.  
    77. }
    78.  
    79. public void pcSave(){
    80. try {
    81. playerConfig.save(playersdata);
    82. } catch(IOException e) {
    83.  
    84. }
    85. }
    86.  
    87. public Scoreboard getScoreboard(){
    88. return this.sb;
    89. }
    90.  
    91. }
     
  6. Offline

    stormneo7

    cockatoo2
    Code:java
    1. playerConfig.set("Players." + pID + ".Current Username:.", p.getName());

    That's not how configs work.
     
  7. Offline

    cockatoo2

    stormneo7 Dragonphase First of all I am trying to fix a scoreboard problem and secondly that is a custom config.yml defined somewhere else in my code but as I said earlier how can I fix the problem I just listed, (my next post above this one)
     
  8. Offline

    stormneo7

    Do you know how to debug errors?... Your error's coming from that line. I suggest that you look carefully.

    Quoting your error:
    Caused by: java.lang.IllegalArgumentException: Cannot set to an empty path

    Secondly, how're you concluding that the error's with your scoreboard?
    You can't.
    Your code malfunctions before it even GETS to the scoreboard part, stopping the script entirely.
     
  9. Offline

    cockatoo2

    stormneo7 I know it is coming from that line and that is what I need help fixing I have tried rearranging my code and asked other bukkit devs for help like timtower but we can't figure it out. So can you help me fix that line?
     
  10. Offline

    stormneo7

    Explain to me what the dots [.] do inside a configuration path. If you know what they do, you should have no problem finding your issue.
     
  11. Offline

    cockatoo2

    stormneo7 the dots in a config path help separate the paths. Like:

    Players:
    UUID:​
    Name:​
    Rank:​
    Different UUID:​

    They are used to create separate sections. But I don't know how that would help
     
  12. Offline

    stormneo7

    playerConfig.set("Players." + pID + ".Current Username:.", p.getName());
    Now read the path you're trying to set.
    You should figure out why
    Caused by: java.lang.IllegalArgumentException: Cannot set to an empty path
    was thrown. If you still can't, I'll explain it.
     
  13. Offline

    cockatoo2

    stormneo7 But I am not trying to read that path! I need the get the path of
    not the player name.
     
  14. Offline

    stormneo7

    Dammit I give up.
    I'll explain it now.
    Code:
    at me.Cockatoo2X.Main.Main.onPlayerJoin(Main.java:54) ~[?:?]
    Caused by: java.lang.IllegalArgumentException: Cannot set to an empty path
    This was your error log. The problem occurs at Main.java class in line 54. I don't know where you got 65 from.
    Line 54 is
    Code:java
    1. playerConfig.set("Players." + pID + ".Current Username:.", p.getName());

    Your path for the variable is
    Code:
    "Players." + pID + ".Current Username:."
    Do you not see what's wrong with that?
    Why do you have a :. at the end of the path?
    That basically means you're trying to set
    Players => [UUID] => CurrentUsername: => null
    That's where your error triggers. Your path ends up nowhere due to the '.' at the end.

    Your code cuts off before you even reach the scoreboard coding.
     
  15. Offline

    cockatoo2

    stormneo7 ok let my try that

    stormneo7 Yeah...It worked and I feel like an idiot...

    Now let me ask one more question you will find dumb, How do I have 2 or More chat colors combined? Like Bold Red and Magic?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  16. Offline

    stormneo7

    cockatoo2
    Minecraft uses the § symbol to identify colour. So you could do p.sendMessage("§4§lHi");
    Another option is (ChatColor.RED + "" + ChatColor.BOLD + "Hi");
     
Thread Status:
Not open for further replies.

Share This Page