Skip to content

Commit 7b4c2be

Browse files
committed
Edge cases for nbt vector
1 parent cd147b7 commit 7b4c2be

File tree

1 file changed

+29
-0
lines changed
  • common/src/main/kotlin/com/lambda/util/extension

1 file changed

+29
-0
lines changed

common/src/main/kotlin/com/lambda/util/extension/Nbt.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,39 @@ package com.lambda.util.extension
2020
import net.minecraft.nbt.NbtCompound
2121
import net.minecraft.nbt.NbtInt
2222
import net.minecraft.nbt.NbtList
23+
import net.minecraft.util.math.Vec3i
2324

2425
/**
2526
* Puts a list of integer into the component, this is not the same as an int array
2627
*/
2728
fun NbtCompound.putIntList(key: String, vararg values: Int) {
2829
put(key, values.fold(NbtList()) { list, value -> list.add(NbtInt.of(value)); list })
2930
}
31+
32+
/**
33+
* Retrieves a vector from a tuple
34+
*/
35+
fun NbtCompound.getVector(key: String): Vec3i {
36+
val compound = getCompound(key)
37+
38+
var x = compound.getInt("x")
39+
var y = compound.getInt("y")
40+
var z = compound.getInt("z")
41+
42+
if (x == 0 && y == 0 && z == 0) {
43+
x = compound.getInt("X")
44+
y = compound.getInt("Y")
45+
z = compound.getInt("Z")
46+
}
47+
48+
if (compound.isEmpty) {
49+
val arr = getIntArray(key)
50+
if (arr.size == 3) {
51+
x = arr[0]
52+
y = arr[1]
53+
z = arr[2]
54+
}
55+
}
56+
57+
return Vec3i(x, y, z)
58+
}

0 commit comments

Comments
 (0)