From f29aec9384bbd1cae4c5049d8408f3b726d9ca70 Mon Sep 17 00:00:00 2001 From: WakelessSloth56 Date: Thu, 11 Aug 2022 08:06:56 +0800 Subject: [PATCH] feat(utils): methods for operating nbt with double array and aabb --- .../mcmod/arnicalib/utils/game/NbtUtils.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/org/auioc/mcmod/arnicalib/utils/game/NbtUtils.java diff --git a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/NbtUtils.java b/src/main/java/org/auioc/mcmod/arnicalib/utils/game/NbtUtils.java new file mode 100644 index 00000000..5eefaa66 --- /dev/null +++ b/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)); + } + +}