use of net.minecraft.nbt.Tag in project MinecraftForge by MinecraftForge.
the class RegistryCodecTest method commonSetup.
public void commonSetup(final FMLCommonSetupEvent event) {
// Create our Json to decode
JsonObject json = new JsonObject();
json.addProperty("block", "minecraft:diamond_block");
json.addProperty("item", "minecraft:diamond_pickaxe");
// Decode our Json and log an info in case of success or a warning in case of error
DataResult<Pair<Pair<Block, Item>, JsonElement>> result = CODEC.decode(JsonOps.INSTANCE, json);
result.resultOrPartial(LOGGER::warn).ifPresent(pair -> LOGGER.info("Successfully decoded a diamond block and a diamond pickaxe from json to Block/Item"));
// Create a Pair<Block, Item> to test the serialization of our codec
Pair<Block, Item> pair = Pair.of(Blocks.DIAMOND_BLOCK, Items.DIAMOND_PICKAXE);
// Serialize the Pair to NBT, and log an info in case of success or a warning in case of error
DataResult<Tag> result2 = CODEC.encodeStart(NbtOps.INSTANCE, pair);
result2.resultOrPartial(LOGGER::warn).ifPresent(tag -> LOGGER.info("Successfully encoded a Pair<Block, Item> to a nbt tag: {}", tag));
// Serialize the Pair to JSON using the COMPRESSED JsonOps, this will use the int registry id instead of the ResourceLocation one,
// This is not recommended because int IDs can change, so you should not rely on them
DataResult<JsonElement> result3 = CODEC.encodeStart(JsonOps.COMPRESSED, pair);
result3.resultOrPartial(LOGGER::warn).ifPresent(compressedJson -> LOGGER.info("Successfully encoded a Pair<Block, Item> to a compressed json: {}", compressedJson));
// Create a json to decode using numerical IDs, to be decoded by JsonOps.COMPRESSED
JsonArray jsonCompressed = new JsonArray();
jsonCompressed.add(((ForgeRegistry<Block>) ForgeRegistries.BLOCKS).getID(Blocks.DIAMOND_BLOCK));
jsonCompressed.add(((ForgeRegistry<Item>) ForgeRegistries.ITEMS).getID(Items.DIAMOND_PICKAXE));
// Decode a compressed json to the corresponding Pair<Block, Item>, this time using Codec#parse
DataResult<Pair<Block, Item>> result4 = CODEC.parse(JsonOps.COMPRESSED, jsonCompressed);
result4.resultOrPartial(LOGGER::warn).ifPresent(pair2 -> LOGGER.info("Successfully decoded a diamond block and a diamond pickaxe from compressed json to Block/Item"));
}
use of net.minecraft.nbt.Tag in project MyPet by xXKeyleXx.
the class ItemStackNBTConverter method vanillaCompoundToCompound.
public static TagBase vanillaCompoundToCompound(Tag vanillaTag) {
switch(vanillaTag.getId()) {
case 1:
return new TagByte(((ByteTag) vanillaTag).getAsByte());
case 2:
return new TagShort(((ShortTag) vanillaTag).getAsShort());
case 3:
return new TagInt(((IntTag) vanillaTag).getAsInt());
case 4:
return new TagLong(((LongTag) vanillaTag).getAsLong());
case 5:
return new TagFloat(((FloatTag) vanillaTag).getAsFloat());
case 6:
return new TagDouble(((DoubleTag) vanillaTag).getAsDouble());
case 7:
return new TagByteArray(((ByteArrayTag) vanillaTag).getAsByteArray());
case 8:
return new TagString(vanillaTag.getAsString());
case 9:
ListTag tagList = (ListTag) vanillaTag;
List compoundList = new ArrayList();
try {
ArrayList list = (ArrayList) TAG_LIST_LIST.get(tagList);
for (Object aList : list) {
compoundList.add(vanillaCompoundToCompound((Tag) aList));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return new TagList(compoundList);
case 10:
TagCompound compound = new TagCompound();
CompoundTag tagCompound = ((CompoundTag) vanillaTag);
Set<String> keys = tagCompound.getAllKeys();
for (String tagName : keys) {
compound.getCompoundData().put(tagName, vanillaCompoundToCompound(tagCompound.get(tagName)));
}
return compound;
case 11:
return new TagIntArray(((IntArrayTag) vanillaTag).getAsIntArray());
}
return null;
}
use of net.minecraft.nbt.Tag in project MyPet by xXKeyleXx.
the class ItemStackNBTConverter method vanillaCompoundToCompound.
public static TagBase vanillaCompoundToCompound(Tag vanillaTag) {
switch(vanillaTag.getId()) {
case 1:
return new TagByte(((ByteTag) vanillaTag).getAsByte());
case 2:
return new TagShort(((ShortTag) vanillaTag).getAsShort());
case 3:
return new TagInt(((IntTag) vanillaTag).getAsInt());
case 4:
return new TagLong(((LongTag) vanillaTag).getAsLong());
case 5:
return new TagFloat(((FloatTag) vanillaTag).getAsFloat());
case 6:
return new TagDouble(((DoubleTag) vanillaTag).getAsDouble());
case 7:
return new TagByteArray(((ByteArrayTag) vanillaTag).getAsByteArray());
case 8:
return new TagString(vanillaTag.getAsString());
case 9:
ListTag tagList = (ListTag) vanillaTag;
List compoundList = new ArrayList();
try {
ArrayList list = (ArrayList) TAG_LIST_LIST.get(tagList);
for (Object aList : list) {
compoundList.add(vanillaCompoundToCompound((Tag) aList));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return new TagList(compoundList);
case 10:
TagCompound compound = new TagCompound();
CompoundTag tagCompound = ((CompoundTag) vanillaTag);
Set<String> keys = tagCompound.getAllKeys();
for (String tagName : keys) {
compound.getCompoundData().put(tagName, vanillaCompoundToCompound(tagCompound.get(tagName)));
}
return compound;
case 11:
return new TagIntArray(((IntArrayTag) vanillaTag).getAsIntArray());
}
return null;
}
Aggregations