Add some initial stuff to the library
This commit is contained in:
parent
d9eca18399
commit
54e60c6b2c
29 changed files with 2657 additions and 674 deletions
26
src/main/java/dev/zontreck/ariaslib/events/Event.java
Normal file
26
src/main/java/dev/zontreck/ariaslib/events/Event.java
Normal file
|
@ -0,0 +1,26 @@
|
|||
package dev.zontreck.ariaslib.events;
|
||||
|
||||
|
||||
public abstract class Event {
|
||||
private boolean isCancelled=false;
|
||||
|
||||
public Event()
|
||||
{
|
||||
}
|
||||
|
||||
public abstract boolean isCancellable();
|
||||
|
||||
public boolean isCancelled()
|
||||
{
|
||||
return isCancelled;
|
||||
}
|
||||
|
||||
public void setCancelled(boolean cancel) throws Exception
|
||||
{
|
||||
if(!isCancellable())
|
||||
{
|
||||
throw new Exception("This event cannot be cancelled");
|
||||
}
|
||||
isCancelled=cancel;
|
||||
}
|
||||
}
|
90
src/main/java/dev/zontreck/ariaslib/events/EventBus.java
Normal file
90
src/main/java/dev/zontreck/ariaslib/events/EventBus.java
Normal file
|
@ -0,0 +1,90 @@
|
|||
package dev.zontreck.ariaslib.events;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import dev.zontreck.ariaslib.events.annotations.Subscribe;
|
||||
import dev.zontreck.ariaslib.exceptions.EventRegistrationException;
|
||||
|
||||
public class EventBus
|
||||
{
|
||||
public static final EventBus BUS = new EventBus();
|
||||
public EventsListenerList listeners = new EventsListenerList();
|
||||
/**
|
||||
* METHODS MUST BE STATIC!!!
|
||||
* @param clazz
|
||||
*/
|
||||
public void register(Class<?> clazz)
|
||||
{
|
||||
Arrays.stream(clazz.getMethods())
|
||||
.filter(m->Modifier.isStatic(m.getModifiers()))
|
||||
.filter(m->m.isAnnotationPresent(Subscribe.class))
|
||||
.forEach(m-> {
|
||||
try {
|
||||
registerListeners(clazz, m, m);
|
||||
} catch (EventRegistrationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void registerListeners(final Object target, final Method method, final Method real) throws EventRegistrationException {
|
||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
if(parameterTypes.length != 1)
|
||||
{
|
||||
throw new EventRegistrationException("Method "+method+" has an @Subscribe annotation but must have only 1 argument, no more and no less");
|
||||
}
|
||||
|
||||
Class<?> eventType = parameterTypes[0];
|
||||
|
||||
if(!Event.class.isAssignableFrom(eventType))
|
||||
{
|
||||
throw new EventRegistrationException("Event ["+eventType+"] is not a subtype of ["+Event.class.getName()+"]");
|
||||
}
|
||||
|
||||
|
||||
register(eventType, target, real);
|
||||
}
|
||||
|
||||
private void register(Class<?> eventType, Object target, Method real) {
|
||||
listeners.addEventMethod(eventType, real, real.getDeclaringClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts an event to the event bus
|
||||
* @param event
|
||||
* @return True if the event was cancelled.
|
||||
*/
|
||||
public boolean post(Event event)
|
||||
{
|
||||
boolean cancelled = false;
|
||||
for (Map.Entry<Class<?>, List<EventContainer>> entry : listeners.events.entrySet()) {
|
||||
Class<?> eventClazz = entry.getKey();
|
||||
List<EventContainer> containers = entry.getValue();
|
||||
|
||||
if(eventClazz.isInstance(Event.class))
|
||||
{
|
||||
for (EventContainer eventContainer : containers) {
|
||||
try {
|
||||
eventContainer.function.invoke(event);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if(event.isCancelled()) cancelled=true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package dev.zontreck.ariaslib.events;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class EventContainer {
|
||||
public Method function;
|
||||
public Class<?> containingClass;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package dev.zontreck.ariaslib.events;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class EventsListenerList {
|
||||
|
||||
public Map<Class<?>, List<EventContainer>> events = Maps.newHashMap();
|
||||
|
||||
public void addEventMethod(Class<?> e, Method action, Class<?> clazz)
|
||||
{
|
||||
|
||||
// Add a new container!
|
||||
EventContainer contains = new EventContainer();
|
||||
contains.function=action;
|
||||
contains.containingClass = clazz;
|
||||
|
||||
if(!events.containsKey(e)){
|
||||
List<EventContainer> tmp = Lists.newArrayList();
|
||||
tmp.add(contains);
|
||||
events.put(e, tmp);
|
||||
}else {
|
||||
events.get(e).add(contains);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package dev.zontreck.ariaslib.events;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import dev.zontreck.ariaslib.nbt.CompoundTag;
|
||||
|
||||
/**
|
||||
* This event cannot be cancelled.
|
||||
*/
|
||||
public class NBTLoadedEvent extends Event
|
||||
{
|
||||
public CompoundTag loadedTag;
|
||||
public String fileName;
|
||||
|
||||
@Override
|
||||
public boolean isCancellable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public NBTLoadedEvent(CompoundTag tag, String fileName)
|
||||
{
|
||||
loadedTag=tag;
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package dev.zontreck.ariaslib.events;
|
||||
|
||||
import dev.zontreck.ariaslib.nbt.CompoundTag;
|
||||
|
||||
/**
|
||||
* This event is cancellable. Cancelling this event will result in the saving process being interrupted, and the tag to be saved, replaced right before saving completes
|
||||
*/
|
||||
public class NBTSavingEvent extends Event
|
||||
{
|
||||
public CompoundTag tag;
|
||||
public String file;
|
||||
|
||||
@Override
|
||||
public boolean isCancellable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public NBTSavingEvent(CompoundTag current, String fileName)
|
||||
{
|
||||
tag=current;
|
||||
file=fileName;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package dev.zontreck.ariaslib.events.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(value = RetentionPolicy.RUNTIME)
|
||||
@Target(value = ElementType.METHOD)
|
||||
public @interface Subscribe
|
||||
{
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package dev.zontreck.ariaslib.exceptions;
|
||||
|
||||
public class EventRegistrationException extends Exception{
|
||||
public EventRegistrationException(String message){
|
||||
super(message);
|
||||
}
|
||||
}
|
187
src/main/java/dev/zontreck/ariaslib/nbt/ByteArrayTag.java
Normal file
187
src/main/java/dev/zontreck/ariaslib/nbt/ByteArrayTag.java
Normal file
|
@ -0,0 +1,187 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class ByteArrayTag implements Tag
|
||||
{
|
||||
public static final TagType<ByteArrayTag> TYPE = new TagType<ByteArrayTag>() {
|
||||
|
||||
@Override
|
||||
public ByteArrayTag load(DataInput input) throws IOException {
|
||||
List<Byte> lst = new ArrayList<>();
|
||||
int count = input.readInt();
|
||||
while(count>0)
|
||||
{
|
||||
lst.add(input.readByte());
|
||||
count--;
|
||||
}
|
||||
|
||||
return new ByteArrayTag(toArray(lst));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
load(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Byte_Array";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Byte_Array";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeInt(value.length);
|
||||
for (byte b : value) {
|
||||
output.writeByte(b);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_BYTE_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new ByteArrayTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
|
||||
String indent = makeIndent(indents);
|
||||
String indentInside = makeIndent(indents+1);
|
||||
|
||||
String ret = "\n" + indent + "[\n";
|
||||
|
||||
for (int i = 0; i< value.length; i++)
|
||||
{
|
||||
byte b = value[i];
|
||||
ret += indentInside+String.valueOf(b);
|
||||
|
||||
if((i+1) != value.length)
|
||||
{
|
||||
ret += ", ";
|
||||
}
|
||||
ret+="\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
ret += indent+"]";
|
||||
return ret;
|
||||
}
|
||||
|
||||
private ByteArrayTag(byte[] value)
|
||||
{
|
||||
this.value=value;
|
||||
}
|
||||
private static byte[] toArray(List<Byte> entries)
|
||||
{
|
||||
byte[] ret = new byte[entries.size()];
|
||||
int cur=0;
|
||||
for(byte b : entries)
|
||||
{
|
||||
ret[cur] = b;
|
||||
cur++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static ByteArrayTag valueOf(byte[] b)
|
||||
{
|
||||
return new ByteArrayTag(b);
|
||||
}
|
||||
private byte[] value;
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
|
||||
}
|
142
src/main/java/dev/zontreck/ariaslib/nbt/ByteTag.java
Normal file
142
src/main/java/dev/zontreck/ariaslib/nbt/ByteTag.java
Normal file
|
@ -0,0 +1,142 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ByteTag implements Tag
|
||||
{
|
||||
public static final TagType<ByteTag> TYPE = new TagType<ByteTag>() {
|
||||
|
||||
@Override
|
||||
public ByteTag load(DataInput input) throws IOException {
|
||||
return ByteTag.valueOf(input.readByte());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
input.skipBytes(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Byte";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Byte";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeByte(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_BYTE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new ByteTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
|
||||
String val = String.valueOf(value);
|
||||
val+="b";
|
||||
return val;
|
||||
}
|
||||
|
||||
private ByteTag(byte value)
|
||||
{
|
||||
this.value=value;
|
||||
}
|
||||
public static ByteTag valueOf(byte b)
|
||||
{
|
||||
return new ByteTag(b);
|
||||
}
|
||||
private byte value;
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
|
||||
}
|
305
src/main/java/dev/zontreck/ariaslib/nbt/CompoundTag.java
Normal file
305
src/main/java/dev/zontreck/ariaslib/nbt/CompoundTag.java
Normal file
|
@ -0,0 +1,305 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class CompoundTag implements Tag
|
||||
{
|
||||
public static final TagType<CompoundTag> TYPE = new TagType<CompoundTag>() {
|
||||
|
||||
@Override
|
||||
public CompoundTag load(DataInput input) throws IOException {
|
||||
CompoundTag newTag = new CompoundTag();
|
||||
|
||||
byte type;
|
||||
while((type = input.readByte()) != TAG_END)
|
||||
{
|
||||
TagType<?> v = TagTypes.getType(type);
|
||||
String tagName = input.readUTF();
|
||||
Tag finalTag = v.load(input);
|
||||
newTag.put(tagName, finalTag);
|
||||
}
|
||||
|
||||
return newTag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
load(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Compound";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Compound";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
for (Map.Entry<String,Tag> it : list.entrySet()) {
|
||||
// We're saving everything now
|
||||
output.writeByte(it.getValue().getId());
|
||||
output.writeUTF(it.getKey());
|
||||
it.getValue().write(output);
|
||||
}
|
||||
output.writeByte(TAG_END);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return Tag.TAG_COMPOUND;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new CompoundTag(new HashMap<>(list));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
|
||||
String indent = makeIndent(indents);
|
||||
String indentInside = makeIndent(indents+1);
|
||||
|
||||
String ret = "\n"+indent+"{\n";
|
||||
Iterator<Entry<String,Tag>> entries = list.entrySet().iterator();
|
||||
while(entries.hasNext())
|
||||
{
|
||||
Entry<String,Tag> entry = entries.next();
|
||||
ret += indentInside+entry.getValue().getType().getPrettyName() + " ["+entry.getKey()+"]: ";
|
||||
ret += entry.getValue().getAsString(indents+1);
|
||||
|
||||
if(entries.hasNext())
|
||||
{
|
||||
ret+=",";
|
||||
}
|
||||
ret += "\n";
|
||||
}
|
||||
|
||||
ret += indent+"}";
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
// Return the values of getAsString
|
||||
return getAsString(0);
|
||||
}
|
||||
|
||||
private Map<String, Tag> list;
|
||||
|
||||
public CompoundTag()
|
||||
{
|
||||
list = Maps.newHashMap();
|
||||
}
|
||||
|
||||
protected CompoundTag(Map< String, Tag > map)
|
||||
{
|
||||
list=map;
|
||||
}
|
||||
|
||||
public void put(String name, Tag entry)
|
||||
{
|
||||
list.put(name, entry);
|
||||
}
|
||||
|
||||
public void putString(String name, String value)
|
||||
{
|
||||
list.put(name, StringTag.valueOf(value));
|
||||
}
|
||||
|
||||
public String getString(String name)
|
||||
{
|
||||
return list.get(name).asString();
|
||||
}
|
||||
public void putByte(String name, byte value)
|
||||
{
|
||||
list.put(name, ByteTag.valueOf(value));
|
||||
}
|
||||
|
||||
public byte getByte(String name)
|
||||
{
|
||||
return list.get(name).asByte();
|
||||
}
|
||||
public void putDouble(String name, double value)
|
||||
{
|
||||
list.put(name, DoubleTag.valueOf(value));
|
||||
}
|
||||
|
||||
public double getDouble(String name)
|
||||
{
|
||||
return list.get(name).asDouble();
|
||||
}
|
||||
public void putFloat(String name, float value)
|
||||
{
|
||||
list.put(name, FloatTag.valueOf(value));
|
||||
}
|
||||
|
||||
public float getFloat(String name)
|
||||
{
|
||||
return list.get(name).asFloat();
|
||||
}
|
||||
public void putInt(String name, int value)
|
||||
{
|
||||
list.put(name, IntTag.valueOf(value));
|
||||
}
|
||||
|
||||
public int getInt(String name)
|
||||
{
|
||||
return list.get(name).asInt();
|
||||
}
|
||||
|
||||
public ListTag getList(String name)
|
||||
{
|
||||
return (ListTag)list.get(name);
|
||||
}
|
||||
public void putLong(String name, long value)
|
||||
{
|
||||
list.put(name, LongTag.valueOf(value));
|
||||
}
|
||||
|
||||
public long getLong(String name)
|
||||
{
|
||||
return list.get(name).asLong();
|
||||
}
|
||||
public void putShort(String name, short value)
|
||||
{
|
||||
list.put(name, ShortTag.valueOf(value));
|
||||
}
|
||||
|
||||
public short getShort(String name)
|
||||
{
|
||||
return list.get(name).asShort();
|
||||
}
|
||||
public void putByteArray(String name, byte[] value)
|
||||
{
|
||||
list.put(name, ByteArrayTag.valueOf(value));
|
||||
}
|
||||
|
||||
public ByteArrayTag getByteArray(String name)
|
||||
{
|
||||
return (ByteArrayTag)list.get(name);
|
||||
}
|
||||
public void putIntArray(String name, int[] value)
|
||||
{
|
||||
list.put(name, IntArrayTag.valueOf(value));
|
||||
}
|
||||
|
||||
public IntArrayTag getIntArray(String name)
|
||||
{
|
||||
return (IntArrayTag)list.get(name);
|
||||
}
|
||||
public void putLongArray(String name, long[] value)
|
||||
{
|
||||
list.put(name, LongArrayTag.valueOf(value));
|
||||
}
|
||||
|
||||
public LongArrayTag getLongArray(String name)
|
||||
{
|
||||
return (LongArrayTag)list.get(name);
|
||||
}
|
||||
public void putCompound(String name, CompoundTag value)
|
||||
{
|
||||
list.put(name, value);
|
||||
}
|
||||
|
||||
public CompoundTag getCompound(String name)
|
||||
{
|
||||
return (CompoundTag)list.get(name);
|
||||
}
|
||||
|
||||
public int size()
|
||||
{
|
||||
return list.size();
|
||||
}
|
||||
|
||||
public void clear()
|
||||
{
|
||||
list.clear();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
}
|
137
src/main/java/dev/zontreck/ariaslib/nbt/DoubleTag.java
Normal file
137
src/main/java/dev/zontreck/ariaslib/nbt/DoubleTag.java
Normal file
|
@ -0,0 +1,137 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DoubleTag implements Tag
|
||||
{
|
||||
public static final TagType<DoubleTag> TYPE = new TagType<DoubleTag>() {
|
||||
|
||||
@Override
|
||||
public DoubleTag load(DataInput input) throws IOException {
|
||||
return DoubleTag.valueOf(input.readDouble());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
input.readDouble(); // fastest way to skip it is to read but not assign
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Double";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Double";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeDouble(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_DOUBLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new DoubleTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
private double value;
|
||||
private DoubleTag(double value)
|
||||
{
|
||||
this.value=value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public static DoubleTag valueOf(double val)
|
||||
{
|
||||
return new DoubleTag(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
|
||||
}
|
124
src/main/java/dev/zontreck/ariaslib/nbt/EndTag.java
Normal file
124
src/main/java/dev/zontreck/ariaslib/nbt/EndTag.java
Normal file
|
@ -0,0 +1,124 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class EndTag implements Tag
|
||||
{
|
||||
public static final EndTag INSTANCE = new EndTag();
|
||||
public static final TagType<EndTag> TYPE = new TagType<EndTag>() {
|
||||
|
||||
@Override
|
||||
public EndTag load(DataInput input) throws IOException {
|
||||
return EndTag.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "End";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_End";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
// Nothing to write here! We are the end tag
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return Tag.TAG_END;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
return "";
|
||||
}
|
||||
|
||||
public EndTag valueOf(Object input) {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
}
|
137
src/main/java/dev/zontreck/ariaslib/nbt/FloatTag.java
Normal file
137
src/main/java/dev/zontreck/ariaslib/nbt/FloatTag.java
Normal file
|
@ -0,0 +1,137 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FloatTag implements Tag
|
||||
{
|
||||
public static final TagType<FloatTag> TYPE = new TagType<FloatTag>() {
|
||||
|
||||
@Override
|
||||
public FloatTag load(DataInput input) throws IOException {
|
||||
return FloatTag.valueOf(input.readFloat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
input.readFloat(); // fastest way to skip it is to read but not assign
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Float";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Float";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeFloat(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_FLOAT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new FloatTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
return String.valueOf(value) + "f";
|
||||
}
|
||||
|
||||
private Float value;
|
||||
private FloatTag(Float value)
|
||||
{
|
||||
this.value=value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public static FloatTag valueOf(Float val)
|
||||
{
|
||||
return new FloatTag(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
}
|
183
src/main/java/dev/zontreck/ariaslib/nbt/IntArrayTag.java
Normal file
183
src/main/java/dev/zontreck/ariaslib/nbt/IntArrayTag.java
Normal file
|
@ -0,0 +1,183 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class IntArrayTag implements Tag
|
||||
{
|
||||
public static final TagType<IntArrayTag> TYPE = new TagType<IntArrayTag>() {
|
||||
|
||||
@Override
|
||||
public IntArrayTag load(DataInput input) throws IOException {
|
||||
List<Integer> lst = new ArrayList<>();
|
||||
int count = input.readInt();
|
||||
while(count>0)
|
||||
{
|
||||
lst.add(input.readInt());
|
||||
count--;
|
||||
}
|
||||
|
||||
return new IntArrayTag(toArray(lst));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
load(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Int_Array";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Int_Array";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeInt(value.length);
|
||||
for (int b : value) {
|
||||
output.writeInt(b);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_INT_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new IntArrayTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
String indent = makeIndent(indents);
|
||||
String indentInside = makeIndent(indents+1);
|
||||
|
||||
String ret = "\n"+indent + "[\n";
|
||||
for (int i = 0; i< value.length; i++)
|
||||
{
|
||||
int b = value[i];
|
||||
ret += indentInside + String.valueOf(b);
|
||||
|
||||
if((i+1) != value.length)
|
||||
{
|
||||
ret += ", ";
|
||||
}
|
||||
ret += "\n";
|
||||
|
||||
}
|
||||
|
||||
ret += indent+"]";
|
||||
return ret;
|
||||
}
|
||||
|
||||
private IntArrayTag(int[] value)
|
||||
{
|
||||
this.value=value;
|
||||
}
|
||||
private static int[] toArray(List<Integer> entries)
|
||||
{
|
||||
int[] ret = new int[entries.size()];
|
||||
int cur=0;
|
||||
for(int b : entries)
|
||||
{
|
||||
ret[cur] = b;
|
||||
cur++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static IntArrayTag valueOf(int[] b)
|
||||
{
|
||||
return new IntArrayTag(b);
|
||||
}
|
||||
private int[] value;
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
|
||||
}
|
136
src/main/java/dev/zontreck/ariaslib/nbt/IntTag.java
Normal file
136
src/main/java/dev/zontreck/ariaslib/nbt/IntTag.java
Normal file
|
@ -0,0 +1,136 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class IntTag implements Tag
|
||||
{
|
||||
public static final TagType<IntTag> TYPE = new TagType<IntTag>() {
|
||||
|
||||
@Override
|
||||
public IntTag load(DataInput input) throws IOException {
|
||||
return IntTag.valueOf(input.readInt());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
input.readInt(); // fastest way to skip it is to read but not assign
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Int";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Int";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeFloat(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_INT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new IntTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
private int value;
|
||||
private IntTag(int value)
|
||||
{
|
||||
this.value=value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public static IntTag valueOf(int val)
|
||||
{
|
||||
return new IntTag(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
}
|
216
src/main/java/dev/zontreck/ariaslib/nbt/ListTag.java
Normal file
216
src/main/java/dev/zontreck/ariaslib/nbt/ListTag.java
Normal file
|
@ -0,0 +1,216 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class ListTag implements Tag
|
||||
{
|
||||
public static final TagType<ListTag> TYPE = new TagType<ListTag>() {
|
||||
|
||||
@Override
|
||||
public ListTag load(DataInput input) throws IOException {
|
||||
ListTag newTag = new ListTag();
|
||||
byte type = input.readByte();
|
||||
int count = input.readInt();
|
||||
if(type == 0)
|
||||
{
|
||||
return newTag;
|
||||
}
|
||||
else{
|
||||
// Read the entries
|
||||
TagType<?> typ = TagTypes.getType(type);
|
||||
while(count>0)
|
||||
{
|
||||
Tag entry = typ.load(input);
|
||||
newTag.add(entry);
|
||||
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
return newTag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
load(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "List";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_List";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
// Nothing to write here! We are the end tag
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return Tag.TAG_LIST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
|
||||
String indent = makeIndent(indents);
|
||||
String indentInside = makeIndent(indents+1);
|
||||
|
||||
String ret = "\n" + indent +"[\n";
|
||||
for (int i = 0; i< list.size(); i++)
|
||||
{
|
||||
Tag b = list.get(i);
|
||||
ret += indentInside+b.getAsString(indents+1);
|
||||
|
||||
if((i+1) != list.size())
|
||||
{
|
||||
ret += ", ";
|
||||
}
|
||||
ret += "\n";
|
||||
|
||||
}
|
||||
|
||||
ret +=indent+ "]";
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
// Return the values of getAsString
|
||||
return getAsString(0);
|
||||
}
|
||||
|
||||
private List<Tag> list;
|
||||
private byte type = TAG_END;
|
||||
|
||||
public ListTag()
|
||||
{
|
||||
list = new ArrayList<>();
|
||||
type = TAG_END;
|
||||
}
|
||||
|
||||
public int size()
|
||||
{
|
||||
return list.size();
|
||||
}
|
||||
|
||||
public boolean add(Tag value)
|
||||
{
|
||||
if(type == TAG_END || type == (byte)value.getId())
|
||||
{
|
||||
type = (byte)value.getId();
|
||||
list.add(value);
|
||||
|
||||
return true;
|
||||
} else return false;
|
||||
}
|
||||
|
||||
public Tag get(int num)
|
||||
{
|
||||
return list.get(num);
|
||||
}
|
||||
|
||||
public void remove(int num)
|
||||
{
|
||||
list.remove(num);
|
||||
}
|
||||
|
||||
public int indexOf(Tag tag)
|
||||
{
|
||||
return list.indexOf(tag);
|
||||
}
|
||||
|
||||
public void clear()
|
||||
{
|
||||
list= Lists.newArrayList();
|
||||
type=TAG_END;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
}
|
186
src/main/java/dev/zontreck/ariaslib/nbt/LongArrayTag.java
Normal file
186
src/main/java/dev/zontreck/ariaslib/nbt/LongArrayTag.java
Normal file
|
@ -0,0 +1,186 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LongArrayTag implements Tag
|
||||
{
|
||||
public static final TagType<LongArrayTag> TYPE = new TagType<LongArrayTag>() {
|
||||
|
||||
@Override
|
||||
public LongArrayTag load(DataInput input) throws IOException {
|
||||
List<Long> lst = new ArrayList<>();
|
||||
int count = input.readInt();
|
||||
while(count>0)
|
||||
{
|
||||
lst.add(input.readLong());
|
||||
count--;
|
||||
}
|
||||
|
||||
return new LongArrayTag(toArray(lst));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
load(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Long_Array";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Long_Array";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeInt(value.length);
|
||||
for (long b : value) {
|
||||
output.writeLong(b);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_LONG_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new LongArrayTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
|
||||
String indent = makeIndent(indents);
|
||||
String indentInside = makeIndent(indents+1);
|
||||
|
||||
String ret = "\n" + indent + "[\n";
|
||||
for (int i = 0; i< value.length; i++)
|
||||
{
|
||||
long b = value[i];
|
||||
ret += String.valueOf(b);
|
||||
|
||||
if((i+1) != value.length)
|
||||
{
|
||||
ret += ", ";
|
||||
}
|
||||
|
||||
ret += "\n";
|
||||
|
||||
}
|
||||
|
||||
ret +=indent+ "]";
|
||||
return ret;
|
||||
}
|
||||
|
||||
private LongArrayTag(long[] value)
|
||||
{
|
||||
this.value=value;
|
||||
}
|
||||
private static long[] toArray(List<Long> entries)
|
||||
{
|
||||
long[] ret = new long[entries.size()];
|
||||
int cur=0;
|
||||
for(long b : entries)
|
||||
{
|
||||
ret[cur] = b;
|
||||
cur++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static LongArrayTag valueOf(long[] b)
|
||||
{
|
||||
return new LongArrayTag(b);
|
||||
}
|
||||
private long[] value;
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
136
src/main/java/dev/zontreck/ariaslib/nbt/LongTag.java
Normal file
136
src/main/java/dev/zontreck/ariaslib/nbt/LongTag.java
Normal file
|
@ -0,0 +1,136 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class LongTag implements Tag
|
||||
{
|
||||
public static final TagType<LongTag> TYPE = new TagType<LongTag>() {
|
||||
|
||||
@Override
|
||||
public LongTag load(DataInput input) throws IOException {
|
||||
return LongTag.valueOf(input.readLong());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
input.readLong(); // fastest way to skip it is to read but not assign
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Long";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Long";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeLong(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_LONG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new LongTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
private Long value;
|
||||
private LongTag(Long value)
|
||||
{
|
||||
this.value=value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public static LongTag valueOf(Long val)
|
||||
{
|
||||
return new LongTag(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
}
|
53
src/main/java/dev/zontreck/ariaslib/nbt/NBTIO.java
Normal file
53
src/main/java/dev/zontreck/ariaslib/nbt/NBTIO.java
Normal file
|
@ -0,0 +1,53 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import dev.zontreck.ariaslib.events.EventBus;
|
||||
import dev.zontreck.ariaslib.events.NBTLoadedEvent;
|
||||
import dev.zontreck.ariaslib.events.NBTSavingEvent;
|
||||
|
||||
public class NBTIO {
|
||||
public static CompoundTag read(File file) throws FileNotFoundException, IOException
|
||||
{
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
CompoundTag tag;
|
||||
|
||||
DataInputStream dis = new DataInputStream(fis);
|
||||
// Start loading
|
||||
|
||||
// We can only be a compound tag
|
||||
dis.skipBytes(1); // Skip the compound tag type bit
|
||||
tag = (CompoundTag) TagTypes.Compound.type.load(dis);
|
||||
|
||||
EventBus.BUS.post(new NBTLoadedEvent(tag, file.getName()));
|
||||
|
||||
// Complete
|
||||
return tag;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void write(File file, CompoundTag tag) throws IOException
|
||||
{
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
DataOutputStream dos = new DataOutputStream(fos);
|
||||
|
||||
NBTSavingEvent event = new NBTSavingEvent(tag, file.getName());
|
||||
if(EventBus.BUS.post(event)){
|
||||
tag=event.tag;
|
||||
}
|
||||
|
||||
// Write a unnamed tag
|
||||
dos.writeByte(Tag.TAG_COMPOUND);
|
||||
tag.write(dos);
|
||||
|
||||
dos.flush();
|
||||
fos.close();
|
||||
}
|
||||
}
|
137
src/main/java/dev/zontreck/ariaslib/nbt/ShortTag.java
Normal file
137
src/main/java/dev/zontreck/ariaslib/nbt/ShortTag.java
Normal file
|
@ -0,0 +1,137 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ShortTag implements Tag
|
||||
{
|
||||
public static final TagType<ShortTag> TYPE = new TagType<ShortTag>() {
|
||||
|
||||
@Override
|
||||
public ShortTag load(DataInput input) throws IOException {
|
||||
return ShortTag.valueOf(input.readShort());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
input.readShort(); // fastest way to skip it is to read but not assign
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Short";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_Short";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeFloat(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_SHORT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new ShortTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
private short value;
|
||||
private ShortTag(short value)
|
||||
{
|
||||
this.value=value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public static ShortTag valueOf(short val)
|
||||
{
|
||||
return new ShortTag(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asString'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
|
||||
}
|
138
src/main/java/dev/zontreck/ariaslib/nbt/StringTag.java
Normal file
138
src/main/java/dev/zontreck/ariaslib/nbt/StringTag.java
Normal file
|
@ -0,0 +1,138 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class StringTag implements Tag{
|
||||
|
||||
public static final TagType<StringTag> TYPE = new TagType<StringTag>() {
|
||||
@Override
|
||||
public StringTag load(DataInput input) throws IOException {
|
||||
return StringTag.valueOf(input.readUTF());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
input.readUTF();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "String";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_String";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeUTF(toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return TAG_STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType<?> getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag copy() {
|
||||
return new StringTag(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString(int indents) {
|
||||
return "\""+value+"\"";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public static StringTag valueOf(String input) {
|
||||
if(input instanceof String)
|
||||
{
|
||||
return new StringTag(input);
|
||||
} else return null;
|
||||
}
|
||||
|
||||
private StringTag(String input)
|
||||
{
|
||||
this.value=input;
|
||||
}
|
||||
private String value;
|
||||
|
||||
@Override
|
||||
public Byte asByte() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByte'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float asFloat() {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asFloat'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer asInt() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asInt'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short asShort() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asShort'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long asLong() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLong'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double asDouble() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asDouble'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] asByteArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asByteArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] asIntArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asIntArray'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asLongArray() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
|
||||
}
|
||||
|
||||
}
|
68
src/main/java/dev/zontreck/ariaslib/nbt/Tag.java
Normal file
68
src/main/java/dev/zontreck/ariaslib/nbt/Tag.java
Normal file
|
@ -0,0 +1,68 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Tag {
|
||||
/*
|
||||
* The type values were taken from Minecraft's documented format.
|
||||
*/
|
||||
|
||||
byte TAG_END = 0;
|
||||
byte TAG_BYTE = 1;
|
||||
byte TAG_SHORT = 2;
|
||||
byte TAG_INT = 3;
|
||||
byte TAG_LONG = 4;
|
||||
byte TAG_FLOAT = 5;
|
||||
byte TAG_DOUBLE = 6;
|
||||
byte TAG_BYTE_ARRAY = 7;
|
||||
byte TAG_STRING = 8;
|
||||
byte TAG_LIST = 9;
|
||||
byte TAG_COMPOUND = 10;
|
||||
byte TAG_INT_ARRAY = 11;
|
||||
byte TAG_LONG_ARRAY = 12;
|
||||
int OBJECT_HEADER = 64;
|
||||
int ARRAY_HEADER = 96;
|
||||
int OBJECT_REFERENCE = 32;
|
||||
int STRING_SIZE = 224;
|
||||
byte TAG_ANY_NUMERIC = 99;
|
||||
int MAX_DEPTH = 512;
|
||||
// End of direct minecraft values
|
||||
|
||||
|
||||
int TAG_INVALID = 1024;
|
||||
|
||||
default String makeIndent(int num)
|
||||
{
|
||||
String ret = "";
|
||||
int id = num;
|
||||
|
||||
while(id>0)
|
||||
{
|
||||
ret += " ";
|
||||
id--;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void write(DataOutput output) throws IOException;
|
||||
int getId();
|
||||
TagType<?> getType();
|
||||
Tag copy();
|
||||
String getAsString(int indents);
|
||||
|
||||
|
||||
// Value specific functions
|
||||
Byte asByte();
|
||||
Float asFloat();
|
||||
String asString();
|
||||
Integer asInt();
|
||||
Short asShort();
|
||||
Long asLong();
|
||||
Double asDouble();
|
||||
byte[] asByteArray();
|
||||
int[] asIntArray();
|
||||
long[] asLongArray();
|
||||
|
||||
}
|
48
src/main/java/dev/zontreck/ariaslib/nbt/TagType.java
Normal file
48
src/main/java/dev/zontreck/ariaslib/nbt/TagType.java
Normal file
|
@ -0,0 +1,48 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface TagType <T extends Tag>
|
||||
{
|
||||
T load(DataInput input) throws IOException;
|
||||
|
||||
void skip(DataInput input) throws IOException;
|
||||
|
||||
boolean hasValue();
|
||||
|
||||
String getName();
|
||||
|
||||
String getPrettyName();
|
||||
|
||||
static TagType<EndTag> invalid(int num)
|
||||
{
|
||||
return new TagType<EndTag>() {
|
||||
|
||||
@Override
|
||||
public EndTag load(DataInput input) throws IOException {
|
||||
throw new IOException("Invalid operation, this tag is invalid");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skip(DataInput input) throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "INVALID";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyName() {
|
||||
return "TAG_INVALID";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasValue() {
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
}
|
39
src/main/java/dev/zontreck/ariaslib/nbt/TagTypes.java
Normal file
39
src/main/java/dev/zontreck/ariaslib/nbt/TagTypes.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
package dev.zontreck.ariaslib.nbt;
|
||||
|
||||
public enum TagTypes {
|
||||
End(EndTag.TYPE), // 0
|
||||
Byte(ByteTag.TYPE), // 1
|
||||
Short(ShortTag.TYPE), // 2
|
||||
Int(IntTag.TYPE), // 3
|
||||
Long(LongTag.TYPE), // 4
|
||||
Float(FloatTag.TYPE), // 5
|
||||
Double(DoubleTag.TYPE), // 6
|
||||
ByteArray(ByteArrayTag.TYPE), // 7
|
||||
String(StringTag.TYPE), // 8
|
||||
List(ListTag.TYPE), // 9
|
||||
Compound(CompoundTag.TYPE), // 10
|
||||
IntArray(IntArrayTag.TYPE), // 11
|
||||
LongArray(LongArrayTag.TYPE); // 12
|
||||
|
||||
//public static final TagType<?>[] TYPES = new TagType[]{EndTag.TYPE, ByteTag.TYPE};
|
||||
|
||||
public TagType<?> type;
|
||||
/**
|
||||
* Locates the type in this enum
|
||||
* @param value
|
||||
* @return A TagType for the requested value
|
||||
*/
|
||||
public static TagType<?> getType(int value)
|
||||
{
|
||||
if(value>values().length)
|
||||
{
|
||||
return End.type;
|
||||
}else {
|
||||
return values()[value].type;
|
||||
}
|
||||
}
|
||||
TagTypes(TagType<?> type)
|
||||
{
|
||||
this.type=type;
|
||||
}
|
||||
}
|
19
src/main/java/dev/zontreck/ariaslib/nbt/readme.md
Normal file
19
src/main/java/dev/zontreck/ariaslib/nbt/readme.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
LibAC NBT
|
||||
====
|
||||
|
||||
This was heavily inspired by the NBT File format used by minecraft for the ability to save things reliably at a compact size.
|
||||
|
||||
Disclaimer
|
||||
====
|
||||
|
||||
The code used here is of my own creation, as i tried to model it after the NBT format in use in minecraft, due to me wanting a format that i was familiar with using the API of while writing mods.
|
||||
|
||||
However, i made the decision to make it not binary compatible with Minecraft. I am sure with some tweaks it would be, but my decision is final as this format will be expanded upon in the future to deviate from the standard minecraft NBT.
|
||||
|
||||
|
||||
Planned Changes
|
||||
=========
|
||||
|
||||
I plan to add more data types, including a actual List implementation.
|
||||
|
||||
Another change that is planned, is optimizing this a little bit more, and adding other tag types.
|
Loading…
Add table
Add a link
Reference in a new issue