Solved My Message Command work but it show player name

Discussion in 'Spigot Plugin Development' started by AnnoyingMan, May 28, 2016.

  1. My Message Command work but it show player looks like this:
    Wrong msg.PNG
    Any can fix it?
    My /reply command works perfectly fine
    My code:
    http://pastebin.com/PMYJZhxu
    package my.verelite.cool.commands;

    import java.util.HashMap;
    import java.util.Map;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;

    public class PrivateMessages implements CommandExecutor, Listener {

    public Map<Player, Player> reply = new HashMap<Player, Player>();

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    {
    if(cmd.getName().equalsIgnoreCase("msg")) {
    if(!(sender instanceof Player)) {
    sender.sendMessage("no");
    return true;
    }
    Player p = (Player) sender;
    if(args.length == 0) {
    p.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + "CMS " + ChatColor.DARK_GRAY + "» " + ChatColor.GOLD + "Please use" + ChatColor.AQUA + "/msg (player) (messages)");
    return true;
    }
    if (args.length == 1)
    {
    p.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + "CMS " + ChatColor.DARK_GRAY + "» " + ChatColor.GOLD + "Please use" + ChatColor.AQUA + "/msg (player) (messages)");
    return true;
    }

    if(args.length >= 2) {
    Player target = Bukkit.getServer().getPlayer(args[0]);

    if (target == null)
    {
    p.sendMessage(ChatColor.RED + " " + ChatColor.BOLD + "CMS " + ChatColor.DARK_GRAY + "» " + ChatColor.GOLD + "That player is not online!");
    return true;
    }

    StringBuilder msg = new StringBuilder();
    for (String arg : args) {
    msg.append(arg);
    msg.append(" ");
    }


    target.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&r" + sender.getName() + " &c» &6Me&8]&r " + msg));
    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&6Me &c» &r" + target.getName() + "&8]&r " + msg));
    target.playSound(target.getLocation(), Sound.LEVEL_UP, 1, 1);
    return true;
    }


    }
    if(cmd.getName().equalsIgnoreCase("reply")) {
    if(!(sender instanceof Player)) {
    sender.sendMessage("no");
    return true;
    }

    if(args.length == 0) {
    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lSystem &8» &6Usage: &b/r (messages)"));
    return true;
    }

    if(args.length >= 1) {
    Player player = (Player) sender;
    Player r = (Player) reply.get(sender);
    if(r == null) {
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lSystem &8» &6You have nobody to reply to"));
    return true;
    }

    StringBuilder msg = new StringBuilder();
    for (String arg : args) {
    msg.append(arg);
    msg.append(" ");
    }


    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&6Me &c» &r" + r.getName() + "&8]&r " + msg));
    r.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&r" + sender.getName() + " &c» &6Me&8]&r " + msg));
    r.playSound(r.getLocation(), Sound.LEVEL_UP, 1, 1);
    return true;
    }


    }
    return true;

    }
    }
     
    #1 AnnoyingMan, May 28, 2016
    Last edited: May 28, 2016
  2. That's because you're starting the looping through the arguments at 0 - that means the player name will also be iterated through, because it's the first argument. Try using a standard for loop instead (if there's a technical name for this type of loop, please correct me, couldn't find one from my 30 second Google):

    Code (Java):
    for(int i = 1; /* Starting the index at 1 instead of 0 so the first arg (the player name) is skipped. */ i < args.length; i++)
     
  3. Next time please use the Code format :)
     
  4. ok i will test it out
     
  5. can you give a code how to do it?
     
  6. I've given you the necessary code there.. Look at what I gave you (remove the comment I put into it), and replace your existing for loop. If you don't know how to use what I gave you, you'd be better off reading a beginners' guide on Java first.
     
  7. Thanks @Yellow_ i just make it and its work