Why doesn't this plugin work?

Discussion in 'Plugin Development' started by john2342, Jul 22, 2014.

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

    john2342

    I already have the correct setup, this is just 1 command out of like 5 and they all work so I know this one is weird. Any help?
    Code:java
    1.  
    2. if(cmd.getName().equalsIgnoreCase("message")) {
    3. if (args.length < 2) {
    4. p.sendMessage("§cIncorect syntax. Usage: /message <player> <message>");
    5. }else{
    6. Player targetPlayer = p.getServer().getPlayer(args[1]);
    7. if (targetPlayer == null) {
    8. p.sendMessage("§c" + args[1] + "§c is offline");
    9. }else{
    10. targetPlayer.sendMessage("§a[§a§l" + p.getName() + " whispers§a]:§f " + args[2]);
    11. p.sendMessage("§a[§a§lYou whisper to " + targetPlayer.getName() + "§a]:§f " + args[2]);
    12. }
     
  2. Offline

    LugnutsK

    You should probably change .getPlayer(args[1]) to .getPlayer(args[0]) as well as change args.length < 1 to args.length <2. I would also recommend checking if targetPlayer is not null, but that is not necessary for it to work.
     
  3. Offline

    john2342

    I did change both of those previously, but they didn't work so I tried changing it and thanks for the idea of checking if the player s null

    edit: I changed the code but still doesn't work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    Necrodoom

    Paste edited code and explain error.
     
  5. Offline

    tomudding

    Did you register the command 'message'?

    This is what should be working:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. String message = "";
    4.  
    5. if(sender instanceof Player) {
    6. Player player = (Player)sender;
    7.  
    8. if(label.equalsIgnoreCase("message")) {
    9. if(args.length <= 0) {
    10. player.sendMessage(ChatColor.RED + "Incorrect syntax. Usage: /message <player> [message]");
    11. return true;
    12. } else if(args.length == 1) {
    13. player.sendMessage(ChatColor.RED + "Provide a message");
    14. return true;
    15. }
    16.  
    17. if(args.length >= 2) { //Checking for more args because message contains space
    18. Player targetPlayer = player.getServer().getPlayer(args[0]);
    19.  
    20. if(targetPlayer == null) {
    21. player.sendMessage(ChatColor.RED + "The player " + ChatColor.YELLOW + targetPlayer + ChatColor.RED + " cannot be found");
    22. return true;
    23. }
    24.  
    25. for(int i = 1; i < args.length; i++) {
    26. message = message + args[i] + " ";
    27. }
    28.  
    29. player.sendMessage(ChatColor.GREEN + "[ " + ChatColor.DARK_BLUE + "To " + targetPlayer.getName() + ChatColor.GREEN + "]: " + ChatColor.GRAY + message);
    30. targetPlayer.sendMessage(ChatColor.GREEN + "[ " + ChatColor.DARK_BLUE + "From " + player.getName() + ChatColor.GREEN + "] " + ChatColor.GRAY + message);
    31. return true;
    32. }
    33. }
    34. }
    35. }[/i]
     
  6. Offline

    EgyptianKing

    I'm not sure but I think:
    Code:java
    1. Player targetPlayer = p.getServer().getPlayer(args[1]);

    Should be:
    Code:java
    1. Player targetPlayer = Bukkit.getServer().getPlayer(args[0]);
     
  7. Offline

    tomudding

    EgyptianKing that was already mentioned.
     
  8. Offline

    EgyptianKing

    tomudding
    Look closer:
    p.getServer()
    to
    Bukkit.getServer().
     
  9. EgyptianKing Technically wouldn't matter since they both return Server
     
Thread Status:
Not open for further replies.

Share This Page