Inventory help

Discussion in 'Spigot Plugin Development' started by Mhalllz, May 29, 2018.

  1. Hi guys its been so long since ive made a plugin and i forget a lot could anyone clean up my code and help me figure out why i cant set my items displayname. :)

    Code (Java):
    package me.MC_Hoxen.staff;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin implements Listener {

        private Inventory inv;

        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getServer().getLogger().info("Staff has been enabled!");
            Bukkit.getServer().getLogger().info("Version 1.0 By MC_Hoxen");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }

        public void onDisable() {
            Bukkit.getServer().getLogger().info("Mob Money has been disabled!");
        }

        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender theSender, Command cmd,
                String commandLabel, String[] args) {

            Player player = (Player) theSender;

            if (commandLabel.equalsIgnoreCase("Staff")) {

                ItemStack builder = new ItemStack(Material.getMaterial(getConfig()
                        .getInt("Builder-Item")), 1);
                Inventory inv = Bukkit.createInventory(null, getConfig().getInt("Chest-Size"), ChatColor
                        .translateAlternateColorCodes('&',
                                getConfig().getString("GUI-Title")));
                inv.addItem(builder);
                player.openInventory(inv);
               
                inv.setItem(getConfig().getInt("Builder-Slot"), builder);
               
                ItemMeta im = builder.getItemMeta();
               
                im.setDisplayName(ChatColor
                        .translateAlternateColorCodes('&',
                                getConfig().getString("Builder-Text")));
               
                inv.getItem(getConfig().getInt("Builder-Slot")).getItemMeta().setDisplayName(ChatColor
                        .translateAlternateColorCodes('&',
                                getConfig().getString("Builder-Text")));
               
            }

            return true;

        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {

            if (!e.getInventory().equals(inv)){
               
                return;
               
            }

            if(e.getSlot() == getConfig().getInt("Builder-Slot")){
               
                e.getWhoClicked().sendMessage("Why would you like to be builder?");
               
                return;
            }
           
        }

    }
     
     
  2. Don't try to set the display name after using the getItemMeta method
    Try builder.setItemMeta(im);
    That should work

    This was sent from mobile
     
  3. You need to set the items meta to the itemstack.. Add builder.SetItemMeta()....
     
  4. Also you dont need this

    inv.getItem(getConfig().getInt("Builder-Slot")).getItemMeta().setDisplayName(ChatColor
    .translateAlternateColorCodes('&',
    getConfig().getString("Builder-Text")));

    you set it here

    im.setDisplayName(ChatColor
    .translateAlternateColorCodes('&',
    getConfig().getString("Builder-Text")));

    just forgot to add the item meta to the item.
     
  5. For starters instead of using the commandLabel to check the command you should really check for the cmd.getName().

    Seconly, let me explain what you do set by step:
    Code (Java):

    if (commandLabel.equalsIgnoreCase("Staff")) { // self explanatory

                       // creating an item stack by getting it from config.
                ItemStack builder = new ItemStack(Material.getMaterial(getConfig()
                        .getInt("Builder-Item")), 1);

              // Creating an inventory by getting it from config.
                Inventory inv = Bukkit.createInventory(null, getConfig().getInt("Chest-Size"), ChatColor
                        .translateAlternateColorCodes('&',
                                getConfig().getString("GUI-Title")));
                inv.addItem(builder); // adding the item you previously created in the inventory
                player.openInventory(inv); // opening the inv to the player.
             
                inv.setItem(getConfig().getInt("Builder-Slot"), builder); // setting the item you previously created in a specific slot.
             
                ItemMeta im = builder.getItemMeta(); // getting it's itemmeta
             
                im.setDisplayName(ChatColor
                        .translateAlternateColorCodes('&',
                                getConfig().getString("Builder-Text"))); // setting it's displayname
             
                inv.getItem(getConfig().getInt("Builder-Slot")).getItemMeta().setDisplayName(ChatColor
                        .translateAlternateColorCodes('&',
                                getConfig().getString("Builder-Text"))); // setting once again ithe item's displayname
             
            }
     
    Your issue? You never set the meta to the item. Therefore any changes you make never get applied. You should use the ItemStack.setItemMeta(ItemMeta) method.
     
  6. I couldent figure out where to put it could you please show me