refac: переосмысление
This commit is contained in:
10
.editorconfig
Normal file
10
.editorconfig
Normal file
@@ -0,0 +1,10 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_size = 4
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
max_line_length = 120
|
||||
tab_width = 4
|
||||
3
.gitattributes
vendored
Normal file
3
.gitattributes
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
gradlew text eol=lf
|
||||
gradlew.bat text eol=crlf
|
||||
gradle/wrapper/gradle-wrapper.properties text eol=lf
|
||||
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# Java
|
||||
/hs_err_pid*
|
||||
|
||||
# Gradle
|
||||
/.gradle/
|
||||
build/
|
||||
|
||||
# IDEA
|
||||
/.idea/
|
||||
/*.iml
|
||||
34
build.gradle
Normal file
34
build.gradle
Normal file
@@ -0,0 +1,34 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
wrapper {
|
||||
gradleVersion = '8.5'
|
||||
distributionType = Wrapper.DistributionType.BIN
|
||||
}
|
||||
|
||||
group = 'mc.nbt'
|
||||
version = '2.0-SNAPSHOT'
|
||||
|
||||
compileJava {
|
||||
targetCompatibility = sourceCompatibility = JavaVersion.VERSION_17
|
||||
options.encoding = 'UTF-8'
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
def junit5_version = '5.9.2'
|
||||
testImplementation("org.junit.jupiter:junit-jupiter-api:${junit5_version}")
|
||||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junit5_version}")
|
||||
testImplementation("org.junit.jupiter:junit-jupiter-params:${junit5_version}")
|
||||
|
||||
testImplementation('org.apache.commons:commons-lang3:3.12.0')
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
7
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
7
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
249
gradlew
vendored
Executable file
249
gradlew
vendored
Executable file
@@ -0,0 +1,249 @@
|
||||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Copyright © 2015-2021 the original authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
# Gradle start up script for POSIX generated by Gradle.
|
||||
#
|
||||
# Important for running:
|
||||
#
|
||||
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||
# noncompliant, but you have some other compliant shell such as ksh or
|
||||
# bash, then to run this script, type that shell name before the whole
|
||||
# command line, like:
|
||||
#
|
||||
# ksh Gradle
|
||||
#
|
||||
# Busybox and similar reduced shells will NOT work, because this script
|
||||
# requires all of these POSIX shell features:
|
||||
# * functions;
|
||||
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||
# * compound commands having a testable exit status, especially «case»;
|
||||
# * various built-in commands including «command», «set», and «ulimit».
|
||||
#
|
||||
# Important for patching:
|
||||
#
|
||||
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||
#
|
||||
# The "traditional" practice of packing multiple parameters into a
|
||||
# space-separated string is a well documented source of bugs and security
|
||||
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||
# options in "$@", and eventually passing that to Java.
|
||||
#
|
||||
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||
# see the in-line comments for details.
|
||||
#
|
||||
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (3) This script is generated from the Groovy template
|
||||
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# within the Gradle project.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
|
||||
# Resolve links: $0 may be a link
|
||||
app_path=$0
|
||||
|
||||
# Need this for daisy-chained symlinks.
|
||||
while
|
||||
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||
[ -h "$app_path" ]
|
||||
do
|
||||
ls=$( ls -ld "$app_path" )
|
||||
link=${ls#*' -> '}
|
||||
case $link in #(
|
||||
/*) app_path=$link ;; #(
|
||||
*) app_path=$APP_HOME$link ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
} >&2
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
} >&2
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "$( uname )" in #(
|
||||
CYGWIN* ) cygwin=true ;; #(
|
||||
Darwin* ) darwin=true ;; #(
|
||||
MSYS* | MINGW* ) msys=true ;; #(
|
||||
NONSTOP* ) nonstop=true ;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||
else
|
||||
JAVACMD=$JAVA_HOME/bin/java
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD=java
|
||||
if ! command -v java >/dev/null 2>&1
|
||||
then
|
||||
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
MAX_FD=$( ulimit -H -n ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
case $MAX_FD in #(
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
ulimit -n "$MAX_FD" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command, stacking in reverse order:
|
||||
# * args from the command line
|
||||
# * the main class name
|
||||
# * -classpath
|
||||
# * -D...appname settings
|
||||
# * --module-path (only if needed)
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if "$cygwin" || "$msys" ; then
|
||||
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||
|
||||
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
for arg do
|
||||
if
|
||||
case $arg in #(
|
||||
-*) false ;; # don't mess with options #(
|
||||
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||
[ -e "$t" ] ;; #(
|
||||
*) false ;;
|
||||
esac
|
||||
then
|
||||
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||
fi
|
||||
# Roll the args list around exactly as many times as the number of
|
||||
# args, so each arg winds up back in the position where it started, but
|
||||
# possibly modified.
|
||||
#
|
||||
# NB: a `for` loop captures its iteration list before it begins, so
|
||||
# changing the positional parameters here affects neither the number of
|
||||
# iterations, nor the values presented in `arg`.
|
||||
shift # remove old arg
|
||||
set -- "$@" "$arg" # push replacement arg
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Collect all arguments for the java command:
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||
# and any embedded shellness will be escaped.
|
||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||
# treated as '${Hostname}' itself on the command line.
|
||||
|
||||
set -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
-classpath "$CLASSPATH" \
|
||||
org.gradle.wrapper.GradleWrapperMain \
|
||||
"$@"
|
||||
|
||||
# Stop when "xargs" is not available.
|
||||
if ! command -v xargs >/dev/null 2>&1
|
||||
then
|
||||
die "xargs is not available"
|
||||
fi
|
||||
|
||||
# Use "xargs" to parse quoted args.
|
||||
#
|
||||
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||
#
|
||||
# In Bash we could simply go:
|
||||
#
|
||||
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||
# set -- "${ARGS[@]}" "$@"
|
||||
#
|
||||
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||
# character that might be a shell metacharacter, then use eval to reverse
|
||||
# that process (while maintaining the separation between arguments), and wrap
|
||||
# the whole thing up as a single "set" statement.
|
||||
#
|
||||
# This will of course break if any of these variables contains a newline or
|
||||
# an unmatched quote.
|
||||
#
|
||||
|
||||
eval "set -- $(
|
||||
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||
xargs -n1 |
|
||||
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||
tr '\n' ' '
|
||||
)" '"$@"'
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
92
gradlew.bat
vendored
Normal file
92
gradlew.bat
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%"=="" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%"=="" set DIRNAME=.
|
||||
@rem This is normally unused
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if %ERRORLEVEL% equ 0 goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if %ERRORLEVEL% equ 0 goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
set EXIT_CODE=%ERRORLEVEL%
|
||||
if %EXIT_CODE% equ 0 set EXIT_CODE=1
|
||||
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
|
||||
exit /b %EXIT_CODE%
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
||||
1
settings.gradle
Normal file
1
settings.gradle
Normal file
@@ -0,0 +1 @@
|
||||
rootProject.name = 'nbt-lib'
|
||||
41
src/main/java/mc/nbt/ByteArrayTag.java
Normal file
41
src/main/java/mc/nbt/ByteArrayTag.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ByteArrayTag extends TagValue {
|
||||
private final byte[] value;
|
||||
|
||||
public ByteArrayTag(byte[] value) {
|
||||
this.value = Objects.requireNonNull(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.BYTE_ARRAY;
|
||||
}
|
||||
|
||||
public byte[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ByteArrayTag that = (ByteArrayTag) o;
|
||||
return Arrays.equals(getValue(), that.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ByteArrayTag{" +
|
||||
"value.length=" + value.length +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/ByteTag.java
Normal file
40
src/main/java/mc/nbt/ByteTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ByteTag extends TagValue {
|
||||
private final byte value;
|
||||
|
||||
public ByteTag(byte value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.BYTE;
|
||||
}
|
||||
|
||||
public byte getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ByteTag byteTag = (ByteTag) o;
|
||||
return getValue() == byteTag.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ByteTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
113
src/main/java/mc/nbt/CompoundTag.java
Normal file
113
src/main/java/mc/nbt/CompoundTag.java
Normal file
@@ -0,0 +1,113 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
public class CompoundTag extends TagValue implements Map<String, TagValue>, Serializable {
|
||||
private final LinkedHashMap<String, TagValue> map;
|
||||
|
||||
public CompoundTag() {
|
||||
this.map = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public CompoundTag(String key, TagValue value) {
|
||||
this.map = new LinkedHashMap<>();
|
||||
this.map.put(key, value);
|
||||
}
|
||||
|
||||
public CompoundTag(Map<String, TagValue> map) {
|
||||
this.map = new LinkedHashMap<>(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.COMPOUND;
|
||||
}
|
||||
|
||||
public CompoundTag append(String key, TagValue value) {
|
||||
this.map.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
// Map methods
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return map.containsValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue get(Object key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue put(String key, TagValue value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue remove(Object value) {
|
||||
return map.remove(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends String, ? extends TagValue> map) {
|
||||
this.map.putAll(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> keySet() {
|
||||
return map.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<TagValue> values() {
|
||||
return map.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<String, TagValue>> entrySet() {
|
||||
return map.entrySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
CompoundTag that = (CompoundTag) o;
|
||||
return map.equals(that.map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CompoundTag{"
|
||||
+ map
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/DoubleTag.java
Normal file
40
src/main/java/mc/nbt/DoubleTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class DoubleTag extends TagValue {
|
||||
private final double value;
|
||||
|
||||
public DoubleTag(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.DOUBLE;
|
||||
}
|
||||
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
DoubleTag doubleTag = (DoubleTag) o;
|
||||
return Double.compare(doubleTag.getValue(), getValue()) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DoubleTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
27
src/main/java/mc/nbt/EndTag.java
Normal file
27
src/main/java/mc/nbt/EndTag.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package mc.nbt;
|
||||
|
||||
public class EndTag extends TagValue {
|
||||
private static final EndTag INSTANCE = new EndTag();
|
||||
|
||||
private EndTag() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.END;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static EndTag getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/FloatTag.java
Normal file
40
src/main/java/mc/nbt/FloatTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class FloatTag extends TagValue {
|
||||
private final float value;
|
||||
|
||||
public FloatTag(float value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.FLOAT;
|
||||
}
|
||||
|
||||
public float getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
FloatTag floatTag = (FloatTag) o;
|
||||
return Float.compare(floatTag.getValue(), getValue()) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FloatTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
41
src/main/java/mc/nbt/IntegerArrayTag.java
Normal file
41
src/main/java/mc/nbt/IntegerArrayTag.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public class IntegerArrayTag extends TagValue {
|
||||
private final int[] value;
|
||||
|
||||
public IntegerArrayTag(int[] value) {
|
||||
this.value = Objects.requireNonNull(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.INTEGER_ARRAY;
|
||||
}
|
||||
|
||||
public int[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
IntegerArrayTag that = (IntegerArrayTag) o;
|
||||
return Arrays.equals(getValue(), that.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IntegerArrayTag{" +
|
||||
"value.length=" + value.length +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/IntegerTag.java
Normal file
40
src/main/java/mc/nbt/IntegerTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class IntegerTag extends TagValue {
|
||||
private final int value;
|
||||
|
||||
public IntegerTag(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.INTEGER;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
IntegerTag that = (IntegerTag) o;
|
||||
return getValue() == that.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IntegerTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
173
src/main/java/mc/nbt/ListTag.java
Normal file
173
src/main/java/mc/nbt/ListTag.java
Normal file
@@ -0,0 +1,173 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ListTag extends TagValue implements List<TagValue> {
|
||||
|
||||
private final TagType listType;
|
||||
private final List<TagValue> list;
|
||||
|
||||
public ListTag(TagType listType, int initialCapacity) {
|
||||
this.listType = Objects.requireNonNull(listType);
|
||||
this.list = new ArrayList<>(initialCapacity);
|
||||
}
|
||||
|
||||
public ListTag(TagType listType) {
|
||||
this.listType = Objects.requireNonNull(listType);
|
||||
this.list = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.LIST;
|
||||
}
|
||||
|
||||
public TagType getListType() {
|
||||
return listType;
|
||||
}
|
||||
|
||||
public ListTag append(TagValue tag) {
|
||||
list.add(tag);
|
||||
return this;
|
||||
}
|
||||
|
||||
// List methods
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return list.contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<TagValue> iterator() {
|
||||
return list.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return list.toArray();
|
||||
}
|
||||
|
||||
@SuppressWarnings("SuspiciousToArrayCall")
|
||||
@Override
|
||||
public <A> A[] toArray(A[] array) {
|
||||
return list.toArray(array);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(TagValue tag) {
|
||||
return list.add(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return list.remove(o);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SlowListContainsAll")
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> collection) {
|
||||
return list.containsAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends TagValue> collection) {
|
||||
return list.addAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends TagValue> collection) {
|
||||
return list.addAll(index, collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> collection) {
|
||||
return list.removeAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> collection) {
|
||||
return list.retainAll(collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
list.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue get(int index) {
|
||||
return list.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue set(int index, TagValue element) {
|
||||
return list.set(index, element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, TagValue element) {
|
||||
list.add(index, element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagValue remove(int index) {
|
||||
return list.remove(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
return list.indexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
return list.lastIndexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<TagValue> listIterator() {
|
||||
return list.listIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<TagValue> listIterator(int index) {
|
||||
return list.listIterator(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TagValue> subList(int fromIndex, int toIndex) {
|
||||
return list.subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ListTag listTag = (ListTag) o;
|
||||
return list.equals(listTag.list) && getListType() == listTag.getListType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(list, getListType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ListTag{" +
|
||||
"listType=" + listType +
|
||||
", list=" + list +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
41
src/main/java/mc/nbt/LongArrayTag.java
Normal file
41
src/main/java/mc/nbt/LongArrayTag.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public class LongArrayTag extends TagValue {
|
||||
private final long[] value;
|
||||
|
||||
public LongArrayTag(long[] value) {
|
||||
this.value = Objects.requireNonNull(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.LONG_ARRAY;
|
||||
}
|
||||
|
||||
public long[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
LongArrayTag that = (LongArrayTag) o;
|
||||
return Arrays.equals(getValue(), that.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LongArrayTag{" +
|
||||
"value.length=" + value.length +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/LongTag.java
Normal file
40
src/main/java/mc/nbt/LongTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class LongTag extends TagValue {
|
||||
private final long value;
|
||||
|
||||
public LongTag(long value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.LONG;
|
||||
}
|
||||
|
||||
public long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
LongTag longTag = (LongTag) o;
|
||||
return getValue() == longTag.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LongTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/ShortTag.java
Normal file
40
src/main/java/mc/nbt/ShortTag.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ShortTag extends TagValue {
|
||||
private final short value;
|
||||
|
||||
public ShortTag(short value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.SHORT;
|
||||
}
|
||||
|
||||
public short getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ShortTag shortTag = (ShortTag) o;
|
||||
return getValue() == shortTag.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ShortTag{" +
|
||||
"value=" + value +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
41
src/main/java/mc/nbt/StringTag.java
Normal file
41
src/main/java/mc/nbt/StringTag.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package mc.nbt;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class StringTag extends TagValue {
|
||||
|
||||
private final String value;
|
||||
|
||||
public StringTag(String value) {
|
||||
this.value = Objects.requireNonNull(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagType getType() {
|
||||
return TagType.STRING;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
StringTag stringTag = (StringTag) o;
|
||||
return getValue().equals(stringTag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "StringTag{" +
|
||||
"value='" + value + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/TagType.java
Normal file
40
src/main/java/mc/nbt/TagType.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt;
|
||||
|
||||
public enum TagType {
|
||||
BYTE(1),
|
||||
SHORT(2),
|
||||
INTEGER(3),
|
||||
LONG(4),
|
||||
FLOAT(5),
|
||||
DOUBLE(6),
|
||||
|
||||
BYTE_ARRAY(7),
|
||||
INTEGER_ARRAY(11),
|
||||
LONG_ARRAY(12),
|
||||
|
||||
STRING(8),
|
||||
LIST(9),
|
||||
COMPOUND(10),
|
||||
|
||||
END(0);
|
||||
|
||||
private final int id;
|
||||
|
||||
TagType(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public static TagType valueOfId(int id) {
|
||||
for (TagType type : TagType.values()) {
|
||||
if (type.id == id) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException("Unknown tag id: " + id); //TODO семантический exception
|
||||
}
|
||||
}
|
||||
135
src/main/java/mc/nbt/TagValue.java
Normal file
135
src/main/java/mc/nbt/TagValue.java
Normal file
@@ -0,0 +1,135 @@
|
||||
package mc.nbt;
|
||||
|
||||
public abstract class TagValue {
|
||||
protected String name;
|
||||
|
||||
TagValue() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public abstract TagType getType();
|
||||
|
||||
public ByteTag asByte() {
|
||||
throwIfInvalidType(TagType.BYTE);
|
||||
return (ByteTag) this;
|
||||
}
|
||||
|
||||
public ByteArrayTag asByteArray() {
|
||||
throwIfInvalidType(TagType.BYTE_ARRAY);
|
||||
return (ByteArrayTag) this;
|
||||
}
|
||||
|
||||
public ShortTag asShort() {
|
||||
throwIfInvalidType(TagType.SHORT);
|
||||
return (ShortTag) this;
|
||||
}
|
||||
|
||||
public IntegerTag asInteger() {
|
||||
throwIfInvalidType(TagType.INTEGER);
|
||||
return (IntegerTag) this;
|
||||
}
|
||||
|
||||
public IntegerArrayTag asIntegerArray() {
|
||||
throwIfInvalidType(TagType.INTEGER_ARRAY);
|
||||
return (IntegerArrayTag) this;
|
||||
}
|
||||
|
||||
public LongTag asLong() {
|
||||
throwIfInvalidType(TagType.LONG);
|
||||
return (LongTag) this;
|
||||
}
|
||||
|
||||
public LongArrayTag asLongArray() {
|
||||
throwIfInvalidType(TagType.LONG_ARRAY);
|
||||
return (LongArrayTag) this;
|
||||
}
|
||||
|
||||
public FloatTag asFloat() {
|
||||
throwIfInvalidType(TagType.FLOAT);
|
||||
return (FloatTag) this;
|
||||
}
|
||||
|
||||
public DoubleTag asDouble() {
|
||||
throwIfInvalidType(TagType.DOUBLE);
|
||||
return (DoubleTag) this;
|
||||
}
|
||||
|
||||
public StringTag asString() {
|
||||
throwIfInvalidType(TagType.STRING);
|
||||
return (StringTag) this;
|
||||
}
|
||||
|
||||
public ListTag asList() {
|
||||
throwIfInvalidType(TagType.LIST);
|
||||
return (ListTag) this;
|
||||
}
|
||||
|
||||
public CompoundTag asCompound() {
|
||||
throwIfInvalidType(TagType.COMPOUND);
|
||||
return (CompoundTag) this;
|
||||
}
|
||||
|
||||
public boolean isByte() {
|
||||
return getType() == TagType.BYTE;
|
||||
}
|
||||
|
||||
public boolean isByteArray() {
|
||||
return getType() == TagType.BYTE_ARRAY;
|
||||
}
|
||||
|
||||
public boolean isShort() {
|
||||
return getType() == TagType.SHORT;
|
||||
}
|
||||
|
||||
public boolean isInteger() {
|
||||
return getType() == TagType.INTEGER;
|
||||
}
|
||||
|
||||
public boolean isIntegerArray() {
|
||||
return getType() == TagType.INTEGER_ARRAY;
|
||||
}
|
||||
|
||||
public boolean isLong() {
|
||||
return getType() == TagType.LONG;
|
||||
}
|
||||
|
||||
public boolean isLongArray() {
|
||||
return getType() == TagType.LONG_ARRAY;
|
||||
}
|
||||
|
||||
public boolean isFloat() {
|
||||
return getType() == TagType.FLOAT;
|
||||
}
|
||||
|
||||
public boolean isDouble() {
|
||||
return getType() == TagType.DOUBLE;
|
||||
}
|
||||
|
||||
public boolean isString() {
|
||||
return getType() == TagType.STRING;
|
||||
}
|
||||
|
||||
public boolean isCompound() {
|
||||
return getType() == TagType.COMPOUND;
|
||||
}
|
||||
|
||||
public boolean isEnd() {
|
||||
return getType() == TagType.END;
|
||||
}
|
||||
|
||||
private void throwIfInvalidType(TagType expectedType) {
|
||||
if (getType() != expectedType) {
|
||||
//TODO семантический exception
|
||||
throw new RuntimeException(
|
||||
"Value expected to be type %s is of unexpected type %s"
|
||||
.formatted(expectedType, getType()));
|
||||
}
|
||||
}
|
||||
}
|
||||
26
src/main/java/mc/nbt/codec/ByteArrayTagCodec.java
Normal file
26
src/main/java/mc/nbt/codec/ByteArrayTagCodec.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.ByteArrayTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ByteArrayTagCodec implements Codec<ByteArrayTag> {
|
||||
|
||||
@Override
|
||||
public void encode(ByteArrayTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeInt(tag.getValue().length);
|
||||
outputStream.write(tag.getValue());
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
@Override
|
||||
public ByteArrayTag decode(NbtInputStream inputStream) throws IOException {
|
||||
int len = inputStream.readInt();
|
||||
byte[] bytes = new byte[len];
|
||||
inputStream.read(bytes);
|
||||
|
||||
return new ByteArrayTag(bytes);
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/ByteTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/ByteTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.ByteTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ByteTagCodec implements Codec<ByteTag> {
|
||||
|
||||
@Override
|
||||
public void encode(ByteTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeByte(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new ByteTag(inputStream.readByte());
|
||||
}
|
||||
}
|
||||
14
src/main/java/mc/nbt/codec/Codec.java
Normal file
14
src/main/java/mc/nbt/codec/Codec.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Codec<T extends TagValue> {
|
||||
|
||||
void encode(T tag, NbtOutputStream outputStream) throws IOException;
|
||||
|
||||
T decode(NbtInputStream inputStream) throws IOException;
|
||||
}
|
||||
39
src/main/java/mc/nbt/codec/CodecRegistry.java
Normal file
39
src/main/java/mc/nbt/codec/CodecRegistry.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.TagType;
|
||||
import mc.nbt.TagValue;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class CodecRegistry {
|
||||
private static final CodecRegistry INSTANCE = new CodecRegistry();
|
||||
|
||||
private final Map<TagType, Codec<? extends TagValue>> codecs;
|
||||
|
||||
private CodecRegistry() {
|
||||
codecs = Map.ofEntries(
|
||||
Map.entry(TagType.BYTE, new ByteTagCodec()),
|
||||
Map.entry(TagType.SHORT, new ShortTagCodec()),
|
||||
Map.entry(TagType.INTEGER, new IntegerTagCodec()),
|
||||
Map.entry(TagType.LONG, new LongTagCodec()),
|
||||
Map.entry(TagType.FLOAT, new FloatTagCodec()),
|
||||
Map.entry(TagType.DOUBLE, new DoubleTagCodec()),
|
||||
Map.entry(TagType.BYTE_ARRAY, new ByteArrayTagCodec()),
|
||||
Map.entry(TagType.INTEGER_ARRAY, new IntegerArrayTagCodec()),
|
||||
Map.entry(TagType.LONG_ARRAY, new LongArrayTagCodec()),
|
||||
Map.entry(TagType.STRING, new StringTagCodec()),
|
||||
Map.entry(TagType.LIST, new ListTagCodec()),
|
||||
Map.entry(TagType.COMPOUND, new CompoundTagCodec()),
|
||||
Map.entry(TagType.END, new EndTagCodec())
|
||||
);
|
||||
}
|
||||
|
||||
public Codec<? extends TagValue> get(TagType tagType) {
|
||||
return codecs.get(Objects.requireNonNull(tagType));
|
||||
}
|
||||
|
||||
public static CodecRegistry getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
40
src/main/java/mc/nbt/codec/CompoundTagCodec.java
Normal file
40
src/main/java/mc/nbt/codec/CompoundTagCodec.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.CompoundTag;
|
||||
import mc.nbt.EndTag;
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class CompoundTagCodec implements Codec<CompoundTag> {
|
||||
|
||||
@Override
|
||||
public void encode(CompoundTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
for (Map.Entry<String, TagValue> entry : tag.entrySet()) {
|
||||
if (entry.getValue() == null) //TODO по-хорошему, пользователю нужно запретить вставлять null-теги
|
||||
continue;
|
||||
|
||||
outputStream.writeTag(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
outputStream.writeTag(EndTag.getInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag decode(NbtInputStream inputStream) throws IOException {
|
||||
var compoundTag = new CompoundTag();
|
||||
|
||||
do {
|
||||
var tagValue = inputStream.readTag();
|
||||
if (tagValue.isEnd())
|
||||
break;
|
||||
|
||||
compoundTag.append(tagValue.getName(), tagValue);
|
||||
} while (true);
|
||||
|
||||
return compoundTag;
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/DoubleTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/DoubleTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.DoubleTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class DoubleTagCodec implements Codec<DoubleTag> {
|
||||
|
||||
@Override
|
||||
public void encode(DoubleTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeDouble(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DoubleTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new DoubleTag(inputStream.readDouble());
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/EndTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/EndTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.EndTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class EndTagCodec implements Codec<EndTag> {
|
||||
|
||||
@Override
|
||||
public void encode(EndTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
// nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public EndTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return EndTag.getInstance();
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/FloatTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/FloatTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.FloatTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class FloatTagCodec implements Codec<FloatTag> {
|
||||
|
||||
@Override
|
||||
public void encode(FloatTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeFloat(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public FloatTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new FloatTag(inputStream.readFloat());
|
||||
}
|
||||
}
|
||||
30
src/main/java/mc/nbt/codec/IntegerArrayTagCodec.java
Normal file
30
src/main/java/mc/nbt/codec/IntegerArrayTagCodec.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.IntegerArrayTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class IntegerArrayTagCodec implements Codec<IntegerArrayTag> {
|
||||
|
||||
@Override
|
||||
public void encode(IntegerArrayTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeInt(tag.getValue().length);
|
||||
for (int item : tag.getValue()) {
|
||||
outputStream.writeInt(item);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntegerArrayTag decode(NbtInputStream inputStream) throws IOException {
|
||||
int len = inputStream.readInt();
|
||||
int[] ints = new int[len];
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
ints[i] = inputStream.readInt();
|
||||
}
|
||||
|
||||
return new IntegerArrayTag(ints);
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/IntegerTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/IntegerTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.IntegerTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class IntegerTagCodec implements Codec<IntegerTag> {
|
||||
|
||||
@Override
|
||||
public void encode(IntegerTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeInt(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntegerTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new IntegerTag(inputStream.readInt());
|
||||
}
|
||||
}
|
||||
45
src/main/java/mc/nbt/codec/ListTagCodec.java
Normal file
45
src/main/java/mc/nbt/codec/ListTagCodec.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.ListTag;
|
||||
import mc.nbt.TagType;
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ListTagCodec implements Codec<ListTag> {
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@Override
|
||||
public void encode(ListTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeByte(tag.getListType().getId());
|
||||
outputStream.writeInt(tag.size());
|
||||
|
||||
Codec codec = CodecRegistry.getInstance().get(tag.getListType());
|
||||
for (TagValue tagValue : tag) {
|
||||
codec.encode(tagValue, outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public ListTag decode(NbtInputStream inputStream) throws IOException {
|
||||
int listTypeId = inputStream.readByte();
|
||||
TagType listType = TagType.valueOfId(listTypeId);
|
||||
|
||||
int size = inputStream.readInt();
|
||||
ListTag list = new ListTag(listType, size);
|
||||
|
||||
Codec codec = CodecRegistry.getInstance().get(listType);
|
||||
|
||||
if (size > 0) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
var tagValue = codec.decode(inputStream);
|
||||
list.add(tagValue);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
30
src/main/java/mc/nbt/codec/LongArrayTagCodec.java
Normal file
30
src/main/java/mc/nbt/codec/LongArrayTagCodec.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.LongArrayTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class LongArrayTagCodec implements Codec<LongArrayTag> {
|
||||
|
||||
@Override
|
||||
public void encode(LongArrayTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeInt(tag.getValue().length);
|
||||
for (long value : tag.getValue()) {
|
||||
outputStream.writeLong(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongArrayTag decode(NbtInputStream inputStream) throws IOException {
|
||||
int len = inputStream.readInt();
|
||||
long[] longs = new long[len];
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
longs[i] = inputStream.readLong();
|
||||
}
|
||||
|
||||
return new LongArrayTag(longs);
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/LongTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/LongTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.LongTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class LongTagCodec implements Codec<LongTag> {
|
||||
|
||||
@Override
|
||||
public void encode(LongTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeLong(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new LongTag(inputStream.readLong());
|
||||
}
|
||||
}
|
||||
20
src/main/java/mc/nbt/codec/ShortTagCodec.java
Normal file
20
src/main/java/mc/nbt/codec/ShortTagCodec.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.ShortTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ShortTagCodec implements Codec<ShortTag> {
|
||||
|
||||
@Override
|
||||
public void encode(ShortTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeShort(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ShortTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new ShortTag(inputStream.readShort());
|
||||
}
|
||||
}
|
||||
19
src/main/java/mc/nbt/codec/StringTagCodec.java
Normal file
19
src/main/java/mc/nbt/codec/StringTagCodec.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package mc.nbt.codec;
|
||||
|
||||
import mc.nbt.StringTag;
|
||||
import mc.nbt.io.NbtInputStream;
|
||||
import mc.nbt.io.NbtOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class StringTagCodec implements Codec<StringTag> {
|
||||
@Override
|
||||
public void encode(StringTag tag, NbtOutputStream outputStream) throws IOException {
|
||||
outputStream.writeString(tag.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringTag decode(NbtInputStream inputStream) throws IOException {
|
||||
return new StringTag(inputStream.readString());
|
||||
}
|
||||
}
|
||||
91
src/main/java/mc/nbt/io/NbtInputStream.java
Normal file
91
src/main/java/mc/nbt/io/NbtInputStream.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package mc.nbt.io;
|
||||
|
||||
import mc.nbt.EndTag;
|
||||
import mc.nbt.TagType;
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.codec.Codec;
|
||||
import mc.nbt.codec.CodecRegistry;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
public class NbtInputStream extends InputStream {
|
||||
|
||||
private final DataInputStream dataInputStream;
|
||||
|
||||
public NbtInputStream(InputStream inputStream, boolean compressed) throws IOException {
|
||||
this.dataInputStream = new DataInputStream(compressed
|
||||
? new GZIPInputStream(inputStream)
|
||||
: inputStream);
|
||||
}
|
||||
|
||||
public NbtInputStream(InputStream inputStream) throws IOException {
|
||||
this(inputStream, false);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public TagValue readTag() throws IOException {
|
||||
var tagType = TagType.valueOfId(read());
|
||||
if (tagType == TagType.END) {
|
||||
return EndTag.getInstance();
|
||||
}
|
||||
|
||||
Codec codec = CodecRegistry.getInstance().get(tagType);
|
||||
String name = readString();
|
||||
var tagValue = codec.decode(this);
|
||||
|
||||
tagValue.setName(name);
|
||||
return tagValue;
|
||||
}
|
||||
|
||||
public byte readByte() throws IOException {
|
||||
return dataInputStream.readByte();
|
||||
}
|
||||
|
||||
public short readShort() throws IOException {
|
||||
return dataInputStream.readShort();
|
||||
}
|
||||
|
||||
public int readInt() throws IOException {
|
||||
return dataInputStream.readInt();
|
||||
}
|
||||
|
||||
public long readLong() throws IOException {
|
||||
return dataInputStream.readLong();
|
||||
}
|
||||
|
||||
public float readFloat() throws IOException {
|
||||
return dataInputStream.readFloat();
|
||||
}
|
||||
|
||||
public double readDouble() throws IOException {
|
||||
return dataInputStream.readDouble();
|
||||
}
|
||||
|
||||
public String readString() throws IOException {
|
||||
int length = readShort();
|
||||
if (length <= 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
byte[] buff = new byte[length];
|
||||
if (read(buff, 0, length) < length) {
|
||||
throw new IOException("Unexpected end of stream");
|
||||
}
|
||||
|
||||
return new String(buff, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return dataInputStream.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
dataInputStream.close();
|
||||
}
|
||||
}
|
||||
84
src/main/java/mc/nbt/io/NbtOutputStream.java
Normal file
84
src/main/java/mc/nbt/io/NbtOutputStream.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package mc.nbt.io;
|
||||
|
||||
import mc.nbt.TagValue;
|
||||
import mc.nbt.codec.Codec;
|
||||
import mc.nbt.codec.CodecRegistry;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class NbtOutputStream extends OutputStream {
|
||||
|
||||
private final DataOutputStream dataOutputStream;
|
||||
|
||||
public NbtOutputStream(OutputStream outputStream, boolean compressed) throws IOException {
|
||||
this.dataOutputStream = new DataOutputStream(compressed
|
||||
? new GZIPOutputStream(outputStream)
|
||||
: outputStream);
|
||||
}
|
||||
|
||||
public NbtOutputStream(OutputStream outputStream) throws IOException {
|
||||
this(outputStream, false);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public void writeTag(String name, TagValue tagValue) throws IOException {
|
||||
Codec codec = CodecRegistry.getInstance().get(tagValue.getType());
|
||||
|
||||
writeByte(tagValue.getType().getId());
|
||||
writeString(name);
|
||||
|
||||
codec.encode(tagValue, this);
|
||||
}
|
||||
|
||||
public void writeTag(TagValue tagValue) throws IOException {
|
||||
writeTag(tagValue.getName(), tagValue);
|
||||
}
|
||||
|
||||
public void writeByte(int value) throws IOException {
|
||||
dataOutputStream.writeByte(value);
|
||||
}
|
||||
|
||||
public void writeShort(int value) throws IOException {
|
||||
dataOutputStream.writeShort(value);
|
||||
}
|
||||
|
||||
public void writeInt(int value) throws IOException {
|
||||
dataOutputStream.writeInt(value);
|
||||
}
|
||||
|
||||
public void writeLong(long value) throws IOException {
|
||||
dataOutputStream.writeLong(value);
|
||||
}
|
||||
|
||||
public void writeFloat(float value) throws IOException {
|
||||
dataOutputStream.writeFloat(value);
|
||||
}
|
||||
|
||||
public void writeDouble(double value) throws IOException {
|
||||
dataOutputStream.writeDouble(value);
|
||||
}
|
||||
|
||||
public void writeString(String value) throws IOException {
|
||||
if (value == null || value.isEmpty()) {
|
||||
writeShort(0);
|
||||
} else {
|
||||
writeShort(value.length());
|
||||
write(value.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
dataOutputStream.write(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.dataOutputStream.close();
|
||||
}
|
||||
}
|
||||
38
src/test/java/mc/nbt/io/NbtInputStreamTest.java
Normal file
38
src/test/java/mc/nbt/io/NbtInputStreamTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package mc.nbt.io;
|
||||
|
||||
import mc.nbt.CompoundTag;
|
||||
import mc.nbt.StringTag;
|
||||
import mc.nbt.TagType;
|
||||
import mc.nbt.TagValue;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class NbtInputStreamTest {
|
||||
|
||||
@Test
|
||||
void testSimple() throws IOException {
|
||||
try (var stream = Objects.requireNonNull(NbtInputStreamTest.class.getResourceAsStream("/hello_world.nbt"));
|
||||
NbtInputStream nbtInputStream = new NbtInputStream(stream)) {
|
||||
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertNotNull(tag);
|
||||
assertEquals(TagType.COMPOUND, tag.getType());
|
||||
|
||||
CompoundTag compoundTag = tag.asCompound();
|
||||
assertEquals("hello world", compoundTag.getName());
|
||||
assertEquals(1, compoundTag.size());
|
||||
|
||||
assertTrue(compoundTag.containsKey("name"));
|
||||
tag = compoundTag.get("name");
|
||||
assertEquals(TagType.STRING, tag.getType());
|
||||
|
||||
StringTag stringTag = tag.asString();
|
||||
assertEquals("name", stringTag.getName());
|
||||
assertEquals("Bananrama", stringTag.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
381
src/test/java/mc/nbt/io/NbtOutputStreamTest.java
Normal file
381
src/test/java/mc/nbt/io/NbtOutputStreamTest.java
Normal file
@@ -0,0 +1,381 @@
|
||||
package mc.nbt.io;
|
||||
|
||||
import mc.nbt.*;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class NbtOutputStreamTest {
|
||||
|
||||
ByteArrayOutputStream baos;
|
||||
NbtOutputStream nbtOutputStream;
|
||||
|
||||
@BeforeEach
|
||||
void before() throws IOException {
|
||||
baos = new ByteArrayOutputStream();
|
||||
nbtOutputStream = new NbtOutputStream(baos);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeEndTag() throws IOException {
|
||||
nbtOutputStream.writeTag(EndTag.getInstance());
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.END, tag.getType());
|
||||
assertNull(tag.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagByte")
|
||||
void writeByteTag(ByteTag byteTag) throws IOException {
|
||||
nbtOutputStream.writeTag(byteTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.BYTE, tag.getType());
|
||||
assertEquals(byteTag.getValue(), tag.asByte().getValue());
|
||||
assertName(byteTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagShort")
|
||||
void writeShortTag(ShortTag shortTag) throws IOException {
|
||||
nbtOutputStream.writeTag(shortTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.SHORT, tag.getType());
|
||||
assertEquals(shortTag.getValue(), tag.asShort().getValue());
|
||||
assertName(shortTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagInt")
|
||||
void writeIntegerTag(IntegerTag integerTag) throws IOException {
|
||||
nbtOutputStream.writeTag(integerTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.INTEGER, tag.getType());
|
||||
assertEquals(integerTag.getValue(), tag.asInteger().getValue());
|
||||
assertName(integerTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagLong")
|
||||
void writeLongTag(LongTag longTag) throws IOException {
|
||||
nbtOutputStream.writeTag(longTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.LONG, tag.getType());
|
||||
assertEquals(longTag.getValue(), tag.asLong().getValue());
|
||||
assertName(longTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagFloat")
|
||||
void writeFloatTag(FloatTag floatTag) throws IOException {
|
||||
nbtOutputStream.writeTag(floatTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.FLOAT, tag.getType());
|
||||
assertEquals(floatTag.getValue(), tag.asFloat().getValue());
|
||||
assertName(floatTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagDouble")
|
||||
void writeDoubleTag(DoubleTag doubleTag) throws IOException {
|
||||
nbtOutputStream.writeTag(doubleTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.DOUBLE, tag.getType());
|
||||
assertEquals(doubleTag.getValue(), tag.asDouble().getValue());
|
||||
assertName(doubleTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagByteArray")
|
||||
void writeByteArrayTag(ByteArrayTag byteArrayTag) throws IOException {
|
||||
nbtOutputStream.writeTag(byteArrayTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.BYTE_ARRAY, tag.getType());
|
||||
assertArrayEquals(byteArrayTag.getValue(), tag.asByteArray().getValue());
|
||||
assertName(byteArrayTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagString")
|
||||
void writeStringTag(StringTag stringTag) throws IOException {
|
||||
nbtOutputStream.writeTag(stringTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.STRING, tag.getType());
|
||||
assertEquals(stringTag.getValue(), tag.asString().getValue());
|
||||
assertName(stringTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagList")
|
||||
void writeListTag(ListTag listTag) throws IOException {
|
||||
nbtOutputStream.writeTag(listTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.LIST, tag.getType());
|
||||
assertEquals(listTag.size(), tag.asList().size());
|
||||
assertEquals(listTag.getListType(), tag.asList().getListType());
|
||||
assertName(listTag, tag);
|
||||
|
||||
for (int i = 0; i < listTag.size(); i++) {
|
||||
LongTag longTag1 = listTag.get(i).asLong();
|
||||
LongTag longTag2 = tag.asList().get(i).asLong();
|
||||
|
||||
assertNull(longTag2.getName());
|
||||
assertEquals(longTag1.getValue(), longTag2.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagCompound")
|
||||
void writeCompoundTag(CompoundTag compoundTag) throws IOException {
|
||||
nbtOutputStream.writeTag(compoundTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.COMPOUND, tag.getType());
|
||||
assertEquals(compoundTag.size(), tag.asCompound().size());
|
||||
assertName(compoundTag, tag);
|
||||
|
||||
for (int i = 0; i < compoundTag.size(); i++) {
|
||||
compoundTag.forEach((key, tagValue) -> {
|
||||
assertTrue(tag.asCompound().containsKey(key));
|
||||
|
||||
LongTag longTag1 = tagValue.asLong();
|
||||
LongTag longTag2 = tag.asCompound().get(key).asLong();
|
||||
|
||||
assertNotNull(longTag2.getName());
|
||||
assertEquals(longTag1.getValue(), longTag2.getValue());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagIntArray")
|
||||
void writeIntegerArrayTag(IntegerArrayTag integerArrayTag) throws IOException {
|
||||
nbtOutputStream.writeTag(integerArrayTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.INTEGER_ARRAY, tag.getType());
|
||||
assertArrayEquals(integerArrayTag.getValue(), tag.asIntegerArray().getValue());
|
||||
assertName(integerArrayTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("streamOfTagLongArray")
|
||||
void writeLongArrayTag(LongArrayTag longArrayTag) throws IOException {
|
||||
nbtOutputStream.writeTag(longArrayTag);
|
||||
|
||||
try (var nbtInputStream = createNbtInputStream()) {
|
||||
TagValue tag = nbtInputStream.readTag();
|
||||
assertEquals(TagType.LONG_ARRAY, tag.getType());
|
||||
assertArrayEquals(longArrayTag.getValue(), tag.asLongArray().getValue());
|
||||
assertName(longArrayTag, tag);
|
||||
}
|
||||
}
|
||||
|
||||
// region Methods for ParameterizedTest
|
||||
// -------------------------------------------------------------------------------------------------------------
|
||||
|
||||
private static Stream<Arguments> streamOfTagByte() {
|
||||
final byte value = RandomUtils.nextBytes(1)[0];
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new ByteTag(value)),
|
||||
Arguments.of(new ByteTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagShort() {
|
||||
final short value = (short) RandomUtils.nextInt(0, Short.MAX_VALUE);
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new ShortTag(value)),
|
||||
Arguments.of(new ShortTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagInt() {
|
||||
final int value = RandomUtils.nextInt();
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new IntegerTag(value)),
|
||||
Arguments.of(new IntegerTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagLong() {
|
||||
final long value = RandomUtils.nextLong();
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new LongTag(value)),
|
||||
Arguments.of(new LongTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagFloat() {
|
||||
final float value = RandomUtils.nextFloat();
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new FloatTag(value)),
|
||||
Arguments.of(new FloatTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagDouble() {
|
||||
final double value = RandomUtils.nextDouble();
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new DoubleTag(value)),
|
||||
Arguments.of(new DoubleTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagByteArray() {
|
||||
final byte[] value = RandomUtils.nextBytes(RandomUtils.nextInt(1, 1000));
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new ByteArrayTag(value)),
|
||||
Arguments.of(new ByteArrayTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagString() {
|
||||
final String value = randomString();
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new StringTag(value)),
|
||||
Arguments.of(new StringTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagList() {
|
||||
return Stream.of(
|
||||
Arguments.of(new ListTag(TagType.LONG).append(new LongTag(1L))),
|
||||
Arguments.of(new ListTag(TagType.LONG) {{
|
||||
this.setName(randomString());
|
||||
}}.append(new LongTag(1L)))
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagCompound() {
|
||||
return Stream.of(
|
||||
Arguments.of(new CompoundTag()
|
||||
.append("TestName1", new LongTag(RandomUtils.nextLong()))
|
||||
.append("TestName2", new LongTag(RandomUtils.nextLong()))
|
||||
.append("TestName3", new LongTag(RandomUtils.nextLong()))),
|
||||
Arguments.of(new CompoundTag() {{
|
||||
this.setName(randomString());
|
||||
}}
|
||||
.append("TestName1", new LongTag(RandomUtils.nextLong()))
|
||||
.append("TestName2", new LongTag(RandomUtils.nextLong()))
|
||||
.append("TestName3", new LongTag(RandomUtils.nextLong())))
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagIntArray() {
|
||||
final int[] value = new int[RandomUtils.nextInt(1, 1000)];
|
||||
|
||||
for (int i = 0; i < value.length; i++) {
|
||||
value[i] = RandomUtils.nextInt();
|
||||
}
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new IntegerArrayTag(value)),
|
||||
Arguments.of(new IntegerArrayTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> streamOfTagLongArray() {
|
||||
final long[] value = new long[RandomUtils.nextInt(1, 1000)];
|
||||
|
||||
for (int i = 0; i < value.length; i++) {
|
||||
value[i] = RandomUtils.nextLong();
|
||||
}
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(new LongArrayTag(value)),
|
||||
Arguments.of(new LongArrayTag(value) {{
|
||||
this.setName(randomString());
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------
|
||||
// endregion
|
||||
|
||||
private NbtInputStream createNbtInputStream() throws IOException {
|
||||
return new NbtInputStream(new ByteArrayInputStream(baos.toByteArray()));
|
||||
}
|
||||
|
||||
private static String randomString() {
|
||||
return RandomStringUtils.randomAscii(RandomUtils.nextInt(1, Short.MAX_VALUE));
|
||||
}
|
||||
|
||||
private void assertName(TagValue expectedTag, TagValue actualTag) {
|
||||
if (expectedTag.getName() == null) {
|
||||
assertTrue(actualTag.getName().isEmpty());
|
||||
} else {
|
||||
assertEquals(expectedTag.getName(), actualTag.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
src/test/resources/hello_world.nbt
Normal file
BIN
src/test/resources/hello_world.nbt
Normal file
Binary file not shown.
Reference in New Issue
Block a user