Bukkit commands help

Discussion in 'Plugin Development' started by HiPPiE1337, Jun 15, 2011.

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

    HiPPiE1337

    I have a question, the script below is for a give command with multiple variables.
    But how does bukkit knows that the command is "give".
    Thanks
    Code:
    package com.dinnerbone.bukkit.scrap.commands;
    
    import com.dinnerbone.bukkit.scrap.ScrapBukkit;
    import org.bukkit.ChatColor;
    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;
    
    public class GivePluginCommand implements CommandExecutor {
        private final ScrapBukkit plugin;
    
        public GivePluginCommand(ScrapBukkit plugin) {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if ((args.length > 3) || (args.length == 0)) {
                return false;
            }
            if (!sender.isOp()) {
                sender.sendMessage("You do not have permission to give players items");
                return false;
            }
    
            Player player = null;
            Material material = null;
            int count = 1;
            String[] gData = null;
            Byte bytedata = null;
            if (args.length >= 1) {
                gData = args[0].split(":");
                material = Material.matchMaterial(gData[0]);
                if (gData.length == 2) {
                    bytedata = Byte.valueOf(gData[1]);
                }
            }
            if (args.length >= 2) {
                try {
                    count = Integer.parseInt(args[1]);
                } catch (NumberFormatException ex) {
                    sender.sendMessage(ChatColor.RED + "'" + args[1] + "' is not a number!");
                    return false;
                }
            }
            if (args.length == 3) {
                player = plugin.getServer().getPlayer(args[2]);
                if (player == null) {
                    sender.sendMessage(ChatColor.RED + "'" + args[2] + "' is not a valid player!");
                    return false;
                }
            } else {
                if (plugin.anonymousCheck(sender)) {
                    return false;
                } else {
                    player = (Player) sender;
                }
            }
            if (material == null) {
                sender.sendMessage(ChatColor.RED + "Unknown item");
                return false;
            }
            if (bytedata != null) {
                player.getInventory().addItem(new ItemStack(material, count, (short) 0, bytedata));
            } else {
                player.getInventory().addItem(new ItemStack(material, count));
            }
            sender.sendMessage("Given " + player.getDisplayName() + " " + count + " " + material.toString());
            return true;
        }
    }
     
  2. Offline

    Jako

    You should have added that command to your plugin.yml file.

    Should be like this:
    Code:
    name: Blah
    main: something.something.Blah.Blah
    version: 1.0
    commands:
        give:
            description: give things
            usage: type /give blah blah
    
    When bukkit runs your plugin it will then send any command that is /give to your plugin's onCommand event.
     
  3. Offline

    HiPPiE1337

    So if the class of the command is located at: com.HiPPiE1337.givecommand
    And i put that at main: it will process that class from that class?

    Thank you already for your help
     
  4. Offline

    Jako

    Seems like it, if you have created your plugin.yml file already, it should already look something like this:
    Code:
    name: givecommand
    main: com.HiPPiE1337.givecommand.givecommand
    version: 1.0
    Now what you need to add is the commands part.
    But yes basically, if you have com.HiPPiE1337.givecommand.givecommand it should send what ever is under "commands:" to your givecommand class's onCommand event. Hope that helps.

    Edit: If that doesn't seem to work, post the error messages and your plugin.yml file. Also make sure there are no tabs in your plugin.yml file. To remove them use find and replace (Eclipse is CTRL+F) and find "\t" and replace with four spaces " ". You can do this in notepad++ also using CTRL+H.
     
  5. Offline

    HiPPiE1337

    Thanks, i will try it tommorow cause i dont have time to do it now.;)
     
Thread Status:
Not open for further replies.

Share This Page