Correct way to handle multiple commands

Discussion in 'Plugin Development' started by herghost, Nov 13, 2011.

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

    herghost

    Hi All

    I have followed the tuturial on how to use commands, and I have a quick question, how do you have more than one command! Is this the correct way to do it? with the onCommand number changing each time?

    Code:
    package me.herghost.Fiery;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
     
    public class FieryCommandEx implements CommandExecutor {
    
        private Fiery plugin;
    
        public FieryCommandEx(Fiery plugin)
        {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("item")&& sender instanceof Player)
                {
                    Player player = (Player) sender;
                    int idOfItems = Material.matchMaterial(args[0]).getId();
                    int numberOfItems = Integer.parseInt(args[1]);
                    ItemStack ItemToAdd = new ItemStack(idOfItems,numberOfItems);
                    PlayerInventory inventory = player.getInventory();
                    inventory.addItem(ItemToAdd);
    
                    Material itemGivenMat = Material.getMaterial(idOfItems);
                     String itemGiven = itemGivenMat.toString();
                     sender.sendMessage("§cYou have gained §e" + args[1] + "§6 " + itemGiven + "§c.");
    
                    return true;
                }
            return false;
        }
    
        public boolean onCommand1(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("give")&& sender instanceof Player)
                {
                 Player player = Bukkit.getPlayerExact(args[0]);
                 if (player != null)
                 {
                     Material material = Material.matchMaterial(args[1]);
                     if (material != null)
                         {
                             Command.broadcastCommandMessage(sender, "Giving " + player.getName() + " some " + material.getId() + " (" + material + ")");
                             int amount = 1;
                             if (args.length >= 3)
                                 {
                                     try
                                     {
                                          amount = Integer.parseInt(args[2]);
                                     }
    
                                     catch (NumberFormatException ex) {}
                                     if (amount < 1) amount = 1;
                                     if (amount > 64) amount = 64;
                                 }
                             player.getInventory().addItem(new ItemStack(material, amount));
                         }
                             else
                                 {
                                 sender.sendMessage("There's no item called " + args[1]);
                                 }
                 }
                             else
                                 {
                                   sender.sendMessage("Can't find user " + args[0]);
                                 }
                             return true;
    
                 }
            return false;
        }
    
        public boolean onCommand2(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("kick")&& sender instanceof Player)
                {
                Player player = Bukkit.getPlayerExact(args[0]);
                if (player != null) {
                Command.broadcastCommandMessage(sender, "Kicking " + player.getName());
                 player.kickPlayer("Kicked by admin");
                }
                else
                    {
                    sender.sendMessage("Can't find user " + args[0] + ". No kick.");
                    }
    
                           return true;
     
                }
            return false;
        }
       
    }
    Thanks
     
  2. Offline

    Jacek

    Not exactly...

    You can create a separate class to handle each command, then in that class the onCommand() will be called only for the command you set it as the executor for. This also means there is no need to check the command name in the onCommand since that will only be called for the command you set it as the executor for.

    So where you have something like

    Code:
    this.getCommand('give').setExecutor(commandExecutor);
    this.getCommand('kick').setExecutor(commandExecutor);
    this.getCommand('item').setExecutor(commandExecutor);
    
    you would want

    Code:
    this.getCommand('give').setExecutor(giveCommandExecutor);
    this.getCommand('kick').setExecutor(kickCommandExecutor);
    this.getCommand('item').setExecutor(itemCommandExecutor);
    
     
  3. Offline

    ItsHarry

    onCommand1, onCommand2? That won't work.

    You either have to use a CommandExecutor, or add if statements in the onCommand method.
     
  4. Offline

    Xandaros

    Just remove every "return false;" at the ende, put them all into onCommand() and put the "return false;" at the VERY end.

    That will do it, quick and dirty.

    For the "better" solutions, look above. (better is a matter of definition, I am pretty sure that this quick and dirty method is better for performance, while the other way is actually readable)
     
  5. Offline

    herghost

    Thanks for the info :) however I am having problems actually getting it to work!

    I like nice and easy layouts, so I wanted something like:

    src/package/fiery.java // to be the main file
    src/package/commands/itemCommand //to be where the command class 'lives'

    I am still getting some wrong as the line

    Code:
    this.getCommand("item").setExecutor(itemCommand);
    
    has an error with itemCommand

    my full code:

    src/me.herghost.Fiery.Fiery.java

    Code:
    package me.herghost.Fiery;
    
    import java.util.logging.Logger;
    
    import me.herghost.Fiery.commands.itemCommand;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Fiery extends JavaPlugin {
    	Logger log = Logger.getLogger("Minecraft");
    	public void onEnable(){
    		log.info("Fiery Plugin Enabled");
    		this.getCommand("item").setExecutor(itemCommand);
    	}
    	public void onDisable(){
    		log.info("Fiery Plugin Disabled");
    	}
    
    }
    
    and

    src/me.herghost.Fiery/commads/itemCommand.java

    Code:
    package me.herghost.Fiery.commands;
    
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class itemCommand {
    	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    	{
    		if(cmd.getName().equalsIgnoreCase("item")&& sender instanceof Player)
    			{
    				Player player = (Player) sender;
    				int idOfItems = Material.matchMaterial(args[0]).getId();
    				int numberOfItems = Integer.parseInt(args[1]);
    				ItemStack ItemToAdd = new ItemStack(idOfItems,numberOfItems);
    				PlayerInventory inventory = player.getInventory();
    				inventory.addItem(ItemToAdd);
    				Material itemGivenMat = Material.getMaterial(idOfItems);
    	             String itemGiven = itemGivenMat.toString();
    	             sender.sendMessage("§cYou have gained §e" + args[1] + "§6 " + itemGiven + "§c.");
    				return true;
    			}
    		return false;
    	}
    }
    
    
    Much thanks for all the useful info so far :)
     
  6. Offline

    Jacek

    itemCommand needs to be an instance of the CommandExecutor

    Code:
    itemCommand = new ItemCommand();
    
    if you only use the executor once you can just pass it in directly.

    Code:
    this.getCommand("item").setExecutor(new ItemCommand());
     
  7. Offline

    herghost

    Ahh thanks :)

    Should have stuck to php :)
     
Thread Status:
Not open for further replies.

Share This Page