Use id 12 to LongArrayTag, add unregistering to Registries

This commit is contained in:
creeper123123321 2019-03-23 15:10:22 -03:00
parent a86b513901
commit db1fd63840
No known key found for this signature in database
GPG Key ID: 0AC57D54786721D1
6 changed files with 94 additions and 21 deletions

View File

@ -1,24 +1,14 @@
package com.github.steveice10.opennbt.conversion;
import com.github.steveice10.opennbt.conversion.builtin.ByteArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.ByteTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.CompoundTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.DoubleTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.FloatTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.IntArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.IntTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.ListTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.LongTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.ShortTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.StringTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.*;
import com.github.steveice10.opennbt.conversion.builtin.custom.DoubleArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.FloatArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.LongArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.SerializableArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.SerializableTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.ShortArrayTagConverter;
import com.github.steveice10.opennbt.conversion.builtin.custom.StringArrayTagConverter;
import com.github.steveice10.opennbt.tag.TagRegisterException;
import com.github.steveice10.opennbt.tag.TagUnregisterException;
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
@ -33,7 +23,7 @@ import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.github.steveice10.opennbt.tag.builtin.custom.DoubleArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.FloatArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableTag;
import com.github.steveice10.opennbt.tag.builtin.custom.ShortArrayTag;
@ -66,10 +56,10 @@ public class ConverterRegistry {
register(ListTag.class, List.class, new ListTagConverter());
register(CompoundTag.class, Map.class, new CompoundTagConverter());
register(IntArrayTag.class, int[].class, new IntArrayTagConverter());
register(LongArrayTag.class, long[].class, new LongArrayTagConverter());
register(DoubleArrayTag.class, double[].class, new DoubleArrayTagConverter());
register(FloatArrayTag.class, float[].class, new FloatArrayTagConverter());
register(LongArrayTag.class, long[].class, new LongArrayTagConverter());
register(SerializableArrayTag.class, Serializable[].class, new SerializableArrayTagConverter());
register(SerializableTag.class, Serializable.class, new SerializableTagConverter());
register(ShortArrayTag.class, short[].class, new ShortArrayTagConverter());
@ -99,6 +89,28 @@ public class ConverterRegistry {
typeToConverter.put(type, converter);
}
/**
* Unregisters a converter.
*
* @param <T> Tag type to unregister.
* @param <V> Value type to unregister.
* @param tag Tag type class to unregister.
* @param type Value type class to unregister.
* @throws ConverterUnregisterException If an error occurs while unregistering the converter.
*/
public static <T extends Tag, V> void unregister(Class<T> tag, Class<V> type) throws ConverterUnregisterException {
if (!tagToConverter.containsKey(tag)) {
throw new TagUnregisterException("Type conversion to tag " + tag.getName() + " is not registered.");
}
if (!typeToConverter.containsKey(type)) {
throw new TagUnregisterException("Tag conversion to type " + type.getName() + " is not registered.");
}
tagToConverter.remove(tag);
typeToConverter.remove(type);
}
/**
* Converts the given tag to a value.
*

View File

@ -0,0 +1,24 @@
package com.github.steveice10.opennbt.conversion;
/**
* An exception thrown when an error occurs while unregistering a converter.
*/
public class ConverterUnregisterException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L;
public ConverterUnregisterException() {
super();
}
public ConverterUnregisterException(String message) {
super(message);
}
public ConverterUnregisterException(Throwable cause) {
super(cause);
}
public ConverterUnregisterException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -1,7 +1,7 @@
package com.github.steveice10.opennbt.conversion.builtin.custom;
package com.github.steveice10.opennbt.conversion.builtin;
import com.github.steveice10.opennbt.conversion.TagConverter;
import com.github.steveice10.opennbt.tag.builtin.custom.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
/**
* A converter that converts between LongArrayTag and long[].

View File

@ -14,7 +14,7 @@ import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.github.steveice10.opennbt.tag.builtin.custom.DoubleArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.FloatArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableArrayTag;
import com.github.steveice10.opennbt.tag.builtin.custom.SerializableTag;
import com.github.steveice10.opennbt.tag.builtin.custom.ShortArrayTag;
@ -43,10 +43,10 @@ public class TagRegistry {
register(9, ListTag.class);
register(10, CompoundTag.class);
register(11, IntArrayTag.class);
register(12, LongArrayTag.class);
register(60, DoubleArrayTag.class);
register(61, FloatArrayTag.class);
register(62, LongArrayTag.class);
register(63, SerializableArrayTag.class);
register(64, SerializableTag.class);
register(65, ShortArrayTag.class);
@ -73,6 +73,21 @@ public class TagRegistry {
tagToId.put(tag, id);
}
/**
* Unregisters a tag class.
*
* @param id ID of the tag to unregister.
* @throws TagUnregisterException If an error occurs while unregistering the tag.
*/
public static void unregister(int id) throws TagUnregisterException {
if(!idToTag.containsKey(id)) {
throw new TagUnregisterException("Tag ID \"" + id + "\" isn't registered.");
}
tagToId.remove(getClassFor(id));
idToTag.remove(id);
}
/**
* Gets the tag class with the given id.
*

View File

@ -0,0 +1,24 @@
package com.github.steveice10.opennbt.tag;
/**
* An exception thrown when an error occurs while unregistering a tag.
*/
public class TagUnregisterException extends RuntimeException {
private static final long serialVersionUID = -2022049594558041160L;
public TagUnregisterException() {
super();
}
public TagUnregisterException(String message) {
super(message);
}
public TagUnregisterException(Throwable cause) {
super(cause);
}
public TagUnregisterException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -1,6 +1,4 @@
package com.github.steveice10.opennbt.tag.builtin.custom;
import com.github.steveice10.opennbt.tag.builtin.Tag;
package com.github.steveice10.opennbt.tag.builtin;
import java.io.DataInput;
import java.io.DataOutput;