Weird error with CraftPlayer :(

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

  1. yhl

    yhl

    I just had an error with this, but now its a lot different. I keep getting CraftPlayer={name=Buse} with this code (found it on the internet so dont hate m8):
    Code (Text):
                            Set set2 = blocksTraveled.entrySet();
                             Iterator iterator2 = set2.iterator();
                             while(iterator2.hasNext()) {
                                  Map.Entry me2 = (Map.Entry)iterator2.next();
                                  Player Val = Bukkit.getServer().getPlayer(me2.getKey().toString());
                                  p.sendMessage(me2.getKey().toString() + ": ");
                                  p.sendMessage(me2.getValue().toString());
    and I don't know what to do
     
  2. What are you even trying to do?

    If you found this on the internet and don't understand it should not just copy and paste it in.
     
  3. yhl

    yhl

    Oh sorry forgot to say. I'm making a leaderboard (heres the full code: https://hastebin.com/enojikaxur.swift) and the code was to sort a hashmap
     
    • Like Like x 1
  4. yhl

    yhl

  5. Just a tip, instead of // everyline you want to not be ran you do /* and then do */ and everything inbetween them will not be read :)
     
  6. Try changing
    p.sendMessage(me2.getKey().toString() + ": ");
    to...
    p.sendMessage(p.getName() + ": ");

    because you are getting the CraftPlayer object not the name
     
  7. electronicboy

    IRC Staff

    It's not an error, you're calling toString and sending that on a CraftPlayer object (Player objects are actually CraftPlayer objects inside Spigot/CraftBukkit server)
     
  8. That is what I said! xD
     
  9. yhl

    yhl

    I don't, I highlight the text then CTRL+/
     
  10. yhl

    yhl

    I can't do that bc I'm sorting the hashmap into a treemap, so if I just use p.getName() it'll mess it up
     
  11. Choco

    Moderator

    That doesn't matter... You're still printing out the Player object. Invoke #getName() on the value returned from #getKey(), and take a second to learn what the #toString() method does
    Code (Java):
    Player val = me2.getKey();
    p.sendMessage(val.getName() + ": ");
     
  12. yhl

    yhl

    Youre casting an object with a player object..
     
  13. yhl

    yhl

    nvm fixed it

    Code (Text):
    Player val = (Player) me2.getKey();
    p.sendMessage(val.getName() + ": ");
     
  14. Choco

    Moderator

    Oh dear... you really have a Map<Object, Integer>? That's actually terrifying ._. Let's not even take into consideration of the fact that you should be holding Player objects in a Map, but to hold an Object? Reaaaally?
     
  15. yhl

    yhl

    What? Can you check the code before you try to act like you know more than everyone? My hashmap has a Player and Integer, not Object.. If you knew how maps worked then you would know that a key wouldnt be able to cast as a Player object
     
    • Funny Funny x 1
  16. Keep your mouth shut if you don't know what you're talking about, kid.
     
  17. Choco

    Moderator

    I'm sorry. I really should respect your only plugin of size 30kb (that is really only less than 5kb because DarkBlade's ParticleAPI is a good 25kb on its own) done all in a single class. I know your knowledge far exceeds mine and I am honestly disgraced that I am even talking to you in such a condescending manor. Only the greatest of programmers would ask a question regarding the result of a #toString() call. My apologies
    /s

    The contents of your reply that I quoted make no sense whatsoever. If you took about 3 seconds to take a look at Map#keySet(), you would know that it returns a type parameter, K, being the object stored in a Map's key. I was going purely based on your code (quoted below) that seems rather poorly written.
    Since you're passing me2.getKey().toString() to the message and it's printing "CraftPlayer={name=Buse}", I presumed that it was returning a Player object, (rightfully so. That is the result of Player#toString()). The only way that you could possibly get a syntax error is if you were storing "Object" (or some other non-Player object) as the key in a Map, because Entry#getKey() ALSO returns the type parameter K of the Map that the entries were retrieved from. On top of that, Object can be cast to Player because every single class in Java extends Object. It may not be safe to cast Object to Player without first checking, but it is certainly possible.

    So before you tell me that I am not knowledgeable of Maps, please, as many will say...

    [​IMG]

    (With an attitude like that, I should have never even attempted to help you in the first place. Best of luck on your own or with someone else that may be willing to help you)
     
  18. yhl

    yhl

    woah, that really hurt my feelings....