Command Returns Nothing

Discussion in 'Plugin Development' started by boss86741, Jun 2, 2013.

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

    boss86741

    I am making a class plugin. Basically, I ran the command /class choose mage in-game and I also tried all the other classes and it didn't return any value. Not even a console error.

    Main Class:
    Code:
    package com.boss86741.plugins.godlyclasses;
     
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.logging.Level;
     
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class GodlyClasses extends JavaPlugin {
       
        // Config Stuffs Implementation
        private FileConfiguration customConfig = null;
        private File customConfigFile = null;
       
        public void reloadCustomConfig() {
            if (customConfigFile == null) {
            customConfigFile = new File(getDataFolder(), "config.yml");
            }
            customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
       
            // Look for defaults in the jar
            InputStream defConfigStream = this.getResource("config.yml");
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                customConfig.setDefaults(defConfig);
            }
        }
       
        public FileConfiguration getCustomConfig() {
            if (customConfig == null) {
                this.reloadCustomConfig();
            }
            return customConfig;
        }
       
        public void saveCustomConfig() {
            if (customConfig == null || customConfigFile == null) {
            return;
            }
            try {
                getCustomConfig().save(customConfigFile);
            } catch (IOException ex) {
                this.getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);
            }
        }
       
        @Override
        public void onEnable() {
            getLogger().info("Say hi to the class plugin :)");
            GodlyCommands gc = new GodlyCommands(this);
            getCommand("class").setExecutor(gc);
            this.saveCustomConfig();
        }
       
        @Override
        public void onDisable() {
            getLogger().info("Bye, bye!");
        }
    }
    

    Command Class:
    Code:
    package com.boss86741.plugins.godlyclasses;
     
    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;
    import org.bukkit.inventory.PlayerInventory;
     
    public class GodlyCommands implements CommandExecutor {
     
        // implement the plugin method.
        private GodlyClasses plugin;
       
        public GodlyCommands(GodlyClasses plugin) {
            this.plugin = plugin;
        }
       
        String plName;
        // command code
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {
            if (cmd.getName().equalsIgnoreCase("class")) {
                if (args.length == 2) {
                    if (args[1] == "choose") {
                        // choose the archer class
                        if (args[2] == "archer") {
                            sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class archer!");
                            plName = sender.getName();
                            plugin.getConfig().getStringList("archer").add(plName);
                            // Give them the kit
                            Player player = (Player) sender;
                            ItemStack[] items = { new ItemStack(Material.BOW), new ItemStack(Material.BOW) };
                            PlayerInventory inv = player.getInventory();
                            for (ItemStack i : items) {
                                inv.setItem(inv.firstEmpty(), i);
                            }
                            ItemStack[] otherItems = {new ItemStack(Material.ARROW), new ItemStack(Material.ARROW)};
                            player.getInventory().addItem(otherItems);
                           
                        } else {
                            // choose the warrior class
                            if (args[2] == "warrior") {
                                sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class warrior!");
                                String plName = sender.getName();
                                plugin.getConfig().getStringList("warrior").add(plName);
                                // Give them the kit
                                Player player = (Player) sender;
                                ItemStack[] items = {new ItemStack(Material.IRON_SWORD),/* Now for the chain armor.*/ new ItemStack(Material.CHAINMAIL_BOOTS), new ItemStack(Material.CHAINMAIL_LEGGINGS), new ItemStack(Material.CHAINMAIL_CHESTPLATE), new ItemStack(Material.CHAINMAIL_HELMET)};
                                PlayerInventory inv = player.getInventory();
                                for (ItemStack i : items){
                                    inv.setItem(inv.firstEmpty(), i);
                                }
                            } else {
                                // choose the Mage class
                                if (args[2] == "mage") {
                                    sender.sendMessage(ChatColor.DARK_GREEN + "[Classes]" + ChatColor.RED + " You have chosen the class mage!");
                                    String plName = sender.getName();
                                    plugin.getConfig().getStringList("mage").add(plName);
                                    // Give them the kit
                                    Player player = (Player) sender;
                                    ItemStack[] items = {new ItemStack(Material.BREAD), new ItemStack(Material.FIREBALL), new ItemStack(Material.WATER)};
                                    PlayerInventory inv = player.getInventory();
                                    for (ItemStack i : items){
                                        inv.setItem(inv.firstEmpty(), i);
                                    }
                                } else {
                                    sender.sendMessage(ChatColor.DARK_RED + "Use /class list to see the list of classes.");
                                }
                            }
                        }
                    }
                } else {
                    // wrong argument amount
                    sender.sendMessage(ChatColor.RED + "To see a list of commands, type /class help 1. Use this command like this: /class choose <classname>");
                }
            return true;
            }
        return false;
        }
    }
    
    plugin.yml:
    Code:
    main: com.boss86741.plugins.godlyclasses.GodlyClasses
    name: GodlyClasses
    version: 0.1
    author: boss86741
    description: Class plugin.
    prefix: GodlySurvival
    website: http://www.boss86741.com/
    commands:
      class:
          description: This command is the main command of the class plugin.
          permission: classes.*
    permissions:
      classes.choose:
          description: Allows you to choose your class.
          default: true
      classes.*:
          description: Allows use of all class commands.
          default: op
     
  2. Offline

    chasechocolate

    Arguments start at 0. So you should be checking if args[0] is choose and args[1] is whatever class (your args.length == 2 check is fine). Also, compare strings with .equalsIgnoreCase().
     
Thread Status:
Not open for further replies.

Share This Page