Solved Sending player a message

Discussion in 'Plugin Development' started by Beekie, Jun 7, 2014.

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

    Beekie

    Hey,
    I want it so when a player types in the command /pay <player> <amount> he pays the player. Well, I got that part. But now I want it so when the player types in that command the player who he pays gets also a message that a player payed him.
    My code(yes I have more classes):
    Code:java
    1. package me.Lucas.WatchBlocks;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class EconPay implements CommandExecutor
    10. {
    11. private static Main plugin = EconManager.getPlugin();
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender cs, Command cmd, String s, String[] args) {
    15. Player p = (Player) cs;
    16. if(args.length !=2)
    17. {
    18. cs.sendMessage(ChatColor.RED+"Hey! You can not use that command like that!");
    19. cs.sendMessage(ChatColor.GREEN+"Usage: /pay <player> <amount>");
    20. return true;
    21. }
    22.  
    23. if(cs.hasPermission("WatchBlocks.econ.pay"))
    24. {
    25. if(!EconManager.hasAccount(args[0]))
    26. {
    27. cs.sendMessage(ChatColor.RED+"Error: Player does not have an account");
    28. return true;
    29. }
    30. double amount = 0;
    31. try
    32. {
    33. amount = Double.parseDouble(args[1]);
    34. }catch (Exception e)
    35. {
    36. cs.sendMessage(ChatColor.RED+"You have to enter a number!");
    37. return true;
    38. }
    39. EconManager.setBalance(args[0], EconManager.getBalance(args[0]) + amount);
    40. EconManager.setBalance(p.getName(), EconManager.getBalance(p.getName()) - amount);
    41. cs.sendMessage("You payed "+p.getName()+" "+amount);
    42. }
    43. return false;
    44. }
    45. }
    46.  
     
  2. Offline

    Gater12

    Beekie
    Make sure getting the player base on String is actually online. Then get the player from the string and send them the message.
     
  3. Offline

    Compressions

    Beekie In your code, cs is the same as p.
     
  4. Offline

    Beekie

    Uhhmm, so..?

    I don't get it... D:

    Sooo,, I did some research and tried this:
    Code:java
    1. package me.Lucas.WatchBlocks;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class EconPay implements CommandExecutor
    11. {
    12. private static Main plugin = EconManager.getPlugin();
    13.  
    14. @Override
    15. public boolean onCommand(CommandSender cs, Command cmd, String s, String[] args) {
    16. Player p = (Player) cs;
    17. Player online = Bukkit.getPlayerExact(args[0]);
    18. if(online == null)
    19. {
    20. cs.sendMessage("The player could not be found!");
    21. return true;
    22. }
    23. if(args.length !=2)
    24. {
    25. cs.sendMessage(ChatColor.RED+"Hey! You can not use that command like that!");
    26. cs.sendMessage(ChatColor.GREEN+"Usage: /pay <player> <amount>");
    27. return true;
    28. }
    29.  
    30. if(cs.hasPermission("WatchBlocks.econ.pay"))
    31. {
    32. if(!EconManager.hasAccount(args[0]))
    33. {
    34. cs.sendMessage(ChatColor.RED+"Error: Player does not have an account");
    35. return true;
    36. }
    37. double amount = 0;
    38. try
    39. {
    40. amount = Double.parseDouble(args[1]);
    41. }catch (Exception e)
    42. {
    43. cs.sendMessage(ChatColor.RED+"You have to enter a number!");
    44. return true;
    45. }
    46. EconManager.setBalance(args[0], EconManager.getBalance(args[0]) + amount);
    47. EconManager.setBalance(p.getName(), EconManager.getBalance(p.getName()) - amount);
    48. cs.sendMessage("You payed "+online+" "+amount);
    49. online.sendMessage("You received "+amount+" from "+cs);
    50. }
    51. return false;
    52. }
    53. }
    54.  

    It sends the message to the other player but it is kinda weird...
    Message: "You received 1.0 from CraftPlayer{name=Beekie}
    And the other player(that did /pay Beekie 1) gets: "You payed CraftPlayer{name=lucas_beek} 1.0

    How can I remove the "CraftPlayer{name=} ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 2, 2016
  5. Offline

    teej107

    Beekie You aren't checking to see if the CommandSender is a player first before casting. And you are sending the message to the CommandSender, not the player. I don't know if that is the problem but try it. (Especially the first)
     
  6. Offline

    Beekie

    teej107 Huh? I don't get it....

    NOTE: I want it to send a message to the CommandSender and the player which the CommandSender pays...
     
  7. Offline

    teej107

    Beekie Who is executing the command? A player?
     
  8. Offline

    Beekie

    teej107 Yes
     
  9. Offline

    teej107

    Beekie First off make sure the CommandSender is an instance of Player before casting it to avoid problems. I'm currently looking in your code more deeply.

    Edit: You are casting the CommandSender to Player and yet you are not using the player. Have the Player that you casted to send the meesage instead of the CommandSender. Try that.
     
  10. Offline

    Beekie

    teej107 It sends the message perfectly but the message is: "You payed CraftPlayer{name=lucas_beek} 1.0"
    You see the "CraftPlayer{name=lucas_beek}"?
    How can I change the code so it says "You payed lucas_beek 1.0"?

    NOTE: And for the player that got payed the message is: "You received 1.0 from CraftPlayer{name=Beekie}"
    Again "CraftPlayer{name=Beekie}"
    How can I also change the code so it says "You received 1.0 from Beekie"
     
  11. Offline

    teej107

    Oh. I see your error.
    Code:java
    1. cs.sendMessage("You payed "+online+" "+amount);
    to
    Code:java
    1. cs.sendMessage("You payed "+online.getName()+" "+amount);


    Edit: You are sending the player in the message and not the player name.
     
  12. Offline

    Beekie

    Wow.. That was stupid xD
    A big thanks to you! :D
     
Thread Status:
Not open for further replies.

Share This Page