Still searching for a...

Discussion in 'Spigot Plugin Development' started by NaJated, May 14, 2015.

  1. ...simple and easy method to make an itemstack and be able to call it. I tried this a while back and it didn't work.


    Code (Text):
    package me.TheGamesHawk2001.MineChill;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class PvPGUI implements CommandExecutor{

        Inventory inventory = Bukkit.createInventory(null, 27, "HI THERE");


        public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args) {
            if (cmd.getName().equalsIgnoreCase("pvp")) {
                if (args.length == 0) {
                    Player p = (Player) sender;
                    p.openInventory(inventory);
                    p.sendMessage(ChatColor.RED + "Opening GUI.");
                    return true;
                }
            }
            return false;
        }


        public ItemStack createItem(Material material, String dName, int slotInInventory, Inventory name) {
            ItemStack stack = new ItemStack(material);
            ItemMeta im = stack.getItemMeta();
            im.setDisplayName(ChatColor.translateAlternateColorCodes('&', dName));
            stack.setItemMeta(im);
            return stack;
        }
        public void items() {
            createItem(Material.DIAMOND, "&bITEM LLOOLL", 2, inventory);
        }
    }
     
  2. JamesJ

    Supporter

    What "didn't work"?
     
  3. Like @JamesJ has pointed out, what didn't work? Also, you're not even invoking the createItem method other than in the items method, and what purpose does that even serve? It doesn't even return anything, so how do you except to add it to the inventory?
     
  4. You have to register item stacks in a method (in this case it's the items method)

    updated code
    Code (Text):
    package me.TheGamesHawk2001.MineChill;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class PvPGUI implements Listener, CommandExecutor{

        Inventory inventory = Bukkit.createInventory(null, 27, "HI THERE");


        public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args) {
            if (cmd.getName().equalsIgnoreCase("pvp")) {
                if (args.length == 0) {
                    Player p = (Player) sender;
                    p.openInventory(inventory);
                    p.sendMessage(ChatColor.RED + "Opening GUI.");
                    return true;
                }
            }
            return false;
        }


        public ItemStack createItem(Material material, String dName, int slotInInventory, Inventory name) {
            ItemStack stack = new ItemStack(material);
            ItemMeta im = stack.getItemMeta();
            im.setDisplayName(ChatColor.translateAlternateColorCodes('&', dName));
            stack.setItemMeta(im);
            int slot = slotInInventory - 1;
            name.setItem(slot, stack);
            return stack;
        }
        public void items() {
            createItem(Material.DIAMOND, "&bITEM LLOOLL", 2, inventory);
        }
    }
     
  5. Filter out the things you don't need though, otherwise you might get some surprises.
    Note the Placeholders is another class of mine
    Code (Java):
    public class CustomItem extends ItemStack{
        public CustomItem(Material material, int amount, int durability, String name, List<String> lore, Enchantment e){
            super(new ItemStack(material, amount));
            ItemMeta im = getItemMeta();
            im.setDisplayName(Placeholders.parse(name));
            List<String> lines = new ArrayList();
            for(String line : lore){
                if(!line.equalsIgnoreCase("")) lines.add(Placeholders.parse(line));
            }
            im.setLore(lore);
            setItemMeta(im);
            setDurability((short)durability);
            addEnchantment(e, 1);
        }
     
        public CustomItem(Material material, int amount, int durability, String name, String[] lore, Enchantment e){
            super(new ItemStack(material, amount));
            ItemMeta im = getItemMeta();
            im.setDisplayName(Placeholders.parse(name));
            List<String> lines = new ArrayList();
            for(String line : lore){
                if(!line.equalsIgnoreCase("")) lines.add(Placeholders.parse(line));
            }
            im.setLore(lines);
            setItemMeta(im);
            setDurability((short)durability);
            addEnchantment(e, 1);
        }
     
        public CustomItem(Material material, int amount, int durability, String name, String lore, Enchantment e){
            super(new ItemStack(material, amount));
            ItemMeta im = getItemMeta();
            im.setDisplayName(Placeholders.parse(name));
            if(!lore.equalsIgnoreCase("")){
                List<String> lines = new ArrayList();
                for(String line : lore.split("\\|")){
                    lines.add(Placeholders.parse(line));
                }
                im.setLore(lines);
            }
            setItemMeta(im);
            setDurability((short)durability);
            addEnchantment(e, 1);
        }
     
        public CustomItem(Material material, int amount, int durability, String name, List<String> lore){
            super(new ItemStack(material, amount));
            ItemMeta im = getItemMeta();
            im.setDisplayName(Placeholders.parse(name));
            List<String> lines = new ArrayList();
            for(String line : lore){
                if(!line.equalsIgnoreCase("")) lines.add(Placeholders.parse(line));
            }
            im.setLore(lore);
            setItemMeta(im);
            setDurability((short)durability);
        }
     
        public CustomItem(Material material, int amount, int durability, String name, String[] lore){
            super(new ItemStack(material, amount));
            ItemMeta im = getItemMeta();
            im.setDisplayName(Placeholders.parse(name));
            List<String> lines = new ArrayList();
            for(String line : lore){
                if(!line.equalsIgnoreCase("")) lines.add(Placeholders.parse(line));
            }
            im.setLore(lines);
            setItemMeta(im);
            setDurability((short)durability);
        }
     
        public CustomItem(Material material, int amount, int durability, String name, String lore){
            super(new ItemStack(material, amount));
            ItemMeta im = getItemMeta();
            im.setDisplayName(Placeholders.parse(name));
            if(!lore.equalsIgnoreCase("")){
                List<String> lines = new ArrayList();
                for(String line : lore.split("\\|")){
                    lines.add(Placeholders.parse(line));
                }
                im.setLore(lines);
            }
            setItemMeta(im);
            setDurability((short)durability);
        }
     
        public CustomItem(Material material, int amount, int durability, String name){
            super(new ItemStack(material, amount));
            ItemMeta im = getItemMeta();
            im.setDisplayName(Placeholders.parse(name));
            setItemMeta(im);
            setDurability((short)durability);
        }
     
        public CustomItem(Material material, int amount, int durability){
            super(new ItemStack(material, amount));
            setDurability((short)durability);
        }
     
        public CustomItem(Material material, int amount) {
            super(new ItemStack(material, amount));
        }
     
        public CustomItem(ItemStack is) {
            super(is);
        }
     
        public void setName(String name){
            ItemMeta im = getItemMeta();
            im.setDisplayName(Placeholders.parse(name));
            setItemMeta(im);
        }
     
        public void setLore(List<String> lore){
            ItemMeta im = getItemMeta();
            List<String> lines = new ArrayList();
            for(String line : lore){
                if(!line.equalsIgnoreCase("")) lines.add(Placeholders.parse(line));
            }
            im.setLore(lore);
            setItemMeta(im);
        }
     
        public void setLore(String[] lore){
            ItemMeta im = getItemMeta();
            List<String> lines = new ArrayList();
            for(String line : lore){
                if(!line.equalsIgnoreCase("")) lines.add(Placeholders.parse(line));
            }
            im.setLore(lines);
            setItemMeta(im);
        }
     
        public void setLore(String lore){
            if(!lore.equalsIgnoreCase("")) setLore(lore.split("\\|"));
        }
    }
     
  6. This is what I use.

    Code (Text):
     private static ItemStack createItem(Material material, int amount, byte data, String name, ArrayList<String> loreList) {
            ItemStack item = new ItemStack(material, amount, data);
            ItemMeta itemMeta = item.getItemMeta();
            itemMeta.setDisplayName(name);
            if (loreList != null) {
                itemMeta.setLore(loreList);
            }
            item.setItemMeta(itemMeta);
            return item;
        }
     
  7. What's the difference between ours? Yours returns stack and so does mine.
     
  8. @IPv420 that. You don't even do anything with the ItemStacks.
     
  9. I updated the code with the adding the item to the inventory and it still doesn't work.
     
  10. Well....do you ever call the createItem method? I see you're calling it in the items method....is items ever called?
     
  11. Saying "doesn't work" helps no one.

    Post your current code, as well as explaining what doesn't work.
     
  12. Indeed, in the onEnable.
     
  13. Post your main class.
     
  14. Simple problem, you're not calling the items() method in the constructor.

    Here's the fix:
    Code (Text):
    package me.TheGamesHawk2001.MineChill;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class PvPGUI implements CommandExecutor {

        private static ItemStack createItem(Material material, String name) {
            return PvPGUI.createItem(material, 1, name);
        }

        private static ItemStack createItem(Material material, int amount, String name) {
            ItemStack stack = new ItemStack(material);
            ItemMeta meta = stack.getItemMeta();
            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
            stack.setItemMeta(meta);
            return stack;
        }
       
        private final Inventory inventory = Bukkit.createInventory(null, 27, "HI THERE");

        public PvPGUI() {
            inventory.setItem(2, createItem(Material.DIAMOND, "&bITEM LLOOLL"));
        }

        public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args) {
            if (cmd.getName().equalsIgnoreCase("pvp")) {
                if (args.length == 0) {
                    Player p = (Player) sender;
                    p.openInventory(inventory);
                    p.sendMessage(ChatColor.RED + "Opening GUI.");
                    return true;
                }
            }
            return false;
        }
    }
     
     
    • Agree Agree x 1
  15. Hey I did a tutorial two weeks ago. Just look at it. Think it is very simple and easy.
     
    #15 HardTimeStudio, May 15, 2015
    Last edited: May 15, 2015
  16. ???

    Just saying extending ItemStack class is not a good idea, for that purpose you should make all your constructors to public static functions.

    You should not reuse the inventory you've created cause all users will see the changes made by other users in that inventory. Move Inventory and ItemStack creation inside you command handler:
    Code (Text):

    package me.TheGamesHawk2001.MineChill;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class PvPGUI implements CommandExecutor {

        private static ItemStack createItem(Material material, String name) {
            return PvPGUI.createItem(material, 1, name);
        }

        private static ItemStack createItem(Material material, int amount, String name) {
            ItemStack stack = new ItemStack(material);
            ItemMeta meta = stack.getItemMeta();
            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
            stack.setItemMeta(meta);
            return stack;
        }

        public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args) {
            if (cmd.getName().equalsIgnoreCase("pvp")) {
                if (args.length == 0) {
                    Player p = (Player) sender;
                    Inventory inventory = Bukkit.createInventory(null, 27, "HI THERE");
                    inventory.setItem(2, createItem(Material.DIAMOND, "&bITEM LLOOLL"));
                    p.openInventory(inventory);
                    p.sendMessage(ChatColor.RED + "Opening GUI.");
                    return true;
                }
            }
            return false;
        }
    }
     
     
    #16 Janmm14, May 15, 2015
    Last edited by a moderator: May 15, 2015
  17. That depends really on what you're aiming after. Players might not even be allowed to edit this interface, and it just shows some things for players to click on. It would also be really inefficient to be creating a new inventory every single time you opened it. I just left it like how he has it originally, since I really don't know what he's trying to do.
     
    • Agree Agree x 1
  18. You... what?

    @everyone, I think it'd be best if you didn't just post entire classes with the "fix." Show the specific part of the file that needs to be changed.
     
    • Agree Agree x 1
  19. Can you please explain what you're trying to accomplish (in detail), what the error is, and post your main class, as @DarkSeraphim as requested? It is hard to help when you're not giving any details other than "it's not working."
     
  20. Oh, it's finished :p
    thx for input and especially
    :p