Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vec3i serialization #187

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/main/java/io/wispforest/owo/util/VectorSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
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;

import java.util.List;

/**
* Utility class for reading and storing {@link Vec3d} and
Expand Down Expand Up @@ -55,6 +59,22 @@ 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) {

nbt.putIntArray(key, List.of(vec3i.getX(), vec3i.getY(), vec3i.getZ()));

return nbt;
}

/**
* Gets the vector stored at the given key in the
* given nbt compound
Expand Down Expand Up @@ -91,6 +111,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) {

int[] vectorArray = nbt.getIntArray(key);
int x = vectorArray[0];
int y = vectorArray[1];
int z = vectorArray[2];

return new Vec3i(x, y, z);
}

/**
* Writes the given vector into the given packet buffer
*
Expand All @@ -115,6 +153,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.writeInt(vec3i.getX());
buffer.writeInt(vec3i.getY());
buffer.writeInt(vec3i.getZ());
}

/**
* Reads one vector from the given packet buffer
*
Expand All @@ -135,4 +185,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 Vec3i readi(PacketByteBuf buffer) {
return new Vec3i(buffer.readInt(), buffer.readInt(), buffer.readInt());
}
}
Loading