Solved ItemMeta setDisplayName problem

Discussion in 'Plugin Development' started by OcelotcR, May 21, 2013.

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

    OcelotcR

    Hey there, I coded this for a custom inventory plugin, ive researched ItemMeta, and this is what I came upon, But the setting the display name does not seem to work and the item on hover over is still golden leggins. Am I doing something wrong here?

    Code:
                    Inventory inv = Bukkit.getServer().createInventory(p, 9, "Quick Teleport"); //Creates inventory , then names MAKE DIVISIBLE BY 9.
                    ItemStack is = new ItemStack(Material.GOLD_LEGGINGS,1);inv.setItem(0,is);
                    ItemMeta im = is.getItemMeta();
                    im.setDisplayName(ChatColor.YELLOW + "Golden Quest");
                    is.setItemMeta(im);
     
  2. Offline

    Pink__Slime

    Are you getting any errors at all? I use this for all my plugins that involve changing the name and it works fine so I don't understand the problem.
     
  3. Offline

    OcelotcR

    Nope, just the name doesnt display.
     
  4. Offline

    Pink__Slime

    Try removing the ,1 after your material. If you only have 1 of the stack you don't need the amount integer after it.

    If that doesn't change anything, is it possible to show your complete class?
     
  5. Offline

    OcelotcR

    unfortunately didnt work, so here is the whole class.

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class listen implements Listener{
        @EventHandler
        public void quickteleport(PlayerInteractEvent evt){
            Player p = evt.getPlayer();
            Location l = p.getLocation(); //Not needed but for practice
            if(evt.getAction() == Action.RIGHT_CLICK_BLOCK || evt.getAction() == Action.RIGHT_CLICK_AIR){ //Right clicking block or the air.
                if (p.getItemInHand().getType() == Material.COOKED_CHICKEN){ //Change Later
                    evt.setCancelled(true);
                    Inventory inv = Bukkit.getServer().createInventory(p, 9, "Quick Teleport"); //Creates inventory , then names MAKE DIVISIBLE BY 9.
                    ItemStack is = new ItemStack(Material.GOLD_LEGGINGS);inv.setItem(0,is);
                    ItemMeta im = is.getItemMeta();
                    im.setDisplayName(ChatColor.YELLOW + "Gold Quest");
                    is.setItemMeta(im);
                    p.openInventory(inv); //On open inventory
                    p.playSound(l, Sound.EXPLODE, 10, 5); //PLays explode...
                    p.sendMessage(ChatColor.RED + "rasdf"); //yh
                    p.addPotionEffect (new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, 5));
                    p.addPotionEffect (new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 5));
                }
            }
        }
     
  6. Offline

    Pink__Slime

    OcelotcR
    Have you registered your listener?
     
  7. Offline

    OcelotcR

    that is the listener class D;
     
  8. Offline

    Pink__Slime

    OcelotcR
    I can see :p Have you registered the event in your onEnable?
     
  9. Offline

    OcelotcR

    Code:
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class hub extends JavaPlugin{
        @Override
        public void onEnable(){
            getLogger().info("Hub has loaded!");
            getServer().getPluginManager().registerEvents(new listen(), this);
            getCommand("help").setExecutor(new commands());
        }
    yeesessssss
     
  10. Try using System.out.println() methods to debug your code and tell us what you come back with ;)
     
  11. Offline

    OcelotcR

    Surely that would not affect it ?
     
  12. Offline

    Pink__Slime

    A few questions:
    1. Does the inventory open with the gold leggings in the first slot?
    2. Does the explosion play?
    3. Do you get the message set?
    4. Do you get the 2 potion effects?

    I'm trying to work out if anything there does work. I know a way to fix this but it requires a little more work. When I get home from school, if you haven't yet fixed this, I'll post some code.
     
  13. OcelotcR
    I meant use Println statements to test how far your code executes then tell us how far the code executes until it stops working.
     
  14. Offline

    OcelotcR

    1. Yes
    2. Yes
    3. Yes
    4. Yes
    Just the name isnt loading :(

    Ok, thanks well its 3:20am in the morning here and its frying my brain so I may head off to bed anyway

    everything runs, its just that doesnt change. Like the sounds and everything like that play.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  15. Offline

    1SmallVille1

    OcelotcR
    ok so what I think you did wrong is you added the item before you changed it's name. You need to set the itemmeta before you add it to your inventory. Like this:

    Code:Java
    1. ItemStack is = new ItemStack(Material.GOLD_LEGGINGS);
    2. ItemMeta im - is.getItemMeta();
    3. im.setDisplayName(ChatColor.YELLOW + "Gold Quest");
    4. is.setItemMeta(im);
    5. inv.addItem(is);
     
    NotSoCivil likes this.
  16. Offline

    OcelotcR

    oh of course! Thank you very much! it works like a charm
     
  17. Offline

    Pink__Slime

    This is my method of doing it. A little neater. Not at home yet so I'm not sure if this is exactly right.

    Main class:
    Code:java
    1. public static InventoryHolder player;
    2. public static Inventory inv = Bukkit.getServer().createInventory(player, 9, "Quick Teleport");
    3.  
    4. public static createMenu(){
    5.  
    6. ItemStack it = new ItemStack(Material.GOLD_LEGGINGS);
    7. ItemMeta itMeta = it.getItemMeta();
    8. itMeta.setDisplayName(ChatColor.YELLOW + "Gold Quest");
    9. it.setItemMeta();
    10.  
    11. inv.setItem(0, it);
    12. }
    13.  
    14. public static openMenu(Player player){
    15. player.openInventory(inv);
    16. }
    17.  
    18. public void onEnable(){
    19. getServer().getPluginManager().registerEvents(new listen(), this);
    20. createMenu();
    21. }


    Listener:
    Code:java
    1. public class listen implements Listener{
    2. @EventHandler
    3. public void quickteleport(PlayerInteractEvent evt){
    4. Player p = evt.getPlayer();
    5. Location l = p.getLocation(); //Not needed but for practice
    6. if(evt.getAction() == Action.RIGHT_CLICK_BLOCK || evt.getAction() == Action.RIGHT_CLICK_AIR){ //Right clicking block or the air.
    7. if (p.getItemInHand().getType() == Material.COOKED_CHICKEN){ //Change Later
    8. evt.setCancelled(true);
    9. openMenu(p);
    10. p.playSound(l, Sound.EXPLODE, 10, 5); //PLays explode...
    11. p.sendMessage(ChatColor.RED + "rasdf"); //yh
    12. p.addPotionEffect (new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, 5));
    13. p.addPotionEffect (new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 5));
    14. }
    15. }
    16. }
    17. }
     
  18. Offline

    1SmallVille1

    OcelotcR likes this.
  19. Offline

    NotSoCivil

    I have been looking for a solution to this for a long time now i got it, Thanks appreciate it :)
     
Thread Status:
Not open for further replies.

Share This Page