Skip to content

Commit

Permalink
Merge 066084b into 947c750
Browse files Browse the repository at this point in the history
  • Loading branch information
Querz committed Dec 12, 2018
2 parents 947c750 + 066084b commit dc0fbc9
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 127 deletions.
140 changes: 54 additions & 86 deletions src/main/java/net/querz/nbt/ListTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,35 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

public class ListTag<T extends Tag> extends Tag<List<T>> implements Iterable<T> {

private byte typeID = 0;
private Class<? extends Tag> typeClass = EndTag.class;

public ListTag() {}
protected ListTag() {}

public ListTag(Class<T> typeClass) {
this.typeClass = typeClass;
}

@Override
public byte getID() {
return 9;
}

public byte getTypeID() {
return typeID;
return size() == 0 ? 0 : typeID;
}

public Class<? extends Tag> getTypeClass() {
return typeClass;
return size() == 0 ? EndTag.class : typeClass;
}

@Override
Expand All @@ -36,14 +44,11 @@ public int size() {
}

public T remove(int index) {
T removed = getValue().remove(index);
checkEmpty();
return removed;
return getValue().remove(index);
}

public void clear() {
getValue().clear();
checkEmpty();
}

public boolean contains(T t) {
Expand All @@ -69,8 +74,7 @@ public void forEach(Consumer<? super T> action) {
}

public T set(int index, T t) {
checkValue(t);
return getValue().set(index, t);
return getValue().set(index, checkNull(t));
}

/**
Expand All @@ -83,10 +87,12 @@ public void add(T t) {
}

public void add(int index, T t) {
checkValue(t);
checkNull(t);
getValue().add(index, t);
typeID = t.getID();
typeClass = t.getClass();
if (typeID == 0) {
typeID = t.getID();
typeClass = t.getClass();
}
}

public void addAll(Collection<T> t) {
Expand All @@ -103,59 +109,48 @@ public void addAll(int index, Collection<T> t) {
}
}

@SuppressWarnings("unchecked")
public void addBoolean(boolean value) {
add((T) new ByteTag(value));
addUnchecked(new ByteTag(value));
}

@SuppressWarnings("unchecked")
public void addByte(byte value) {
add((T) new ByteTag(value));
addUnchecked(new ByteTag(value));
}

@SuppressWarnings("unchecked")
public void addShort(short value) {
add((T) new ShortTag(value));
addUnchecked(new ShortTag(value));
}

@SuppressWarnings("unchecked")
public void addInt(int value) {
add((T) new IntTag(value));
addUnchecked(new IntTag(value));
}

@SuppressWarnings("unchecked")
public void addLong(long value) {
add((T) new LongTag(value));
addUnchecked(new LongTag(value));
}

@SuppressWarnings("unchecked")
public void addFloat(float value) {
add((T) new FloatTag(value));
addUnchecked(new FloatTag(value));
}

@SuppressWarnings("unchecked")
public void addDouble(double value) {
add((T) new DoubleTag(value));
addUnchecked(new DoubleTag(value));
}

@SuppressWarnings("unchecked")
public void addString(String value) {
add((T) new StringTag(checkNull(value)));
addUnchecked(new StringTag(checkNull(value)));
}

@SuppressWarnings("unchecked")
public void addByteArray(byte[] value) {
add((T) new ByteArrayTag(checkNull(value)));
addUnchecked(new ByteArrayTag(checkNull(value)));
}

@SuppressWarnings("unchecked")
public void addIntArray(int[] value) {
add((T) new IntArrayTag(checkNull(value)));
addUnchecked(new IntArrayTag(checkNull(value)));
}

@SuppressWarnings("unchecked")
public void addLongArray(long[] value) {
add((T) new LongArrayTag(checkNull(value)));
addUnchecked(new LongArrayTag(checkNull(value)));
}

public T get(int index) {
Expand All @@ -172,83 +167,59 @@ public <L extends Tag> ListTag<L> asTypedList(Class<L> type) {
return (ListTag<L>) this;
}

@SuppressWarnings("unchecked")
public ListTag<ByteTag> asByteTagList() {
checkTypeClass(ByteTag.class);
return (ListTag<ByteTag>) this;
return asTypedList(ByteTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<ShortTag> asShortTagList() {
checkTypeClass(ShortTag.class);
return (ListTag<ShortTag>) this;
return asTypedList(ShortTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<IntTag> asIntTagList() {
checkTypeClass(IntTag.class);
return (ListTag<IntTag>) this;
return asTypedList(IntTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<LongTag> asLongTagList() {
checkTypeClass(LongTag.class);
return (ListTag<LongTag>) this;
return asTypedList(LongTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<FloatTag> asFloatTagList() {
checkTypeClass(FloatTag.class);
return (ListTag<FloatTag>) this;
return asTypedList(FloatTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<DoubleTag> asDoubleTagList() {
checkTypeClass(DoubleTag.class);
return (ListTag<DoubleTag>) this;
return asTypedList(DoubleTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<StringTag> asStringTagList() {
checkTypeClass(StringTag.class);
return (ListTag<StringTag>) this;
return asTypedList(StringTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<ByteArrayTag> asByteArrayTagList() {
checkTypeClass(ByteArrayTag.class);
return (ListTag<ByteArrayTag>) this;
return asTypedList(ByteArrayTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<IntArrayTag> asIntArrayTagList() {
checkTypeClass(IntArrayTag.class);
return (ListTag<IntArrayTag>) this;
return asTypedList(IntArrayTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<LongArrayTag> asLongArrayTagList() {
checkTypeClass(LongArrayTag.class);
return (ListTag<LongArrayTag>) this;
return asTypedList(LongArrayTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<ListTag<?>> asListTagList() {
checkTypeClass(ListTag.class);
return (ListTag<ListTag<?>>) this;
public ListTag<ListTag> asListTagList() {
return asTypedList(ListTag.class);
}

@SuppressWarnings("unchecked")
public ListTag<CompoundTag> asCompoundTagList() {
checkTypeClass(CompoundTag.class);
return (ListTag<CompoundTag>) this;
return asTypedList(CompoundTag.class);
}

@Override
public void serializeValue(DataOutputStream dos, int depth) throws IOException {
dos.writeByte(typeID);
dos.writeByte(getTypeID());
dos.writeInt(size());
if (typeID != 0) {
if (size() != 0) {
for (T t : getValue()) {
t.serializeValue(dos, incrementDepth(depth));
}
Expand All @@ -268,7 +239,10 @@ public void deserializeValue(DataInputStream dis, int depth) throws IOException
add((T) tag);
}
}
checkEmpty();
if (size() == 0) {
typeID = 0;
typeClass = EndTag.class;
}
}

@Override
Expand Down Expand Up @@ -330,13 +304,14 @@ public ListTag<T> clone() {
return copy;
}

private void checkValue(T t) {
checkNull(t);
if (typeID != 0 && t.getID() != typeID) {
@SuppressWarnings("unchecked")
private void addUnchecked(Tag tag) {
if (typeClass != EndTag.class && typeClass != tag.getClass() || typeID != 0 && tag.getID() != typeID) {
throw new IllegalArgumentException(String.format(
"cannot add %s to ListTag<%s>",
t.getClass().getSimpleName(), typeClass.getSimpleName()));
tag.getClass().getSimpleName(), typeClass.getSimpleName()));
}
add(size(), (T) tag);
}

private void checkTypeClass(Class<?> clazz) {
Expand All @@ -346,11 +321,4 @@ private void checkTypeClass(Class<?> clazz) {
typeClass.getSimpleName(), clazz.getSimpleName()));
}
}

private void checkEmpty() {
if (size() == 0) {
typeID = 0;
typeClass = EndTag.class;
}
}
}
26 changes: 13 additions & 13 deletions src/main/java/net/querz/nbt/mca/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public class Chunk {
private ListTag<CompoundTag> tileEntities;
private ListTag<CompoundTag> tileTicks;
private ListTag<CompoundTag> liquidTicks;
private ListTag<ListTag<?>> lights;
private ListTag<ListTag<?>> liquidsToBeTicked;
private ListTag<ListTag<?>> toBeTicked;
private ListTag<ListTag<?>> postProcessing;
private ListTag<ListTag> lights;
private ListTag<ListTag> liquidsToBeTicked;
private ListTag<ListTag> toBeTicked;
private ListTag<ListTag> postProcessing;
private String status;
private CompoundTag structures;

Expand Down Expand Up @@ -247,35 +247,35 @@ public void setLiquidTicks(ListTag<CompoundTag> liquidTicks) {
this.liquidTicks = liquidTicks;
}

public ListTag<ListTag<?>> getLights() {
public ListTag<ListTag> getLights() {
return lights;
}

public void setLights(ListTag<ListTag<?>> lights) {
public void setLights(ListTag<ListTag> lights) {
this.lights = lights;
}

public ListTag<ListTag<?>> getLiquidsToBeTicked() {
public ListTag<ListTag> getLiquidsToBeTicked() {
return liquidsToBeTicked;
}

public void setLiquidsToBeTicked(ListTag<ListTag<?>> liquidsToBeTicked) {
public void setLiquidsToBeTicked(ListTag<ListTag> liquidsToBeTicked) {
this.liquidsToBeTicked = liquidsToBeTicked;
}

public ListTag<ListTag<?>> getToBeTicked() {
public ListTag<ListTag> getToBeTicked() {
return toBeTicked;
}

public void setToBeTicked(ListTag<ListTag<?>> toBeTicked) {
public void setToBeTicked(ListTag<ListTag> toBeTicked) {
this.toBeTicked = toBeTicked;
}

public ListTag<ListTag<?>> getPostProcessing() {
public ListTag<ListTag> getPostProcessing() {
return postProcessing;
}

public void setPostProcessing(ListTag<ListTag<?>> postProcessing) {
public void setPostProcessing(ListTag<ListTag> postProcessing) {
this.postProcessing = postProcessing;
}

Expand Down Expand Up @@ -328,7 +328,7 @@ public CompoundTag updateHandle(int xPos, int zPos) {
if (postProcessing != null) level.put("PostProcessing", postProcessing);
level.putString("Status", status);
if (structures != null) level.put("Structures", structures);
ListTag<CompoundTag> sections = new ListTag<>();
ListTag<CompoundTag> sections = new ListTag<>(CompoundTag.class);
for (int i = 0; i < this.sections.length; i++) {
if (this.sections[i] != null) {
sections.add(this.sections[i].updateHandle(i));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/mca/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void setSkyLight(byte[] skyLight) {
public static Section newSection() {
Section s = new Section();
s.blockStates = new long[256];
s.palette = new ListTag<>();
s.palette = new ListTag<>(CompoundTag.class);
CompoundTag air = new CompoundTag();
air.putString("Name", "minecraft:air");
s.palette.add(air);
Expand Down
Loading

0 comments on commit dc0fbc9

Please sign in to comment.