-
Notifications
You must be signed in to change notification settings - Fork 39
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
vec3i serialization #187
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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