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");
}
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);
}
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);
}
}
}
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);
}
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);
}
Aggregations