Do some code cleanup, add helper functions

This commit is contained in:
Zontreck 2023-10-13 18:34:36 -07:00
parent 142aafbf15
commit 7f5dc475ea
7 changed files with 89 additions and 81 deletions

View file

@ -4,19 +4,17 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class ByteTag extends Tag
{
private byte value;
public ByteTag()
{
public class ByteTag extends Tag {
public byte value;
public ByteTag() {
value = 0;
}
public ByteTag(byte val)
{
public ByteTag(byte val) {
value = val;
}
@Override
public String getCanonicalName() {
return "TAG_Byte";
@ -33,10 +31,9 @@ public class ByteTag extends Tag
}
@Override
public String PrettyPrint(int indent)
{
public String PrettyPrint(int indent) {
String builder = super.PrettyPrint(indent);
builder += ": "+ value;
builder += ": " + value;
return builder;
}

View file

@ -4,15 +4,14 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class IntTag extends Tag
{
private int value;
public IntTag()
{
public class IntTag extends Tag {
public int value;
public IntTag() {
value = 0;
}
public IntTag(int val)
{
public IntTag(int val) {
value = val;
}
@ -32,8 +31,7 @@ public class IntTag extends Tag
}
@Override
public String PrettyPrint(int indent)
{
public String PrettyPrint(int indent) {
String builder = super.PrettyPrint(indent);
builder += ": " + value;

View file

@ -4,16 +4,14 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class ShortTag extends Tag
{
private short value;
public ShortTag()
{
public class ShortTag extends Tag {
public short value;
public ShortTag() {
value = 0;
}
public ShortTag(short val)
{
public ShortTag(short val) {
value = val;
}
@ -23,8 +21,7 @@ public class ShortTag extends Tag
}
@Override
public String PrettyPrint(int indent)
{
public String PrettyPrint(int indent) {
String builder = super.PrettyPrint(indent);
builder += ": " + value;
return builder;

View file

@ -4,28 +4,24 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class StringTag extends Tag
{
private String value;
public class StringTag extends Tag {
public String value;
public StringTag(){
public StringTag() {
value = "";
}
public StringTag(String val)
{
public StringTag(String val) {
value = val;
}
@Override
public String getCanonicalName() {
return "TAG_String";
}
@Override
public String PrettyPrint(int indent)
{
public String PrettyPrint(int indent) {
String builder = super.PrettyPrint(indent);
builder += ": \"" + value + "\"";

View file

@ -5,15 +5,12 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
public abstract class Tag
{
public abstract class Tag {
public Type type = Type.End;
public static String MakeIndent(int indent)
{
public static String MakeIndent(int indent) {
String builder = "";
for(int i = 0;i<indent;i++)
{
for (int i = 0; i < indent; i++) {
builder += " ";
}
@ -21,8 +18,8 @@ public abstract class Tag
}
public abstract String getCanonicalName();
public String PrettyPrint(int indent)
{
public String PrettyPrint(int indent) {
String builder = "";
builder += MakeIndent(indent) + getCanonicalName();
@ -40,13 +37,14 @@ public abstract class Tag
* <p>
* Then, initializes an instance of the new tag and returns it.
* <p>
* NOTE: Value is not read by this method. The separate ReadValue method must be called after reading a name, or other data as necessary
* NOTE: Value is not read by this method. The separate ReadValue method must be
* called after reading a name, or other data as necessary
*
* @param dis
* @return
* @throws IOException
*/
public static Tag Read(DataInputStream dis) throws IOException
{
public static Tag Read(DataInputStream dis) throws IOException {
Type type = Type.valueOf(dis.readByte());
Class<? extends Tag> base = TagTypeRegistry.getByType(type);
try {
@ -66,4 +64,32 @@ public abstract class Tag
public abstract void ReadValue(DataInputStream dis) throws IOException;
public StringTag asString() {
if (this instanceof StringTag st)
return st;
else
return new StringTag();
}
public ShortTag asShort() {
if (this instanceof ShortTag st)
return st;
else
return new ShortTag();
}
public ByteTag asByte() {
if (this instanceof ByteTag bt)
return bt;
else
return new ByteTag();
}
public IntTag asInt() {
if (this instanceof IntTag it)
return it;
else
return new IntTag();
}
}

View file

@ -3,18 +3,16 @@ package dev.zontreck.registry.v3;
import java.util.HashMap;
import java.util.Map;
public class TagTypeRegistry
{
public class TagTypeRegistry {
private static Map<Type, Class<? extends Tag>> types = new HashMap<>();
public static void RegisterType(Type T, Class<? extends Tag> clazz)
{
public static void RegisterType(Type T, Class<? extends Tag> clazz) {
types.put(T, clazz);
}
public static Class<? extends Tag> getByType(Type T)
{
if(types.containsKey(T))return types.get(T);
public static Class<? extends Tag> getByType(Type T) {
if (types.containsKey(T))
return types.get(T);
return null;
}
}

View file

@ -1,41 +1,37 @@
package dev.zontreck.registry.v3;
public enum Type
{
End (0),
Byte (1),
Short (2),
Int (3),
Long (4),
public enum Type {
End(0),
Byte(1),
Short(2),
Int(3),
Long(4),
Float(5),
Double (6),
ByteArray (7),
String (8),
List (9),
Key (10),
IntArray (11),
LongArray (12);
Double(6),
ByteArray(7),
String(8),
List(9),
Key(10),
IntArray(11),
LongArray(12);
byte value;
Type(int val)
{
value = (byte)val;
Type(int val) {
value = (byte) val;
}
public static void PerformRegister()
{
public static void PerformRegister() {
TagTypeRegistry.RegisterType(String, StringTag.class);
TagTypeRegistry.RegisterType(Byte, ByteTag.class);
TagTypeRegistry.RegisterType(Short, ShortTag.class);
TagTypeRegistry.RegisterType(Int, IntTag.class);
}
public static Type valueOf(byte b)
{
for (Type T :
values()) {
if (T.value == b) return T;
public static Type valueOf(byte b) {
for (Type T : values()) {
if (T.value == b)
return T;
}
return null;
}