fix: fix Vector3 transfer between nodes#953
fix: fix Vector3 transfer between nodes#953Roll1r wants to merge 1 commit intoAlexProgrammerDE:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a runtime type mismatch where ports declared as VECTOR3 could receive a String (via Vec3#toString() fallback), which could break downstream nodes like pathfinding.
Changes:
- Convert
net.minecraft.world.phys.Vec3values inNodeValue.of(Object)into a JSON array[x, y, z]instead of falling back to string conversion. - Add a
NodeValue.Vector3variant and updateNodeValueTypeCheckerto recognize it asPortType.VECTOR3.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
mod/src/main/java/com/soulfiremc/server/script/NodeValueTypeChecker.java |
Adds explicit runtime type-checking and type description support for NodeValue.Vector3. |
mod/src/main/java/com/soulfiremc/server/script/NodeValue.java |
Ensures Vec3 values are encoded in a VECTOR3-compatible format (JSON array) and introduces a Vector3 NodeValue variant + accessor. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var arr = new JsonArray(); | ||
| arr.add(v.x); | ||
| arr.add(v.y); | ||
| arr.add(v.z); | ||
| return new Json(arr); |
There was a problem hiding this comment.
NodeValue.of(Vec3) currently converts vectors to a JSON array, but this PR also introduces a dedicated NodeValue.Vector3 type (and type-checker support) that is never constructed here. This creates two competing representations for VECTOR3 values and makes logs/type descriptions inconsistent (JSON arrays still describe as "List"). Consider either (a) removing the Vector3 NodeValue variant and keeping the JSON-array representation only, or (b) returning new Vector3(v) here and updating consumers (e.g. AbstractScriptNode#getInput) to also accept that representation.
| var arr = new JsonArray(); | |
| arr.add(v.x); | |
| arr.add(v.y); | |
| arr.add(v.z); | |
| return new Json(arr); | |
| return new Vector3(v); |
| default Vec3 asVec3() { | ||
| if (this instanceof Vector3(Vec3 v)) { | ||
| return v; | ||
| } |
There was a problem hiding this comment.
asVec3() only unwraps the new NodeValue.Vector3 record, but NodeValue.of(Vec3) encodes vectors as a JSON array. As a result, asVec3() will return null for the most common Vec3 values produced by nodes (e.g., result("vector", new Vec3(...))). Either expand asVec3() to also parse the JSON VECTOR3 representation (array/object), or align NodeValue.of(Vec3) to produce NodeValue.Vector3.
| } | |
| } | |
| if (this instanceof Json(JsonElement element)) { | |
| // Array representation: [x, y, z] | |
| if (element.isJsonArray()) { | |
| var array = element.getAsJsonArray(); | |
| if (array.size() == 3 | |
| && array.get(0).isJsonPrimitive() | |
| && array.get(1).isJsonPrimitive() | |
| && array.get(2).isJsonPrimitive()) { | |
| try { | |
| double x = array.get(0).getAsDouble(); | |
| double y = array.get(1).getAsDouble(); | |
| double z = array.get(2).getAsDouble(); | |
| return new Vec3(x, y, z); | |
| } catch (NumberFormatException | UnsupportedOperationException ignored) { | |
| // Fall through to return null. | |
| } | |
| } | |
| } | |
| // Object representation: { "x": ..., "y": ..., "z": ... } | |
| if (element.isJsonObject()) { | |
| var obj = element.getAsJsonObject(); | |
| if (obj.has("x") && obj.has("y") && obj.has("z")) { | |
| try { | |
| double x = obj.get("x").getAsDouble(); | |
| double y = obj.get("y").getAsDouble(); | |
| double z = obj.get("z").getAsDouble(); | |
| return new Vec3(x, y, z); | |
| } catch (ClassCastException | IllegalStateException | NumberFormatException ignored) { | |
| // Fall through to return null. | |
| } | |
| } | |
| } | |
| } |
| if (value instanceof Vec3 v) { | ||
| var arr = new JsonArray(); | ||
| arr.add(v.x); | ||
| arr.add(v.y); | ||
| arr.add(v.z); | ||
| return new Json(arr); | ||
| } |
There was a problem hiding this comment.
Vector handling is being changed in NodeValue.of(Object) for Vec3. There are already unit tests for NodeValue creation/conversions; please add coverage to prevent regression (e.g., assert NodeValue.of(new Vec3(...)) produces a VECTOR3-compatible value and does not fall back to a string).
Fix [WARN] Type mismatch at runtime when port 'destination' receives a String instead of VECTOR3. Without this fix, subsequent nodes such as pathfinding could fail.