Skip to content

Commit

Permalink
Add missing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroHc committed Dec 15, 2023
1 parent 025eb95 commit a170ea3
Show file tree
Hide file tree
Showing 38 changed files with 1,736 additions and 227 deletions.
14 changes: 5 additions & 9 deletions src/main/java/net/sandrohc/schematic4j/SchematicFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@
import net.sandrohc.schematic4j.parser.SpongeParser;

public enum SchematicFormat {
SPONGE_V1 ("schem", SpongeParser::new),
SPONGE_V2 ("schem", SpongeParser::new),
SPONGE_V3 ("schem", SpongeParser::new),
LITEMATICA ("litematic"),
SCHEMATICA ("schematic", SchematicaParser::new),
WORLD_EDITOR ("schematic"),
MCEDIT ("schematic"),
MCEDIT_UNIFIED ("schematic"),
MCEDIT2 ("schematic"),
SPONGE_V1("schem", SpongeParser::new),
SPONGE_V2("schem", SpongeParser::new),
SPONGE_V3("schem", SpongeParser::new),
LITEMATICA("litematic", LitematicaParser::new),
SCHEMATICA("schematic", SchematicaParser::new),
UNKNOWN;

private static final Logger log = LoggerFactory.getLogger(SchematicFormat.class);
Expand Down
45 changes: 43 additions & 2 deletions src/main/java/net/sandrohc/schematic4j/nbt/Deserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,60 @@
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;

/**
* A generic NBT deserializer.
*
* @param <T> Type of the deserialized value
*/
public interface Deserializer<T> {

/**
* Deserialize NBT from an input stream.
*
* @param stream The input stream containing the NBT data
* @return The deserialized NBT
* @throws IOException In case there's an error reading from the input stream
*/
T fromStream(InputStream stream) throws IOException;

/**
* Deserialize NBT from a file.
*
* @param file The file containing the NBT data
* @return The deserialized NBT
* @throws IOException In case there's an error reading from the file
*/
default T fromFile(File file) throws IOException {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
try (BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
return fromStream(bis);
}
}

/**
* Deserialize NBT from a byte array.
*
* @param data The byte array containing the NBT data
* @return The deserialized NBT
* @throws IOException In case there's an error reading from the byte array
*/
default T fromBytes(byte[] data) throws IOException {
ByteArrayInputStream stream = new ByteArrayInputStream(data);
return fromStream(stream);
}

/**
* Deserialize NBT from a JAR resource.
*
* @param clazz The class belonging to the JAR that contains the resource
* @param path The resource path containing the NBT data
* @return The deserialized NBT
* @throws IOException In case there's an error reading from the resource
*/
default T fromResource(Class<?> clazz, String path) throws IOException {
try (InputStream stream = clazz.getClassLoader().getResourceAsStream(path)) {
if (stream == null) {
Expand All @@ -33,6 +67,13 @@ default T fromResource(Class<?> clazz, String path) throws IOException {
}
}

/**
* Deserialize NBT from a URL.
*
* @param url The URL containing the NBT data
* @return The deserialized NBT
* @throws IOException In case there's an error reading from the URL
*/
default T fromURL(URL url) throws IOException {
try (InputStream stream = url.openStream()) {
return fromStream(stream);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
/* Vendored version of Quertz NBT 6.1 - https://github.com/Querz/NBT */
package net.sandrohc.schematic4j.nbt;

/**
* A bi-function that may throw an exception.
*
* @param <T> First value type
* @param <U> Second value type
* @param <R> Return type
* @param <E> Exception type
*/
@FunctionalInterface
public interface ExceptionBiFunction <T, U, R, E extends Exception> {
public interface ExceptionBiFunction<T, U, R, E extends Exception> {

/**
* @param t The first value
* @param u The second value
* @return The return value
* @throws E The exception
*/
R accept(T t, U u) throws E;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
/* Vendored version of Quertz NBT 6.1 - https://github.com/Querz/NBT */
package net.sandrohc.schematic4j.nbt;

/**
* A tri-consumer that may throw an exception.
*
* @param <T> First value type
* @param <U> Second value type
* @param <V> Third value type
* @param <E> Exception type
*/
@FunctionalInterface
public interface ExceptionTriConsumer<T, U, V, E extends Exception> {

/**
* @param t The first value
* @param u The second value
* @param v The third value
* @throws E The exception
*/
void accept(T t, U u, V v) throws E;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/**
* Exception indicating that the maximum (de-)serialization depth has been reached.
*/
@SuppressWarnings("serial")
public class MaxDepthReachedException extends RuntimeException {

public MaxDepthReachedException(String msg) {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/net/sandrohc/schematic4j/nbt/io/NBTInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,26 @@

import net.sandrohc.schematic4j.nbt.tag.Tag;

/**
* A generic NBT input
*/
public interface NBTInput {

/**
* Read a named tag from the input.
*
* @param maxDepth Maximum depth before failing deserialization
* @return The named tag read
* @throws IOException In case of error reading from the input
*/
NamedTag readTag(int maxDepth) throws IOException;

/**
* Read a tag from the input.
*
* @param maxDepth Maximum depth before failing deserialization
* @return The tag read
* @throws IOException In case of error reading from the input
*/
Tag<?> readRawTag(int maxDepth) throws IOException;
}
Loading

0 comments on commit a170ea3

Please sign in to comment.