Make some changes to the file format

This commit is contained in:
Aria 2023-03-14 15:07:36 -07:00
parent 32f79fcc65
commit 079bff1d33
15 changed files with 178 additions and 14 deletions

View file

@ -13,6 +13,7 @@ public class ByteArrayTag implements Tag
@Override
public ByteArrayTag load(DataInput input) throws IOException {
List<Byte> lst = new ArrayList<>();
int count = input.readInt();
while(count>0)
@ -183,5 +184,16 @@ public class ByteArrayTag implements Tag
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -138,5 +138,15 @@ public class ByteTag implements Tag
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -26,6 +26,10 @@ public class CompoundTag implements Tag
TagType<?> v = TagTypes.getType(type);
String tagName = input.readUTF();
Tag finalTag = v.load(input);
if(finalTag instanceof CompoundTag){
((CompoundTag)finalTag).setName(tagName);
}
finalTag.setParent(newTag);
newTag.put(tagName, finalTag);
}
@ -56,6 +60,10 @@ public class CompoundTag implements Tag
@Override
public void write(DataOutput output) throws IOException {
if(parent==null)
{
output.writeUTF(tagName);
}
for (Map.Entry<String,Tag> it : list.entrySet()) {
// We're saving everything now
output.writeByte(it.getValue().getId());
@ -65,6 +73,12 @@ public class CompoundTag implements Tag
output.writeByte(TAG_END);
}
public String tagName = "";
public void setName(String name)
{
tagName = name;
}
@Override
public int getId() {
return Tag.TAG_COMPOUND;
@ -86,12 +100,15 @@ public class CompoundTag implements Tag
String indent = makeIndent(indents);
String indentInside = makeIndent(indents+1);
String ret = "\n"+indent+"{\n";
String ret = "";
ret = "\n"+indent+getType().getPrettyName()+" ["+tagName+"] {\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()+"]: ";
if(!(entry.getValue() instanceof CompoundTag))
ret += indentInside+entry.getValue().getType().getPrettyName() + " ["+entry.getKey()+"]: ";
ret += entry.getValue().getAsString(indents+1);
if(entries.hasNext())
@ -126,12 +143,13 @@ public class CompoundTag implements Tag
public void put(String name, Tag entry)
{
entry.setParent(this);
list.put(name, entry);
}
public void putString(String name, String value)
{
list.put(name, StringTag.valueOf(value));
put(name, StringTag.valueOf(value));
}
public String getString(String name)
@ -140,7 +158,7 @@ public class CompoundTag implements Tag
}
public void putByte(String name, byte value)
{
list.put(name, ByteTag.valueOf(value));
put(name, ByteTag.valueOf(value));
}
public byte getByte(String name)
@ -149,7 +167,7 @@ public class CompoundTag implements Tag
}
public void putDouble(String name, double value)
{
list.put(name, DoubleTag.valueOf(value));
put(name, DoubleTag.valueOf(value));
}
public double getDouble(String name)
@ -158,7 +176,7 @@ public class CompoundTag implements Tag
}
public void putFloat(String name, float value)
{
list.put(name, FloatTag.valueOf(value));
put(name, FloatTag.valueOf(value));
}
public float getFloat(String name)
@ -167,7 +185,7 @@ public class CompoundTag implements Tag
}
public void putInt(String name, int value)
{
list.put(name, IntTag.valueOf(value));
put(name, IntTag.valueOf(value));
}
public int getInt(String name)
@ -181,7 +199,7 @@ public class CompoundTag implements Tag
}
public void putLong(String name, long value)
{
list.put(name, LongTag.valueOf(value));
put(name, LongTag.valueOf(value));
}
public long getLong(String name)
@ -190,7 +208,7 @@ public class CompoundTag implements Tag
}
public void putShort(String name, short value)
{
list.put(name, ShortTag.valueOf(value));
put(name, ShortTag.valueOf(value));
}
public short getShort(String name)
@ -199,7 +217,7 @@ public class CompoundTag implements Tag
}
public void putByteArray(String name, byte[] value)
{
list.put(name, ByteArrayTag.valueOf(value));
put(name, ByteArrayTag.valueOf(value));
}
public ByteArrayTag getByteArray(String name)
@ -208,7 +226,7 @@ public class CompoundTag implements Tag
}
public void putIntArray(String name, int[] value)
{
list.put(name, IntArrayTag.valueOf(value));
put(name, IntArrayTag.valueOf(value));
}
public IntArrayTag getIntArray(String name)
@ -217,7 +235,7 @@ public class CompoundTag implements Tag
}
public void putLongArray(String name, long[] value)
{
list.put(name, LongArrayTag.valueOf(value));
put(name, LongArrayTag.valueOf(value));
}
public LongArrayTag getLongArray(String name)
@ -226,7 +244,7 @@ public class CompoundTag implements Tag
}
public void putCompound(String name, CompoundTag value)
{
list.put(name, value);
put(name, value);
}
public CompoundTag getCompound(String name)
@ -302,4 +320,14 @@ public class CompoundTag implements Tag
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
}
public Tag parent=null;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -134,4 +134,14 @@ public class DoubleTag implements Tag
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -121,4 +121,14 @@ public class EndTag implements Tag
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -134,4 +134,14 @@ public class FloatTag implements Tag
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -180,4 +180,14 @@ public class IntArrayTag implements Tag
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -133,4 +133,14 @@ public class IntTag implements Tag
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -28,6 +28,7 @@ public class ListTag implements Tag
while(count>0)
{
Tag entry = typ.load(input);
entry.setParent(newTag);
newTag.add(entry);
count--;
@ -61,7 +62,9 @@ public class ListTag implements Tag
@Override
public void write(DataOutput output) throws IOException {
// Nothing to write here! We are the end tag
for (Tag tag : list) {
tag.write(output);
}
}
@Override
@ -129,6 +132,7 @@ public class ListTag implements Tag
if(type == TAG_END || type == (byte)value.getId())
{
type = (byte)value.getId();
value.setParent(this);
list.add(value);
return true;
@ -213,4 +217,14 @@ public class ListTag implements Tag
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -182,5 +182,15 @@ public class LongArrayTag implements Tag
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -133,4 +133,14 @@ public class LongTag implements Tag
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -26,7 +26,9 @@ public class NBTIO {
// We can only be a compound tag
dis.skipBytes(1); // Skip the compound tag type bit
String name = dis.readUTF();
tag = (CompoundTag) TagTypes.Compound.type.load(dis);
tag.setName(name);
EventBus.BUS.post(new NBTLoadedEvent(tag, file.getName()));

View file

@ -134,4 +134,14 @@ public class ShortTag implements Tag
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -135,4 +135,14 @@ public class StringTag implements Tag{
throw new UnsupportedOperationException("Unimplemented method 'asLongArray'");
}
public Tag parent;
@Override
public void setParent(Tag parent) {
this.parent=parent;
}
@Override
public Tag getParent() {
return parent;
}
}

View file

@ -31,6 +31,14 @@ public interface Tag {
int TAG_INVALID = 1024;
void setParent(Tag parent);
Tag getParent();
default boolean canHaveName(){
if(getParent() instanceof ListTag) return false;
else return true;
}
default String makeIndent(int num)
{