Util ItemUtils

Discussion in 'Resources' started by Orange Tabby, Nov 30, 2015.

?

Was this helpful?

  1. Yes!

    42.9%
  2. No.

    28.6%
  3. I like kitties :D

    71.4%
Multiple votes are allowed.
Thread Status:
Not open for further replies.
  1. Offline

    Orange Tabby

    Hello, I often find myself having to write a lot of code to check if an item has a name, to set an items name, to get a player skull and to get colored blocks(WOOL/STAINED_GLASS/STAINED_CLAY etc.). So to make my life more easy and maybe yours too, I made this ItemUtills, with methods to check if an item has a name, to set an items name, to get a player skull and to get a colored block.
    ItemUtils Class (open)

    Too use, copy this code into a class named ItemUtils(the class name doesn't really matter)
    Code:
    public class ItemUtils {
    
        public static boolean isNamedItem(ItemStack item, String name) {
            if (item.hasItemMeta() && item.getItemMeta().hasDisplayName()
                    && ChatColor.stripColor(item.getItemMeta().getDisplayName()).equals(ChatColor.stripColor(name))) {
                return true;
            }
            return false;
        }
    
        public static ItemStack setItemName(ItemStack item, String name) {
            ItemMeta im = item.getItemMeta();
            im.setDisplayName(name);
            item.setItemMeta(im);
            return item;
        }
    
        public static ItemStack getPlayerSkull(String owner, int amount) {
            ItemStack item = new ItemStack(Material.SKULL_ITEM, amount, (short) 3);
            SkullMeta itemMeta = (SkullMeta) item.getItemMeta();
            itemMeta.setOwner(owner);
            item.setItemMeta(itemMeta);
            return item;
        }
    
        public static ItemStack getColoredBlock(ColoredBlock block, BlockColor color, int amount) {
            ItemStack item = new ItemStack(block.getType(), amount, (byte) color.getColor());
            return item;
        }
    
        public enum ColoredBlock {
            WOOL(Material.WOOL), STAINED_GLASS(Material.STAINED_GLASS), STAINED_GLASS_PANE(
                    Material.STAINED_GLASS_PANE), STAINED_CLAY(Material.STAINED_CLAY);
    
            private Material coloredBlock;
    
            private ColoredBlock(Material coloredBlock) {
                this.coloredBlock = coloredBlock;
            }
    
            public Material getType() {
                return coloredBlock;
            }
    
        }
    
        public enum BlockColor {
    
            WHITE(0), ORANGE(1), MAGENTA(2), LIGHT_BLUE(3), YELLOW(4), LIME_GREEN(5), PINK(6), GRAY(7), LIGHT_GRAY(8), CYAN(
                    9), PURPLE(10), BLUE(11), BROWN(12), GREEN(13), RED(14), BLACK(15);
            private int color;
    
            private BlockColor(int color) {
                this.color = color;
            }
    
            public int getColor() {
                return color;
            }
    
        }

    Example (open)

    isNamedItem:
    Code:
    ItemUtils.isNamedItem(itemstack, "your item name") returns boolean
    setItemName:
    Code:
    ItemUtils.setItemName(itemstack, "your item name") retunrs Itemstack
    getPlayerSkull
    Code:
    ItemUtils.getPlayerSkull("player name", amount) returns Itemstack
    getColoredBlock:
    Code:
    ItemUtils.getColoredBlock(ColoredBlock.Block_Type, BlockColor.Your_Color, amount) returns Itemstack
     
Thread Status:
Not open for further replies.

Share This Page