Skip to content

Commit

Permalink
feat: specialized put methods and improved packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
BitBuf committed Aug 28, 2021
1 parent e22b700 commit 5c2f4fb
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 49 deletions.
4 changes: 4 additions & 0 deletions src/main/java/dev/dewy/nbt/Nbt.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package dev.dewy.nbt;

import dev.dewy.nbt.io.CompressionType;
import dev.dewy.nbt.io.NbtReader;
import dev.dewy.nbt.io.NbtWriter;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.collection.CompoundTag;
import lombok.NonNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.dewy.nbt;
package dev.dewy.nbt.io;

import java.io.BufferedInputStream;
import java.io.IOException;
Expand All @@ -25,7 +25,7 @@ public enum CompressionType {
*/
ZLIB;

static CompressionType getCompression(InputStream in) throws IOException {
public static CompressionType getCompression(InputStream in) throws IOException {
if (!in.markSupported()) {
in = new BufferedInputStream(in);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.dewy.nbt;
package dev.dewy.nbt.io;

import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import dev.dewy.nbt.tags.collection.CompoundTag;
import lombok.AllArgsConstructor;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.dewy.nbt;
package dev.dewy.nbt.io;

import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import dev.dewy.nbt.tags.collection.CompoundTag;
import lombok.AllArgsConstructor;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt;
package dev.dewy.nbt.registry;

import dev.dewy.nbt.exceptions.TagTypeRegistryException;
import dev.dewy.nbt.tags.Tag;
import dev.dewy.nbt.tags.TagType;
import lombok.NonNull;

import java.lang.reflect.Constructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package dev.dewy.nbt.exceptions;

import dev.dewy.nbt.TagTypeRegistry;
package dev.dewy.nbt.registry;

/**
* Checked exception thrown when any issue arises relating to the {@link TagTypeRegistry}.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/Tag.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.dewy.nbt.tags;

import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;

import java.io.DataInput;
import java.io.DataOutput;
Expand All @@ -11,7 +11,7 @@
*
* @author dewy
*/
public abstract class Tag {
public abstract class Tag implements Cloneable {
private String name;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.dewy.nbt;
package dev.dewy.nbt.tags;

import dev.dewy.nbt.exceptions.TagTypeRegistryException;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistryException;
import dev.dewy.nbt.tags.array.ByteArrayTag;
import dev.dewy.nbt.tags.array.IntArrayTag;
import dev.dewy.nbt.tags.array.LongArrayTag;
Expand Down Expand Up @@ -84,7 +85,7 @@ public byte getId() {
return (byte) id;
}

static void registerAll(TagTypeRegistry registry) {
public static void registerAll(TagTypeRegistry registry) {
try {
registry.registerTagType(BYTE.getId(), ByteTag.class);
registry.registerTagType(SHORT.getId(), ShortTag.class);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/array/ByteArrayTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt.tags.array;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import dev.dewy.nbt.tags.primitive.ByteTag;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/array/IntArrayTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt.tags.array;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import dev.dewy.nbt.tags.primitive.IntTag;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/array/LongArrayTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt.tags.array;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import dev.dewy.nbt.tags.primitive.LongTag;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
Expand Down
80 changes: 73 additions & 7 deletions src/main/java/dev/dewy/nbt/tags/collection/CompoundTag.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package dev.dewy.nbt.tags.collection;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.exceptions.TagTypeRegistryException;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistryException;
import dev.dewy.nbt.tags.Tag;
import dev.dewy.nbt.tags.TagType;
import dev.dewy.nbt.tags.array.ByteArrayTag;
import dev.dewy.nbt.tags.array.IntArrayTag;
import dev.dewy.nbt.tags.array.LongArrayTag;
import dev.dewy.nbt.tags.primitive.*;
import lombok.AllArgsConstructor;
import lombok.NonNull;

Expand Down Expand Up @@ -141,12 +145,74 @@ public boolean isEmpty() {
* Adds a given tag to this compound. The tag must have a name, or NPE is thrown.
*
* @param tag the named tag to be added to the compound.
* @param <T> the tag type you believe you may be replacing (optional).
* @return the previous value mapped with the tag's name as type T if provided, or null if there wasn't any.
* @param <E> the type of an existing tag you believe you may be replacing (optional).
* @return the previous value mapped with the tag's name as type E if provided, or null if there wasn't any.
* @throws NullPointerException if the tag's name is null.
*/
public <T extends Tag> T put(@NonNull Tag tag) {
return (T) this.value.put(tag.getName(), tag);
public <E extends Tag> E put(@NonNull Tag tag) {
return (E) this.value.put(tag.getName(), tag);
}

/**
* Adds a given tag to this compound. Be careful, the tag's name is set to the {@code name} parameter automatically.
*
* @param name the tag's name (key).
* @param tag the tag to be added to the compound.
* @param <E> the type of an existing tag you believe you may be replacing (optional).
* @return the previous value mapped with the tag's name as type E if provided, or null if there wasn't any.
*/
public <E extends Tag> E put(@NonNull String name, @NonNull Tag tag) {
tag.setName(name);

return this.put(tag);
}

public void putByte(@NonNull String name, byte value) {
this.put(name, new ByteTag(name, value));
}

public void putShort(@NonNull String name, short value) {
this.put(name, new ShortTag(name, value));
}

public void putInt(@NonNull String name, int value) {
this.put(name, new IntTag(name, value));
}

public void putLong(@NonNull String name, long value) {
this.put(name, new LongTag(name, value));
}

public void putFloat(@NonNull String name, float value) {
this.put(name, new FloatTag(name, value));
}

public void putDouble(@NonNull String name, double value) {
this.put(name, new DoubleTag(name, value));
}

public void putByteArray(@NonNull String name, @NonNull byte[] value) {
this.put(name, new ByteArrayTag(name, value));
}

public void putString(@NonNull String name, @NonNull String value) {
this.put(name, new StringTag(name, value));
}

public <T extends Tag> void putList(@NonNull String name, List<T> value) {
this.put(name, new ListTag<>(name, value));
}

public void putCompound(@NonNull String name, @NonNull Map<@NonNull String, @NonNull Tag> value) {
this.put(name, new CompoundTag(name, value));
}

public void putIntArray(@NonNull String name, @NonNull int[] value) {
this.put(name, new IntArrayTag(name, value));
}

public void putLongArray(@NonNull String name, @NonNull long[] value) {
this.put(name, new LongArrayTag(name, value));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/dewy/nbt/tags/collection/ListTag.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.dewy.nbt.tags.collection;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.exceptions.TagTypeRegistryException;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistryException;
import dev.dewy.nbt.tags.Tag;
import dev.dewy.nbt.tags.TagType;
import lombok.AllArgsConstructor;
import lombok.NonNull;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/primitive/ByteTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt.tags.primitive;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.NonNull;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/primitive/DoubleTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt.tags.primitive;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/primitive/FloatTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt.tags.primitive;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/primitive/IntTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt.tags.primitive;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/primitive/LongTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt.tags.primitive;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/primitive/ShortTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dewy.nbt.tags.primitive;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.TagType;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.NonNull;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dewy/nbt/tags/primitive/StringTag.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.dewy.nbt.tags.primitive;

import dev.dewy.nbt.TagType;
import dev.dewy.nbt.TagTypeRegistry;
import dev.dewy.nbt.registry.TagTypeRegistry;
import dev.dewy.nbt.tags.Tag;
import dev.dewy.nbt.tags.TagType;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.NonNull;
Expand Down
14 changes: 9 additions & 5 deletions src/test/java/dev/dewy/nbt/test/NbtTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.dewy.nbt.test;

import dev.dewy.nbt.CompressionType;
import dev.dewy.nbt.io.CompressionType;
import dev.dewy.nbt.Nbt;
import dev.dewy.nbt.tags.array.ByteArrayTag;
import dev.dewy.nbt.tags.array.IntArrayTag;
Expand Down Expand Up @@ -30,9 +30,13 @@ public static void main(String[] args) throws IOException {
root.put(new ShortTag("short", 345));
root.put(new IntTag("int", -981735));
root.put(new LongTag("long", -398423290489L));
root.put(new FloatTag("float", 12.5F));
root.put(new DoubleTag("double", -19040912.1235));
root.put(new StringTag("string", "https://dewy.dev"));

// more primitives, using the specialized put methods
root.putFloat("float", 12.5F);
root.putDouble("double", -19040912.1235);

// putting a previously unnamed tag.
root.put("string", new StringTag("https://dewy.dev"));

// array NBT tags
root.put(new ByteArrayTag("bytes", new byte[] {0, -124, 13, -6, Byte.MAX_VALUE}));
Expand Down Expand Up @@ -63,7 +67,7 @@ public static void main(String[] args) throws IOException {
// list containing an empty list of ints
ListTag<ListTag<IntTag>> listsOfInts = new ListTag<>("listofints");
listsOfInts.add(new ListTag<>());
root.put(listsOfInts);
root.putList("listofints", listsOfInts.getValue());

// writing to file (no compression type provided for no compression)
NBT.toFile(root, new File("sample.nbt"));
Expand Down

0 comments on commit 5c2f4fb

Please sign in to comment.