This commit is contained in:
parent
f9e5a04155
commit
6774b6f8aa
|
@ -3,7 +3,7 @@ plugins {
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'io.github.p2vman'
|
group = 'io.github.p2vman'
|
||||||
version = '1.0'
|
version = '1.2'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
@ -11,6 +11,8 @@ repositories {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation "com.google.code.gson:gson:2.8.9"
|
implementation "com.google.code.gson:gson:2.8.9"
|
||||||
|
annotationProcessor 'org.projectlombok:lombok:1.18.30'
|
||||||
|
compileOnly 'org.projectlombok:lombok:1.18.30'
|
||||||
}
|
}
|
||||||
|
|
||||||
task createJar(type: Jar) {
|
task createJar(type: Jar) {
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package io.github.p2vman.nbt;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.tag.*;
|
||||||
|
import io.github.p2vman.utils.Pair;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class NbtIo {
|
||||||
|
private Map<Character, Class<? extends Tag>> idclass;
|
||||||
|
|
||||||
|
public NbtIo(Map<Character, Class<? extends Tag>> idclass) {
|
||||||
|
this.idclass = idclass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Serializer getSerializer() {
|
||||||
|
return new Serializer(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NbtIo() {
|
||||||
|
this.idclass = new HashMap<>();
|
||||||
|
for (Tag tag : new Tag[] {
|
||||||
|
new TagEnd(),
|
||||||
|
new TagFloat(),
|
||||||
|
new TagInt(),
|
||||||
|
new TagCompound(),
|
||||||
|
new TagIntArray(),
|
||||||
|
new TagList(),
|
||||||
|
new TagByte(),
|
||||||
|
new TagByteArray(),
|
||||||
|
new TagDouble(),
|
||||||
|
new TagLong(),
|
||||||
|
new TagLongArray(),
|
||||||
|
new TagShort(),
|
||||||
|
new TagString()
|
||||||
|
}) {
|
||||||
|
idclass.put(tag.getID(), tag.getClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pair<String, Tag> read(DataInputStream stream) throws IOException {
|
||||||
|
TagReader reader = new TagReader(this.idclass);
|
||||||
|
return reader.read(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(DataOutputStream stream, Tag tag) throws IOException {
|
||||||
|
TagWriter writer = new TagWriter(this.idclass);
|
||||||
|
writer.write(stream, tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TagCompound readCompound(DataInputStream stream) throws IOException {
|
||||||
|
TagReader reader = new TagReader(this.idclass);
|
||||||
|
Tag tag = reader.read(stream).v;
|
||||||
|
return tag instanceof TagCompound ? (TagCompound) tag : new TagCompound();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NbtIo create(Tag... tags) {
|
||||||
|
return new NbtIo();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package io.github.p2vman.nbt;
|
||||||
|
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.tag.*;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
public class Serializer {
|
||||||
|
private NbtIo io;
|
||||||
|
public Tag get(Field field, Object obj) throws IllegalArgumentException, IllegalAccessException {
|
||||||
|
Object fobj = field.get(obj);
|
||||||
|
if (fobj instanceof Integer) {
|
||||||
|
return new TagInt((Integer) fobj);
|
||||||
|
} else if (fobj instanceof Byte) {
|
||||||
|
return new TagByte((Byte) fobj);
|
||||||
|
} else if (fobj instanceof Short) {
|
||||||
|
return new TagShort((Short) fobj);
|
||||||
|
} else if (fobj instanceof String) {
|
||||||
|
return new TagString((String) fobj);
|
||||||
|
} else if (fobj instanceof Long) {
|
||||||
|
return new TagLong((Long) fobj);
|
||||||
|
} else {
|
||||||
|
return new TagEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TagCompound objr(Object obj) throws IllegalArgumentException, IllegalAccessException {
|
||||||
|
TagCompound compound = new TagCompound();
|
||||||
|
Class<?> cls = obj.getClass();
|
||||||
|
compound.put("!", new TagString(cls.getTypeName()));
|
||||||
|
|
||||||
|
for (Field field : cls.getFields()) {
|
||||||
|
Tag tag = get(field, obj);
|
||||||
|
if (tag instanceof TagEnd) {
|
||||||
|
compound.put(field.getName(), objr(field.get(obj)));
|
||||||
|
} else {
|
||||||
|
compound.put(field.getName(), tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return compound;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Serializer(NbtIo io) {
|
||||||
|
this.io = io;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(DataOutputStream stream, Object obj) throws IOException, IllegalArgumentException, IllegalAccessException {
|
||||||
|
io.write(stream, objr(obj));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package io.github.p2vman.nbt;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.tag.Tag;
|
||||||
|
import io.github.p2vman.nbt.tag.TagEnd;
|
||||||
|
import io.github.p2vman.utils.Pair;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class TagReader {
|
||||||
|
public final Map<Character, Class<? extends Tag>> idclass;
|
||||||
|
public TagReader(Map<Character, Class<? extends Tag>> idclass) {
|
||||||
|
this.idclass = idclass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pair<String, Tag> read(DataInputStream stream) throws IOException {
|
||||||
|
char type = (char) stream.readByte();
|
||||||
|
if (type == 0) {
|
||||||
|
return new Pair<>("", new TagEnd());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Map.Entry<Character, Class<? extends Tag>> entry : idclass.entrySet()) {
|
||||||
|
if (entry.getKey() == type) {
|
||||||
|
try {
|
||||||
|
Pair<String, Tag> pair = new Pair<>(stream.readUTF(), entry.getValue().getDeclaredConstructor().newInstance());
|
||||||
|
pair.v.read(stream, this);
|
||||||
|
return pair;
|
||||||
|
} catch (java.lang.InstantiationException | java.lang.IllegalAccessException | java.lang.NoSuchMethodException | java.lang.reflect.InvocationTargetException e) {
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Pair<>("", new TagEnd());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package io.github.p2vman.nbt;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.tag.Tag;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class TagWriter {
|
||||||
|
public final Map<Character, Class<? extends Tag>> idclass;
|
||||||
|
public TagWriter(Map<Character, Class<? extends Tag>> idclass) {
|
||||||
|
this.idclass = idclass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(DataOutputStream stream, Tag tag) throws IOException {
|
||||||
|
write(stream, tag, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(DataOutputStream stream, Tag tag, String name) throws IOException {
|
||||||
|
for (Map.Entry<Character, Class<? extends Tag>> entry : idclass.entrySet()) {
|
||||||
|
if (entry.getValue() == tag.getClass()) {
|
||||||
|
stream.write(tag.getID());
|
||||||
|
stream.writeUTF(name);
|
||||||
|
tag.write(stream, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public abstract class Tag {
|
||||||
|
public static final char TagByte = 1;
|
||||||
|
public static final char TagShort = 2;
|
||||||
|
public static final char TagInt = 3;
|
||||||
|
public static final char TagLong = 4;
|
||||||
|
public static final char TagFloat = 5;
|
||||||
|
public static final char TagDouble = 6;
|
||||||
|
public static final char TagByteArray = 7;
|
||||||
|
public static final char TagString = 8;
|
||||||
|
public static final char TagList = 9;
|
||||||
|
public static final char TagCompound = 10;
|
||||||
|
public static final char TagIntArray = 11;
|
||||||
|
public static final char TagLongArray = 12;
|
||||||
|
public abstract char getID();
|
||||||
|
|
||||||
|
|
||||||
|
public abstract void write(DataOutputStream stream, TagWriter writer) throws IOException;
|
||||||
|
public abstract void read(DataInputStream stream, TagReader reader) throws IOException;
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagByte extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private byte value;
|
||||||
|
public TagByte(byte value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
byte[] buffer = new byte[1];
|
||||||
|
stream.read(buffer);
|
||||||
|
value = buffer[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.write(value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagByteArray extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private byte[] value;
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
if (value != null) return value.length;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
public TagByteArray(byte[] value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
byte[] buffer = new byte[4];
|
||||||
|
stream.read(buffer);
|
||||||
|
value = new byte[ByteBuffer.wrap(buffer).getInt()];
|
||||||
|
stream.read(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.write(ByteBuffer.allocate(Integer.BYTES).putInt(value.length).array());
|
||||||
|
stream.write(value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import io.github.p2vman.utils.Pair;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagCompound extends Tag {
|
||||||
|
private Map<String, Tag> tagMap = new HashMap<>();
|
||||||
|
public void put(String name, Tag tag) {
|
||||||
|
tagMap.put(name, tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tag get(String name) {
|
||||||
|
return tagMap.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean is(String name) {
|
||||||
|
return tagMap.containsKey(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
for (Map.Entry<String, Tag> entry : tagMap.entrySet()) {
|
||||||
|
writer.write(stream, entry.getValue(), entry.getKey());
|
||||||
|
}
|
||||||
|
stream.writeByte(0x00);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
this.tagMap.clear();
|
||||||
|
while (true) {
|
||||||
|
Pair<String, Tag> tagPair = reader.read(stream);
|
||||||
|
if (tagPair.getV().getID() == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tagMap.put(tagPair.k, tagPair.v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagDouble extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private double value;
|
||||||
|
public TagDouble(double value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
byte[] buffer = new byte[Double.BYTES];
|
||||||
|
stream.read(buffer);
|
||||||
|
value = ByteBuffer.wrap(buffer).getDouble();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.write(ByteBuffer.allocate(Double.BYTES).putDouble(value).array());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagEnd extends Tag {
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.write(0x00);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagFloat extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private float value;
|
||||||
|
public TagFloat(float value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
byte[] buffer = new byte[Float.BYTES];
|
||||||
|
stream.read(buffer);
|
||||||
|
value = ByteBuffer.wrap(buffer).getFloat();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.write(ByteBuffer.allocate(Float.BYTES).putFloat(value).array());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagInt extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private int value;
|
||||||
|
public TagInt(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
byte[] buffer = new byte[Integer.BYTES];
|
||||||
|
stream.read(buffer);
|
||||||
|
value = ByteBuffer.wrap(buffer).getInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.write(ByteBuffer.allocate(Integer.BYTES).putInt(value).array());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagIntArray extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private int[] value;
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
if (value != null) return value.length;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
public TagIntArray(int[] value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 11;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
byte[] buffer = new byte[4];
|
||||||
|
stream.read(buffer);
|
||||||
|
value = new int[ByteBuffer.wrap(buffer).getInt()];
|
||||||
|
for (int i = 0; i < value.length; i++) {
|
||||||
|
stream.read(buffer);
|
||||||
|
value[i] = ByteBuffer.wrap(buffer).getInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.write(ByteBuffer.allocate(Integer.BYTES).putInt(value.length).array());
|
||||||
|
for (int i = 0; i < value.length; i++) {
|
||||||
|
stream.write(ByteBuffer.allocate(Integer.BYTES).putInt(value[i]).array());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagList extends Tag implements Iterable<Tag> {
|
||||||
|
@Getter
|
||||||
|
private char type;
|
||||||
|
private List<Tag> values = new ArrayList<>(0);
|
||||||
|
public int size() {
|
||||||
|
return values.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean add(Tag tag) {
|
||||||
|
return values.add(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tag remove(int index) {
|
||||||
|
return values.remove(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean remove(Tag tag) {
|
||||||
|
return values.remove(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TagList(char type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.writeByte(type);
|
||||||
|
stream.writeInt(values.size());
|
||||||
|
for (Tag tag : values) {
|
||||||
|
tag.write(stream, writer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
this.values.clear();
|
||||||
|
this.type = (char) stream.readByte();
|
||||||
|
int s = stream.readInt();
|
||||||
|
Class<? extends Tag> cls = reader.idclass.get(type);
|
||||||
|
for (int i = 0; i < s; i++) {
|
||||||
|
try {
|
||||||
|
Tag tag = cls.getDeclaredConstructor().newInstance();
|
||||||
|
tag.read(stream, reader);
|
||||||
|
values.add(tag);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<Tag> iterator() {
|
||||||
|
return values.iterator();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagLong extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private long value;
|
||||||
|
public TagLong(long value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
value = stream.readLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.writeLong(value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagLongArray extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private long[] value;
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
if (value != null) return value.length;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
public TagLongArray(long[] value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
value = new long[stream.readInt()];
|
||||||
|
for (int i = 0; i < value.length; i++) {
|
||||||
|
value[i] = stream.readLong();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.writeInt(value.length);
|
||||||
|
for (int i = 0; i < value.length; i++) {
|
||||||
|
stream.writeLong(value[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagShort extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private short value;
|
||||||
|
public TagShort(short value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
value = stream.readShort();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.writeShort(value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package io.github.p2vman.nbt.tag;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.TagWriter;
|
||||||
|
import io.github.p2vman.nbt.TagReader;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class TagString extends Tag {
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private String value;
|
||||||
|
public TagString(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getID() {
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(DataInputStream stream, TagReader reader) throws IOException {
|
||||||
|
value = stream.readUTF();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(DataOutputStream stream, TagWriter writer) throws IOException {
|
||||||
|
stream.writeUTF(value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package io.github.p2vman.utils;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@ToString
|
||||||
|
public class Pair<K, V> {
|
||||||
|
public K k;
|
||||||
|
public V v;
|
||||||
|
}
|
|
@ -6,7 +6,9 @@ import io.github.p2vman.Static;
|
||||||
import io.github.p2vman.Utils;
|
import io.github.p2vman.Utils;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package org.eptalist;
|
||||||
|
|
||||||
|
public class Constants {
|
||||||
|
public static final int bstats_id = 24527;
|
||||||
|
}
|
|
@ -0,0 +1,157 @@
|
||||||
|
package org.eptalist.storge;
|
||||||
|
|
||||||
|
import io.github.p2vman.nbt.NbtIo;
|
||||||
|
import io.github.p2vman.nbt.tag.Tag;
|
||||||
|
import io.github.p2vman.nbt.tag.TagCompound;
|
||||||
|
import io.github.p2vman.nbt.tag.TagList;
|
||||||
|
import io.github.p2vman.nbt.tag.TagString;
|
||||||
|
import io.github.p2vman.utils.Pair;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
|
public class NBT extends ArrayList<String> implements Data<String> {
|
||||||
|
private final NbtIo io = new NbtIo();
|
||||||
|
public void close() throws IOException {
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addUser(String name) {
|
||||||
|
if (is(name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return add(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean is(String name) {
|
||||||
|
return contains(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeUser(String name) {
|
||||||
|
if (!is(name)) {
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> data;
|
||||||
|
|
||||||
|
public NBT(Map<String, Object> data) {
|
||||||
|
this.data = data;
|
||||||
|
if (!(size()>0)) {
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load() {
|
||||||
|
clear();
|
||||||
|
|
||||||
|
try {
|
||||||
|
InputStream stream = new FileInputStream((String) this.data.get("file"));
|
||||||
|
|
||||||
|
if (this.data.containsKey("gzip") && (Boolean) this.data.get("gzip")) {
|
||||||
|
stream = new GZIPInputStream(stream);
|
||||||
|
}
|
||||||
|
DataInputStream datastream = new DataInputStream(stream);
|
||||||
|
|
||||||
|
Pair<String, Tag> tag = io.read(datastream);
|
||||||
|
if (tag.v instanceof TagCompound compound) {
|
||||||
|
if (compound.is("whitelist") && (compound.get("whitelist") instanceof TagList list)) {
|
||||||
|
Iterator<Tag> it = list.iterator();
|
||||||
|
while (it.hasNext()) if (it.next() instanceof TagString palyer) {
|
||||||
|
add(palyer.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
datastream.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save() {
|
||||||
|
try {
|
||||||
|
OutputStream stream = new FileOutputStream((String) this.data.get("file"));
|
||||||
|
|
||||||
|
if (this.data.containsKey("gzip") && (Boolean) this.data.get("gzip")) {
|
||||||
|
stream = new GZIPOutputStream(stream);
|
||||||
|
}
|
||||||
|
DataOutputStream datastream = new DataOutputStream(stream);
|
||||||
|
|
||||||
|
TagCompound compound = new TagCompound();
|
||||||
|
{
|
||||||
|
TagList list = new TagList(Tag.TagString);
|
||||||
|
for (String s : this) list.add(new TagString(s));
|
||||||
|
compound.put("whitelist", list);
|
||||||
|
}
|
||||||
|
io.write(datastream, compound);
|
||||||
|
datastream.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean add(String s) {
|
||||||
|
if (super.add(s)) {
|
||||||
|
save();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remove(Object o) {
|
||||||
|
if (super.remove(o)) {
|
||||||
|
save();
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sort(Comparator<? super String> c) {
|
||||||
|
super.sort(c);
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> toList() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean is(String name, List<String> info) {
|
||||||
|
return contains(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeUser(String name, List<String> info) {
|
||||||
|
if (!is(name)) {
|
||||||
|
info.add("&r" + name + "is not in the whitelist");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addUser(String name, List<String> info) {
|
||||||
|
if (is(name)) {
|
||||||
|
info.add("&r" + name + "is already on the whitelist");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return add(name);
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,12 +10,12 @@ allprojects {
|
||||||
|
|
||||||
task mergeJars(type: Jar) {
|
task mergeJars(type: Jar) {
|
||||||
archiveBaseName = 'EptaList'
|
archiveBaseName = 'EptaList'
|
||||||
archiveVersion = '1.1-release'
|
archiveVersion = '1.2'
|
||||||
|
|
||||||
def jarPaths = [
|
def jarPaths = [
|
||||||
"C:\\Users\\User\\IdeaProjects\\EptaListProject\\velocity\\build\\libs\\velocity-1.0.jar",
|
"C:\\Users\\User\\IdeaProjects\\EptaListProject\\velocity\\build\\libs\\velocity-"+'1.2'+".jar",
|
||||||
"C:\\Users\\User\\IdeaProjects\\EptaListProject\\spigot\\build\\libs\\spigot-1.0.jar",
|
"C:\\Users\\User\\IdeaProjects\\EptaListProject\\spigot\\build\\libs\\spigot-"+'1.2'+".jar",
|
||||||
"C:\\Users\\User\\IdeaProjects\\EptaListProject\\base\\build\\libs\\base-1.0.jar"
|
"C:\\Users\\User\\IdeaProjects\\EptaListProject\\base\\build\\libs\\base-"+'1.2'+".jar"
|
||||||
]
|
]
|
||||||
|
|
||||||
from jarPaths.collect { zipTree(it) }
|
from jarPaths.collect { zipTree(it) }
|
||||||
|
|
|
@ -3,7 +3,7 @@ plugins {
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'org.eg4rerer'
|
group = 'org.eg4rerer'
|
||||||
version = "1.0"
|
version = "1.2"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|
|
@ -5,6 +5,7 @@ import io.github.p2vman.profiling.ExempleProfiler;
|
||||||
import io.github.p2vman.profiling.Profiler;
|
import io.github.p2vman.profiling.Profiler;
|
||||||
import org.eptalist.Config;
|
import org.eptalist.Config;
|
||||||
import io.github.p2vman.Identifier;
|
import io.github.p2vman.Identifier;
|
||||||
|
import org.eptalist.Constants;
|
||||||
import org.eptalist.spigot.metrics.Metrics;
|
import org.eptalist.spigot.metrics.Metrics;
|
||||||
import org.eptalist.storge.Data;
|
import org.eptalist.storge.Data;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
@ -57,7 +58,7 @@ public final class EptaList extends JavaPlugin {
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
profiler.push("init");
|
profiler.push("init");
|
||||||
metrics = new Metrics(this, 24527);
|
metrics = new Metrics(this, Constants.bstats_id);
|
||||||
|
|
||||||
File data = getDataFolder();
|
File data = getDataFolder();
|
||||||
if (!data.exists()) {
|
if (!data.exists()) {
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
name: EptaList
|
name: EptaList
|
||||||
version: "1.1-release"
|
version: "1.2"
|
||||||
main: org.eptalist.spigot.EptaList
|
main: org.eptalist.spigot.EptaList
|
||||||
|
|
|
@ -5,7 +5,7 @@ plugins {
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'org.eg4rerer'
|
group = 'org.eg4rerer'
|
||||||
version = "1.1-release"
|
version = "1.2"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|
|
@ -15,6 +15,7 @@ import io.github.p2vman.profiling.ExempleProfiler;
|
||||||
import io.github.p2vman.profiling.Profiler;
|
import io.github.p2vman.profiling.Profiler;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.eptalist.Config;
|
import org.eptalist.Config;
|
||||||
|
import org.eptalist.Constants;
|
||||||
import org.eptalist.storge.Data;
|
import org.eptalist.storge.Data;
|
||||||
import org.eptalist.velocity.metrics.Metrics;
|
import org.eptalist.velocity.metrics.Metrics;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -80,7 +81,7 @@ public class Velocity {
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onProxyInitialization(ProxyInitializeEvent event) {
|
public void onProxyInitialization(ProxyInitializeEvent event) {
|
||||||
Metrics metrics = metricsFactory.make(this, 24527);
|
Metrics metrics = metricsFactory.make(this, Constants.bstats_id);
|
||||||
|
|
||||||
metrics.addCustomChart(new Metrics.SimplePie("data_type", () -> mode.storage));
|
metrics.addCustomChart(new Metrics.SimplePie("data_type", () -> mode.storage));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue