@Override public TileEntity apply(Position position) { NBTCompound tag = new NBTCompound(); tag.setTag("color", new NBTInt(color)); return new TileEntity(TileEntityType.BED, position, tag); }
public TileEntity(NBTCompound fullNbt) { this.type = TileEntityType.getByRegistryId(fullNbt.getTagOfType("id", NBTType.STRING).getValue()); this.position = new Position(fullNbt.getNumberTag("x").getAsInt(), fullNbt.getNumberTag("y").getAsInt(), fullNbt.getNumberTag("z").getAsInt()); this.nbt = fullNbt; }
@Override public NBTCompound remapTag(ProtocolVersion version, String locale, NetworkItemStack itemstack, NBTCompound tag) { tag.setTag(CommonNBT.MODERN_ENCHANTMENTS, filterEnchantList(version, tag.getTagListOfType(CommonNBT.MODERN_ENCHANTMENTS, NBTType.COMPOUND))); tag.setTag(CommonNBT.BOOK_ENCHANTMENTS, filterEnchantList(version, tag.getTagListOfType(CommonNBT.BOOK_ENCHANTMENTS, NBTType.COMPOUND))); return tag; }
public static NBTCompound getOrCreateDisplayTag(NBTCompound rootTag) { NBTCompound display = rootTag.getTagOfType(DISPLAY, NBTType.COMPOUND); if (display == null) { display = new NBTCompound(); rootTag.setTag(DISPLAY, display); } return display; }
@Override public NBTCompound remapTag(ProtocolVersion version, String locale, NetworkItemStack itemstack, NBTCompound tag) { NBTList<NBTCompound> enchTag = tag.getTagListOfType(CommonNBT.LEGACY_ENCHANTMENTS, NBTType.COMPOUND); if (enchTag != null) { tag.removeTag(CommonNBT.LEGACY_ENCHANTMENTS); tag.setTag(CommonNBT.MODERN_ENCHANTMENTS, remapEnchantList(enchTag)); } NBTList<NBTCompound> bookEnch = tag.getTagListOfType(CommonNBT.BOOK_ENCHANTMENTS, NBTType.COMPOUND); if (bookEnch != null) { tag.setTag(CommonNBT.BOOK_ENCHANTMENTS, remapEnchantList(bookEnch)); } return tag; }
@Test public void testCompound() { String testtagname = "test"; NBTCompound compound = new NBTCompound(); compound.setTag(testtagname, new NBTShort((short) 20)); String testlisttagname = "testlist"; NBTList<NBTInt> list = new NBTList<>(NBTType.INT); list.addTag(new NBTInt(30)); compound.setTag(testlisttagname, list); Assertions.assertNotNull(compound.getTag(testtagname)); Assertions.assertNotNull(compound.getTagOfType(testtagname, NBTType.SHORT)); Assertions.assertNull(compound.getTagOfType(testtagname, NBTType.INT)); Assertions.assertNotNull(compound.getNumberTag(testtagname)); Assertions.assertNotNull(compound.getTag(testlisttagname)); Assertions.assertNotNull(compound.getTagOfType(testlisttagname, NBTType.LIST)); Assertions.assertNull(compound.getTagOfType(testlisttagname, NBTType.DOUBLE)); Assertions.assertNotNull(compound.getTagListOfType(testlisttagname, NBTType.INT)); Assertions.assertNull(compound.getTagListOfType(testlisttagname, NBTType.SHORT)); Assertions.assertNotNull(compound.getNumberTagList(testlisttagname)); }
@Override public NetworkItemStack remap(ProtocolVersion version, String locale, NetworkItemStack itemstack) { NBTCompound tag = itemstack.getNBT(); if (tag == null) { return itemstack; } String potion = NBTString.getValueOrNull(tag.getTagOfType("Potion", NBTType.STRING)); NBTList<NBTCompound> customPotionEffects = tag.getTagListOfType("CustomPotionEffects", NBTType.COMPOUND); if ((customPotionEffects != null) && (customPotionEffects.size() >= 1)) { potion = PotionData.getNameById(customPotionEffects.getTag(0).getNumberTag("Id").getAsInt()); } if (potion != null) { itemstack.setLegacyData(LegacyPotionId.toLegacyId(potion, isThrowablePotion)); String basicTypeName = LegacyPotionId.getBasicTypeName(potion); if (basicTypeName != null) { NBTCompound display = CommonNBT.getOrCreateDisplayTag(tag); display.setTag(CommonNBT.DISPLAY_NAME, new NBTString(ChatAPI.toJSON(new TextComponent(TranslationAPI.translate(locale, basicTypeName))))); } } return itemstack; }
@Override public NBTCompound remapTag(ProtocolVersion version, String locale, NetworkItemStack itemstack, NBTCompound tag) { NBTCompound displayTag = tag.getTagOfType(CommonNBT.DISPLAY, NBTType.COMPOUND); if (displayTag != null) { NBTString displayNameTag = displayTag.getTagOfType(CommonNBT.DISPLAY_NAME, NBTType.STRING); if (displayNameTag != null) { displayTag.setTag(CommonNBT.DISPLAY_NAME, new NBTString(ChatAPI.fromJSON(displayNameTag.getValue()).toLegacyText(locale))); } } return tag; }
NBTCompound nbt = itemstack.getNBT(); if (nbt == null) { nbt = new NBTCompound(); displayNBT.setTag(CommonNBT.DISPLAY_NAME, new NBTString(ChatAPI.toJSON(forcedDisplayName))); NBTList<NBTString> loreNBT = displayNBT.getTagListOfType(CommonNBT.DISPLAY_LORE, NBTType.STRING); if (loreNBT == null) { loreNBT = new NBTList<>(NBTType.STRING); loreNBT.addTag(new NBTString(lore)); displayNBT.setTag(CommonNBT.DISPLAY_LORE, loreNBT);
public static void remap(NBTCompound tag, String tagname, String newtagname) { NBTCompound gameprofileTag = tag.getTagOfType(tagname, NBTType.COMPOUND); if (gameprofileTag != null) { GameProfile gameprofile = GameProfileSerializer.deserialize(gameprofileTag); if (gameprofile.getName() != null) { tag.setTag(newtagname, new NBTString(gameprofile.getName())); } else { tag.removeTag(tagname); } } }
public static GameProfile deserialize(NBTCompound tag) { String name = NBTString.getValueOrNull(tag.getTagOfType(NAME_KEY, NBTType.STRING)); UUID uuid = null; try { uuid = UUID.fromString(NBTString.getValueOrNull(tag.getTagOfType(UUID_KEY, NBTType.STRING))); } catch (Throwable t) { } if (StringUtils.isEmpty(name) && (uuid == null)) { return null; } GameProfile gameProfile = new GameProfile(uuid, name); NBTCompound propertiesTag = tag.getTagOfType(PROPERTIES_KEY, NBTType.COMPOUND); if (propertiesTag != null) { for (String propertyName : propertiesTag.getTagNames()) { NBTList<NBTCompound> propertiesListTag = propertiesTag.getTagListOfType(propertyName, NBTType.COMPOUND); if (propertiesListTag != null) { for (NBTCompound propertyTag : propertiesListTag.getTags()) { gameProfile.addProperty(new ProfileProperty( propertyName, NBTString.getValueOrNull(propertyTag.getTagOfType(PROPERTY_VALUE_KEY, NBTType.STRING)), NBTString.getValueOrNull(propertyTag.getTagOfType(PROPERTY_SIGNATURE_KEY, NBTType.STRING)) )); } } } } return gameProfile; }
NBTType.COMPOUND, 10, stream -> { NBTCompound compound = new NBTCompound(); NBTType<?> valueType = null; while ((valueType = readTagType(stream)) != NBTType.END) { compound.setTag(readTagName(stream), readTag(stream, valueType)); for (Entry<String, NBT> entry : tag.getTags().entrySet()) { NBT value = entry.getValue(); writeTagType(stream, value.getType());
protected NBTList<NBTCompound> remapEnchantList(NBTList<NBTCompound> oldList) { NBTList<NBTCompound> newList = new NBTList<>(NBTType.COMPOUND); for (NBTCompound enchData : oldList.getTags()) { NBTNumber enchId = enchData.getNumberTag("id"); if (enchId != null) { Enchantment ench = LegacyEnchantmentId.getById(enchId.getAsInt()); if (ench != null) { enchData.setTag("id", new NBTString(ench.getKey().toString())); newList.addTag(enchData); } } } return newList; }
@SuppressWarnings("unchecked") public <T extends NBT> NBTList<T> getTagListOfType(String key, NBTType<T> type) { NBTList<? extends NBT> list = getTagOfType(key, NBTType.LIST); if ((list != null) && (list.getTagsType() == type)) { return (NBTList<T>) list; } return null; }
public TileEntity(TileEntityType type, Position position, NBTCompound nbt) { this.type = type; this.position = position; this.nbt = nbt; this.nbt.setTag("id", new NBTString(type.getRegistryId())); this.nbt.setTag("x", new NBTInt(position.getX())); this.nbt.setTag("y", new NBTInt(position.getY())); this.nbt.setTag("z", new NBTInt(position.getZ())); }
@Override public NetworkItemStack remap(ProtocolVersion version, String locale, NetworkItemStack itemstack) { NBTNumber map; if ((itemstack.getNBT() != null) && ((map = itemstack.getNBT().getNumberTag("map")) != null)) { itemstack.setLegacyData(map.getAsInt()); } return itemstack; }
@Override public NBTCompound remapTag(ProtocolVersion version, String locale, NetworkItemStack itemstack, NBTCompound tag) { NBTList<NBTCompound> enchTag = tag.getTagListOfType(CommonNBT.MODERN_ENCHANTMENTS, NBTType.COMPOUND); if (enchTag != null) { tag.removeTag(CommonNBT.MODERN_ENCHANTMENTS); tag.setTag(CommonNBT.LEGACY_ENCHANTMENTS, remapEnchantList(enchTag)); } NBTList<NBTCompound> bookEnch = tag.getTagListOfType(CommonNBT.BOOK_ENCHANTMENTS, NBTType.COMPOUND); if (bookEnch != null) { tag.setTag(CommonNBT.BOOK_ENCHANTMENTS, remapEnchantList(bookEnch)); } return tag; }
@Override public NBTCompound remapTag(ProtocolVersion version, String locale, NetworkItemStack itemstack, NBTCompound tag) { NBTCompound displayTag = tag.getTagOfType(CommonNBT.DISPLAY, NBTType.COMPOUND); if (displayTag != null) { NBTString displayNameTag = displayTag.getTagOfType(CommonNBT.DISPLAY_NAME, NBTType.STRING); if (displayNameTag != null) { displayTag.setTag(CommonNBT.DISPLAY_NAME, new NBTString(ChatAPI.toJSON(new TextComponent(displayNameTag.getValue())))); } } return tag; }
public static String[] getSignLines(NBTCompound tag) { String[] lines = new String[4]; for (int i = 0; i < lines.length; i++) { lines[i] = NBTString.getValueOrDefault(tag.getTagOfType("Text" + (i + 1), NBTType.STRING), ""); } return lines; }
@Override public void accept(TileEntity tile) { tile.getNBT().setTag("id", new NBTString(name)); }