Skip to content

Commit

Permalink
refactor(utils): adjust the arguments order of ParticlePainter
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Aug 11, 2022
1 parent 6a9c5de commit 9eb163c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
Expand Up @@ -16,39 +16,43 @@
public class ClientboundDrawParticleShapePacket implements IHPacket {

private final Shape shape;
private final CompoundTag data;
private final Options options;
private final CompoundTag data;

public ClientboundDrawParticleShapePacket(Shape shape, CompoundTag data, Options options) {
public ClientboundDrawParticleShapePacket(Shape shape, Options options, CompoundTag data) {
this.shape = shape;
this.data = data;
this.options = options;
this.data = data;
}

@Override
@OnlyIn(Dist.CLIENT)
public void handle(Context ctx) {
ParticlePainter.Client.draw(this.shape, this.data, this.options);
ParticlePainter.Client.draw(this.shape, this.options, this.data);
}

@Override
public void encode(FriendlyByteBuf buffer) {
buffer.writeEnum(this.shape);
buffer.writeNbt(this.data);
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();
var stepLength = buffer.readDouble();
var _particleType = ForgeRegistries.PARTICLE_TYPES.getValue(buffer.readResourceLocation());
var particle = readParticle(buffer, _particleType);
var force = buffer.readBoolean();
return new ClientboundDrawParticleShapePacket(shape, data, new Options(stepLength, particle, force));
return new ClientboundDrawParticleShapePacket(shape, options, data);
}

@SuppressWarnings("deprecation")
Expand Down
Expand Up @@ -26,11 +26,11 @@ 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) {
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(double x1, double y1, double z1, double x2, double y2, double z2, Options options) {
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);
Expand All @@ -47,42 +47,42 @@ 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;
drawPoint(x, y, z, options);
drawPoint(options, x, y, z);
}
}

public static void drawPolygon(Vec3[] vertexes, Options options) {
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(a.x, a.y, a.z, b.x, b.y, b.z, options);
drawLine(options, a.x, a.y, a.z, b.x, b.y, b.z);
}
}

public static void drawCuboid(AABB aabb, Options options) {
public static void drawCuboid(Options options, AABB aabb) {
double[][] edges = AABBUtils.getEdges(aabb);
for (double[] edge : edges) {
drawLine(edge[0], edge[1], edge[2], edge[3], edge[4], edge[5], options);
drawLine(options, edge[0], edge[1], edge[2], edge[3], edge[4], edge[5]);
}
}

public static void draw(Shape shape, CompoundTag data, Options options) {
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(p1[0], p1[1], p1[2], p2[0], p2[1], p2[2], options);
drawLine(options, p1[0], p1[1], p1[2], p2[0], p2[1], p2[2]);
}
case CUBOID -> {
drawCuboid(NbtUtils.getAABB(data, "AABB"), options);
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(vertexes, options);
drawPolygon(options, vertexes);
}
default -> throw new IllegalArgumentException("Unexpected shape: " + shape);
}
Expand All @@ -95,23 +95,23 @@ public static class Server {
private static void draw(ServerPlayer player, Shape shape, Options options, Consumer<CompoundTag> serializer) {
var nbt = new CompoundTag();
serializer.accept(nbt);
AHPacketHandler.sendToClient(player, new ClientboundDrawParticleShapePacket(shape, nbt, options));
AHPacketHandler.sendToClient(player, new ClientboundDrawParticleShapePacket(shape, options, nbt));
}

public static void drawLine(ServerPlayer player, double x1, double y1, double z1, double x2, double y2, double z2, Options options) {
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 drawCuboid(ServerPlayer player, AABB aabb, Options options) {
public static void drawCuboid(ServerPlayer player, Options options, AABB aabb) {
draw(player, Shape.CUBOID, options, (nbt) -> {
nbt.put("AABB", NbtUtils.writeAABB(aabb));
});
}

public static void drawPolygon(ServerPlayer player, Vec3[] vertexes, Options options) {
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) {
Expand Down

0 comments on commit 9eb163c

Please sign in to comment.