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; }
@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; }
@SuppressWarnings("unchecked") public NBTList<NBTNumber> getNumberTagList(String key) { NBTList<? extends NBT> list = getTagOfType(key, NBTType.LIST); if ((list != null) && NBTNumber.class.isAssignableFrom(list.getTagsType().getNBTTagClass())) { return (NBTList<NBTNumber>) list; } return null; }
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) { 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; }
@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 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; }
protected NBTList<NBTCompound> filterEnchantList(ProtocolVersion version, NBTList<NBTCompound> oldList) { if (oldList == null) { return null; } GenericSkippingTable<String> enchSkip = GenericIdSkipper.ENCHANT.getTable(version); NBTList<NBTCompound> newList = new NBTList<>(NBTType.COMPOUND); for (NBTCompound enchData : oldList.getTags()) { if (!enchSkip.shouldSkip(NBTString.getValueOrDefault(enchData.getTagOfType("id", NBTType.STRING), ""))) { newList.addTag(enchData); } } return newList; }
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); } } }
protected NBTList<NBTCompound> remapEnchantList(NBTList<NBTCompound> oldList) { NBTList<NBTCompound> newList = new NBTList<>(NBTType.COMPOUND); for (NBTCompound enchData : oldList.getTags()) { Enchantment ench = Enchantment.getByKey(NamespacedKeyUtils.fromString(NBTString.getValueOrNull(enchData.getTagOfType("id", NBTType.STRING)))); if (ench != null) { enchData.setTag("id", new NBTShort(LegacyEnchantmentId.getId(ench))); newList.addTag(enchData); } } return newList; }
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; }
@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; }