From 29338a6ce9fd61514fabe13d32291f62c5f73c39 Mon Sep 17 00:00:00 2001 From: chyzman <32403637+chyzman@users.noreply.github.com> Date: Tue, 24 Oct 2023 16:29:39 -0400 Subject: [PATCH 1/4] vec3i serialization that it --- .../wispforest/owo/util/VectorSerializer.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/main/java/io/wispforest/owo/util/VectorSerializer.java b/src/main/java/io/wispforest/owo/util/VectorSerializer.java index bf953727..d28d9741 100644 --- a/src/main/java/io/wispforest/owo/util/VectorSerializer.java +++ b/src/main/java/io/wispforest/owo/util/VectorSerializer.java @@ -3,7 +3,9 @@ import net.minecraft.nbt.*; import net.minecraft.network.PacketByteBuf; import net.minecraft.util.math.Vec3d; +import net.minecraft.util.math.Vec3i; import org.joml.Vector3f; +import org.joml.Vector3i; /** * Utility class for reading and storing {@link Vec3d} and @@ -55,6 +57,27 @@ public static NbtCompound putf(NbtCompound nbt, String key, Vector3f vec3f) { return nbt; } + /** + * Stores the given vector as an array at the + * given key in the given nbt compound + * + * @param vec3i The vector to serialize + * @param nbt The nbt compound to serialize into + * @param key The key to use + * @return {@code nbt} + */ + public static NbtCompound puti(NbtCompound nbt, String key, Vec3i vec3i) { + + NbtList vectorArray = new NbtList(); + vectorArray.add(NbtInt.of(vec3i.getX())); + vectorArray.add(NbtInt.of(vec3i.getY())); + vectorArray.add(NbtInt.of(vec3i.getZ())); + + nbt.put(key, vectorArray); + + return nbt; + } + /** * Gets the vector stored at the given key in the * given nbt compound @@ -91,6 +114,24 @@ public static Vector3f getf(NbtCompound nbt, String key) { return new Vector3f(x, y, z); } + /** + * Gets the vector stored at the given key in the + * given nbt compound + * + * @param nbt The nbt compound to read from + * @param key The key the read from + * @return The deserialized vector + */ + public static Vec3i geti(NbtCompound nbt, String key) { + + NbtList vectorArray = nbt.getList(key, NbtElement.INT_TYPE); + int x = vectorArray.getInt(0); + int y = vectorArray.getInt(1); + int z = vectorArray.getInt(2); + + return new Vec3i(x, y, z); + } + /** * Writes the given vector into the given packet buffer * @@ -115,6 +156,18 @@ public static void writef(PacketByteBuf buffer, Vector3f vec3f) { buffer.writeFloat(vec3f.z); } + /** + * Writes the given vector into the given packet buffer + * + * @param vec3i The vector to write + * @param buffer The packet buffer to write into + */ + public static void writei(PacketByteBuf buffer, Vec3i vec3i) { + buffer.writeFloat(vec3i.getX()); + buffer.writeFloat(vec3i.getY()); + buffer.writeFloat(vec3i.getZ()); + } + /** * Reads one vector from the given packet buffer * @@ -135,4 +188,13 @@ public static Vector3f readf(PacketByteBuf buffer) { return new Vector3f(buffer.readFloat(), buffer.readFloat(), buffer.readFloat()); } + /** + * Reads one vector from the given packet buffer + * + * @param buffer The buffer to read from + * @return The deserialized vector + */ + public static Vector3f readi(PacketByteBuf buffer) { + return new Vector3f(buffer.readInt(), buffer.readInt(), buffer.readInt()); + } } From ebd3108c1facf8d773098324459e7fc1cf526e76 Mon Sep 17 00:00:00 2001 From: chyzman <32403637+chyzman@users.noreply.github.com> Date: Sun, 29 Oct 2023 20:31:58 -0400 Subject: [PATCH 2/4] Update VectorSerializer.java --- .../java/io/wispforest/owo/util/VectorSerializer.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/wispforest/owo/util/VectorSerializer.java b/src/main/java/io/wispforest/owo/util/VectorSerializer.java index d28d9741..cad54bd2 100644 --- a/src/main/java/io/wispforest/owo/util/VectorSerializer.java +++ b/src/main/java/io/wispforest/owo/util/VectorSerializer.java @@ -163,9 +163,9 @@ public static void writef(PacketByteBuf buffer, Vector3f vec3f) { * @param buffer The packet buffer to write into */ public static void writei(PacketByteBuf buffer, Vec3i vec3i) { - buffer.writeFloat(vec3i.getX()); - buffer.writeFloat(vec3i.getY()); - buffer.writeFloat(vec3i.getZ()); + buffer.writeInt(vec3i.getX()); + buffer.writeInt(vec3i.getY()); + buffer.writeInt(vec3i.getZ()); } /** @@ -194,7 +194,7 @@ public static Vector3f readf(PacketByteBuf buffer) { * @param buffer The buffer to read from * @return The deserialized vector */ - public static Vector3f readi(PacketByteBuf buffer) { - return new Vector3f(buffer.readInt(), buffer.readInt(), buffer.readInt()); + public static Vec3i readi(PacketByteBuf buffer) { + return new Vec3i(buffer.readInt(), buffer.readInt(), buffer.readInt()); } } From 594ed66982194219293cd957a908206921d6d363 Mon Sep 17 00:00:00 2001 From: chyzman <32403637+chyzman@users.noreply.github.com> Date: Sun, 29 Oct 2023 20:36:11 -0400 Subject: [PATCH 3/4] Update VectorSerializer.java --- .../io/wispforest/owo/util/VectorSerializer.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/wispforest/owo/util/VectorSerializer.java b/src/main/java/io/wispforest/owo/util/VectorSerializer.java index cad54bd2..e06ec4ad 100644 --- a/src/main/java/io/wispforest/owo/util/VectorSerializer.java +++ b/src/main/java/io/wispforest/owo/util/VectorSerializer.java @@ -68,10 +68,7 @@ public static NbtCompound putf(NbtCompound nbt, String key, Vector3f vec3f) { */ public static NbtCompound puti(NbtCompound nbt, String key, Vec3i vec3i) { - NbtList vectorArray = new NbtList(); - vectorArray.add(NbtInt.of(vec3i.getX())); - vectorArray.add(NbtInt.of(vec3i.getY())); - vectorArray.add(NbtInt.of(vec3i.getZ())); + NbtIntArray vectorArray = new NbtIntArray(List.of(vec3i.getX(), vec3i.getY(), vec3i.getZ())); nbt.put(key, vectorArray); @@ -124,10 +121,10 @@ public static Vector3f getf(NbtCompound nbt, String key) { */ public static Vec3i geti(NbtCompound nbt, String key) { - NbtList vectorArray = nbt.getList(key, NbtElement.INT_TYPE); - int x = vectorArray.getInt(0); - int y = vectorArray.getInt(1); - int z = vectorArray.getInt(2); + int[] vectorArray = nbt.getIntArray(key); + int x = vectorArray[0]; + int y = vectorArray[1]; + int z = vectorArray[2]; return new Vec3i(x, y, z); } From 6e45b925e2982228e9458e236e63e4dbb9654c2b Mon Sep 17 00:00:00 2001 From: chyzman <32403637+chyzman@users.noreply.github.com> Date: Sun, 29 Oct 2023 20:46:23 -0400 Subject: [PATCH 4/4] Update VectorSerializer.java --- src/main/java/io/wispforest/owo/util/VectorSerializer.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/wispforest/owo/util/VectorSerializer.java b/src/main/java/io/wispforest/owo/util/VectorSerializer.java index e06ec4ad..47c97238 100644 --- a/src/main/java/io/wispforest/owo/util/VectorSerializer.java +++ b/src/main/java/io/wispforest/owo/util/VectorSerializer.java @@ -7,6 +7,8 @@ import org.joml.Vector3f; import org.joml.Vector3i; +import java.util.List; + /** * Utility class for reading and storing {@link Vec3d} and * {@link Vector3f} from and into {@link net.minecraft.nbt.NbtCompound} @@ -68,9 +70,7 @@ public static NbtCompound putf(NbtCompound nbt, String key, Vector3f vec3f) { */ public static NbtCompound puti(NbtCompound nbt, String key, Vec3i vec3i) { - NbtIntArray vectorArray = new NbtIntArray(List.of(vec3i.getX(), vec3i.getY(), vec3i.getZ())); - - nbt.put(key, vectorArray); + nbt.putIntArray(key, List.of(vec3i.getX(), vec3i.getY(), vec3i.getZ())); return nbt; }