Solved Add ntb tags only to items from kit

AggerTV

New member
Sep 27, 2023
7
0
1
Hello! I am making a guard plugin for my server, and I need a way for when a guard obtains a kit, they are linked to the items from the kit, so that they cannot abuse by giving away their items. So, this is what I've come up with

Java:
@Override
    public void giveGuardKit(Player player, String kitName) {
        try {
            User user = essentials.getUser(player);

            Kit kit = new Kit(kitName, essentials);
            kit.expandItems(user);

            Inventory inventory = player.getInventory();

            for (ItemStack item : inventory.getContents()) {
                if (item == null) {
                    continue;
                }

                ItemMeta meta = item.getItemMeta();
                NamespacedKey guardKey = new NamespacedKey(plugin, "is_guard_item");
                PersistentDataContainer container = meta.getPersistentDataContainer();

                if (container.has(guardKey)) {
                    continue;
                }

                List<Component> lore = new ArrayList<>();
                lore.add(empty());

                SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                Date currentDate = new Date();
                String dateString = dateFormat.format(currentDate);

                lore.add(text().content("Tilhører: " + player.getName()).color(YELLOW).decoration(ITALIC, false).build());
                lore.add(text().content("Dato: " + dateString).color(YELLOW).decoration(ITALIC, false).build());
                meta.lore(lore);

                NamespacedKey ownerKey = new NamespacedKey(plugin, "owner_uuid");

                container.set(guardKey, PersistentDataType.BOOLEAN, true);
                container.set(ownerKey, PersistentDataType.STRING, player.getUniqueId().toString());

                item.setItemMeta(meta);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

Whenever a guard obtains their kit, I associate a ntb tag with the item, as well as adding some lore that displays the players name.
However, the big issue with this code, is that it will tag every item the guard might have in their inventory, and I obviously only want to tag items from the kit which they just recieved, so I would appriciate if anyone has a solution for this, as I just can't think of any.

Thanks :)
 

stefvanschie

Moderator
Staff member
Dec 17, 2021
102
3
16
18
I'd recommend iterating over the items in the kit, adding the appropriate data to those items and then giving them to the player.
 

AggerTV

New member
Sep 27, 2023
7
0
1
I'd recommend iterating over the items in the kit, adding the appropriate data to those items and then giving them to the player.
Yeah that makes sense, but I just can't figure out how to get the items from the kit.

The javadocs doesn't really say that much, all I could find was a getItems method which doesn't return any items, rarther a map containing strings with the items information (javadocs), see this example of what it looks like when I print it picture
and I can't really figure out how essentials does it internally either, but I think it has something to do with this class
 

electronicboy

Administrator
Staff member
Dec 11, 2021
227
10
38
28

here is some code I threw together at 6am a few years ago

for the most part, you can basically copy what Ess's code is doing, that code was written for a multi year old fork of Essentials, and might be missing some features, etc
 

AggerTV

New member
Sep 27, 2023
7
0
1

here is some code I threw together at 6am a few years ago

for the most part, you can basically copy what Ess's code is doing, that code was written for a multi year old fork of Essentials, and might be missing some features, etc
That did the trick, thank you :)