How can I change an Item's lore in an inventory if a player is in a specific arraylist?

Discussion in 'Plugin Development' started by xpaintall, Apr 7, 2021.

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

    xpaintall

    Ok, another question this time. I've been trying to find an answer to this question for like 2 hours and I cannot find it and I know I am stupid and the answer is simple. Explaination: I am making an inventory that has items for the player playing to unlock. Now the unlocking system is easy peasy for me, but I can't figure out how to change the lore of the item. For example if the player got the item it would put them in an arraylist and what should happen is the item's lore should change in the inventory "UNLOCKED", but if the player hasn't unlocked the item yet, the lore would say "LOCKED". Is there a way I can get around this? I tried to do it with InventoryOpenEvent and check for the item if it has been unlocked by the player, but it is not working. How do I change the lore of an item if a player is in a certain arraylist? Here is my code that should change the lore:
    Code:
     GadgetsGUI gui = new GadgetsGUI();
    
        Gadgets g = new Gadgets();
    
    
        @EventHandler
        public void onSomething(InventoryOpenEvent event) {
            Player player = (Player) event.getPlayer();
    
            if(event.getInventory() == null) { return; }
            if(event.getInventory().getHolder() instanceof GadgetsGUI) {
                if(g.superspeed.contains(player.getUniqueId())) {
                    ItemStack superspeed = new ItemStack(Material.POTION, 1, (short) 16386);
                    ItemMeta speedmeta = superspeed.getItemMeta();
                    speedmeta.setDisplayName("§bSuper speed Gadget§7 | COMMON");
                    speedmeta.setLore(Collections.singletonList("§aUNLOCKED"));
                    gui.cosGadgetGUI.setItem(9, superspeed);
                } else {
                    ItemStack superspeed = new ItemStack(Material.POTION, 1, (short) 16386);
                    ItemMeta speedmeta = superspeed.getItemMeta();
                    speedmeta.setDisplayName("§bSuper speed Gadget§7 | COMMON");
                    speedmeta.setLore(Collections.singletonList("§cLOCKED"));
                    gui.cosGadgetGUI.setItem(9, superspeed);
                }
    }
    (The gadgets class is where I keep my ArrayLists)
     
  2. Offline

    Newdel

    You forgot Itemstack#setItemMeta(ItemMeta) before you add the item to the inventory
     
  3. Offline

    xpaintall

    Still doesn't work for some reason even tho I added the superspeed#setItemMeta(speedmeta);. Idk why.
     
    Last edited: Apr 7, 2021
  4. Offline

    KarimAKL

    @xpaintall The trick is creating an inventory for every player and then adjusting the items to each player.
     
  5. Offline

    Strahan

    ^ what he said. Generate the GUI dynamically. Also just wanted to mention you should be using abstraction to prevent duplication of code and effort. You also do not need brackets when it's just a singular command, but that's up to personal preference. (I find it messy IMO). Example of what I mean about abstraction:

    Code:
    @EventHandler
    public void onSomething(InventoryOpenEvent event) {
      if (event.getInventory() == null) return;
      if (!(event.getInventory().getHolder() instanceof GadgetsGUI)) return;
    
      Player player = (Player)event.getPlayer();
      gui.cosGadgetGUI.setItem(9, getItemGUI("Super Speed Gadget", "common", g.superspeed.contains(player.getUniqueId()));
    }
    
    public ItemStack getItemGUI(String itemName, String rarity, bool unlocked) {
      ItemStack i = new ItemStack(Material.POTION, 1, (short)16386);
      ItemMeta im = i.getItemMeta();
      im.setDisplayName(ChatColor.BLUE + itemName + ChatColor.GREY + " | " + rarity.toUpperCase());
      im.setLore(Collections.singletonList(unlocked?ChatColor.GREEN + "UNLOCKED":ChatColor.RED + "LOCKED"));
      i.setItemMeta(im);
      return i;
    }
    I didn't put this in my IDE, so there may be mistakes and I may have misremembered the colors but it will give you the general idea. Speaking of colors, it's best to use the ChatColor enum not embed the color character BTW. Either direct reference as in the example or use of ChatColor#translateAlternateColorCodes.
     
    Newdel, xpaintall and KarimAKL like this.
  6. Offline

    xpaintall

    @Strahan I used your code example, but still nothing. Is there a way I can do this with HashMaps or?
    Code:
    public class CosmeticBoxItemGUI_Listener implements Listener{
    
        public Inventory gadgets;
        InventoryHolder h;
        Gadgets g = new Gadgets();
    
    
        @EventHandler
        public void onClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            if(event.getClickedInventory() == null) { return; }
            if(event.getClickedInventory().getHolder() instanceof CosmeticBoxItemGUI) {
                event.setCancelled(true);
                if(event.getCurrentItem() == null) { return; }
                if(event.getSlot() == 10) {
                    gadgets = Bukkit.createInventory(player, 27, ChatColor.YELLOW + "Gadget menu:");
                    ItemStack panes = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 4);
                    for(int i = 0; i < 9; i++) {
                        gadgets.setItem(i, panes);
                    }
                    for(int i = 18; i < 27; i++) {
                        gadgets.setItem(i, panes);
                    }
    
                    ItemStack superspeed = generateItem(ChatColor.AQUA + "Super speed Gadget", Material.CHAINMAIL_BOOTS, "common", ChatColor.GRAY, g.superspeed.contains(player.getUniqueId()), player);
                    gadgets.setItem(9, superspeed);
                    ItemStack superjump = generateItem(ChatColor.AQUA + "Super jump Gadget", Material.GOLD_BOOTS, "common", ChatColor.GRAY, g.superjump.contains(player.getUniqueId()), player);
                    gadgets.setItem(10, superjump);
                    ItemStack boom = generateItem(ChatColor.AQUA + "BOOM Gadget", Material.FLINT_AND_STEEL, "common", ChatColor.GRAY, g.boom.contains(player.getUniqueId()), player);
                    gadgets.setItem(11, boom);
                    ItemStack firework = generateItem(ChatColor.AQUA + "Firework launcher Gadget", Material.FIREWORK, "rare", ChatColor.BLUE, g.firework.contains(player.getUniqueId()), player);
                    gadgets.setItem(12, firework);
                    ItemStack disco = generateItem(ChatColor.AQUA + "Disco Gadget", Material.JUKEBOX, "rare", ChatColor.BLUE, g.disco.contains(player.getUniqueId()), player);
                    gadgets.setItem(13, disco);
    
                    player.openInventory(gadgets);
    
                }
            }
    
    
        }
    
    
        public ItemStack generateItem(String name, Material material, String rarity, ChatColor raritycolor, boolean unlocked, Player player) {
            ItemStack item = new ItemStack(material, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.AQUA + name + raritycolor + " | " + rarity.toUpperCase());
            meta.setLore(Collections.singletonList(unlocked?ChatColor.GREEN + "UNLOCKED":ChatColor.RED + "LOCKED"));
            item.setItemMeta(meta);
            return item;
        }
    
    
    
    }
     
  7. Offline

    KarimAKL

    @xpaintall You should not create the inventory when the InventoryClickEvent is called, but instead when you want to open it. Use the InventoryClickEvent to determine what should happen when the player clicks an item in your inventory.
     
    Strahan likes this.
  8. Offline

    xpaintall

    @KarimAKL So basically I should create the inventory when I want to open it?

    Edit: Ok, so I've tried a couple of things, but it seems like none of that works, I just get and empty GUI.

    Code:
    public class GadgetGUIAndListener implements Listener {
    
        public Inventory gad = Bukkit.createInventory(null, 27, ChatColor.YELLOW + "Gadgets menu");
        Gadgets g = new Gadgets();
    
    
        @EventHandler
        public void onOpenInvenotry(InventoryOpenEvent event) {
            if(event.getInventory() == null) return;
            if(event.getInventory().getName().matches(ChatColor.YELLOW + "Gadgets menu")) {
                Player player = (Player) event.getPlayer();
                registerInventory(player);
                player.sendMessage("working");
    
            }
        }
    
        public ItemStack generateItem(String name, Material material, String rarity, ChatColor raritycolor, boolean unlocked, Player player) {
            ItemStack item = new ItemStack(material, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.AQUA + name + raritycolor + " | " + rarity.toUpperCase());
            meta.setLore(Collections.singletonList(unlocked?ChatColor.GREEN + "UNLOCKED":ChatColor.RED + "LOCKED"));
            item.setItemMeta(meta);
            return item;
        }
    
        public void registerInventory(Player player) {
            ItemStack panes = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 4);
            for(int i = 0; i < 9; i++) {
                gad.setItem(i, panes);
            }
            for(int i = 18; i < 27; i++) {
                gad.setItem(i, panes);
            }
    
            ItemStack superspeed = generateItem(ChatColor.AQUA + "Super speed Gadget", Material.CHAINMAIL_BOOTS, "common", ChatColor.GRAY, g.superspeed.contains(player.getUniqueId()), player);
            gad.setItem(9, superspeed);
            ItemStack superjump = generateItem(ChatColor.AQUA + "Super jump Gadget", Material.GOLD_BOOTS, "common", ChatColor.GRAY, g.superjump.contains(player.getUniqueId()), player);
            gad.setItem(10, superjump);
            ItemStack boom = generateItem(ChatColor.AQUA + "BOOM Gadget", Material.FLINT_AND_STEEL, "common", ChatColor.GRAY, g.boom.contains(player.getUniqueId()), player);
            gad.setItem(11, boom);
            ItemStack firework = generateItem(ChatColor.AQUA + "Firework launcher Gadget", Material.FIREWORK, "rare", ChatColor.BLUE, g.firework.contains(player.getUniqueId()), player);
            gad.setItem(12, firework);
            ItemStack disco = generateItem(ChatColor.AQUA + "Disco Gadget", Material.JUKEBOX, "rare", ChatColor.BLUE, g.disco.contains(player.getUniqueId()), player);
            gad.setItem(13, disco);
        }
    
    
    }
    
    I know there is a huge mistake with the code somewhere, but I don't know where.
     
    Last edited: Apr 9, 2021
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page