Set sender as variable?

Discussion in 'Plugin Development' started by TheEnderCrafter9, Apr 3, 2016.

Thread Status:
Not open for further replies.
  1. I'm a novice programmer working on a private server and recently created my first plugin (just /heal and /kill).

    I am working on a hug command that will tell someone they have been hugged by X (or sender)

    I have successfully created a variable for the target using arguments (args[0])

    Is it possible to set the sender as a variable that I can use with the ' sendMessage(); method'?

    For example setting the sender to DERP which when printed would print the username of the sender

    sort of like:

    Player Derp = sender;


    SUMMARY
    I am trying to make a variable based of the Commandsender (in our case DERP) that will read the sender's username when printed in a sendMessage instance.

    Btw, feel free to correct my java terms if I classified them wrong.
     
  2. Offline

    cluter123

    Check if the sender is an instance of a player. If it is, cast the sender to the player. Then you can do target.sendMessage("You have been huged by " + Derp.getName();
     
  3. String = varible (sender or plarer).getname
     
  4. Offline

    Irantwomiles

    Player derp = (Player) sender;
     
  5. Offline

    mcdorli

    Please, read the main post

    So, there are 2 possibilities, 1.: you have a fix message, and you want to put the player's name in it somewhere. To do this, you need to first cast the sender to a player (make sure sender is a player before you cast), then use Player#getName(), and concatenate it with other strings ("You was hugged by " + player.getName())
    The other possibility, is you have a dynamic strong with a placeholder in it (for example {PLAYER}), then all you need to use is String result = yourMessage.replace("{PLAYER}", plaser.getName())
     
Thread Status:
Not open for further replies.

Share This Page