Search in sources :

Example 1 with CustomItemTagContainer

use of org.bukkit.inventory.meta.tags.CustomItemTagContainer in project ProtocolStringReplacer by Rothes.

the class PsrUser method saveUserMetaCache.

public void saveUserMetaCache(ItemStack originalItem, ItemStack replacedItem) {
    if (this.hasPermission("protocolstringreplacer.feature.usermetacache") && originalItem.hasItemMeta()) {
        ItemMeta originalMeta = originalItem.getItemMeta();
        ItemMeta replacedMeta = replacedItem.getItemMeta();
        if (!originalMeta.equals(replacedMeta)) {
            Short uniqueCacheKey = this.nextUniqueCacheKey();
            if (ProtocolStringReplacer.getInstance().getServerMajorVersion() >= 13) {
                CustomItemTagContainer tagContainer = replacedMeta.getCustomTagContainer();
                tagContainer.setCustomTag(getUserCacheKey(), ItemTagType.SHORT, uniqueCacheKey);
            } else {
                addCacheLegacy(replacedMeta, uniqueCacheKey);
            }
            replacedItem.setItemMeta(replacedMeta);
            this.getMetaCache().put(uniqueCacheKey, originalMeta);
        }
    }
}
Also used : ItemMeta(org.bukkit.inventory.meta.ItemMeta) CustomItemTagContainer(org.bukkit.inventory.meta.tags.CustomItemTagContainer)

Example 2 with CustomItemTagContainer

use of org.bukkit.inventory.meta.tags.CustomItemTagContainer in project ProtectionStones by espidev.

the class ProtectionStones method isProtectBlockItem.

/**
 * Check if an item is a valid protection block, and if checkNBT is true, check if it was created by
 * ProtectionStones. Be aware that blocks may have restrict-obtaining off, meaning that it is ignored whether or not
 * the item is created by ProtectionStones (in this case have checkNBT false).
 *
 * @param item     the item to check
 * @param checkNBT whether or not to check if the plugin signed off on the item (restrict-obtaining)
 * @return whether or not the item is a valid protection block item, and was created by protection stones
 */
public static boolean isProtectBlockItem(ItemStack item, boolean checkNBT) {
    if (item == null)
        return false;
    // check basic item
    if (!ProtectionStones.isProtectBlockType(BlockUtil.getProtectBlockType(item)))
        return false;
    // if not checking nbt, you only need to check type
    if (!checkNBT)
        return true;
    boolean tag = false;
    // otherwise, check if the item was created by protection stones (stored in custom tag)
    if (item.getItemMeta() != null) {
        CustomItemTagContainer tagContainer = item.getItemMeta().getCustomTagContainer();
        try {
            // check if tag byte is 1
            Byte isPSBlock = tagContainer.getCustomTag(new NamespacedKey(ProtectionStones.getInstance(), "isPSBlock"), ItemTagType.BYTE);
            tag = isPSBlock != null && isPSBlock == 1;
        } catch (IllegalArgumentException es) {
            try {
                // some nbt data may be using a string (legacy nbt from ps version 2.0.0 -> 2.0.6)
                String isPSBlock = tagContainer.getCustomTag(new NamespacedKey(ProtectionStones.getInstance(), "isPSBlock"), ItemTagType.STRING);
                tag = isPSBlock != null && isPSBlock.equals("true");
            } catch (IllegalArgumentException ignored) {
            }
        }
    }
    // whether or not the nbt tag was found
    return tag;
}
Also used : CustomItemTagContainer(org.bukkit.inventory.meta.tags.CustomItemTagContainer)

Example 3 with CustomItemTagContainer

use of org.bukkit.inventory.meta.tags.CustomItemTagContainer in project MechanicsMain by WeaponMechanics.

the class NBT_1_13_R2 method getBukkitCompound.

private CustomItemTagContainer getBukkitCompound(ItemMeta meta, String plugin) {
    if (plugin == null) {
        meta.getCustomTagContainer();
    }
    NamespacedKey key = new NamespacedKey(MechanicsCore.getPlugin(), plugin);
    CustomItemTagContainer nbt = meta.getCustomTagContainer().getCustomTag(key, ItemTagType.TAG_CONTAINER);
    if (nbt == null) {
        nbt = new CraftCustomItemTagContainer(new CraftCustomTagTypeRegistry());
        meta.getCustomTagContainer().setCustomTag(key, ItemTagType.TAG_CONTAINER, nbt);
    }
    return nbt;
}
Also used : CraftCustomItemTagContainer(org.bukkit.craftbukkit.v1_13_R2.inventory.tags.CraftCustomItemTagContainer) NamespacedKey(org.bukkit.NamespacedKey) CraftCustomTagTypeRegistry(org.bukkit.craftbukkit.v1_13_R2.inventory.CraftCustomTagTypeRegistry) CraftCustomItemTagContainer(org.bukkit.craftbukkit.v1_13_R2.inventory.tags.CraftCustomItemTagContainer) CustomItemTagContainer(org.bukkit.inventory.meta.tags.CustomItemTagContainer)

Example 4 with CustomItemTagContainer

use of org.bukkit.inventory.meta.tags.CustomItemTagContainer in project MechanicsMain by WeaponMechanics.

the class NBT_1_13_R2 method setInt.

@Override
public void setInt(@Nonnull ItemStack bukkitItem, @Nullable String plugin, @Nonnull String key, int value) {
    ItemMeta meta = bukkitItem.getItemMeta();
    CustomItemTagContainer nbt = getBukkitCompound(meta, plugin);
    nbt.setCustomTag(getKey(key), ItemTagType.INTEGER, value);
    bukkitItem.setItemMeta(meta);
}
Also used : ItemMeta(org.bukkit.inventory.meta.ItemMeta) CraftCustomItemTagContainer(org.bukkit.craftbukkit.v1_13_R2.inventory.tags.CraftCustomItemTagContainer) CustomItemTagContainer(org.bukkit.inventory.meta.tags.CustomItemTagContainer)

Example 5 with CustomItemTagContainer

use of org.bukkit.inventory.meta.tags.CustomItemTagContainer in project MechanicsMain by WeaponMechanics.

the class NBT_1_13_R2 method getString.

@Override
public String getString(@Nonnull ItemStack bukkitItem, @Nullable String plugin, @Nonnull String key, String def) {
    CustomItemTagContainer nbt = getBukkitCompound(bukkitItem.getItemMeta(), plugin);
    NamespacedKey tag = getKey(key);
    return nbt.hasCustomTag(tag, ItemTagType.STRING) ? nbt.getCustomTag(tag, ItemTagType.STRING) : def;
}
Also used : NamespacedKey(org.bukkit.NamespacedKey) CraftCustomItemTagContainer(org.bukkit.craftbukkit.v1_13_R2.inventory.tags.CraftCustomItemTagContainer) CustomItemTagContainer(org.bukkit.inventory.meta.tags.CustomItemTagContainer)

Aggregations

CustomItemTagContainer (org.bukkit.inventory.meta.tags.CustomItemTagContainer)15 CraftCustomItemTagContainer (org.bukkit.craftbukkit.v1_13_R2.inventory.tags.CraftCustomItemTagContainer)7 ItemMeta (org.bukkit.inventory.meta.ItemMeta)7 NamespacedKey (org.bukkit.NamespacedKey)6 PersistentDataContainer (org.bukkit.persistence.PersistentDataContainer)3 CraftPersistentDataContainer (org.bukkit.craftbukkit.v1_16_R3.persistence.CraftPersistentDataContainer)2 CraftCustomTagTypeRegistry (org.bukkit.craftbukkit.v1_13_R2.inventory.CraftCustomTagTypeRegistry)1 CraftPersistentDataContainer (org.bukkit.craftbukkit.v1_18_R2.persistence.CraftPersistentDataContainer)1