diff --git a/src/main/java/org/auioc/mcmod/arnicalib/Initialization.java b/src/main/java/org/auioc/mcmod/arnicalib/Initialization.java index 01cd3ca5..005d96ee 100644 --- a/src/main/java/org/auioc/mcmod/arnicalib/Initialization.java +++ b/src/main/java/org/auioc/mcmod/arnicalib/Initialization.java @@ -3,7 +3,6 @@ import org.auioc.mcmod.arnicalib.client.config.AHClientConfig; import org.auioc.mcmod.arnicalib.client.event.AHClientEventHandler; import org.auioc.mcmod.arnicalib.common.itemgroup.AHCreativeModeTabs; -import org.auioc.mcmod.arnicalib.common.network.AHPacketHandler; import org.auioc.mcmod.arnicalib.server.event.AHServerEventHandler; import org.auioc.mcmod.arnicalib.server.loot.AHGlobalLootModifiers; import org.auioc.mcmod.arnicalib.server.loot.AHLootItemConditions; @@ -50,7 +49,6 @@ public CommonSetup(final IEventBus modEventBus, final IEventBus forgeEventBus) { public void registerConfig() {} private void modSetup() { - AHPacketHandler.init(); modEventBus.register(AHGlobalLootModifiers.class); AHLootItemConditions.LOOT_CONDITION_TYPES.register(modEventBus); AHLootItemFunctions.LOOT_FUNCTION_TYPES.register(modEventBus); diff --git a/src/main/java/org/auioc/mcmod/arnicalib/common/network/AHPacketHandler.java b/src/main/java/org/auioc/mcmod/arnicalib/common/network/AHPacketHandler.java deleted file mode 100644 index c8aba62c..00000000 --- a/src/main/java/org/auioc/mcmod/arnicalib/common/network/AHPacketHandler.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.auioc.mcmod.arnicalib.common.network; - -import org.auioc.mcmod.arnicalib.ArnicaLib; -import org.auioc.mcmod.arnicalib.game.network.HPacketHandler; -import org.auioc.mcmod.arnicalib.game.network.IHPacket; -import net.minecraft.server.level.ServerPlayer; - -public final class AHPacketHandler { - - private static final String PROTOCOL_VERSION = Integer.toString(1); - protected static final HPacketHandler HANDLER = new HPacketHandler(ArnicaLib.id("main"), PROTOCOL_VERSION); - - public static void init() { - AHPackets.register(); - } - - public static void sendToServer(MSG msg) { - HANDLER.sendToServer(msg); - } - - public static void sendToClient(ServerPlayer player, MSG msg) { - HANDLER.sendToClient(player, msg); - } - -} diff --git a/src/main/java/org/auioc/mcmod/arnicalib/common/network/AHPackets.java b/src/main/java/org/auioc/mcmod/arnicalib/common/network/AHPackets.java deleted file mode 100644 index f8f0dd46..00000000 --- a/src/main/java/org/auioc/mcmod/arnicalib/common/network/AHPackets.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.auioc.mcmod.arnicalib.common.network; - -import static org.auioc.mcmod.arnicalib.common.network.AHPacketHandler.HANDLER; -import org.auioc.mcmod.arnicalib.common.network.packet.client.ClientboundDrawParticleShapePacket; - -public final class AHPackets { - - protected static void register() { - HANDLER.registerServerToClient(ClientboundDrawParticleShapePacket.class, ClientboundDrawParticleShapePacket::decode); - } - -} diff --git a/src/main/java/org/auioc/mcmod/arnicalib/common/network/packet/client/ClientboundDrawParticleShapePacket.java b/src/main/java/org/auioc/mcmod/arnicalib/common/network/packet/client/ClientboundDrawParticleShapePacket.java deleted file mode 100644 index 8d870e74..00000000 --- a/src/main/java/org/auioc/mcmod/arnicalib/common/network/packet/client/ClientboundDrawParticleShapePacket.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.auioc.mcmod.arnicalib.common.network.packet.client; - -import org.auioc.mcmod.arnicalib.base.phys.Shape; -import org.auioc.mcmod.arnicalib.game.network.IHPacket; -import org.auioc.mcmod.arnicalib.utils.game.ParticlePainter; -import org.auioc.mcmod.arnicalib.utils.game.ParticlePainter.Options; -import net.minecraft.core.particles.ParticleOptions; -import net.minecraft.core.particles.ParticleType; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; -import net.minecraftforge.network.NetworkEvent.Context; -import net.minecraftforge.registries.ForgeRegistries; - -public class ClientboundDrawParticleShapePacket implements IHPacket { - - private final Shape shape; - private final Options options; - private final CompoundTag data; - - public ClientboundDrawParticleShapePacket(Shape shape, Options options, CompoundTag data) { - this.shape = shape; - this.options = options; - this.data = data; - } - - @Override - @OnlyIn(Dist.CLIENT) - public void handle(Context ctx) { - ParticlePainter.Client.draw(this.shape, this.options, this.data); - } - - @Override - public void encode(FriendlyByteBuf buffer) { - buffer.writeEnum(this.shape); - buffer.writeDouble(this.options.stepLength()); - buffer.writeResourceLocation(this.options.particle().getType().getRegistryName()); - this.options.particle().writeToNetwork(buffer); - buffer.writeBoolean(this.options.force()); - buffer.writeNbt(this.data); - } - - public static ClientboundDrawParticleShapePacket decode(FriendlyByteBuf buffer) { - var shape = buffer.readEnum(Shape.class); - Options options; - { - var stepLength = buffer.readDouble(); - var _particleType = ForgeRegistries.PARTICLE_TYPES.getValue(buffer.readResourceLocation()); - var particle = readParticle(buffer, _particleType); - var force = buffer.readBoolean(); - options = new Options(stepLength, particle, force); - } - var data = buffer.readNbt(); - return new ClientboundDrawParticleShapePacket(shape, options, data); - } - - @SuppressWarnings("deprecation") - private static T readParticle(FriendlyByteBuf buffer, ParticleType particleType) { - return particleType.getDeserializer().fromNetwork(particleType, buffer); - } - -} diff --git a/src/main/java/org/auioc/mcmod/arnicalib/game/particle/ClientParticlePainter.java b/src/main/java/org/auioc/mcmod/arnicalib/game/particle/ClientParticlePainter.java new file mode 100644 index 00000000..49ad7666 --- /dev/null +++ b/src/main/java/org/auioc/mcmod/arnicalib/game/particle/ClientParticlePainter.java @@ -0,0 +1,81 @@ +package org.auioc.mcmod.arnicalib.game.particle; + +import org.auioc.mcmod.arnicalib.base.phys.Shape; +import org.auioc.mcmod.arnicalib.utils.game.AABBUtils; +import org.auioc.mcmod.arnicalib.utils.game.NbtUtils; +import net.minecraft.client.Minecraft; +import net.minecraft.nbt.CompoundTag; +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; + +@OnlyIn(Dist.CLIENT) +public class ClientParticlePainter { + + @SuppressWarnings("resource") + public static void drawPoint(ParticlePainterOptions options, double x, double y, double z) { + Minecraft.getInstance().levelRenderer.addParticle(options.particle(), options.force(), !options.force(), x, y, z, 0.0D, 0.0D, 0.0D); + } + + public static void drawLine(ParticlePainterOptions options, double x1, double y1, double z1, double x2, double y2, double z2) { + double diffX = (x2 - x1); + double diffY = (y2 - y1); + double diffZ = (z2 - z1); + + double lineLength = Mth.length(diffX, diffY, diffZ); + + long pointCount = Math.round(lineLength / options.stepLength()); + + double projectedStepLengthX = diffX / pointCount; + double projectedStepLengthY = diffY / pointCount; + double projectedStepLengthZ = diffZ / pointCount; + + for (long i = 0; i < pointCount; i++) { + double x = x1 + projectedStepLengthX * i; + double y = y1 + projectedStepLengthY * i; + double z = z1 + projectedStepLengthZ * i; + drawPoint(options, x, y, z); + } + } + + public static void drawPolygon(ParticlePainterOptions options, Vec3[] vertexes) { + for (int i = 0, l = vertexes.length; i < l; i++) { + Vec3 a = vertexes[i]; + Vec3 b = vertexes[(i + 1 == l) ? 0 : i + 1]; + drawLine(options, a.x, a.y, a.z, b.x, b.y, b.z); + } + } + + public static void drawCuboid(ParticlePainterOptions options, AABB aabb) { + double[][] edges = AABBUtils.getEdges(aabb); + for (double[] edge : edges) { + drawLine(options, edge[0], edge[1], edge[2], edge[3], edge[4], edge[5]); + } + } + + + public static void draw(Shape shape, ParticlePainterOptions options, CompoundTag data) { + switch (shape) { + case LINE -> { + double[] p1 = NbtUtils.getDoubleArray(data, "StartPoint"); + double[] p2 = NbtUtils.getDoubleArray(data, "EndPoint"); + drawLine(options, p1[0], p1[1], p1[2], p2[0], p2[1], p2[2]); + } + case CUBOID -> { + drawCuboid(options, NbtUtils.getAABB(data, "AABB")); + } + 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(options, vertexes); + } + default -> throw new IllegalArgumentException("Unexpected shape: " + shape); + } + } + +} diff --git a/src/main/java/org/auioc/mcmod/arnicalib/game/particle/ParticlePainterOptions.java b/src/main/java/org/auioc/mcmod/arnicalib/game/particle/ParticlePainterOptions.java new file mode 100644 index 00000000..d26efe12 --- /dev/null +++ b/src/main/java/org/auioc/mcmod/arnicalib/game/particle/ParticlePainterOptions.java @@ -0,0 +1,7 @@ +package org.auioc.mcmod.arnicalib.game.particle; + +import net.minecraft.core.particles.ParticleOptions; + +public record ParticlePainterOptions(double stepLength, ParticleOptions particle, boolean force) { + +} 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 deleted file mode 100644 index 3ab1b422..00000000 --- a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/ParticlePainter.java +++ /dev/null @@ -1,162 +0,0 @@ -package org.auioc.mcmod.arnicalib.utils.game; - -import java.util.function.Consumer; -import org.auioc.mcmod.arnicalib.base.phys.Shape; -import org.auioc.mcmod.arnicalib.common.network.AHPacketHandler; -import org.auioc.mcmod.arnicalib.common.network.packet.client.ClientboundDrawParticleShapePacket; -import net.minecraft.client.Minecraft; -import net.minecraft.core.Vec3i; -import net.minecraft.core.particles.ParticleOptions; -import net.minecraft.core.particles.ParticleTypes; -import net.minecraft.nbt.CompoundTag; -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; - -public class ParticlePainter { - - public static final Options DEFAULT_OPTIONS = new Options(0.25D, ParticleTypes.ELECTRIC_SPARK, true); - - public static record Options(double stepLength, ParticleOptions particle, boolean force) { - } - - @OnlyIn(Dist.CLIENT) - public static class Client { - - @SuppressWarnings("resource") - public static void drawPoint(Options options, double x, double y, double z) { - Minecraft.getInstance().levelRenderer.addParticle(options.particle, options.force, !options.force, x, y, z, 0.0D, 0.0D, 0.0D); - } - - public static void drawLine(Options options, double x1, double y1, double z1, double x2, double y2, double z2) { - double diffX = (x2 - x1); - double diffY = (y2 - y1); - double diffZ = (z2 - z1); - - double lineLength = Mth.length(diffX, diffY, diffZ); - - long pointCount = Math.round(lineLength / options.stepLength); - - double projectedStepLengthX = diffX / pointCount; - double projectedStepLengthY = diffY / pointCount; - double projectedStepLengthZ = diffZ / pointCount; - - for (long i = 0; i < pointCount; i++) { - double x = x1 + projectedStepLengthX * i; - double y = y1 + projectedStepLengthY * i; - double z = z1 + projectedStepLengthZ * i; - drawPoint(options, x, y, z); - } - } - - public static void drawPolygon(Options options, Vec3[] vertexes) { - for (int i = 0, l = vertexes.length; i < l; i++) { - Vec3 a = vertexes[i]; - Vec3 b = vertexes[(i + 1 == l) ? 0 : i + 1]; - drawLine(options, a.x, a.y, a.z, b.x, b.y, b.z); - } - } - - public static void drawCuboid(Options options, AABB aabb) { - double[][] edges = AABBUtils.getEdges(aabb); - for (double[] edge : edges) { - drawLine(options, edge[0], edge[1], edge[2], edge[3], edge[4], edge[5]); - } - } - - - public static void draw(Shape shape, Options options, CompoundTag data) { - switch (shape) { - case LINE -> { - double[] p1 = NbtUtils.getDoubleArray(data, "StartPoint"); - double[] p2 = NbtUtils.getDoubleArray(data, "EndPoint"); - drawLine(options, p1[0], p1[1], p1[2], p2[0], p2[1], p2[2]); - } - case CUBOID -> { - drawCuboid(options, NbtUtils.getAABB(data, "AABB")); - } - 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(options, vertexes); - } - default -> throw new IllegalArgumentException("Unexpected shape: " + shape); - } - } - - } - - public static class Server { - - private static void draw(ServerPlayer player, Shape shape, Options options, Consumer serializer) { - var nbt = new CompoundTag(); - serializer.accept(nbt); - AHPacketHandler.sendToClient(player, new ClientboundDrawParticleShapePacket(shape, options, nbt)); - } - - - public static void drawLine(ServerPlayer player, Options options, double x1, double y1, double z1, double x2, double y2, double z2) { - draw(player, Shape.LINE, options, (nbt) -> { - nbt.put("StartPoint", NbtUtils.writeDoubleArray(x1, y1, z1)); - nbt.put("EndPoint", NbtUtils.writeDoubleArray(x2, y2, z2)); - }); - } - - public static void drawPolygon(ServerPlayer player, Options options, Vec3[] vertexes) { - 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)); - }); - } - - public static void drawCuboid(ServerPlayer player, Options options, AABB aabb) { - draw(player, Shape.CUBOID, options, (nbt) -> { - nbt.put("AABB", NbtUtils.writeAABB(aabb)); - }); - } - - - // #region overload - - public static void drawLine(ServerPlayer player, double x1, double y1, double z1, double x2, double y2, double z2) { - drawLine(player, DEFAULT_OPTIONS, x1, y1, z1, x2, y2, z2); - } - - public static void drawLine(ServerPlayer player, Vec3 start, Vec3 end) { - drawLine(player, DEFAULT_OPTIONS, start.x, start.y, start.z, end.x, end.y, end.z); - } - - public static void drawLine(ServerPlayer player, Vec3i start, Vec3i end) { - drawLine(player, Vec3.atCenterOf(start), Vec3.atCenterOf(end)); - } - - public static void drawPolygon(ServerPlayer player, Vec3[] vertexes) { - drawPolygon(player, DEFAULT_OPTIONS, vertexes); - } - - public static void drawPolygon(ServerPlayer player, Vec3i[] vertexes) { - Vec3[] _vertexes = new Vec3[vertexes.length]; - for (int i = 0; i < vertexes.length; i++) _vertexes[i] = Vec3.atCenterOf(vertexes[i]); - drawPolygon(player, _vertexes); - } - - public static void drawCuboid(ServerPlayer player, AABB aabb) { - drawCuboid(player, DEFAULT_OPTIONS, aabb); - } - - // #endregion overload - - } - -}