From 970087575850a0eee1bca6683be7bcd446ea32c3 Mon Sep 17 00:00:00 2001 From: WakelessSloth56 Date: Thu, 11 Aug 2022 17:38:41 +0800 Subject: [PATCH] feat(utils): methods for drawing particle cuboid --- .../arnicalib/utils/game/ParticlePainter.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/ParticlePainter.java b/src/main/java/org/auioc/mcmod/arnicalib/utils/game/ParticlePainter.java index d1f1d9d4..0afc7233 100644 --- a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/ParticlePainter.java +++ b/src/main/java/org/auioc/mcmod/arnicalib/utils/game/ParticlePainter.java @@ -11,6 +11,7 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraft.util.Mth; import net.minecraft.world.phys.AABB; +import net.minecraft.world.phys.Vec3; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -25,6 +26,10 @@ public static record Options(double stepLength, ParticleOptions particle, boolea public static class Client { @SuppressWarnings("resource") + public static void drawPoint(double x, double y, double z, Options options) { + Minecraft.getInstance().levelRenderer.addParticle(options.particle, options.force, !options.force, x, y, z, 0.0D, 0.0D, 0.0D); + } + public static void drawLine(double x1, double y1, double z1, double x2, double y2, double z2, Options options) { double diffX = (x2 - x1); double diffY = (y2 - y1); @@ -42,7 +47,15 @@ public static void drawLine(double x1, double y1, double z1, double x2, double y double x = x1 + projectedStepLengthX * i; double y = y1 + projectedStepLengthY * i; double z = z1 + projectedStepLengthZ * i; - Minecraft.getInstance().levelRenderer.addParticle(options.particle, options.force, !options.force, x, y, z, 0.0D, 0.0D, 0.0D); + drawPoint(x, y, z, options); + } + } + + public static void drawPolygon(Vec3[] vertexes, Options options) { + for (int i = 0, l = vertexes.length; i < l; i++) { + Vec3 a = vertexes[i]; + Vec3 b = vertexes[(i + 1 == l) ? 0 : i + 1]; + drawLine(a.x, a.y, a.z, b.x, b.y, b.z, options); } } @@ -63,6 +76,14 @@ public static void draw(Shape shape, CompoundTag data, Options options) { case CUBOID -> { drawCuboid(NbtUtils.getAABB(data, "AABB"), options); } + case POLYGON -> { + double[] p = NbtUtils.getDoubleArray(data, "Vertexes"); + Vec3[] vertexes = new Vec3[p.length / 3]; + for (int i = 0, j = 0; i < vertexes.length; i++, j = i * 3) { + vertexes[i] = new Vec3(p[j], p[j + 1], p[j + 2]); + } + drawPolygon(vertexes, options); + } default -> throw new IllegalArgumentException("Unexpected shape: " + shape); } } @@ -90,6 +111,18 @@ public static void drawCuboid(ServerPlayer player, AABB aabb, Options options) { }); } + public static void drawPolygon(ServerPlayer player, Vec3[] vertexes, Options options) { + draw(player, Shape.POLYGON, options, (nbt) -> { + double[] p = new double[vertexes.length * 3]; + for (int i = 0, j = 0; i < vertexes.length; i++, j = i * 3) { + p[j] = vertexes[i].x; + p[j + 1] = vertexes[i].y; + p[j + 2] = vertexes[i].z; + } + nbt.put("Vertexes", NbtUtils.writeDoubleArray(p)); + }); + } + } }