Search in sources :

Example 1 with ItemData

use of io.github.plugindustry.wheelcore.interfaces.item.ItemData in project WheelCore by Plugindustry.

the class NBTBasedProvider method getData.

@Nullable
@Override
public ItemData getData(@Nullable ItemStack itemStack) {
    if (itemStack == null)
        return null;
    Optional<NbtWrapper<?>> nbtWrapperOptional = NbtFactory.fromItemOptional((ItemStack) ShadowManager.shadowUnpack(CraftItemStack.asCraftCopy(itemStack)));
    if (nbtWrapperOptional.isEmpty())
        return null;
    NbtWrapper<?> nbtWrapper = nbtWrapperOptional.get();
    if (nbtWrapper.getType() != NbtType.TAG_COMPOUND)
        return null;
    NbtCompound compound = NbtFactory.asCompound(nbtWrapper);
    if (!compound.containsKey("wheel_core_item_data"))
        return null;
    Object data = compound.getObject("wheel_core_item_data");
    return data instanceof String ? gson.fromJson((String) data, ItemData.class) : null;
}
Also used : NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) NbtWrapper(com.comphenix.protocol.wrappers.nbt.NbtWrapper) ItemData(io.github.plugindustry.wheelcore.interfaces.item.ItemData) Nullable(javax.annotation.Nullable)

Example 2 with ItemData

use of io.github.plugindustry.wheelcore.interfaces.item.ItemData in project WheelCore by Plugindustry.

the class PersistenceBasedProvider method setData.

@Override
public void setData(@Nonnull ItemStack itemStack, @Nullable ItemData data) {
    if (data == null) {
        if (itemStack.hasItemMeta()) {
            ItemMeta meta = Objects.requireNonNull(itemStack.getItemMeta());
            meta.getPersistentDataContainer().remove(ITEM_DATA_KEY);
            itemStack.setItemMeta(meta);
        }
    } else {
        ItemMeta meta = itemStack.hasItemMeta() ? Objects.requireNonNull(itemStack.getItemMeta()) : Bukkit.getItemFactory().getItemMeta(itemStack.getType());
        Objects.requireNonNull(meta).getPersistentDataContainer().set(ITEM_DATA_KEY, PersistentDataType.STRING, gson.toJson(data, ItemData.class));
        itemStack.setItemMeta(meta);
    }
}
Also used : ItemMeta(org.bukkit.inventory.meta.ItemMeta) ItemData(io.github.plugindustry.wheelcore.interfaces.item.ItemData)

Example 3 with ItemData

use of io.github.plugindustry.wheelcore.interfaces.item.ItemData in project WheelCore by Plugindustry.

the class I18n method getOriginalItem.

@Nonnull
private static ItemStack getOriginalItem(@Nonnull ItemStack item) {
    ItemData itemData = MainManager.getItemData(item);
    if (itemData instanceof TranslatedItemData) {
        ItemStack orgItem = Optional.ofNullable(((TranslatedItemData) itemData).originalItem).orElse(item);
        orgItem.setAmount(item.getAmount());
        return orgItem;
    } else
        return item;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) ItemData(io.github.plugindustry.wheelcore.interfaces.item.ItemData) Nonnull(javax.annotation.Nonnull)

Aggregations

ItemData (io.github.plugindustry.wheelcore.interfaces.item.ItemData)3 NbtCompound (com.comphenix.protocol.wrappers.nbt.NbtCompound)1 NbtWrapper (com.comphenix.protocol.wrappers.nbt.NbtWrapper)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 ItemStack (org.bukkit.inventory.ItemStack)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1