Search in sources :

Example 21 with CompoundTag

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

the class CodecsTest method testFluidStackCodec.

/**
 * Makes sure FluidStack.CODEC produces the same data as FluidStack.write
 */
private void testFluidStackCodec() {
    FluidStack noTag = new FluidStack(Fluids.WATER, 10);
    FluidStack withTag = new FluidStack(Fluids.LAVA, 10);
    withTag.getOrCreateChildTag("test").putString("value", "This is a test");
    CompoundTag noTag_write = noTag.writeToNBT(new CompoundTag());
    CompoundTag withTag_write = withTag.writeToNBT(new CompoundTag());
    CompoundTag noTag_encode = (CompoundTag) FluidStack.CODEC.encodeStart(NbtOps.INSTANCE, noTag).getOrThrow(false, error -> {
        LOGGER.error("Error encoding noTag: {}", error);
    });
    CompoundTag withTag_encode = (CompoundTag) FluidStack.CODEC.encodeStart(NbtOps.INSTANCE, withTag).getOrThrow(false, error -> {
        LOGGER.error("Error encoding withTag: {}", error);
    });
    if (!noTag_write.equals(noTag_encode))
        throw new IllegalStateException("Encoded noTag does not match");
    if (!withTag_write.equals(withTag_encode))
        throw new IllegalStateException("Encoded withTag does not match");
    FluidStack noTag_decode = FluidStack.CODEC.decode(NbtOps.INSTANCE, noTag_encode).getOrThrow(false, error -> {
        LOGGER.error("Error decoding noTag: {}", error);
    }).getFirst();
    FluidStack withTag_decode = FluidStack.CODEC.decode(NbtOps.INSTANCE, withTag_encode).getOrThrow(false, error -> {
        LOGGER.error("Error decoding withTag: {}", error);
    }).getFirst();
    if (!noTag.equals(noTag_decode))
        throw new IllegalStateException("Decoded noTag does not match");
    if (!withTag.equals(withTag_decode))
        throw new IllegalStateException("Decoded withTag does not match");
}
Also used : CompoundTag(net.minecraft.nbt.CompoundTag) Logger(org.apache.logging.log4j.Logger) FluidStack(net.minecraftforge.fluids.FluidStack) Mod(net.minecraftforge.fml.common.Mod) Fluids(net.minecraft.world.level.material.Fluids) NbtOps(net.minecraft.nbt.NbtOps) LogManager(org.apache.logging.log4j.LogManager) FluidStack(net.minecraftforge.fluids.FluidStack) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 22 with CompoundTag

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

the class IForgeMobEffectInstance method writeCurativeItems.

default void writeCurativeItems(CompoundTag nbt) {
    ListTag list = new ListTag();
    getCurativeItems().forEach(s -> list.add(s.save(new CompoundTag())));
    nbt.put("CurativeItems", list);
}
Also used : ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 23 with CompoundTag

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

the class PlatformHelper method applyTagToEntity.

@Override
public void applyTagToEntity(TagCompound tag, Entity bukkitEntity) {
    net.minecraft.world.entity.Entity entity = ((CraftEntity) bukkitEntity).getHandle();
    CompoundTag vanillaNBT = (CompoundTag) ItemStackNBTConverter.compoundToVanillaCompound(tag);
    if (vanillaNBT != null) {
        if (bukkitEntity instanceof Villager) {
            net.minecraft.world.entity.npc.Villager villager = (net.minecraft.world.entity.npc.Villager) entity;
            villager.readAdditionalSaveData(vanillaNBT);
        } else if (bukkitEntity instanceof net.minecraft.world.entity.npc.WanderingTrader) {
            net.minecraft.world.entity.npc.WanderingTrader villager = (net.minecraft.world.entity.npc.WanderingTrader) entity;
            villager.addAdditionalSaveData(vanillaNBT);
        }
    }
}
Also used : CraftEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftEntity) org.bukkit.entity(org.bukkit.entity) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 24 with CompoundTag

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

the class ConfigItem method load.

@Override
public void load(MaterialHolder material, String data) {
    ResourceLocation key = new ResourceLocation(material.getId());
    Item item = Registry.ITEM.get(key);
    // TODO AIR now?
    if (item == null) {
        Block block = Registry.BLOCK.get(key);
        item = block.asItem();
    }
    if (item == null) {
        return;
    }
    net.minecraft.world.item.ItemStack is = new net.minecraft.world.item.ItemStack(item, 1);
    if (data != null) {
        CompoundTag tag = null;
        String nbtString = data.trim();
        if (nbtString.startsWith("{") && nbtString.endsWith("}")) {
            try {
                tag = TagParser.parseTag(nbtString);
            } catch (Exception e) {
                MyPetApi.getLogger().warning("Error" + ChatColor.RESET + " in config: " + ChatColor.UNDERLINE + e.getLocalizedMessage() + ChatColor.RESET + " caused by:");
                MyPetApi.getLogger().warning(item.getDescriptionId() + " " + nbtString);
            }
            if (tag != null) {
                is.setTag(tag);
            }
        }
    }
    this.item = CraftItemStack.asCraftMirror(is);
}
Also used : Item(net.minecraft.world.item.Item) ResourceLocation(net.minecraft.resources.ResourceLocation) Block(net.minecraft.world.level.block.Block) ItemStack(org.bukkit.inventory.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 25 with CompoundTag

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

the class ConfigItem method load.

@Override
public void load(MaterialHolder material, String data) {
    ResourceLocation key = new ResourceLocation(material.getId());
    Item item = Registry.ITEM.get(key);
    // TODO AIR now?
    if (item == null) {
        Block block = Registry.BLOCK.get(key);
        item = block.asItem();
    }
    if (item == null) {
        return;
    }
    net.minecraft.world.item.ItemStack is = new net.minecraft.world.item.ItemStack(item, 1);
    if (data != null) {
        CompoundTag tag = null;
        String nbtString = data.trim();
        if (nbtString.startsWith("{") && nbtString.endsWith("}")) {
            try {
                tag = TagParser.parseTag(nbtString);
            } catch (Exception e) {
                MyPetApi.getLogger().warning("Error" + ChatColor.RESET + " in config: " + ChatColor.UNDERLINE + e.getLocalizedMessage() + ChatColor.RESET + " caused by:");
                MyPetApi.getLogger().warning(item.getDescriptionId() + " " + nbtString);
            }
            if (tag != null) {
                is.setTag(tag);
            }
        }
    }
    this.item = CraftItemStack.asCraftMirror(is);
}
Also used : Item(net.minecraft.world.item.Item) ResourceLocation(net.minecraft.resources.ResourceLocation) Block(net.minecraft.world.level.block.Block) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) ItemStack(org.bukkit.inventory.ItemStack) 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