Solved ItemMeta not cooperating

Discussion in 'Plugin Development' started by maux, Jun 27, 2016.

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

    maux

    Hello.
    So I've been coding a gadget module and I've stumbled upon a problem.
    equiptPlayer() function (open)

    Code:
    public static void equiptPlayer(IPlayer player, Gadget gadget) {
            PlayerInventory inventory = player.a().getInventory();      
            ItemMeta property = gadget.getItemStack().getItemMeta();
          
            property.setDisplayName(gadget.getName());
          
            if (gadget.getDescription() != null) {
                property.setLore(gadget.getDescription());
            }
    
            if (gadget.isEnchanted() == true) {
                property.addEnchant(Enchantment.KNOCKBACK, 10, true);
            }
          
            // TODO getKit()
            inventory.setItem(gadget.getSlot(), gadget.getItemStack());
            player.a().updateInventory();
        }

    Gadget.class (open)

    Code:
    package com.mauxnetwork.maux.playmaux.game.gadget;
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.inventory.ItemStack;
    
    import com.mauxnetwork.maux.playmaux.game.kit.Kit;
    
    public class Gadget {
    
        private String name;
        private Kit kit;
        private ItemStack item;
        private int slot;
        private List<String> lore;
        private boolean enchanted;
      
        public Gadget(String name, Kit kit, ItemStack item, int slot) {
            this.name = name;
            this.kit = kit;
            this.item = item;
            this.slot = slot -1;
        }
      
        public String getName() {
            return this.name;
        }
      
        public Kit getKit() {
            return this.kit;
        }
      
        public ItemStack getItemStack() {
            return this.item;
        }
      
        public int getSlot() {
            return this.slot;
        }
    
        public List<String> getDescription() {
            return this.lore;
        }
      
        public void setDescription(String... text) {
            this.lore = Arrays.asList(text);
        }
      
        public void setEnchanted(boolean enchanted) {
            this.enchanted = enchanted;
        }
      
        public boolean isEnchanted() {
            return this.enchanted;
        }
      
    }
    

    GadgetTest.class (open)

    Code:
    package me.maux.GadgetTest;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    
    import com.mauxnetwork.maux.playmaux.game.gadget.Gadget;
    
    public class GadgetTest extends Gadget {
    
        public GadgetTest() {
            super("§a§lMushroom Soup", null, new ItemStack(Material.MUSHROOM_SOUP), 1);
        }
    
    }
    

    So I displayed the two classes. Here is the problem: When I set the item to the inventory, it doesn't show what I want from the GadgetTest.class. It just shows "Mushroom Soup" instead of "§a§lMushroom Soup." When I join the server, it just shows the default Mushroom Soup text. Does anyone know what the problem is?
     
  2. Offline

    SirGregg

    @maux
    I believe the proper way to name an item in Bukkit is this:

    Code:
    ItemStack apple = new ItemStack(Material.APPLE, 1);
    ItemMeta appleMeta = apple.getItemMeta();
    
    String yourName = "ChatColor.RED  + \"\" + ChatColor.BOLD + \"I like to eat\" + ChatColor.BLUE +\ " apples!\"\";
    
    meta.setDisplayName(yourName);
    apple.setItemMeta(meta)
    The important part:
    Instead of using the § character, use ChatColor.COLOR. If you are programming in eclipse, hold control and press space after you type "ChatColor." to see a list of chat colors.
     
  3. Offline

    Zombie_Striker

    @maux
    You forgot to re-set the item meta. You need to use setItemMeta(itemmeta) in order for the data to be changed.
     
  4. Offline

    maux

Thread Status:
Not open for further replies.

Share This Page