Custom PM command!

Discussion in 'Plugin Development' started by ajs333, Nov 12, 2013.

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

    Xacero

    The_Doctor_123

    I've been here awhile ._. so have they ;P It's pretty active when we're all online.

    ajs333
    Where are you registering the command executor?
     
  2. Offline

    ajs333

    Xacero
    I have registered the Command Executor in the main class...
     
  3. Offline

    The_Doctor_123

    Okay, I'm too lazy to read the past 4 dang pages. What's the holdup?
     
  4. Offline

    Xacero

    elementalgodz11

    You wanted to display the ussage message if no arguments are provided correct?

    ajs333
    Can I see the method?

    @The_Doctor_123
    AJS is having issues registering his command executor.

    elemental wants a custom usage message.
     
  5. Offline

    ajs333

    Xacero
    I don't know what you mean... Here is what I did...

    Code:
            CommandPrivateMessage message = new CommandPrivateMessage();
            getCommand("msg").setExecutor(message);
            getCommand("reply").setExecutor(message);
     
  6. Offline

    Xacero

    elementalgodz11

    if the player types just "/r" that message should send.

    if the player types "/r [argument] [argument...]" I dont see why you'd do that as it would always return true.
    (since the command's proper usage requires atleast >= 2 arguments)

    If you'd like it to send usage if the player is offline (or invalid) replace the "Player is offline" message with your usage message

    If you'd like to show usage every time you run the command regardless of the arguments passed remove the "Return true;" and the if statement.

    Does that help?

    ajs333
    is that in your onEnable method?
     
  7. Offline

    ajs333

  8. Offline

    Xacero

    ajs333
    That's strange.... can you double check this To make sure you're doing it correctly? (it appears you are...)
    I'm not sure how else to help sorry, I do not use CommandExecutors. What I usually do is this

    Code:java
    1. // what would be a command executor
    2. public boolean checkCommand(CommandSender s, Command c, String l, String[] a) {
    3.  
    4. // run my commands here
    5.  
    6. return false;
    7. }
    8.  
    9. //--
    10. // In my Plugin/ Main class
    11. TheClassShowAbove cmd = new TheClassShowAbove();
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender s, Command c, String l, String[] a) {
    15. return cmd.checkCommand(s,c,l,a);
    16. }


    elementalgodz11

    Oh, if you're using "r" I'm assuming you're using the CommandPreprocessEvent ? You'll need to parse the arguments and check them in there wouldn't you?
     
  9. Offline

    ajs333

    Xacero
    I'm not sure what exactly to do...
     
  10. Offline

    Xacero

    elementalgodz11
    You can either put both commands in the usage message or check the command again and send the appropriate usage message. Hope that made sense / is what you're asking, you're quite welcome by the way :)

    Sorry I was a bit cranky before. I felt like I was restating myself , it might have just been poor communication skills on my part, I didn't get very much sleep. :)

    ajs333
    if you're having problems with the CommandExecutor you can always do what I said :)

    elementalgodz11

    You returned false.

    You should return true. :)

    Also the reason it's displaying the same message is because you check if the command is
    "r" OR "reply"

    and you put the thing checking the arguments in that block.

    What you'd need is

    Code:java
    1. if (args.length < 1) {
    2. switch(command.getName().toLowercase()) {
    3. case "reply":
    4. //send reply message
    5. return true;
    6. case "r":
    7. //send r message
    8. return true;
    9. }
    10. }


    (Adjust and apply to what you have)

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

    ajs333

    Xacero
    I'm going to fix up my Main Class a bit and then I'm going to post everything I have and see if you can fix what my issue is...
     
  12. Offline

    The_Doctor_123

    elementalgodz11
    Okay, your if statements aren't laid out very well. You should also use "else if" when you're checking for one if statement to be true or want to just run the first one that executes. It's just good coding practice.
     
  13. Offline

    afistofirony

    elementalgodz11 You should also register r as an alias of reply (or vice-versa) in your plugin.yml, because in this case the command /r is not registered. :p
     
  14. Offline

    Xacero

    elementalgodz11
    You didn't do what I said :p (maybe you missed my edit)

     
  15. Offline

    ajs333

    Ok, here is my main class everyone!

    Code:
    package sEssentials;
     
    import java.util.ArrayList;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import sEssentialsCommands.CommandChat;
    import sEssentialsCommands.CommandClearChat;
    import sEssentialsCommands.CommandGm;
    import sEssentialsCommands.CommandKill;
    import sEssentialsCommands.CommandPrivateMessage;
    import sEssentialsCommands.CommandTp;
    import sEssentialsCommands.CommandUnvanish;
    import sEssentialsCommands.CommandVanish;
     
     
     
    public class sEssentials extends JavaPlugin implements Listener{
       
        SettingsManager settings = SettingsManager.getInstance();
        ArrayList<Player> vanished = new ArrayList<Player>();
     
        public void onEnable () {
            System.out.print("sEssentials Enabled!");
            getServer().getPluginManager().registerEvents(this, this);
            getCommand("invis").setExecutor(new CommandVanish());
            getCommand("vis").setExecutor(new CommandUnvanish());
            getCommand("gm").setExecutor(new CommandGm());
            getCommand("creative").setExecutor(new CommandGm());
            getCommand("adventure").setExecutor(new CommandGm());
            getCommand("survival").setExecutor(new CommandGm());
            getCommand("cc").setExecutor(new CommandClearChat());
            getCommand("clearchat").setExecutor(new CommandClearChat());
            getCommand("kill").setExecutor(new CommandKill());
            getCommand("tp").setExecutor(new CommandTp());
            CommandChat message = new CommandChat();
            getCommand("ce").setExecutor(message);
            getCommand("cd").setExecutor(message);
            getCommand("chatenable").setExecutor(message);
            getCommand("chatdisable").setExecutor(message);
            CommandPrivateMessage message1 = new CommandPrivateMessage();
            getCommand("msg").setExecutor(message1);
            getCommand("reply").setExecutor(message1);
           
        }
       
        public void onDisable () {
            System.out.print("sEssentials Disabled!");
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)  {
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GOLD + "Welcome, " + player.getName() + " to sKits!");
            player.sendMessage(ChatColor.GOLD + "Do /help for a list of commands!");
            event.setJoinMessage("");
            if(player.hasPermission("skits.mod") || player.hasPermission("skits.tmod")) {
                player.hidePlayer(player);
                player.setCanPickupItems(false);
                player.sendMessage(ChatColor.GREEN + "You are now vanished! You are invisible to everyone but MOD and above!");
            }
        }
        @EventHandler
        public void onPlayerPickUp(PlayerPickupItemEvent event) {
            Item item = event.getItem();
            if (item.getItemStack().getType() == Material.BOWL) {
            event.setCancelled(true);
            }
        }
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
                if (this.getConfig().getString(e.getPlayer().getName()) != null) {
                        e.getPlayer().setDisplayName(this.getConfig().getString(e.getPlayer().getName()) + ChatColor.RESET); }
                }
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event) {
            event.setQuitMessage(null);
        }
       
        @EventHandler
        public void onHunger(FoodLevelChangeEvent event) {
            event.setCancelled(true);
        }
     
        public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, final String[] args) {
            final Player player = (Player) sender;
            if (commandLabel.equalsIgnoreCase("help")) {
                player.sendMessage(ChatColor.GRAY + "sKits!");
                player.sendMessage(ChatColor.GRAY + "/Kill - Kills you!");
                player.sendMessage(ChatColor.GRAY + "/Balance - Shows your current balance!");
           
            }
        if (cmd.getName().equalsIgnoreCase("setspawn")) {
                settings.getData().set("spawn.world", player.getLocation().getWorld().getName());
                settings.getData().set("spawn.x", player.getLocation().getX());
                settings.getData().set("spawn.y", player.getLocation().getY());
                settings.getData().set("spawn.z", player.getLocation().getZ());
                settings.saveData();
                player.sendMessage(ChatColor.GREEN + "Spawn set!");
                return true;
        }
       
        if (cmd.getName().equalsIgnoreCase("spawn")) {
                if (settings.getData().getConfigurationSection("spawn") == null) {
                        player.sendMessage(ChatColor.RED + "The spawn has not yet been set!");
                        return true;
                }
                World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn.world"));
                double x = settings.getData().getDouble("spawn.x");
                double y = settings.getData().getDouble("spawn.y");
                double z = settings.getData().getDouble("spawn.z");
                player.teleport(new Location(w, x, y, z));
                player.sendMessage(ChatColor.GREEN + "Welcome to the spawn!");
        }
       
            if (cmd.getName().equalsIgnoreCase("warp")) {
                if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please specify a name!");
                        return true;
                }
                if (settings.getData().getConfigurationSection("warps." + args[0]) == null) {
                        player.sendMessage(ChatColor.RED + "Warp " + args[0] + " does not exist!");
                        return true;
                }
                player.sendMessage(ChatColor.GRAY + "Teleporting in 3 seconds!");
                getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    @Override
                    public void run() {
                        World w = Bukkit.getServer().getWorld(settings.getData().getString("warps." + args[0] + ".world"));
                        double x = settings.getData().getDouble("warps." + args[0] + ".x");
                        double y = settings.getData().getDouble("warps." + args[0] + ".y");
                        double z = settings.getData().getDouble("warps." + args[0] + ".z");
                        player.teleport(new Location(w, x, y, z));
                        player.sendMessage(ChatColor.GREEN + "Teleported to " + args[0] + "!");
                    }
                }, 60L);
        }
            if (cmd.getName().equalsIgnoreCase("setwarp")) {
                if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please specify a name!");
                        return true;
                }
                settings.getData().set("warps." + args[0] + ".world", player.getLocation().getWorld().getName());
                settings.getData().set("warps." + args[0] + ".x", player.getLocation().getX());
                settings.getData().set("warps." + args[0] + ".y", player.getLocation().getY());
                settings.getData().set("warps." + args[0] + ".z", player.getLocation().getZ());
                settings.saveData();
                player.sendMessage(ChatColor.GREEN + "Set warp " + args[0] + "!");
        }
       
            if (cmd.getName().equalsIgnoreCase("delwarp")) {
                if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please specify a name!");
                        return true;
                }
                if (settings.getData().getConfigurationSection("warps." + args[0]) == null) {
                        player.sendMessage(ChatColor.RED + "Warp " + args[0] + " does not exist!");
                        return true;
                }
                settings.getData().set("warps." + args[0], null);
                settings.saveData();
                player.sendMessage(ChatColor.GREEN + "Removed warp " + args[0] + "!");
            }
        return false;
        }
    }
    
    Here is my Private Message Class:

    Code:
    package sEssentialsCommands;
     
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class CommandPrivateMessage implements CommandExecutor {
       
        HashMap<String, String> reply_list = new HashMap<String, String>();
       
        @Override
        public boolean onCommand(CommandSender send, Command cmd, String lbl, String[] args) {
       
            if (cmd.getName().equalsIgnoreCase("msg")) {
               
                Player sender = (Player) send;
                    if (args.length < 2) {
                        sender.sendMessage("Format is /msg [target] [message]");
                        return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        sender.sendMessage("That person is offline!");
                        return true;
                }
                if (!reply_list.containsKey(target.getName()))
                    reply_list.put(target.getName(), sender.getName());
                    reply_list.put(sender.getName(), target.getName());
                    String message = "";
                    for (int i = 1; i < args.length; i++) {
                        message += args;
                        if (i != (args.length - 1))
                            message += " ";
                    }
                target.sendMessage("[" + sender.getName() + "] " + message);
                sender.sendMessage("[" + sender.getName() + " -> " + target.getName() + "] " + message);
                                return true;
                }
               
            if (cmd.getName().equalsIgnoreCase("reply")) {
     
                Player sender = (Player) send;
               
                    if (args.length < 1) {
                        sender.sendMessage("You need to send a message!");
                        return true;
                    }
               
                if (!reply_list.containsKey(sender.getName())) {
                    sender.sendMessage("Nobody has messaged you.");
                        return true;
                    }
                Player target = Bukkit.getServer().getPlayer(reply_list.get(sender.getName()));
                    if (target == null) {
                        sender.sendMessage("That player is offline");
                        return true;
                    }
               
                String message = "";
                    for (int i = 0; i < args.length; i++) {
                        message += args;
                        if (i != (args.length - 1))
                            message += " ";
                }
               
                target.sendMessage("[" + sender.getName() + "] " + message);
                sender.sendMessage("[" + sender.getName() + " -> " + target.getName() + "] " + message);
                        return true;
                }
            return false;
        }
    }
    
    elementalgodz11
    Whats my issue with registering commands?

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

    afistofirony

    ajs333 Looks good, but you're still going to get a hash code when you actually send the message because you're specifying the entire array to be added to the String.

    Change all instances of this...
    Code:
    String message = "";
    for (int i = 1; i < args.length; i++) {
        message += args;
        if (i != (args.length - 1))
            message += " ";
    }
    to this (changed it slightly to be a tiny bit more efficient):
    Code:
    String message = args[1];
    for (int i = 2; i < args.length; i++)
        message += " " + args[i];
     
  17. Offline

    ajs333

    afistofirony
    Still nothing happens... No messages are sent to anyone or the console...

    Is my issue with the command or registering the commands?

    elementalgodz11
    Does that even work... what you told me to use?

    elementalgodz11
    Still nothing happens...

    ARG!!! This is really getting me MAD!!!! I don't understand what is wrong...

    I have found out that those commands just won't do anything...

    Here is my Main Class:

    Code:
    package sEssentials;
     
     
    import java.util.ArrayList;
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import sEssentialsCommands.CommandChat;
    import sEssentialsCommands.CommandClearChat;
    import sEssentialsCommands.CommandGm;
    import sEssentialsCommands.CommandKill;
    import sEssentialsCommands.CommandPrivateMessage;
    import sEssentialsCommands.CommandTp;
    import sEssentialsCommands.CommandUnvanish;
    import sEssentialsCommands.CommandVanish;
     
     
     
    public class sEssentials extends JavaPlugin implements Listener{
       
        SettingsManager settings = SettingsManager.getInstance();
        ArrayList<Player> vanished = new ArrayList<Player>();
        HashMap<String, String> reply_list = new HashMap<String, String>();
     
        public void onEnable () {
            System.out.print("sEssentials Enabled!");
            getServer().getPluginManager().registerEvents(this, this);
            getCommand("invis").setExecutor(new CommandVanish());
            getCommand("vis").setExecutor(new CommandUnvanish());
            getCommand("gm").setExecutor(new CommandGm());
            getCommand("creative").setExecutor(new CommandGm());
            getCommand("adventure").setExecutor(new CommandGm());
            getCommand("survival").setExecutor(new CommandGm());
            getCommand("cc").setExecutor(new CommandClearChat());
            getCommand("clearchat").setExecutor(new CommandClearChat());
            getCommand("kill").setExecutor(new CommandKill());
            getCommand("tp").setExecutor(new CommandTp());
            CommandChat message = new CommandChat();
            getCommand("ce").setExecutor(message);
            getCommand("cd").setExecutor(message);
            getCommand("chatenable").setExecutor(message);
            getCommand("chatdisable").setExecutor(message);
            getCommand("msg").setExecutor(new CommandPrivateMessage());
            getCommand("m").setExecutor(new CommandPrivateMessage());
            getCommand("tell").setExecutor(new CommandPrivateMessage());
            getCommand("whisper").setExecutor(new CommandPrivateMessage());
            getCommand("reply").setExecutor(new CommandPrivateMessage());
            getCommand("r").setExecutor(new CommandPrivateMessage());
     
           
        }
       
        public void onDisable () {
            System.out.print("sEssentials Disabled!");
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)  {
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GOLD + "Welcome, " + player.getName() + " to sKits!");
            player.sendMessage(ChatColor.GOLD + "Do /help for a list of commands!");
            event.setJoinMessage("");
            if(player.hasPermission("skits.mod") || player.hasPermission("skits.tmod")) {
                player.hidePlayer(player);
                player.setCanPickupItems(false);
                player.sendMessage(ChatColor.GREEN + "You are now vanished! You are invisible to everyone but MOD and above!");
            }
        }
        @EventHandler
        public void onPlayerPickUp(PlayerPickupItemEvent event) {
            Item item = event.getItem();
            if (item.getItemStack().getType() == Material.BOWL) {
            event.setCancelled(true);
            }
        }
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
                if (this.getConfig().getString(e.getPlayer().getName()) != null) {
                        e.getPlayer().setDisplayName(this.getConfig().getString(e.getPlayer().getName()) + ChatColor.RESET); }
                }
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event) {
            event.setQuitMessage(null);
        }
        @EventHandler
        public void onHunger(FoodLevelChangeEvent event) {
            event.setCancelled(true);
        }
     
        public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, final String[] args) {
            final Player player = (Player) sender;
            if (commandLabel.equalsIgnoreCase("help")) {
                player.sendMessage(ChatColor.GRAY + "sKits!");
                player.sendMessage(ChatColor.GRAY + "/Kill - Kills you!");
                player.sendMessage(ChatColor.GRAY + "/Balance - Shows your current balance!");
           
            }
        if (cmd.getName().equalsIgnoreCase("setspawn")) {
                settings.getData().set("spawn.world", player.getLocation().getWorld().getName());
                settings.getData().set("spawn.x", player.getLocation().getX());
                settings.getData().set("spawn.y", player.getLocation().getY());
                settings.getData().set("spawn.z", player.getLocation().getZ());
                settings.saveData();
                player.sendMessage(ChatColor.GREEN + "Spawn set!");
                return true;
        }
       
        if (cmd.getName().equalsIgnoreCase("spawn")) {
                if (settings.getData().getConfigurationSection("spawn") == null) {
                        player.sendMessage(ChatColor.RED + "The spawn has not yet been set!");
                        return true;
                }
                World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn.world"));
                double x = settings.getData().getDouble("spawn.x");
                double y = settings.getData().getDouble("spawn.y");
                double z = settings.getData().getDouble("spawn.z");
                player.teleport(new Location(w, x, y, z));
                player.sendMessage(ChatColor.GREEN + "Welcome to the spawn!");
        }
       
            if (cmd.getName().equalsIgnoreCase("warp")) {
                if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please specify a name!");
                        return true;
                }
                if (settings.getData().getConfigurationSection("warps." + args[0]) == null) {
                        player.sendMessage(ChatColor.RED + "Warp " + args[0] + " does not exist!");
                        return true;
                }
                player.sendMessage(ChatColor.GRAY + "Teleporting in 3 seconds!");
                getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    @Override
                    public void run() {
                        World w = Bukkit.getServer().getWorld(settings.getData().getString("warps." + args[0] + ".world"));
                        double x = settings.getData().getDouble("warps." + args[0] + ".x");
                        double y = settings.getData().getDouble("warps." + args[0] + ".y");
                        double z = settings.getData().getDouble("warps." + args[0] + ".z");
                        player.teleport(new Location(w, x, y, z));
                        player.sendMessage(ChatColor.GREEN + "Teleported to " + args[0] + "!");
                    }
                }, 60L);
        }
            if (cmd.getName().equalsIgnoreCase("setwarp")) {
                if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please specify a name!");
                        return true;
                }
                settings.getData().set("warps." + args[0] + ".world", player.getLocation().getWorld().getName());
                settings.getData().set("warps." + args[0] + ".x", player.getLocation().getX());
                settings.getData().set("warps." + args[0] + ".y", player.getLocation().getY());
                settings.getData().set("warps." + args[0] + ".z", player.getLocation().getZ());
                settings.saveData();
                player.sendMessage(ChatColor.GREEN + "Set warp " + args[0] + "!");
        }
       
            if (cmd.getName().equalsIgnoreCase("delwarp")) {
                if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please specify a name!");
                        return true;
                }
                if (settings.getData().getConfigurationSection("warps." + args[0]) == null) {
                        player.sendMessage(ChatColor.RED + "Warp " + args[0] + " does not exist!");
                        return true;
                }
                settings.getData().set("warps." + args[0], null);
                settings.saveData();
                player.sendMessage(ChatColor.GREEN + "Removed warp " + args[0] + "!");
            }
        return false;
        }
    }
    Here is my PrivateChat Class:

    Code:
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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 CommandPrivateMessage implements Listener, CommandExecutor {
       
        HashMap<String, String> reply_list = new HashMap<String, String>();
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            StringBuilder str = new StringBuilder();
            for (int i = 1; i < args.length; i++){
            str.append(args + " ");
            }
           
            if (cmd.getName().equalsIgnoreCase("tell") || cmd.getName().equalsIgnoreCase("whisper") || cmd.getName().equalsIgnoreCase("msg") || cmd.getName().equalsIgnoreCase("m")) {
           
            if (args.length < 2) {
            switch(cmd.getName().toLowerCase()) {
           
            case "msg":
            sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "/msg <player>");
            return true;
           
            case "m":
            sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "/m <player>");
            return true;
           
            case "tell":
            sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "/tell <player>");
            return true;
           
            case "whisper":
            sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "/whisper <player>");
            return true;
            }
            }
           
            Player target = Bukkit.getServer().getPlayer(args[0]);
            if (target == null) {
            sender.sendMessage(ChatColor.RED + "Error: " + ChatColor.GRAY + "Player not found.");
            return true;
            }
            if (!reply_list.containsKey(target.getName()))
            reply_list.put(target.getName(), sender.getName());
            String message = args[1];
            for (int i = 2; i < args.length; i++)
            message += " " + args;
            sender.sendMessage(ChatColor.RED + "[me > " + target.getName() + "] " + ChatColor.GRAY + message);
            target.sendMessage(ChatColor.RED + "[" + sender.getName() + " > me] " + ChatColor.GRAY + message);
            return true;
            }
           
            if (cmd.getName().equalsIgnoreCase("reply") || cmd.getName().equalsIgnoreCase("r")) {
           
            if (args.length < 1) {
            switch(cmd.getName().toLowerCase()) {
            case "reply":
            sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "/reply <player>");
            return true;
            case "r":
            sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "/r <player>");
            return true;
            }
            }
           
            if (args.length > 1) {
            sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "/r <player>");
            return false;
            }
           
            if (!reply_list.containsKey(sender.getName())) {
            sender.sendMessage(ChatColor.RED + "Error: " + ChatColor.GRAY + "You have nobody to reply to.");
            return true;
            }
           
            Player target = Bukkit.getServer().getPlayer(reply_list.get(sender.getName()));
            if (target == null) {
            sender.sendMessage("Error: " + ChatColor.GRAY + "Player not found.");
            return true;
            }
           
            String message = args[0];
            for (int i = 1; i < args.length; i++)
            message += " " + args;
           
            sender.sendMessage(ChatColor.RED + "[me > " + target.getName() + "] " + ChatColor.GRAY + str.toString() + message);
            target.sendMessage(ChatColor.RED + "[" + sender.getName() + " > me] " + ChatColor.GRAY + str.toString() + message);
           
            }
            return true;
            }
            }
    Ok, What I have found out in the least few minutes is that my /tell command isn't working and the normal Bukkit /tell command is Overriding mine... What do I do?

    Xacero
    I think the issue is that, the coding you gave me is for Java 7 and I need Java 6. Can you help me out there?

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

    Xacero

    ajs333
    PlayerCommandPreprocessEvent should allow you to override the bukkit command.

    check if the command is tell and if it is event.setCancelled(true) then put your command code there instead. (you'll have to adapt it to fit the event), or you can always just rename your command to something not used by bukkit. ("msg","message","pm"- ect)

    elementalgodz11


    for the "you will be replying to" just go to the bit that sends the messages and add that line. (sender.sendMessage(ChatColor.RED + "you will ...");

    For the no target just change whats inside of if (!reply_list.contains(sender.getName()) to your message.

    for muting

    if (/*is muted*/) {
    sender.sendMessage(ChatColor.RED + "You are muted");
    return true;
    }

    how you check if the sender is muted is up to you. If you're using your own list you'll need to load and save a file containing the mute list (unless you want it to reset on reload)

    ajs333


    no I sincerely doubt that's the issue. I'm not sure what the problem is... is the plugin loaded at all? Try
    Code:java
    1.  
    2. @Override public void onEnable() {
    3. System.out.println("PLUGIN IS LOADED");
    4. //your onenable stuff
    5. }
    6.  


    does the console print ("PLUGIN IS LOADED") ?

    and please edit your post :p don't make a new one everytime.
     
  19. Offline

    ajs333

    Xacero
    Yeah, the plugin loads! I did some back tracking and I went back to this... The issue is the /r command... It doesn't work for the sender.

    Here is what I have and you can tell me what I did wrong...

    Main Class:
    Code:
    package sEssentials;
     
     
    import java.util.ArrayList;
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import sEssentialsCommands.CommandChat;
    import sEssentialsCommands.CommandClearChat;
    import sEssentialsCommands.CommandGm;
    import sEssentialsCommands.CommandKill;
    import sEssentialsCommands.CommandPrivateMessage;
    import sEssentialsCommands.CommandTp;
    import sEssentialsCommands.CommandUnvanish;
    import sEssentialsCommands.CommandVanish;
     
     
     
    public class sEssentials extends JavaPlugin implements Listener{
       
        SettingsManager settings = SettingsManager.getInstance();
        ArrayList<Player> vanished = new ArrayList<Player>();
        HashMap<String, String> reply_list = new HashMap<String, String>();
     
        public void onEnable () {
            System.out.print("sEssentials Enabled!");
            getServer().getPluginManager().registerEvents(this, this);
            getCommand("invis").setExecutor(new CommandVanish());
            getCommand("vis").setExecutor(new CommandUnvanish());
            getCommand("gm").setExecutor(new CommandGm());
            getCommand("creative").setExecutor(new CommandGm());
            getCommand("adventure").setExecutor(new CommandGm());
            getCommand("survival").setExecutor(new CommandGm());
            getCommand("cc").setExecutor(new CommandClearChat());
            getCommand("clearchat").setExecutor(new CommandClearChat());
            getCommand("kill").setExecutor(new CommandKill());
            getCommand("tp").setExecutor(new CommandTp());
            CommandChat message = new CommandChat();
            getCommand("ce").setExecutor(message);
            getCommand("cd").setExecutor(message);
            getCommand("chatenable").setExecutor(message);
            getCommand("chatdisable").setExecutor(message);
            getCommand("msg").setExecutor(new CommandPrivateMessage());
            getCommand("tell").setExecutor(new CommandPrivateMessage());
            getCommand("reply").setExecutor(new CommandPrivateMessage());
            getCommand("r").setExecutor(new CommandPrivateMessage());
     
           
        }
       
        public void onDisable () {
            System.out.print("sEssentials Disabled!");
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)  {
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GOLD + "Welcome, " + player.getName() + " to sKits!");
            player.sendMessage(ChatColor.GOLD + "Do /help for a list of commands!");
            event.setJoinMessage("");
            if(player.hasPermission("skits.mod") || player.hasPermission("skits.tmod")) {
                player.hidePlayer(player);
                player.setCanPickupItems(false);
                player.sendMessage(ChatColor.GREEN + "You are now vanished! You are invisible to everyone but MOD and above!");
            }
        }
        @EventHandler
        public void onPlayerPickUp(PlayerPickupItemEvent event) {
            Item item = event.getItem();
            if (item.getItemStack().getType() == Material.BOWL) {
            event.setCancelled(true);
            }
        }
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
                if (this.getConfig().getString(e.getPlayer().getName()) != null) {
                        e.getPlayer().setDisplayName(this.getConfig().getString(e.getPlayer().getName()) + ChatColor.RESET); }
                }
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event) {
            event.setQuitMessage(null);
        }
        @EventHandler
        public void onHunger(FoodLevelChangeEvent event) {
            event.setCancelled(true);
        }
     
        public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, final String[] args) {
            final Player player = (Player) sender;
            if (commandLabel.equalsIgnoreCase("help")) {
                player.sendMessage(ChatColor.GRAY + "sKits!");
                player.sendMessage(ChatColor.GRAY + "/Kill - Kills you!");
                player.sendMessage(ChatColor.GRAY + "/Balance - Shows your current balance!");
           
            }
        if (cmd.getName().equalsIgnoreCase("setspawn")) {
                settings.getData().set("spawn.world", player.getLocation().getWorld().getName());
                settings.getData().set("spawn.x", player.getLocation().getX());
                settings.getData().set("spawn.y", player.getLocation().getY());
                settings.getData().set("spawn.z", player.getLocation().getZ());
                settings.saveData();
                player.sendMessage(ChatColor.GREEN + "Spawn set!");
                return true;
        }
       
        if (cmd.getName().equalsIgnoreCase("spawn")) {
                if (settings.getData().getConfigurationSection("spawn") == null) {
                        player.sendMessage(ChatColor.RED + "The spawn has not yet been set!");
                        return true;
                }
                World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn.world"));
                double x = settings.getData().getDouble("spawn.x");
                double y = settings.getData().getDouble("spawn.y");
                double z = settings.getData().getDouble("spawn.z");
                player.teleport(new Location(w, x, y, z));
                player.sendMessage(ChatColor.GREEN + "Welcome to the spawn!");
        }
       
            if (cmd.getName().equalsIgnoreCase("warp")) {
                if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please specify a name!");
                        return true;
                }
                if (settings.getData().getConfigurationSection("warps." + args[0]) == null) {
                        player.sendMessage(ChatColor.RED + "Warp " + args[0] + " does not exist!");
                        return true;
                }
                player.sendMessage(ChatColor.GRAY + "Teleporting in 3 seconds!");
                getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    @Override
                    public void run() {
                        World w = Bukkit.getServer().getWorld(settings.getData().getString("warps." + args[0] + ".world"));
                        double x = settings.getData().getDouble("warps." + args[0] + ".x");
                        double y = settings.getData().getDouble("warps." + args[0] + ".y");
                        double z = settings.getData().getDouble("warps." + args[0] + ".z");
                        player.teleport(new Location(w, x, y, z));
                        player.sendMessage(ChatColor.GREEN + "Teleported to " + args[0] + "!");
                    }
                }, 60L);
        }
            if (cmd.getName().equalsIgnoreCase("setwarp")) {
                if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please specify a name!");
                        return true;
                }
                settings.getData().set("warps." + args[0] + ".world", player.getLocation().getWorld().getName());
                settings.getData().set("warps." + args[0] + ".x", player.getLocation().getX());
                settings.getData().set("warps." + args[0] + ".y", player.getLocation().getY());
                settings.getData().set("warps." + args[0] + ".z", player.getLocation().getZ());
                settings.saveData();
                player.sendMessage(ChatColor.GREEN + "Set warp " + args[0] + "!");
        }
       
            if (cmd.getName().equalsIgnoreCase("delwarp")) {
                if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Please specify a name!");
                        return true;
                }
                if (settings.getData().getConfigurationSection("warps." + args[0]) == null) {
                        player.sendMessage(ChatColor.RED + "Warp " + args[0] + " does not exist!");
                        return true;
                }
                settings.getData().set("warps." + args[0], null);
                settings.saveData();
                player.sendMessage(ChatColor.GREEN + "Removed warp " + args[0] + "!");
            }
        return false;
        }
    }
    
    Private Message Class:
    Code:
    package sEssentialsCommands;
     
    import java.util.HashMap;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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 CommandPrivateMessage implements Listener, CommandExecutor {
       
        HashMap<String, String> reply_list = new HashMap<String, String>();
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            StringBuilder str = new StringBuilder();
            for (int i = 1; i < args.length; i++){
            str.append(args + " ");
            }
            if (cmd.getName().equalsIgnoreCase("tell") || cmd.getName().equalsIgnoreCase("msg")) {
               
                Player target = Bukkit.getServer().getPlayer(args[0]);
           
                reply_list.put(target.getName(), sender.getName());
                reply_list.put(sender.getName(), target.getName());
                {
           
                String message1 = "";
           
                for (int i = 1; i < args.length; i++) {
                message1 = message1 + (args[i] + " ");
                }
           
                sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "<" + ChatColor.GRAY + sender.getName() + ChatColor.WHITE + "" + ChatColor.BOLD + " -> " + ChatColor.GRAY + target.getDisplayName() + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "> " + ChatColor.GRAY + message1);
           
                target.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "<" + ChatColor.GRAY + sender.getName() + ChatColor.WHITE + "" + ChatColor.BOLD + " - > " + ChatColor.GRAY + target.getDisplayName() + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "> " + ChatColor.GRAY + message1);
           
                return true;
                    }
                }
       
           
           
           
           
            if (cmd.getName().equalsIgnoreCase("r") || cmd.getName().equalsIgnoreCase("reply")) {
                String message1 = "";
               
                for (int i = 0; i < args.length; i++) {
                message1 = message1 + (args + " ");
                }
               
                if (reply_list.containsKey(sender.getName())) {
                Player target = Bukkit.getServer().getPlayer(reply_list.get(sender.getName()));
                if (target == null) {
                sender.sendMessage(ChatColor.RED + "That player is offline!");
                return true;
                }
                sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "<" + ChatColor.GRAY + sender.getName() + ChatColor.WHITE + "" + ChatColor.BOLD + " -> " + ChatColor.GRAY + target.getDisplayName() + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "> " + ChatColor.GRAY + message1);
               
                target.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "<" + ChatColor.GRAY + sender.getName() + ChatColor.WHITE + "" + ChatColor.BOLD + " - > " + ChatColor.GRAY + target.getDisplayName() + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "> " + ChatColor.GRAY + message1);
                }
                }
            return false;
        }
    }
     
  20. Offline

    Xacero

    ajs333
    Already told you.

     
  21. Offline

    ajs333

    Xacero
    What about the /r or /reply command... that doesn't work at all... nothing happens.

    elementalgodz11
    Are you coding with Java 7 or Java 6?
     
  22. Offline

    Xacero


    /r is also registered by bukkit pretty sure.
    reply should work. Can you try moving the command to your main class and see if it runs?

    elementalgodz11
    Your code says if player is null (doesnt exist) to display usage and then print a message saying "you will be replying to " + [the players name]

    [the players name] is a value you're trying to get from a player that doesn't exist.

    See the problem?
     
  23. Offline

    ajs333

    Xacero
    Its not an issue that the command isn't registered because I know it is... there is an issue with the coding itself...

    Code:
    StringBuilder str = new StringBuilder();
            for (int i = 1; i < args.length; i++){
            str.append(args + " ");
            }
            if (cmd.getName().equalsIgnoreCase("tell") || cmd.getName().equalsIgnoreCase("msg")) {
               
                Player target = Bukkit.getServer().getPlayer(args[0]);
           
                reply_list.put(target.getName(), sender.getName());
                reply_list.put(sender.getName(), target.getName());
                {
           
                String message1 = "";
           
                for (int i = 1; i < args.length; i++) {
                message1 = message1 + (args[i] + " ");
                }
           
                sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "<" + ChatColor.GRAY + sender.getName() + ChatColor.WHITE + "" + ChatColor.BOLD + " -> " + ChatColor.GRAY + target.getDisplayName() + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "> " + ChatColor.GRAY + message1);
           
                target.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "<" + ChatColor.GRAY + sender.getName() + ChatColor.WHITE + "" + ChatColor.BOLD + " - > " + ChatColor.GRAY + target.getDisplayName() + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "> " + ChatColor.GRAY + message1);
           
                return true;
                    }
                }       
            if (cmd.getName().equalsIgnoreCase("r") || cmd.getName().equalsIgnoreCase("reply")) {
                String message = "";
               
                for (int i = 0; i < args.length; i++) {
                message = message + (args + " ");
                }
               
                if (reply_list.containsKey(sender.getName())) {
                Player target = Bukkit.getServer().getPlayer(reply_list.get(sender.getName()));
                if (target == null) {
                sender.sendMessage(ChatColor.RED + "That player is offline!");
                return true;
                }
                sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "<" + ChatColor.GRAY + sender.getName() + ChatColor.WHITE + "" + ChatColor.BOLD + " -> " + ChatColor.GRAY + target.getDisplayName() + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "> " + ChatColor.GRAY + message);
               
                target.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "<" + ChatColor.GRAY + sender.getName() + ChatColor.WHITE + "" + ChatColor.BOLD + " - > " + ChatColor.GRAY + target.getDisplayName() + ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "> " + ChatColor.GRAY + message);
                }
                }
            return false;
        }
    }
    Xacero
    Also, Just back to my Java 6 and Java 7... It does make a difference because the server I'm testing on is for Java 6 so that is why it wasn't working...
     
  24. Offline

    Xacero

    elementalgodz11
    You're checking if (args < 1) send that message.

    if arguments < 1 that means arguments has to be zero.
    that means there are ZERO arguments.

    You are trying to get a player from the server using argument one.

    See the problem?

    ajs333
    Java 7
     
  25. Offline

    ajs333

    Xacero
    Will that mess up everything I have??
     
  26. Offline

    Xacero

    ajs333

    You're updating your java... it has nothing to do with your projects ...

    elementalgodz11
    Do you have too many closing brackets?

    elementalgodz11
    Aside from being really messy I don't see anything wrong with it :p What's the error?

    Code:java
    1. case "r":
    2. if (!reply_list.contains(sender.getName())) {
    3. sender.sendMessage("You will not be replying to anyone");
    4. return true;
    5. }
    6. Player target1 = Bukkit.getServer().getPlayer(reply_list.get(sender.getName()));
    7. if (target1 == null) {
    8. sender.sendMessage("That player is offline");
    9. return true;
    10. }
    11. sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "/reply <player>");
    12. sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "You will be replying to " + ChatColor.RED + target1.getName());
    13.  
    14. }


    elementalgodz11
    That's the "slightly more efficient" code that someone else gave you.
    The reason I didn't give you that was because it assumes the message is atleast 2 words long.

    Code:java
    1.  
    2. String message = "";
    3. for (int w = 0; w < args.length; w++) {
    4. message += args[w];
    5. if (w != (args.length - 1))
    6. message += " ";
    7. } // used w instead of i because i doesnt play nice with the formatting here.
    8.  
    9. }


    elementalgodz11
    Code:
                if (args.length > 1) {
                sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.GRAY + "/r <player>");
                return false;
                }
    Why is this there? It will always return true and end of the command. either remove the whole thing or remove the return false.

    elementalgodz11
    Don't forget to tag me ;) I only saw that by chance (Accidentally hit refresh instead of back)

    Code:java
    1.  
    2. //if (!reply_list.containsKey(target.getName())) this should have never been here ._.
    3. reply_list.put(target.getName(), sender.getName());
    4. reply_list.put(sender.getName(), target.getName()); // as was given by a previous poster.


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

    The_Doctor_123

    elementalgodz11
     
  28. Offline

    Xacero

    elementalgodz11
    if (target == null) {
    sender.sendMessage(ChatColor.RED + "That player is offline!");
    return true;
    }

    edit - Also you should reaaaaallly consider cleaning that source up ._. it's honestly very messy.

    elementalgodz11
    Code:java
    1. if (!reply_list.containsKey(sender.getName()))


    Pretty sure all these checks are useless now since you're adding sender.getName(), target.getName() to the replylist. (the reply list will ALWAYS contain a value for sender)

    What is the error?

    elementalgodz11
    Same as before.
    check target for null, if it is null show the usage message and return true;

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    elementalgodz11 likes this.
  29. Offline

    Necrodoom

    This train wreck of a thread is now locked.

    ajs333 Stop spamming the post button, you have an edit button, i dont need to see 7 posts in a row on the same thread within a time span of 20 minutes. This isnt twitter. It will also help if you listen to what you are being told by people who try to help.

    elementalgodz11 Do not hijack someone else's thread. If you require support, make your own thread and do not reuse someone else's thread for support.
     
    Cirno, Atakyn, Garris0n and 4 others like this.
Thread Status:
Not open for further replies.

Share This Page