How to check displayname of a Material

Discussion in 'Plugin Development' started by MrMobiLp, Mar 9, 2017.

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

    MrMobiLp

    Hi Guys,
    i need help in my plugin, i made there a new recipe and i need to know how i can check if the name of the matrial equals "Mobuim" i marked the spot with //<----------

    Code:
    package de.mobi.mobiex.recipes;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Recipes {
       
        public static void loadRecipes() {
           
            ItemStack Obsidian = new ItemStack(Material.OBSIDIAN);
            ItemMeta ObsidianMeta = Obsidian.getItemMeta();
            ObsidianMeta.setDisplayName("§n§5Obsidian");
            Obsidian.setItemMeta(ObsidianMeta);
            ShapedRecipe ObsidianRecipe = new ShapedRecipe(Obsidian);
            ObsidianRecipe.shape("AEA", "LAW", "AEA");
            ObsidianRecipe.setIngredient('E', Material.IRON_INGOT);
            ObsidianRecipe.setIngredient('L', Material.LAVA_BUCKET);
            ObsidianRecipe.setIngredient('W', Material.WATER_BUCKET);
           
            Bukkit.addRecipe(ObsidianRecipe);
           
           
        //-----------------------------------------------------------------//
           
           
            ItemStack Mobium = new ItemStack(Material.PRISMARINE_CRYSTALS);
            ItemMeta MobiumMeta = Mobium.getItemMeta();
            MobiumMeta.setDisplayName("§n§5Mobium");
            Mobium.setItemMeta(MobiumMeta);
            ShapedRecipe MobiumRecipe = new ShapedRecipe(Mobium);
            MobiumRecipe.shape("SUS", "GDC", "SRS");
            MobiumRecipe.setIngredient('C', Material.SULPHUR);
            MobiumRecipe.setIngredient('S', Material.SLIME_BALL);
            MobiumRecipe.setIngredient('D', Material.DIAMOND);
            MobiumRecipe.setIngredient('R', Material.REDSTONE);
            MobiumRecipe.setIngredient('G', Material.GLOWSTONE_DUST);
            MobiumRecipe.setIngredient('U', Material.SUGAR);
    
               
            Bukkit.addRecipe(MobiumRecipe);
           
           
        //-----------------------------------------------------------------//
           
           
            ItemStack Netherstar = new ItemStack(Material.NETHER_STAR);
            ItemMeta NetherstarMeta = Netherstar.getItemMeta();
            NetherstarMeta.setDisplayName("§n§5Netherstern");
            Netherstar.setItemMeta(NetherstarMeta);
            ShapedRecipe NetherstarRecipe = new ShapedRecipe(Netherstar);
            NetherstarRecipe.shape("DPD", "PBP", "DPD");
            NetherstarRecipe.setIngredient('P', Material.PRISMARINE_CRYSTALS(/*HERE*/));          // <------------
            NetherstarRecipe.setIngredient('D', Material.DIAMOND);
            NetherstarRecipe.setIngredient('B', Material.DIAMOND_BLOCK);
           
            Bukkit.addRecipe(NetherstarRecipe);
           
           
        //-----------------------------------------------------------------//
               
           
        }
    
    
    
    }
     
  2. Offline

    Zombie_Striker

    @MrMobiLp
    You have to use PrepareItemCraftEvent. Get the ingredient, and if that ingredient has a name, and if that name is equal to the name you want, then continue. If not, set the result to air (or null)
     
  3. Offline

    MrMinecraft15

    You need the name of the item (not the material). You can get it using itemstack.getItemMeta().getDisplayName();
     
  4. Offline

    raunak114

    Use if(ItemStack.getItemMeta().getDisplayName().Equals(your string here){
    //Do something
    }
     
  5. Offline

    FrostedSnowman

    check if it has display name first before, getting it, or you'll get a NPE

    Code:
    private String example = ChatColor.RED + "hey";
    if (!itemStack.getItemMeta().hasDisplayname() || !itemStack.getItemMeta().getDisplayName().equals(example)) {
         return;
    }
    //item has displayname and its displayname is ChatColor.RED + hey
     
Thread Status:
Not open for further replies.

Share This Page