Coloring Leather Armor with Command

Discussion in 'Plugin Development' started by Ravenclaw572, Dec 23, 2012.

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

    Ravenclaw572

    Hello! I've been having trouble trying to get my plugin to work. It's supposed to give you a set of leather armor with the color you enter. No errors in the console, it just displays "/colorarmor [color]" in game. Here's my code:

    Code:
    package com.colorarmor;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Color;
    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;
    import org.bukkit.inventory.meta.LeatherArmorMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class ColorArmor extends JavaPlugin implements CommandExecutor {
     
        public void onEnable(){
            getLogger().info("ColoredArmor has been enabled!");
        }
     
        public void onDisable() {
            getLogger().info("ColoredArmor has been disabled!");
        }
     
        public ItemStack setColor(ItemStack item, Color args) {
              LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
              meta.setColor(args);
              item.setItemMeta(meta);
              return item;
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, Color args){
            if(cmd.getName().equalsIgnoreCase("colorarmor")){
                Player s = (Player)sender;
                PlayerInventory inventory = s.getInventory();
                inventory.addItem(setColor(new ItemStack(Material.LEATHER_HELMET), args));
                inventory.addItem(setColor(new ItemStack(Material.LEATHER_CHESTPLATE), args));
                inventory.addItem(setColor(new ItemStack(Material.LEATHER_LEGGINGS), args));
                inventory.addItem(setColor(new ItemStack(Material.LEATHER_BOOTS), args));
                return true;
            }
            return false;
        }
    }
    Also, if possible, how can I make it dye a specific piece of armor already in your inventory? Thanks for any and all help. :)
     
  2. Offline

    fireblast709

    Code:java
    1. public ItemStack colorize(ItemStack i, int r, int g, int b)
    2. {
    3. String t = i.getType().name().split("_")[0];
    4. if(t.equals("LEATHER"))
    5. {
    6. LeatherArmorMeta lam = (LeatherArmorMeta)i.getItemMeta();
    7. lam.setColor(Color.fromRGB(r << 16 + g << 8 + b));
    8. i.setItemMeta(lam);
    9. }
    10. return i;
    11. }
     
  3. Offline

    Ravenclaw572

    How do I implement that into a command? Sorry, not sure about that part. I'm confused about this as the Javadoc didn't make sense about Metadata. My rank plugin was simpler than this one. :/
     
  4. Offline

    fireblast709

    Use the latest Bukkit to build it, and the latest RB of Craftbukkit to run it. For the rest you can just use it as it is. You simply use it as a method like:
    Code:java
    1. ItemStack i = however you get it
    2. i = colorize(i, red, green, blue);
    Where red, green and blue are a number from 0 - 255
     
  5. Offline

    Wolvereness Bukkit Team Member

    Color.fromRGB(r, g, b) is preferred over doing your own bit shifting.
     
  6. Offline

    fireblast709

    Mmh missed that one ;3
     
  7. Offline

    Ravenclaw572

    I'm still confused. I know what a method is, but how can I designate what r, g, and b are with a command?
     
  8. Offline

    fireblast709

    use the String[] args
     
  9. Offline

    Ravenclaw572

    I've got a ton of errors, and now nothing's working. No idea what happened. How could I reference the method in a command? I'm clearly missing something, and I have no idea what this "something" would be.

    I though the method was this:
    Code:
    public ItemStack colorize(ItemStack i, int r, int g, int b)
    {
    String t = i.getType().name().split("_")[0];
    if(t.equals("LEATHER"))
    {
    LeatherArmorMeta lam = (LeatherArmorMeta)i.getItemMeta();
    lam.setColor(Color.fromRGB(r << 16 + g << 8 + b));
    i.setItemMeta(lam);
    }
    return i;
    }
    How could I get that into a command? Like /colorarmor [Red] [Green] [Blue]?
     
  10. Offline

    fireblast709

    yes, the method and command are correct. You should not forget to pass the itemstacks though. If you have any stacktraces (the long error things in the console), you should post those
     
  11. Offline

    Ravenclaw572

    Just errors in Eclipse. Also, I don't have any command written, at the moment. This is my entire class, at the moment:

    Code:
    package com.colorarmor;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Color;
    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;
    import org.bukkit.inventory.meta.LeatherArmorMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class ColorArmor extends JavaPlugin implements CommandExecutor {
     
        public void onEnable(){
            getLogger().info("ColoredArmor has been enabled!");
        }
     
        public void onDisable() {
            getLogger().info("ColoredArmor has been disabled!");
        }
     
        public ItemStack colorize(ItemStack i, int r, int g, int b)
        {
            String t = i.getType().name().split("_")[0];
            if(t.equals("LEATHER"))
            {
                LeatherArmorMeta lam = (LeatherArmorMeta)i.getItemMeta();
                lam.setColor(Color.fromRGB(r << 16 + g << 8 + b));
                i.setItemMeta(lam);
            }
            return i;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, Color args){
            if(cmd.getName().equalsIgnoreCase("colorarmor")){
                Player s = (Player)sender;
                PlayerInventory inventory = s.getInventory();
                inventory.addItem([COLOR=#ff0000]colorize[/COLOR](new ItemStack(Material.LEATHER_HELMET), args));
                inventory.addItem([COLOR=#ff0000]colorize[/COLOR](new ItemStack(Material.LEATHER_CHESTPLATE), args));
                inventory.addItem([COLOR=#ff0000]colorize[/COLOR](new ItemStack(Material.LEATHER_LEGGINGS), args));
                inventory.addItem([COLOR=#ff0000]colorize[/COLOR](new ItemStack(Material.LEATHER_BOOTS), args));
                return true;
            }
            return false;
        }
    }
    I tried to highlight the errors in red. It says "Create method 'Colorize(ItemStack, Color)'" What should I do to fix that?
     
  12. Offline

    fireblast709

    You should parse the arguments to the integers. So:
    • check if the argument length suffices (so args.length > 2)
    • parse them to int using Integer.parseInt(String). Note: this could throw a NumberFormatException, which you should catch. This means the user tried something like "/colorarmor a b c" while it should be "/colorarmor <number> <number> <number>"
    • then pass those integers into the colorize
     
  13. Offline

    Ravenclaw572

    Now I'm completely confused. I quit. This makes no sense, the old Bukkit code was a lot simpler. :/ So much for PvP Teams...
     
  14. Offline

    fireblast709

    nah its not that hard. It is mostly Java :3
     
    leimekiller and KollegahDerBoss like this.
Thread Status:
Not open for further replies.

Share This Page