More than One Command - need help!

Discussion in 'Plugin Development' started by jojohnson1, Apr 3, 2012.

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

    jojohnson1

    Hey Guys,
    I just wanted to know whether it's true that you can only have one onCommand Method in one class? Eclipse is saying that. And the problem is that the CommandExecutor does not work like they explained it in the wiki. Would you please be so kind and help me? :D

    Screenshots: (My Plugin was named ProjectX)

    projectx.java.png projectxCommandExecutor.java.png
     
  2. Offline

    CorrieKay

    uh... Yes. thats not a bukkit thing, thats a java... thing. :p

    you can either use the onCommand method as a factory, or you can use executors. the onCommand method will be accessed from inside the executor if you register it.
     
  3. Offline

    zhuowei

    Are you putting your onCommand in your plugin main class? A single command executor, including the main class, can handle multiple commands as long as you test for which command is being run.

    Code:
    public boolean onCommand(CommandSender sender, Command command, java.lang.String label, java.lang.String[] args) {
        if (label.equalsIgnoreCase("herp")) {
            sender.sendMessage("Herp!");
            return true;
        } else if (label.equalsIgnoreCase("derp")) {
            sender.sendMessage("Derp!");
            return true;
        }
        return false;
    }
    
    Don't forget to declare the commands in your plugin.yml.

    Edit: added clarification.
     
  4. Offline

    xDrapor

    I would recommend this method:
    Make a separate command class, make it implement a CommandExecutor
    Do your code as you normal would.

    In your main class, add:
    private TheClassName myExecutor;

    Then, in onEnable:
    myExecutor = new TheClassName(this);
    getCommand("thecommandyouset").setExecutor(myExecutor);

    Hope this helps!

    Note: You can also use the else method, but I feel this is easier to update and fix bugs with.
     
  5. Offline

    jojohnson1

    will the new (command) class need an onEnable/Disable, too?
     
  6. Offline

    xDrapor

    Nope.
    Here's an example of one of my command classes:

    Code:
    package me.xdrapor.dynamicban.commands;
     
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.util.List;
    import java.util.logging.Logger;
    import me.xdrapor.dynamicban.DynamicBan;
    import me.xdrapor.dynamicban.commands.DynamicCheckBan;
    import me.xdrapor.dynamicban.commands.DynamicIPBan;
    import me.xdrapor.dynamicban.commands.DynamicIPList;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Server;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    @SuppressWarnings("unused")
    public class DynamicCheckBan implements CommandExecutor
    {
     
        private DynamicBan plugin;
     
        public DynamicCheckBan(DynamicBan plugin)
        {
            this.plugin = plugin;
        }
        public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args)
        {
            if(args.length == 0)
            {
                cs.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.DARK_RED + "DynamicBan" + ChatColor.DARK_PURPLE + "] : " + "The correct command is /dynstatus (name)");
                cs.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.DARK_RED + "DynamicBan" + ChatColor.DARK_PURPLE + "] : " + "Usage: Checks the IP of the specified player.");
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("dynstatus"))
            {
                String IP = plugin.getCustomConfig().getString(args[0].toLowerCase() + ".IP-Address");
                if(cs instanceof Player)
                {
                    Player player = (Player)cs;
                     
                    if(player.hasPermission("dynamicban.banstatus"))
                    {
                        if(plugin.getServer().getIPBans().contains(IP))
                        player.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.DARK_RED + "DynamicBan" + ChatColor.DARK_PURPLE + "] : " + args[0] + " is IP-Banned.");
                    }else
                    if(!(plugin.getServer().getIPBans().contains(IP))){
                        player.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.DARK_RED + "DynamicBan" + ChatColor.DARK_PURPLE + "] : "+ args[0] + " is not IP-Banned.");
                        return true;
                    }
                    else
                    {
                        if(plugin.getServer().getIPBans().contains(IP))
                        player.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.DARK_RED + "DynamicBan" + ChatColor.DARK_PURPLE + "] : "+ args[0] + " is IP-Banned.");
                    }
                    if(!(plugin.getServer().getIPBans().contains(IP))){
                        player.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.DARK_RED + "DynamicBan" + ChatColor.DARK_PURPLE + "] : " + args[0] + " is not IP-Banned.");
                        return true;
                    }
                }
                else
                {
                    if(plugin.getServer().getIPBans().contains(IP))
                    cs.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.DARK_RED + "DynamicBan" + ChatColor.DARK_PURPLE + "] : " + args[0] + " is IP-Banned.");
                }
                if(!(plugin.getServer().getIPBans().contains(IP))){
                    cs.sendMessage(ChatColor.DARK_PURPLE + "[" + ChatColor.DARK_RED + "DynamicBan" + ChatColor.DARK_PURPLE + "] : " + args[0] + " is not IP-Banned.");
                    return true;
                }
            }
            return true;
        }
    }
    
     
  7. Offline

    jojohnson1

    great, no mistake! For the second command, i rename "MyExecutor" in "MyExecutor2" or what?
     
  8. Offline

    CorrieKay

    you can name it whatever the hell you want, to be honest.
     
  9. Offline

    xDrapor

    Whatever you'd like as Corrie said, just an example :)
     
  10. Offline

    jojohnson1

    Wow Guys You're great! 2 commands and it still works! (But too bad that /kill is already used by bukkit :p)
    THNAK YOU!!!!!! [diamond][diamond][diamond]
     
  11. Offline

    xDrapor

    No problem!
     
  12. Offline

    comp8nerd4u2

    It should be noted that plugin sub menus are possible but you will have to write your own command manager for that.
     
  13. Offline

    jojohnson1

    I've antother Question: I saw i can code an inventory manipulation. In the example, they had the event onPlayerJoin but I want to have an onCommand Method there. Can you show me a code how to get the Inventory of Sender?
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            PlayerInventory inventory = sender.getInventory();
    The Error is Java doesn't know the getInventory here...
     
  14. Offline

    xDrapor

    Code:
    Player player = Bukkit.getPlayer(args[0]);
    PlayerInventory inventory = player.getInventory();
    
    Where args[0] in your command will represent the player name.
    So if u did /inv xDrapor it would be getting xDrapors inventory.
    If you want the senders inventory:
    Code:
    Player player = Bukkit.getPlayer(sender.getName());
    PlayerInventory inventory = player.getInventory();
    Hope this helps!
     
  15. Offline

    CorrieKay

    getInventory() is a player method, CommandSender doesnt have that method because not all commandsenders have an inventory.

    //make sure the commandSender is actually a player
    if(sender instanceof Player){
    //then cast the sender as a player
    Player player = (Player)sender;
    //then access the inventory
    player.getInventory();
    }
     
  16. Offline

    xDrapor

    Well, wouldn't my method work too? And for once, I was quicker than u :p
    EDIT: I read your code and realised its the same thing. LOL.
     
  17. Offline

    CorrieKay

    hmm, yes, i guess your method would work too, but it'd throw exceptions if the console sent the command :p
     
  18. Offline

    xDrapor

    Yep, I assumed he knew he had to add the instances, i just gave him what he wanted lolol
     
  19. Offline

    jojohnson1

    Great, works. But I've got still another Question: I want to make a Plugin with a Game. After /start, you have to keep living. After Death (onPlayerDeath) you will get creative Mode:
    Code:
    public class Death implements Listener{
     
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            Player player = event.getEntity();
            String pname = player.getName();
            player.sendMessage(ChatColor.RED + "You Lost!");
            Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "[INFO] " + ChatColor.BLUE + pname + ChatColor.RED + " lost!");
            player.setGameMode(GameMode.CREATIVE);
            player.sendMessage(ChatColor.RED + "You now got Creative Mode.");
        }
    }
    
    My Question is: How can I check whether someone started (/start) the Game?
     
Thread Status:
Not open for further replies.

Share This Page