Config CraftPlayer{name=RisenAngel}?

Discussion in 'Spigot Plugin Development' started by yhl, Apr 25, 2017.

  1. yhl

    yhl

    I have been messing around with a plugin for a while and I've come across this issue where when I type a command with a argument (that becomes a player (target variable in the code)) instead of replacing the value in the config of the actual player it creates a new one called CraftPlayer{name=RisenAngel}

    Heres the code: https://pastebin.com/rgVBY3uw
     
  2. yhl

    yhl

    The command was 'reset' by the way
     
  3. When you try to print a Player, the method: toString() is called. And for the player the toString() return
    Code (Text):
    CraftPlayer{name=RisenAngel}
    So if you want the name of the player you have to do like this:
    Code (Text):
    player.getName(); or player.getDisplayName();
     
  4. That looks like the result of toString(), which is called when you try to save an object as a string.

    Use the player's UUID instead.
     
  5. yhl

    yhl

    Im getting an error on this string:
    Code (Text):
    target = Bukkit.getServer().getPlayer(args[0]).getUniqueId();
    Heres my whole new code: https://pastebin.com/tvzi0Cy9
     
  6. yhl

    yhl

    and I'm still getting the CraftPlayer{name=RisenAngel} thing
     
  7. You can't add anything but Strings, numeric values and ConfigurationSerializables (and maybe one or two things I don't have in mind at the moment) to a config. You have to get other objects like players by one of these things. So, you can't add a UUID to your config, neither. Fortunately, a UUID can easily be fetched from a String.
    String string = player.getUniqueId().toString();
    UUID uuid = UUID.fromString(string);
     
  8. yhl

    yhl

    So How would I use it?
     
  9. You don't need to change anything.. it's just a "mistake" here:

    Code (Text):
    if (cmd.getName().equalsIgnoreCase("reset")) {
                target = Bukkit.getServer().getPlayer(args[0]);
               
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + "Cannot find player '" + args[0] + "'.");
                    return true;
                }
               
                if(args.length == 1){
                    blocksTraveled.put(target.getName(), 0); // HERE HERE HERE
                    p.sendMessage("Reseted.");
                    target.sendMessage("Your stats have been reseted.");
                }
    }
     
  10. yhl

    yhl

    Is that fixed or are u showing me where it is
     
    • Funny Funny x 1
  11. yhl

    yhl

    I still get the same CraftPlayer thing
     
  12. yhl

    yhl

  13. Remove and replace the config file? Probably that
    Code (Text):

    getConfig().set(p.getName() +".traveled", blocksTraveled.get(p));
    Your have an HashMap<String, Integer>. So to get an element you need to set a string as parameter. blockTraveled.get(p.getName())
     
  14. yhl

    yhl

    Code (Text):
                if(args.length == 1){
                    getConfig().set(p.getName() +".traveled", 0);
                    p.sendMessage(Double.toString(getConfig().getDouble(p.getName() +".traveled")));
                    p.sendMessage("Reseted.");
                    target.sendMessage("Your stats have been reseted.");
            }
    But for some reason it sends 0.0 but the number doesnt change in the config (I fixed the CraftPlayer thing by using my old code)
     
  15. yhl

    yhl

    nvm I just needed to saveConfig(); (Final Code: https://pastebin.com/ud7ggKhZ)
     
    • Like Like x 1
  16. Why are casting an Integer to a Double?