Help - Converting CraftPlayer{name=}

Discussion in 'Plugin Development' started by Rocky_990, May 12, 2015.

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

    Rocky_990

    Hey,

    I've recently been developing a plugin although I've reached a stuck point where I'm trying to make the console run a command (from another plugin) using the sender's username.

    Of course, I use: bukkit.dispatchCommand(sender, "command " + sender) although it will not work as it is making the sender's name: CraftPlayer{name=<name>}
    For example: CraftPlayer{name=<name>}

    How can I make a string of JUST the sender's raw username?

    Thanks,
    ~Rocky_990
     
  2. Offline

    meguy26

    @Rocky_990
    1. Cast sender to player with check:
    Code:
    if(sender isntanceof Player){
        Bukkit.dispatchCommand(sender, "command  " + ((Player) sender).getName());
    }
     
    Zombie_Striker and Rocky_990 like this.
  3. Offline

    caderape

  4. @Rocky_990 Always check the Javadocs for the relevant methods when you're entirely sure about the class that you're dealing with :) Of course, I'd link the official ones but they've been down for a long time with no effort to bring them back :(

    You can always use some unoffical ones if you like, there're plenty on this thread.
     
  5. Booth ConsoleCommandSender and Player have the method .getName() (in the console it returns "CONSOLE")
     
  6. @FisheyLP The important thing is that CommandSender has getName(), not that ConsoleCommandSender and Player have it ;) If those two had it, but CommandSender didn't, the cast would indeed be necessary. In this case, it is not.
     
  7. With "they have the method" I meant from the superclass, sorry
     
  8. Offline

    meguy26

    @FisheyLP @AdamQpzm @Rocky_990
    Even though CommandSender has the method getName(), the OP will still want to check if the sender is a player, because according to the OP post he needs a player name...
     
  9. @meguy26 I know, but:
     
  10. @meguy26 That still doesn't make your cast necessary.
     
  11. Offline

    meguy26

  12. invoke player#.getName().
     
Thread Status:
Not open for further replies.

Share This Page