Send a message to a told player

Discussion in 'Plugin Development' started by Suprem20, Aug 28, 2011.

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

    Suprem20

    I want to send a message to a player using a user command.

    with /marriage (player name) you can ask someone for marriage. I want to send a message to the specified player name asking him if he desire to marry the person. Then it would tell back to the player "your request was a success or she/he denied. Any help?
     
  2. If you have a declared player, you can just use the sendMessage(messagestring); method.

    More useful stuff:

    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    boolean isplayer = (sender instanceof Player);
    Player player = (sender instanceof Player) ? (Player)sender : null; // Cast the sender to Player.
    Player targetPlayer = null;
    
    // For example command: /marriage propose someperson
    String base = command.getName(); // Note: base = command. (ex. /marriage)
    String arg1 = args[0].toLowerCase().trim(); // arg1 = First argument. (ex. /propose). In Java/Bukkit, this is args[0].
    String arg2 = args.length>1 ? args[1].toLowerCase().trim() : ""; // Second argument. (ex. someperson.)
    
    if(base.equalsIgnoreCase("marriage") {
        if (isPlayer(arg2) == true) {
            targetPlayer = Bukkit.getServer().getPlayer(arg2);
        }
    // Then do stuff
        player.sendMessage("Hi.");
    }
    
    // Method to convert String to Player
    public static boolean isPlayer(String string) {
    try {
        Player test = Bukkit.getServer().getPlayer(string);
        if (test == null) {
            return false;
        } else {
            return true;
        }
    } catch (Exception ex) {
        return false;
    } 

    Edit: Typed out the onCommand parameters... *sigh*
     
  3. Offline

    Suprem20

    Sorry but, where is the field for command.getname()?
     
  4. Offline

    cholo71796

    second parameter of onCommand
     
  5. Offline

    Suprem20

    I know I sound noobish but, example for the onCommand parameters?
     
  6. Offline

    cholo71796

  7. Offline

    Suprem20

    I get an error with the code above, at the String base = Command.getname; it says cannot find symbol symbol: variable getname location class org.bukkit.command.Command

    help plox?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  8. Offline

    Ryo

    Where did you put the onCommand? what class?

    and from the link cholo gave you just need to type onCommand() and if you have eclipse you can hover over it to import the needed class.
    Also if it cant find the location of the class than you might not have added the bukkit api as an external JAR file or not didn't link to the correct javadocs path.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  9. Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. if (commandLabel.equals(marriage) && sender instanceof Player) {
    3. Player p = (Player) sender;
    4.  
    5. if (args.length >= 1) {
    6. Player target = Bukkit.getServer().getPlayer(args[0]);
    7. if (target != null) {
    8. target.sendMessage(p.getName() + " made you a proposal.");
    9. } else {
    10. p.sendMessage("Invalid player!");
    11. }
    12. } else {
    13. p.sendMessage("Usage: /marriage (player name)");
    14. }
    15. }
    16. }
     
Thread Status:
Not open for further replies.

Share This Page