Skip to content

Commit

Permalink
feat(utils): methods for operating nbt with double array and aabb
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Aug 11, 2022
1 parent f9a6ac1 commit f29aec9
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/main/java/org/auioc/mcmod/arnicalib/utils/game/NbtUtils.java
@@ -0,0 +1,49 @@
package org.auioc.mcmod.arnicalib.utils.game;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.DoubleTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.phys.AABB;

public class NbtUtils {

public static ListTag writeDoubleArray(double... values) {
var listTag = new ListTag();
for (int i = 0; i < values.length; i++) {
listTag.addTag(i, DoubleTag.valueOf(values[i]));
}
return listTag;
}

public static double[] readDoubleArray(ListTag nbt) {
int size = nbt.size();
var result = new double[size];
for (int i = 0; i < size; i++) {
result[i] = nbt.getDouble(i);
}
return result;
}

public static ListTag getDoubleListTag(CompoundTag nbt, String key) {
return nbt.getList(key, 6);
}

public static double[] getDoubleArray(CompoundTag nbt, String key) {
return readDoubleArray(getDoubleListTag(nbt, key));
}


public static ListTag writeAABB(AABB aabb) {
return writeDoubleArray(aabb.minX, aabb.minY, aabb.minZ, aabb.maxX, aabb.maxY, aabb.maxZ);
}

public static AABB readAABB(ListTag nbt) {
double[] p = readDoubleArray(nbt);
return new AABB(p[0], p[1], p[2], p[3], p[4], p[5]);
}

public static AABB getAABB(CompoundTag nbt, String key) {
return readAABB(getDoubleListTag(nbt, key));
}

}

0 comments on commit f29aec9

Please sign in to comment.