Solved private message plugin

Discussion in 'Plugin Development' started by DaanSander, Feb 27, 2015.

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

    DaanSander

    hello i am trying to make a /msg and /r plugin but it doesnt seems to work please help
    sorry for bad english


    code that i have tried:
    Code:
    package me.daansander.hmcessentials.cmds;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import java.util.HashMap;
    
    /**
    * Created by Daan on 27-2-2015.
    */
    public class Msg implements CommandExecutor {
    
        public static HashMap<Player, Player> msg = new HashMap<Player, Player>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if((cmd.getName().equalsIgnoreCase("msg")) || cmd.getName().equalsIgnoreCase("t") || cmd.getName().equalsIgnoreCase("tell") && sender instanceof Player) {
                Player p = (Player) sender;
                Player t = (Player) Bukkit.getPlayerExact(args[0]);
                    if(t != null) {
                        String message = args[1] + args[2] + args[3] + args[4] + args[5] + args[6] + args[7] + args[8] + args[9] + args[10];
                        msg.put(p, t);
                        t.sendMessage("§a§2" + p.getName() + "§r§6§l > " + message);
                        return true;
                    } else {
                        p.sendMessage("§c§lCould not find player!");
                        return true;
                    }
            } if (cmd.getName().equalsIgnoreCase("r") && sender instanceof Player) {
                Player p = (Player) sender;
                Player t = (Player) Bukkit.getPlayerExact(args[0]);
                msg.get(t);
                String message = args[1] + args[2] + args[3] + args[4] + args[5] + args[6] + args[7] + args[8] + args[9] + args[10];
                t.sendMessage("§a§2" + p.getName() + "§r§6§l > " + message);
            }
            return true;
        }
    }
    
     
  2. Offline

    tomudding

    use "commandLabel.equalsIgnoreCase()" instead of cmd.getName().equalsIgnoreCase(). That will solve a few things
     
  3. Offline

    DaanSander

  4. Offline

    RainoBoy97

    False, using "commandLabel" will make you have to manually check for aliases.
     
  5. Offline

    teej107

    That would create more problems than it would solve as explained by @RainoBoy97
    And it wouldn't even fix the current problem to tell you the truth.

    @DaanSander "Doesn't work" is too vague. Please explain more. What happens (or doesn't) and what should happen?
     
  6. Offline

    tomudding

    Well thanks for the info @teej107 and @RainoBoy97.

    @DaanSander here is a reworked working version:
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {      
            if ((commandLabel.equalsIgnoreCase("msg")) || (commandLabel.equalsIgnoreCase("t")) || (commandLabel.equalsIgnoreCase("tell"))) {
                String message = "";
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                     
                    if(args.length <= 0) {
                        player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Usage: /" + commandLabel.toString() + " <player> [message]");
                        return true;
                    } else if(args.length == 1) {
                        player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Provide a message");
                        return true;
                    } else if(args.length >= 2) {
                        Player target = Bukkit.getPlayerExact(args[0]);
                        if(target == null) {
                            player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "The player " + ChatColor.YELLOW + target.getName() + ChatColor.GRAY + " cannot be found");
                            return true;
                        }
                        for(int i = 1; i < args.length; i++) {
                            message = message + args[i] + " ";
                        }
              
                        player.sendMessage(ChatColor.GRAY + "[To " + ChatColor.YELLOW + target.getName() + ChatColor.GRAY + "] " + ChatColor.YELLOW + message);
                        target.sendMessage(ChatColor.GRAY + "[From " + ChatColor.YELLOW + player.getName() + ChatColor.GRAY + "] " + ChatColor.YELLOW + message);
                        msg.put(player.getUniqueId(), target.getUniqueId());
                        msg.put(target.getUniqueId(), player.getUniqueId());
                        return true;
                    }
                } else {
                    sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Only players can use this command");
                    return true;
                }
            }
            if (commandLabel.equalsIgnoreCase("r")) {
                String message = "";
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                  
                    if (args.length <= 0) {
                        player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Usage: /r [message]");
                        return true;
                    } else if (args.length >= 1) {
                        UUID targetUUID = msg.get(player.getUniqueId());
                      
                        if (targetUUID != null) {
                            if (Bukkit.getPlayer(targetUUID) == null) {
                                msg.remove(player.getUniqueId());
                                return true;
                            } else {
                                Player target = Bukkit.getPlayer(targetUUID);
                                for (int i = 0; i < args.length; i++) {
                                    message = message + args[i] + " ";
                                }
                                player.sendMessage(ChatColor.GRAY + "[To " + ChatColor.YELLOW + target.getName() + ChatColor.GRAY + "] " + ChatColor.YELLOW + message);
                                Bukkit.getPlayer(targetUUID).sendMessage(ChatColor.GRAY + "[From " + ChatColor.YELLOW + player.getName() + ChatColor.GRAY + "] " + ChatColor.YELLOW + message);
                                return true;
                            }
                        } else {
                            player.sendMessage(ChatColor.RED + "Error");
                            return true;
                        }
                    }
                } else {
                    sender.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Only players can use this command");
                    return true;
                }
            }
            return false;
        }
    clear the hash map on server restart (or reload or something)
    Edit messages by yourself.

    Edit: I tested it on my server and it worked
     
  7. Offline

    DaanSander

  8. Offline

    MCMatters

Thread Status:
Not open for further replies.

Share This Page