Solved Crafting items with custom names

Discussion in 'Plugin Development' started by blizzblang, Dec 18, 2012.

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

    blizzblang

    I've read several threads for 2 days now trying to find out a way to craft an item with a custom name and recipe ( for a set of armor ). Out of frustration and tiredness i decided to use the forum as a last resort.
    The point of this plugin is to
    add a new recipe of OP armor. check
    give it a custom name. no check
    with enchantments. check
    Here is all my code
    main class
    Code:
    package blizzblang;
     
    import java.util.logging.Logger;
     
    import net.minecraft.server.CraftingManager;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.Server;
    import org.bukkit.block.Block;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.enchantments.EnchantmentWrapper;
    import org.bukkit.entity.Item;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.Recipe;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class main extends JavaPlugin implements Listener {
       
        Logger log;
        Server server;
        PluginDescriptionFile pdf;
          FileConfiguration config;
          @Override
            public void onEnable(){
              addRecipe(); 
             
            }
       
            @Override
            public void onDisable() {
           
            }
           
       
            public void addRecipe() {
                int effectId = 0;  //use the enchantment code you want here
                int enchantmentLevel = 4;
                ItemStack nano_chest = new ItemStack(Material.DIAMOND_CHESTPLATE,1); 
                Namer.setName(nano_chest, "Nano armor");
               
                Enchantment myEnchantment = new EnchantmentWrapper(effectId);  //new enchantment of effect id
                nano_chest.addEnchantment(myEnchantment, enchantmentLevel);  //enchant the item
               
                ShapedRecipe recipe = new ShapedRecipe(nano_chest);
                recipe.shape("CBC", "BAB", "BBB");
                recipe.setIngredient('C', Material.DIAMOND_BLOCK);
                recipe.setIngredient('B', Material.OBSIDIAN);
                recipe.setIngredient('A', Material.BEACON);
                this.getServer().addRecipe(recipe);
            }
    }
    
    stirante's namer class
    Code:
    package blizzblang;
     
    import java.util.ArrayList;
     
    import net.minecraft.server.NBTTagCompound;
    import net.minecraft.server.NBTTagList;
    import net.minecraft.server.NBTTagString;
     
    import org.bukkit.ChatColor;
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.inventory.ItemStack;
     
    public class Namer {
       
        private static CraftItemStack                    craftStack;
        private static net.minecraft.server.ItemStack    itemStack;
       
        public static ItemStack setName(ItemStack item, String name) {
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                Namer.itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                Namer.itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("display", new NBTTagCompound());
                itemStack.tag = tag;
            }
           
            tag = itemStack.tag.getCompound("display");
            tag.setString("Name", ChatColor.RESET + name);
            itemStack.tag.setCompound("display", tag);
            return craftStack;
        }
       
        public static String getName(ItemStack item) {
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                Namer.itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                Namer.itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                return null;
            }
            tag = itemStack.tag.getCompound("display");
            return tag.getString("Name");
        }
       
        public ItemStack setLore(ItemStack item, String... lore) {
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                Namer.itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                Namer.itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("display", new NBTTagCompound());
                itemStack.tag = tag;
            }
            tag = itemStack.tag.getCompound("display");
            NBTTagList list = new NBTTagList();
            for (String l : lore) {
                list.add(new NBTTagString(l));
            }
            tag.set("Lore", list);
            itemStack.tag.setCompound("display", tag);
            return craftStack;
        }
       
        public static ItemStack addLore(ItemStack item, String lore) {
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                Namer.itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                Namer.itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("display", new NBTTagCompound());
                tag.getCompound("display").set("Lore", new NBTTagList());
                itemStack.tag = tag;
            }
           
            tag = itemStack.tag.getCompound("display");
            NBTTagList list = tag.getList("Lore");
            list.add(new NBTTagString(lore));
            tag.set("Lore", list);
            itemStack.tag.setCompound("display", tag);
            return craftStack;
        }
       
        public static String[] getLore(ItemStack item) {
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                Namer.itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                Namer.itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("display", new NBTTagCompound());
                tag.getCompound("display").set("Lore", new NBTTagList());
                itemStack.tag = tag;
            }
            tag = itemStack.tag;
            NBTTagList list = tag.getCompound("display").getList("Lore");
            ArrayList<String> strings = new ArrayList<String>();
            String[] lores = new String[] {};
            for (int i = 0; i < strings.size(); i++)
                strings.add(((NBTTagString) list.get(i)).data);
            strings.toArray(lores);
            return lores;
        }
    }
    I hope i can get some help.
    And btw when i add '' Namer.setName(nano_chest, "Nano armor");" it breaks the crafting recipe.
    I hope my simple misunderstanding or mistake can be fixed, and i appreciate all the help i recive.
     
  2. Offline

    SnowGears

    Use the latest dev build. Bukkit implemented naming item API.

    ItemStack notNamed = new ItemStack(Material.DIRT);
    ItemMeta im = notNamed.getItemMeta();
    etc...
     
    suckycomedian likes this.
  3. Offline

    SmokyMiner

    Good you please go into a bit more detail, I cant seem to get this to work, on another thread I found this snipet of code:
    Code:
    ItemStack is = new ItemStack(Material.GOLD_SWORD, 1);
    ItemMeta im = is.getItemMeta();
    im.setDisplayName("Demon Banisher");
    ArrayList<String> lore = new ArrayList<String>();
    lore.add("Banishes demons");
    lore.add("And cooks your pork");
    im.setLore(lore);
    is.setItemMeta(im);
    But When I try to use it I get a syntax error at im.setDisplayName("Demon Banisher");
    I have both the dev build for bukkit and bukkit craft.

    EDIT: Never Mind, I think I got it.
     
  4. Offline

    SnowGears

    Post the error?
     
  5. Offline

    SmokyMiner

    I was just being dumb, it works, I made a stupid mistake.
     
  6. Offline

    blizzblang

    I will try the latest dev build
    EDIT:
    Thanks so much it works now!
     
  7. Offline

    fireblast709

    lolz my snippet :3
     
    SmokyMiner likes this.
  8. Offline

    SoS_Dylan

    Can you please tell me what you did because I can't seem to get it to work.
     
  9. Offline

    unforgiven5232

    ItemMeta im = is.getItemMeta();
    You wrote that when its suppoed to be
    ItemMeta imMeta = is.getItemMeta();
     
Thread Status:
Not open for further replies.

Share This Page