Search in sources :

Example 16 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class IconMenuInventory method createItemStack.

protected ItemStack createItemStack(IconMenuItem icon) {
    // TODO Check if this works properly
    ItemStack is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(icon.getMaterial(), icon.getAmount()));
    if (is == null) {
        is = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.STONE));
    }
    if (is.getTag() == null) {
        is.setTag(new CompoundTag());
    }
    if (icon.getBukkitMeta() != null) {
        try {
            applyToItemMethod.invoke(icon.getBukkitMeta(), is.getTag());
        } catch (InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    // add enchantment glowing
    if (icon.isGlowing()) {
        TagCompound enchTag = new TagCompound();
        enchTag.put("id", new TagString("minecraft:feather_falling"));
        enchTag.put("lvl", new TagShort(1));
        TagList enchList = new TagList();
        enchList.addTag(enchTag);
        is.getTag().put("Enchantments", ItemStackNBTConverter.compoundToVanillaCompound(enchList));
    } else {
        is.getTag().remove("Enchantments");
    }
    // hide item attributes like attack damage
    is.getTag().putInt("HideFlags", 63);
    // Prepare display tag
    CompoundTag display;
    if (is.getTag().contains("display")) {
        display = is.getTag().getCompound("display");
    } else {
        display = new CompoundTag();
        is.getTag().put("display", display);
    }
    // set Title
    if (!icon.getTitle().equals("")) {
        display.putString("Name", "{\"text\":\"" + icon.getTitle() + "\"}");
    }
    if (icon.getLore().size() > 0) {
        // set Lore
        ListTag loreTag = new ListTag();
        display.put("Lore", loreTag);
        for (String loreLine : icon.getLore()) {
            Component cm = CraftChatMessage.fromStringOrNull(loreLine);
            loreTag.add(StringTag.valueOf(Component.Serializer.toJson(cm)));
        }
    }
    if (icon.hasMeta()) {
        TagCompound tag = new TagCompound();
        icon.getMeta().applyTo(tag);
        CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(tag);
        for (String key : vanillaTag.getAllKeys()) {
            is.getTag().put(key, vanillaTag.get(key));
        }
    }
    if (icon.getTags() != null) {
        CompoundTag vanillaTag = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(icon.getTags());
        for (String key : vanillaTag.getAllKeys()) {
            is.getTag().put(key, vanillaTag.get(key));
        }
    }
    return is;
}
Also used : TagShort(de.keyle.knbt.TagShort) TagString(de.keyle.knbt.TagString) TagString(de.keyle.knbt.TagString) TagCompound(de.keyle.knbt.TagCompound) ListTag(net.minecraft.nbt.ListTag) InvocationTargetException(java.lang.reflect.InvocationTargetException) TagList(de.keyle.knbt.TagList) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) Component(net.minecraft.network.chat.Component) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 17 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class ItemStackComparator method compareTagData.

public static boolean compareTagData(ItemStack i1, ItemStack i2) {
    if (i1 == null || i2 == null) {
        return false;
    }
    if (i1.hasItemMeta() && i2.hasItemMeta()) {
        CompoundTag tag1 = CraftItemStack.asNMSCopy(i1).getTag();
        CompoundTag tag2 = CraftItemStack.asNMSCopy(i2).getTag();
        if (tag1 != null) {
            if (tag1.equals(tag2)) {
                return true;
            } else {
                i1 = CraftItemStack.asBukkitCopy(CraftItemStack.asNMSCopy(i1));
                tag1 = CraftItemStack.asNMSCopy(i1).getTag();
                return tag1.equals(tag2);
            }
        }
        return false;
    }
    return i1.hasItemMeta() == i2.hasItemMeta();
}
Also used : CompoundTag(net.minecraft.nbt.CompoundTag)

Example 18 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class ItemStackNBTConverter method itemStackToCompound.

public static TagCompound itemStackToCompound(ItemStack itemStack) {
    CompoundTag tagCompound = new CompoundTag();
    itemStack.save(tagCompound);
    return (TagCompound) vanillaCompoundToCompound(tagCompound);
}
Also used : TagCompound(de.keyle.knbt.TagCompound) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 19 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.

the class ItemStackComparator method compareTagData.

public static boolean compareTagData(ItemStack i1, ItemStack i2) {
    if (i1 == null || i2 == null) {
        return false;
    }
    if (i1.hasItemMeta() && i2.hasItemMeta()) {
        CompoundTag tag1 = CraftItemStack.asNMSCopy(i1).getTag();
        CompoundTag tag2 = CraftItemStack.asNMSCopy(i2).getTag();
        if (tag1 != null) {
            if (tag1.equals(tag2)) {
                return true;
            } else {
                i1 = CraftItemStack.asBukkitCopy(CraftItemStack.asNMSCopy(i1));
                tag1 = CraftItemStack.asNMSCopy(i1).getTag();
                return tag1.equals(tag2);
            }
        }
        return false;
    }
    return i1.hasItemMeta() == i2.hasItemMeta();
}
Also used : CompoundTag(net.minecraft.nbt.CompoundTag)

Example 20 with CompoundTag

use of net.minecraft.nbt.CompoundTag in project MinecraftForge by MinecraftForge.

the class ForgeChunkManager method readForgeForcedChunks.

/**
 * Reads the forge forced chunks from the NBT compound. Format is List{modid, List{ChunkPos, List{BlockPos}, List{UUID}}}
 *
 * @apiNote Internal
 */
public static void readForgeForcedChunks(CompoundTag nbt, TicketTracker<BlockPos> blockForcedChunks, TicketTracker<UUID> entityForcedChunks) {
    ListTag forcedChunks = nbt.getList("ForgeForced", Tag.TAG_COMPOUND);
    for (int i = 0; i < forcedChunks.size(); i++) {
        CompoundTag forcedEntry = forcedChunks.getCompound(i);
        String modId = forcedEntry.getString("Mod");
        if (ModList.get().isLoaded(modId)) {
            ListTag modForced = forcedEntry.getList("ModForced", Tag.TAG_COMPOUND);
            for (int j = 0; j < modForced.size(); j++) {
                CompoundTag modEntry = modForced.getCompound(j);
                long chunkPos = modEntry.getLong("Chunk");
                readBlockForcedChunks(modId, chunkPos, modEntry, "Blocks", blockForcedChunks.chunks);
                readBlockForcedChunks(modId, chunkPos, modEntry, "TickingBlocks", blockForcedChunks.tickingChunks);
                readEntityForcedChunks(modId, chunkPos, modEntry, "Entities", entityForcedChunks.chunks);
                readEntityForcedChunks(modId, chunkPos, modEntry, "TickingEntities", entityForcedChunks.tickingChunks);
            }
        } else {
            LOGGER.warn("Found chunk loading data for mod {} which is currently not available or active - it will be removed from the world save.", modId);
        }
    }
}
Also used : ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)31 ListTag (net.minecraft.nbt.ListTag)14 TagCompound (de.keyle.knbt.TagCompound)12 TagList (de.keyle.knbt.TagList)8 TagString (de.keyle.knbt.TagString)6 TagShort (de.keyle.knbt.TagShort)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4 ItemStack (org.bukkit.inventory.ItemStack)4 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)3 Dynamic (com.mojang.serialization.Dynamic)2 MyPetBaby (de.Keyle.MyPet.api.entity.MyPetBaby)2 MyAxolotl (de.Keyle.MyPet.api.entity.types.MyAxolotl)2 MyBee (de.Keyle.MyPet.api.entity.types.MyBee)2 MyCat (de.Keyle.MyPet.api.entity.types.MyCat)2 MyCreeper (de.Keyle.MyPet.api.entity.types.MyCreeper)2 MyEnderman (de.Keyle.MyPet.api.entity.types.MyEnderman)2 MyGoat (de.Keyle.MyPet.api.entity.types.MyGoat)2 MyHorse (de.Keyle.MyPet.api.entity.types.MyHorse)2