3.3.0: Add Tag#asRawString

Also fix convertToTag
This commit is contained in:
Nassim Jahnke 2023-12-10 10:53:16 +01:00
parent f04adbae9a
commit ad8ac024c4
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
13 changed files with 78 additions and 8 deletions

View File

@ -5,7 +5,7 @@
<groupId>com.viaversion</groupId>
<artifactId>nbt</artifactId>
<version>3.2.1</version>
<version>3.3.0</version>
<packaging>jar</packaging>
<name>ViaNBT</name>

View File

@ -108,6 +108,7 @@ public class ConverterRegistry {
return null;
}
//noinspection unchecked
TagConverter<T, ? extends V> converter = (TagConverter<T, ? extends V>) TAG_TO_CONVERTER.get(tag.getTagId());
if (converter == null) {
throw new ConversionException("Tag type " + tag.getClass().getName() + " has no converter.");
@ -125,17 +126,26 @@ public class ConverterRegistry {
* @return The converted tag.
* @throws ConversionException If a suitable converter could not be found.
*/
@SuppressWarnings("unchecked")
public static <V, T extends Tag> @Nullable T convertToTag(@Nullable V value) throws ConversionException {
if (value == null) {
return null;
}
// No need to check super classes since registering custom tags is not allowed
// and all the given ones cannot be extended, super class can't be instantiated
Class<?> valueClass = value.getClass();
TagConverter<T, ? super V> converter = (TagConverter<T, ? super V>) TYPE_TO_CONVERTER.get(valueClass);
if (converter == null) {
throw new ConversionException("Value type " + valueClass.getName() + " has no converter.");
// Only check interfaces since you cannot register custom tags
for (Class<?> interfaceClass : valueClass.getInterfaces()) {
converter = (TagConverter<T, ? super V>) TYPE_TO_CONVERTER.get(interfaceClass);
if (converter != null) {
break;
}
}
if (converter == null) {
throw new ConversionException("Value type " + valueClass.getName() + " has no converter.");
}
}
return converter.convert(value);

View File

@ -35,6 +35,11 @@ public class ByteArrayTag extends Tag {
return this.value;
}
@Override
public String asRawString() {
return Arrays.toString(this.value);
}
/**
* Sets the value of this tag.
*

View File

@ -37,6 +37,11 @@ public class ByteTag extends NumberTag {
return this.value;
}
@Override
public String asRawString() {
return Byte.toString(this.value);
}
/**
* Sets the value of this tag.
*

View File

@ -37,6 +37,11 @@ public class DoubleTag extends NumberTag {
return this.value;
}
@Override
public String asRawString() {
return Double.toString(this.value);
}
/**
* Sets the value of this tag.
*

View File

@ -37,6 +37,11 @@ public class FloatTag extends NumberTag {
return this.value;
}
@Override
public String asRawString() {
return Float.toString(this.value);
}
/**
* Sets the value of this tag.
*

View File

@ -38,6 +38,11 @@ public class IntArrayTag extends Tag {
return this.value;
}
@Override
public String asRawString() {
return Arrays.toString(this.value);
}
/**
* Sets the value of this tag.
*

View File

@ -37,6 +37,11 @@ public class IntTag extends NumberTag {
return this.value;
}
@Override
public String asRawString() {
return Integer.toString(this.value);
}
/**
* Sets the value of this tag.
*

View File

@ -38,6 +38,11 @@ public class LongArrayTag extends Tag {
return this.value;
}
@Override
public String asRawString() {
return Arrays.toString(this.value);
}
/**
* Sets the value of this tag.
*

View File

@ -37,6 +37,11 @@ public class LongTag extends NumberTag {
return this.value;
}
@Override
public String asRawString() {
return Long.toString(this.value);
}
/**
* Sets the value of this tag.
*

View File

@ -37,6 +37,11 @@ public class ShortTag extends NumberTag {
return this.value;
}
@Override
public String asRawString() {
return Short.toString(this.value);
}
/**
* Sets the value of this tag.
*

View File

@ -36,6 +36,11 @@ public class StringTag extends Tag {
return this.value;
}
@Override
public String asRawString() {
return this.value;
}
/**
* Sets the value of this tag.
*

View File

@ -20,11 +20,21 @@ public abstract class Tag implements Cloneable {
*/
public abstract Object getValue();
/**
* Returns the raw string representation of the value of this tag.
* For SNBT, use {@link SNBT#serialize(Tag)} or {@link #toString()}.
*
* @return raw string representation of the value of this tag
*/
public String asRawString() {
return this.getValue().toString();
}
/**
* Returns the unchecked value of this tag.
*
* @return unchecked value of this tag
* @param <T> expected type
* @return unchecked value of this tag
*/
public <T> T value() {
return (T) getValue();
@ -43,7 +53,7 @@ public abstract class Tag implements Cloneable {
/**
* Reads this tag from an input stream.
*
* @param in Stream to write to.
* @param in Stream to write to.
* @param tagLimiter taglimiter
* @throws java.io.IOException If an I/O error occurs.
*/
@ -54,8 +64,8 @@ public abstract class Tag implements Cloneable {
/**
* Reads this tag from an input stream.
*
* @param in Stream to write to.
* @param tagLimiter taglimiter
* @param in Stream to write to.
* @param tagLimiter taglimiter
* @param nestingLevel current level of nesting
* @throws java.io.IOException If an I/O error occurs.
*/