Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tee7even authored and Tee7even committed Mar 17, 2017
2 parents 9b7758a + 3894aa6 commit 225fe43
Show file tree
Hide file tree
Showing 68 changed files with 943 additions and 558 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Expand Up @@ -50,10 +50,10 @@ fabric.properties
### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
*.iml
modules.xml
.idea/misc.xml
*.ipr


### Maven ###
Expand Down
Expand Up @@ -13,7 +13,6 @@

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.Optional;
import java.util.Set;

Expand Down

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion api/src/main/java/com/voxelwind/api/server/Player.java
Expand Up @@ -6,7 +6,6 @@
import com.voxelwind.api.game.inventories.PlayerInventory;
import com.voxelwind.api.server.command.CommandExecutorSource;
import com.voxelwind.api.server.event.block.iface.BlockReplacer;
import com.voxelwind.api.server.player.GameMode;
import com.voxelwind.api.server.player.PlayerMessageDisplayType;
import com.voxelwind.api.server.player.PopupMessage;
import com.voxelwind.api.server.player.TranslatedMessage;
Expand Down
2 changes: 1 addition & 1 deletion nbt/pom.xml
Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>4.0.41.Final</version>
<version>4.1.6.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
3 changes: 2 additions & 1 deletion nbt/src/main/java/com/voxelwind/nbt/io/NBTReader.java
Expand Up @@ -59,7 +59,7 @@ private Tag<?> deserialize(TagType type, boolean skipName, int depth) throws IOE
switch (type) {
case END:
if (depth == 0) {
throw new IllegalArgumentException("Found TAG_End but not in a compound or list.");
throw new IllegalArgumentException("Found a TAG_End in root tag!");
}
return EndTag.INSTANCE;
case BYTE:
Expand Down Expand Up @@ -122,6 +122,7 @@ private Tag<?> deserialize(TagType type, boolean skipName, int depth) throws IOE

@Override
public void close() throws IOException {
if (closed) return;
closed = true;
if (input instanceof Closeable) {
((Closeable) input).close();
Expand Down
16 changes: 12 additions & 4 deletions nbt/src/main/java/com/voxelwind/nbt/io/NBTWriter.java
Expand Up @@ -10,6 +10,7 @@
import java.util.Objects;

public class NBTWriter implements Closeable {
private static final int MAXIMUM_DEPTH = 16;
private final DataOutput output;
private final NBTEncoding encoding;
private boolean closed = false;
Expand All @@ -24,14 +25,20 @@ public NBTWriter(DataOutput output, NBTEncoding encoding) {
}

public void write(Tag<?> tag) throws IOException {
if (closed) {
throw new IllegalStateException("closed");
}
Objects.requireNonNull(tag, "tag");
if (!(tag instanceof CompoundTag)) {
throw new IllegalArgumentException("Trying to write a non-compound tag!");
}
serialize(tag, false);
serialize(tag, false, 0);
}

private void serialize(Tag<?> tag, boolean skipHeader) throws IOException {
private void serialize(Tag<?> tag, boolean skipHeader, int depth) throws IOException {
if (depth >= MAXIMUM_DEPTH) {
throw new IllegalArgumentException("Reached depth limit");
}
TagType type = TagType.fromClass(tag.getClass());
if (type == null) {
throw new IllegalArgumentException("Tag " + tag + " is not valid.");
Expand Down Expand Up @@ -96,13 +103,13 @@ private void serialize(Tag<?> tag, boolean skipHeader) throws IOException {
output.writeInt(listt.getValue().size());
}
for (Tag<?> tag1 : listt.getValue()) {
serialize(tag1, true);
serialize(tag1, true, depth+1);
}
break;
case COMPOUND:
CompoundTag compoundTag = (CompoundTag) tag;
for (Tag<?> tag1 : compoundTag.getValue().values()) {
serialize(tag1, false);
serialize(tag1, false, depth+1);
}
output.writeByte(0);
break;
Expand Down Expand Up @@ -133,6 +140,7 @@ private void writeString(String name) throws IOException {

@Override
public void close() throws IOException {
if (closed) return;
closed = true;
if (output instanceof Closeable) {
((Closeable) output).close();
Expand Down
72 changes: 72 additions & 0 deletions nbt/src/main/java/com/voxelwind/nbt/tags/CompoundTag.java
@@ -1,16 +1,80 @@
package com.voxelwind.nbt.tags;

import com.voxelwind.nbt.util.CompoundTagBuilder;

import java.util.*;

public class CompoundTag implements Tag<Map<String, Tag<?>>> {
private final String name;
private final Map<String, Tag<?>> value;

public CompoundTag(String name) {
this.name = name;
this.value = Collections.unmodifiableMap(new HashMap<>());
}

public CompoundTag(String name, Map<String, Tag<?>> value) {
this.name = name;
this.value = Collections.unmodifiableMap(new HashMap<>(value));
}

public void remove(String name) {
if (value.containsKey(name)) {
value.remove(name);
}
}

public CompoundTag tag(Tag<?> tag) {
value.put(tag.getName(), tag);
return this;
}

public CompoundTag tagByte(String name, byte value) {
return tag(new ByteTag(name, value));
}

public CompoundTag tagByteArray(String name, byte[] value) {
return tag(new ByteArrayTag(name, value));
}

public CompoundTag tagDouble(String name, double value) {
return tag(new DoubleTag(name, value));
}

public CompoundTag tagFloat(String name, float value) {
return tag(new FloatTag(name, value));
}

public CompoundTag tagIntArray(String name, int[] value) {
return tag(new IntArrayTag(name, value));
}

public CompoundTag tagInt(String name, int value) {
return tag(new IntTag(name, value));
}

public CompoundTag tagLong(String name, long value) {
return tag(new LongTag(name, value));
}

public CompoundTag tagShort(String name, short value) {
return tag(new ShortTag(name, value));
}

public CompoundTag tagString(String name, String value) {
return tag(new StringTag(name, value));
}

public CompoundTag tagCompoundTag(String name, CompoundTag value) {
this.value.put(name, value);
return this;
}

public CompoundTag tagListTag(String name, ListTag value) {
this.value.put(name, value);
return this;
}

@Override
public String getName() {
return name;
Expand Down Expand Up @@ -61,4 +125,12 @@ public static CompoundTag createFromList(String name, List<Tag<?>> list) {
}
return new CompoundTag(name, map);
}

public CompoundTagBuilder toBuilder() {
return CompoundTagBuilder.from(this);
}

public Tag<?> get(String key) {
return value.get(key);
}
}
82 changes: 82 additions & 0 deletions nbt/src/main/java/com/voxelwind/nbt/util/CompoundTagBuilder.java
@@ -0,0 +1,82 @@
package com.voxelwind.nbt.util;

import com.voxelwind.nbt.tags.*;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CompoundTagBuilder {
private final Map<String, Tag<?>> tagMap = new HashMap<>();

public static CompoundTagBuilder builder() {
return new CompoundTagBuilder();
}

public static CompoundTagBuilder from(CompoundTag tag) {
CompoundTagBuilder builder = new CompoundTagBuilder();
builder.tagMap.putAll(tag.getValue());
return builder;
}

public CompoundTagBuilder tag(Tag<?> tag) {
tagMap.put(tag.getName(), tag);
return this;
}

public CompoundTagBuilder tagByte (String name, byte value) {
return tag(new ByteTag(name, value));
}

public CompoundTagBuilder tagByteArray (String name, byte [] value) {
return tag(new ByteArrayTag(name, value));
}

public CompoundTagBuilder tagDouble (String name, double value) {
return tag(new DoubleTag(name, value));
}

public CompoundTagBuilder tagFloat (String name, float value) {
return tag(new FloatTag(name, value));
}

public CompoundTagBuilder tagIntArray (String name, int[] value) {
return tag(new IntArrayTag(name, value));
}

public CompoundTagBuilder tagInt (String name, int value) {
return tag(new IntTag(name, value));
}

public CompoundTagBuilder tagLong (String name, long value) {
return tag(new LongTag(name, value));
}

public CompoundTagBuilder tagShort (String name, short value) {
return tag(new ShortTag(name, value));
}

public CompoundTagBuilder tagString (String name, String value) {
return tag(new StringTag(name, value));
}

public CompoundTagBuilder tagCompoundTag (String name, CompoundTag value) {
tagMap.put(name, value);
return this;
}

public CompoundTagBuilder tagListTag(String name, ListTag value) {
tagMap.put(name, value);
return this;
}

public CompoundTag buildRootTag() {
return new CompoundTag("", tagMap);
}

public CompoundTag build(String tagName) {
return new CompoundTag(tagName, tagMap);
}
}

0 comments on commit 225fe43

Please sign in to comment.