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 1 commit
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
62 changes: 62 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,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
Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When serializing an array of integers, use an NbtIntArray

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
Expand Down Expand Up @@ -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
*
Expand All @@ -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
*
Expand All @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the greatest idea to read integers from a buffer you've written floats to. Also like, don't write floats :poggus:

}
}
Loading