Why only select the 1 kit?

Discussion in 'Plugin Development' started by Treeline1, Jan 10, 2015.

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

    Treeline1

    Im currently making a plugin that allows players to choose a kit. For some reason, I have it set so they can only choose the 1 kit(archer) when they should be able to choose both by doing /kit archer/pvp

    Main:
    Code:
    package me.liam.main;
    
    import me.liam.commands.ArcherCommand;
    import me.liam.commands.KitCommand;
    import me.liam.commands.SoupCommand;
    import me.liam.commands.SpawnCommand;
    import me.liam.event.BlockListener;
    import me.liam.event.DamageListener;
    import me.liam.event.InventoryListener;
    import me.liam.event.PlayerListener;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Kitpvp extends JavaPlugin {
        public static Kitpvp inst;
       
        public void onEnable() {   
            PluginManager pm = getServer().getPluginManager();
            inst = this;
           
            pm.registerEvents(new DamageListener(), this);
            pm.registerEvents(new BlockListener(), this);
            pm.registerEvents(new InventoryListener(), this);
            pm.registerEvents(new PlayerListener(), this);
            getCommand("kit").setExecutor(new KitCommand());
            getCommand("soup").setExecutor(new SoupCommand());
            getCommand("spawn").setExecutor(new SpawnCommand());
            getCommand("kit").setExecutor(new ArcherCommand());
    
    
           
            System.out.println("KitPVP Enabled!");
        }
       
        public void onDisabled(){
            System.out.println("KitPVP Disabled!");
        }
    
    }
    
    
    PVP:
    Code:
    package me.liam.commands;
    
    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.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class KitCommand implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(!(sender instanceof Player)){
                return false;
            }
            Player player = (Player)sender;
            if(args[0].equalsIgnoreCase("pvp")) {
                clearInventory(player.getInventory());
               
                ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                sword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
               
                ItemStack helmet = new ItemStack(Material.IRON_HELMET, 1);
                helmet.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE, 1);
                chestplate.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS, 1);
                leggings.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack boots = new ItemStack(Material.IRON_BOOTS, 1);
                boots.addEnchantment(Enchantment.DURABILITY, 3);
               
                player.getInventory().setHelmet(helmet);
                player.getInventory().setChestplate(chestplate);
                player.getInventory().setLeggings(leggings);
                player.getInventory().setBoots(boots);
                player.getInventory().addItem(sword);
                player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP, 32));
    
               
                player.sendMessage(ChatColor.RED + "You have chosen the PvP Kit!");
                return true;
            }
            return true;
        }
        public void clearInventory(PlayerInventory inv){
            inv.clear();
            inv.setHelmet(null);
            inv.setChestplate(null);
            inv.setLeggings(null);
            inv.setBoots(null);
        }
       
    }
    Archer:
    Code:
    package me.liam.commands;
    
    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.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class ArcherCommand implements CommandExecutor {
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(!(sender instanceof Player)){
                return false;
            }
            Player player = (Player)sender;
            if(args[0].equalsIgnoreCase("archer")) {
                clearInventory(player.getInventory());
                ItemStack sword = new ItemStack(Material.IRON_SWORD, 1);
                sword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
               
                ItemStack bow = new ItemStack(Material.BOW, 1);
                bow.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                bow.addEnchantment(Enchantment.ARROW_DAMAGE, 2);
                bow.addEnchantment(Enchantment.DURABILITY, 3);   
                ItemStack helmet = new ItemStack(Material.IRON_HELMET, 1);
                helmet.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack chestplate = new ItemStack(Material.GOLD_CHESTPLATE, 1);
                chestplate.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS, 1);
                leggings.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack boots = new ItemStack(Material.LEATHER_BOOTS, 1);
                boots.addEnchantment(Enchantment.DURABILITY, 3);
               
                player.getInventory().setHelmet(helmet);
                player.getInventory().setChestplate(chestplate);
                player.getInventory().setLeggings(leggings);
                player.getInventory().setBoots(boots);
                player.getInventory().addItem(sword);
                player.getInventory().addItem(bow);
                player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP, 32));
                player.getInventory().addItem(new ItemStack(Material.ARROW, 1));
    
               
                player.sendMessage(ChatColor.RED + "You have chosen the Archer Kit!");
                return true;
            }
            return true;
        }
        public void clearInventory(PlayerInventory inv){
            inv.clear();
            inv.setHelmet(null);
            inv.setChestplate(null);
            inv.setLeggings(null);
            inv.setBoots(null);
        }
       
    }
    
    
    Plugin.yml:
    I guess its because the main class has to enable 2 kit commands but only registers one.
    but what if I want it to be /kit (class) for all of them?
     
    Last edited by a moderator: Jan 10, 2015
  2. Offline

    Krizeh

    If you'd like to that then you should merge the two classes.
    Do something like this:

    Code:
            if ((args.length == 1) && (args[0].equalsIgnoreCase("pvp"))) {
    // blah blah blah
    }
            if ((args.length == 1) && (args[0].equalsIgnoreCase("archer"))) {
    //blah blah blooooo
    }
     
  3. Offline

    Treeline1

    If I was doing like 10+ classes it would get too crowded though
     
  4. Offline

    Krizeh

    @Treeline1
    Try inserting
    Code:
        if (cmd.getLabel().equalsIgnoreCase("kit")) {
    Before each of your if statements
    This is SaintSyphers code :p
     
  5. Offline

    Treeline1

    Nope :|
     
  6. Offline

    Krizeh

  7. Offline

    Treeline1

  8. Offline

    Krizeh

    @Treeline1

    Your brackets are messed up?
    Any errors in eclipse?
     
  9. Offline

    Treeline1

    Fixed the brakcets....
    But now when i do /kit pvp it gives me nothing.
    And when i do /kit archer it gives me everything. So /kit pvp doesnt work again

    Also no errors anywhere anymore
     
  10. Offline

    Experminator

    Uhmm, there is no command registered in any class?
    An command can be registered with:
    Code:
    if(cmd.getName().equalsIgnoreCase("kit")){
       // Do Stuff.
    }
    Make sure the code above is written in een onCommand method and that the command is registered in the plugin.yml.
     
  11. Offline

    Krizeh

    @Treeline1

    Yeah, the only thing I can suggest is doing what I said previously. You can shrink lines of code so it doesn't look as messy. You can also create a separate class and make it execute the other kit classes.

    You can create a kit class and do something like this:
    Code:
    if (cmd.getLabel().equalsIgnoreCase("kit")) {
            if ((args.length == 1) && (args[0].equalsIgnoreCase("pvp"))) {
    
    // blah blah bohohohoh
    //Example:
            Kits.pvp.giveKit(player);
     
  12. Offline

    Treeline1

    Ehh that didnt work either... I have this all but I dunno whats wrong...

    Code:
    package me.liam.main;
    
    import me.liam.commands.ArcherCommand;
    import me.liam.commands.KitCommand;
    import me.liam.commands.SoupCommand;
    import me.liam.commands.SpawnCommand;
    import me.liam.event.BlockListener;
    import me.liam.event.DamageListener;
    import me.liam.event.InventoryListener;
    import me.liam.event.PlayerListener;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Kitpvp extends JavaPlugin {
        public static Kitpvp inst;
       
        public void onEnable() {   
            PluginManager pm = getServer().getPluginManager();
            inst = this;
           
            pm.registerEvents(new DamageListener(), this);
            pm.registerEvents(new BlockListener(), this);
            pm.registerEvents(new InventoryListener(), this);
            pm.registerEvents(new PlayerListener(), this);
            getCommand("kit").setExecutor(new KitCommand());
            getCommand("soup").setExecutor(new SoupCommand());
            getCommand("spawn").setExecutor(new SpawnCommand());
            getCommand("kit").setExecutor(new ArcherCommand());
    
    
           
            System.out.println("KitPVP Enabled!");
        }
       
        public void onDisabled(){
            System.out.println("KitPVP Disabled!");
        }
    
    }
    
    Code:
    package me.liam.commands;
    
    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.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class KitCommand implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(!(sender instanceof Player)){
                return false;
            }
            Player player = (Player)sender;
            if (cmd.getLabel().equalsIgnoreCase("kit")) {
            if(args[0].equalsIgnoreCase("pvp")) {
                clearInventory(player.getInventory());
               
                ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                sword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
               
                ItemStack helmet = new ItemStack(Material.IRON_HELMET, 1);
                helmet.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE, 1);
                chestplate.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS, 1);
                leggings.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack boots = new ItemStack(Material.IRON_BOOTS, 1);
                boots.addEnchantment(Enchantment.DURABILITY, 3);
               
                player.getInventory().setHelmet(helmet);
                player.getInventory().setChestplate(chestplate);
                player.getInventory().setLeggings(leggings);
                player.getInventory().setBoots(boots);
                player.getInventory().addItem(sword);
                player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP, 32));
    
               
                player.sendMessage(ChatColor.RED + "You have chosen the PvP Kit!");
                return true;
            }
            }
            return true;
        }
        public void clearInventory(PlayerInventory inv){
            inv.clear();
            inv.setHelmet(null);
            inv.setChestplate(null);
            inv.setLeggings(null);
            inv.setBoots(null);
        }
       
    }
    
    Code:
    package me.liam.commands;
    
    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.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class ArcherCommand implements CommandExecutor {
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(!(sender instanceof Player)){
                return false;
            }
            Player player = (Player)sender;
            if (cmd.getLabel().equalsIgnoreCase("kit")) {
            if(args[0].equalsIgnoreCase("archer")) {
                clearInventory(player.getInventory());
                ItemStack sword = new ItemStack(Material.IRON_SWORD, 1);
                sword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
               
                ItemStack bow = new ItemStack(Material.BOW, 1);
                bow.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                bow.addEnchantment(Enchantment.ARROW_DAMAGE, 2);
                bow.addEnchantment(Enchantment.DURABILITY, 3);   
                ItemStack helmet = new ItemStack(Material.IRON_HELMET, 1);
                helmet.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack chestplate = new ItemStack(Material.GOLD_CHESTPLATE, 1);
                chestplate.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS, 1);
                leggings.addEnchantment(Enchantment.DURABILITY, 3);
                ItemStack boots = new ItemStack(Material.IRON_BOOTS, 1);
                boots.addEnchantment(Enchantment.DURABILITY, 3);
               
                player.getInventory().setHelmet(helmet);
                player.getInventory().setChestplate(chestplate);
                player.getInventory().setLeggings(leggings);
                player.getInventory().setBoots(boots);
                player.getInventory().addItem(sword);
                player.getInventory().addItem(bow);
                player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP, 32));
                player.getInventory().addItem(new ItemStack(Material.ARROW, 1));
    
               
                player.sendMessage(ChatColor.RED + "You have chosen the Archer Kit!");
                return true;
            }
            }
            return true;
        }
        public void clearInventory(PlayerInventory inv){
            inv.clear();
            inv.setHelmet(null);
            inv.setChestplate(null);
            inv.setLeggings(null);
            inv.setBoots(null);
        }
       
    }
    
    
    
    Code:
    name: KitPvP
    main: me.liam.main.Kitpvp
    version: 1.0
    author: Treeline1
    
    commands:
        kit:
        soup:
        spawn:
        archer:
        scoreboard:
     
  13. Offline

    Experminator

    I'm pretty sure it is better for you to use this:
    onCommand method (open)

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if(!(sender instanceof Player)){
            sender.sendMessage(ChatColor.RED + "Only players can use kits.");
            return true;
        }
       
        Player p = (Player) sender;
       
        if(cmd.getName().equalsIgnoreCase("kit")){
            if(args.length == 0){
                p.sendMessage(ChatColor.RED + "Please specify a kit!");
                return true;
            }
           
            if(args.length > 0){
                if(args[0].equalsIgnoreCase("archer")){
                    if(p.hasPermission("kit.archer")){
                        p.getInventory().addItem(new ArcherKit().getKit());
                        p.sendMessage(ChatColor.GREEN + "You have now the archer kit!");
                        return true;
                    } else {
                        p.sendMessage(ChatColor.RED + "You cannot get this kit.");
                        return true;  
                    }
                }
               
                // Repeat the code above much times you want.
            }
        }
       
        return false;
    }
    


    ArcherKit class (open)

    Code:
    public class ArcherKit {
       
        public ArcherKit(){
            getBow();  
        }
       
        public ItemStack getBow(){
            ItemStack bow = new ItemStack(Material.BOW, 1);
            ItemMeta bowMeta = bow.getItemMeta();
           
            bow.setDurability(0); // Note: This will set the durability to infinite.
           
            bowMeta.setDisplayName("Archer's Bow");
            bowMeta.setLore(Arrays.asList("The bow from a archer!"));
           
            bow.setItemMeta(bowMeta);
           
            return bow;
        }
    }


    This is a example for the archer kit.
    For the other kits it is the same way.

    @1Rogue Roque, is my previous code correct and useful or is it shit?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 27, 2017
  14. Offline

    1Rogue

    This doesn't "register" a command, it will just check if the command being executed matches that name.

    Also:
    Code:
                // Repeat the code above much times you want.
    Pretty much goes against the concept of keeping clean code. See: Code smell

    The op's current solution is actually better than reverting to putting everything into a functional design pattern.

    @Treeline1

    You want to look into the Command design pattern: http://en.wikipedia.org/wiki/Command_pattern
     
  15. Offline

    Treeline1

    @1Rogue
    would it make it easier if I was to just have 1 command? so instead of /kit archer... just /archer?
    If so how would go among doing that?
     
  16. Offline

    Experminator

    @Treeline1 Then you don't need to have arguments then can the command be 'archer' instead 'kit'.

    Like this:
    Code:
    if(cmd.getName().equalsIgnoreCase(/*Here the name of the command in a String.*/)){
        // Do stuff with the command.
    }
     
  17. Offline

    1Rogue

    Correct! But you can still have a command design pattern around that and create what I call "subcommands" (as used in my lib: https://github.com/CodeLanx/CodelanxLib/tree/master/src/main/java/com/codelanx/codelanxlib/command )

    It's a functional design pattern, which while a valid pattern, does not make much sense in an OOP language like java.
     
  18. Offline

    Treeline1

    Excellent! That worked. Ill stick with that :) Thanks
     
  19. Offline

    Experminator

    Last edited by a moderator: Jan 10, 2015
Thread Status:
Not open for further replies.

Share This Page