Solved Crafting custom items with custom lore and name.

Discussion in 'Plugin Development' started by Turkeybag, Jun 15, 2013.

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

    Turkeybag

    I've tried time and time again to get this to work but I can't seem to do it! I'm not really sure how I'm meant to do it but this is what I have so far.
    Code:
    package com.hotmail.aturkeybag.PlayerDetonator;
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class PlayerDetonator extends JavaPlugin implements Listener {
        private ItemStack supertnt = new ItemStack(Material.TNT, 1);
        public void onEnable(){
            ShapedRecipe strecipe = new ShapedRecipe(supertnt);
            strecipe.shape("GGG","GTG","GGG");
            strecipe.setIngredient('G', Material.SULPHUR);
            strecipe.setIngredient('T', Material.TNT);
            getServer().addRecipe(strecipe);
        }
        @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void craft(PrepareItemCraftEvent strecipe){
     
            supertnt.addEnchantment(Enchantment.ARROW_KNOCKBACK, 1);
            ItemMeta item = supertnt.getItemMeta();
            item.setDisplayName(ChatColor.ITALIC + "" + ChatColor.BOLD + ChatColor.GREEN + "Super TnT");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.ITALIC + "" + ChatColor.BOLD + ChatColor.YELLOW + "More bang for your buck!");
            item.setLore(lore);
            supertnt.setItemMeta(item);
            strecipe.getInventory().setResult(supertnt);
          }
    }
    Anyone able to help?
     
  2. Offline

    CubieX

    The only mistake I can see is, you did not register your listeners.
    So your event handler never gets called.

    Add this to your onEnable():
    Code:
    getServer().getPluginManager().registerEvents(this, this);
     
  3. Offline

    sheigutn

    Yes, that's the problem. You don't need the event if you set the itemMeta of the supertnt in the onenable() like
    Code:
    supertnt.addEnchantment(Enchantment.ARROW_KNOCKBACK, 1);
            ItemMeta item = supertnt.getItemMeta();
            item.setDisplayName(ChatColor.ITALIC + "" + ChatColor.BOLD + ChatColor.GREEN + "Super TnT");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.ITALIC + "" + ChatColor.BOLD + ChatColor.YELLOW + "More bang for your buck!");
            item.setLore(lore);
            supertnt.setItemMeta(item);
            ShapedRecipe strecipe = new ShapedRecipe(supertnt);
            strecipe.shape("GGG","GTG","GGG");
            strecipe.setIngredient('G', Material.SULPHUR);
            strecipe.setIngredient('T', Material.TNT);
            getServer().addRecipe(strecipe);
     
  4. When creating a shaped reciepe, you pass the changed item stack... There is no need to listen to an event for that, just create a modified ItemStack pass it as the argument when creating the ShapedRecipe(custumItem) and it will automatically appear...
     
  5. Offline

    Turkeybag

    Oh cubieX I had that but I accidentally cut it out when trying to remove all unnecessary code. I tried what you said sheigutn but it didn't work. I've tried about 10 different ways of doing this but none seem to work. Is there something wrong with the server I'm testing it on?
    Here's my full code now:
    Code:
    package com.hotmail.aturkeybag.PlayerDetonator;
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class PlayerDetonator extends JavaPlugin implements Listener {
        private ItemStack supertnt = new ItemStack(Material.TNT, 1);
        public void onEnable(){
            getLogger().info("Player Detonator has been enabled!");
            getServer().getPluginManager().registerEvents(this, this);
            getCommand("pd").setExecutor(new PlayerDetonatorCommandExecutor(this));
     
            supertnt.addEnchantment(Enchantment.ARROW_KNOCKBACK, 1);
            ItemMeta item = supertnt.getItemMeta();
            item.setDisplayName(ChatColor.ITALIC + "" + ChatColor.BOLD + ChatColor.GREEN + "Super TnT");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.ITALIC + "" + ChatColor.BOLD + ChatColor.YELLOW + "More bang for your buck!");
            item.setLore(lore);
            supertnt.setItemMeta(item);
            ShapedRecipe strecipe = new ShapedRecipe(supertnt);
            strecipe.shape("GGG","GTG","GGG");
            strecipe.setIngredient('G', Material.SULPHUR);
            strecipe.setIngredient('T', Material.TNT);
            getServer().addRecipe(strecipe);
        }
     
          @EventHandler
          public void onPlayerQuit(PlayerQuitEvent event) {
            Player player = event.getPlayer();
            PlayerDetonatorCommandExecutor.safe.remove(player.getName());
          }
     
        public void onDisable(){
            for (Player player : getServer().getOnlinePlayers()) {
                  if (PlayerDetonatorCommandExecutor.safe.contains(player.getName())) {
                    player.sendMessage("You are no longer immune to chaining effects!");
                    player.sendMessage("Player Detonator is reloading.");
                  }
            }getLogger().info("Player Detonator has been disabled!");
        }
    }
    EDIT: I fixed it. I simply removed the enchantment thing and it worked! However that brings the question... How do I get an enchantment on it? I tried changing the output to a bow and it worked so is there a bypass or something that I need to use to get punch on tnt?
     
  6. Offline

    sheigutn

    Do you get an exception? If yes can you please post the stacktrace?
     
  7. Offline

    Turkeybag

    I fixed it! I had to use addUnsafeEnchantment instead of addEnchantment as punch isn't meant to go on tnt (I'm just using it to give the tnt that glow effect). Thanks for your help everyone!
     
Thread Status:
Not open for further replies.

Share This Page