refactor: refactored entity DebugLine

This commit is contained in:
Andrey Terentev 2025-04-12 23:17:44 +07:00
parent 5f3cab0634
commit bbcba20499
2 changed files with 33 additions and 68 deletions

View File

@ -2,26 +2,20 @@ package ru.dragonestia.msb3.api.boot;
import lombok.extern.log4j.Log4j2;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.GameMode;
import net.minestom.server.entity.metadata.display.BlockDisplayMeta;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import ru.dragonestia.msb3.api.entity.debug.DebugLine;
import ru.dragonestia.msb3.api.entity.debug.DebugMarker;
import ru.dragonestia.msb3.api.entity.debug.DebugRendererEntity;
import ru.dragonestia.msb3.api.module.FlatWorldModule;
import ru.dragonestia.msb3.api.module.MotdModule;
import ru.dragonestia.msb3.api.module.PrometheusMetricsModule;
import ru.dragonestia.msb3.api.module.ResourcePackRepositoryModule;
import ru.dragonestia.msb3.api.math.Quaternion;
import team.unnamed.creative.ResourcePack;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@Log4j2
@ -63,8 +57,8 @@ public class DefaultBootstrap extends ServerInitializer {
drawLine(instance, startPos, endPos);
}
drawLine(instance, new Vec(3, 20, 0), new Vec(3, 60, 0));
drawLine(instance, new Vec(-3, 60, 0), new Vec(-3, 20, 0));
drawLine(instance, new Vec(3, 50, 0), new Vec(3, 60, 0));
drawLine(instance, new Vec(-3, 60, 0), new Vec(-3, 50, 0));
drawLine(instance, new Vec(10, 60, 3), new Vec(-10, 60, 3));
drawLine(instance, new Vec(-10, 60, -3), new Vec(10, 60, -3));
drawLine(instance, new Vec(0, 57, 10), new Vec(0, 57, -10));
@ -85,24 +79,6 @@ public class DefaultBootstrap extends ServerInitializer {
marker.setInstance(instance, endPos);
}
var dist = startPos.distance(endPos);
record LineColor(Block block, Vec rot) {}
for (var rot: List.of(
new LineColor(Block.RED_CONCRETE, new Vec(3, 0.1, 0.1)),
new LineColor(Block.LIME_CONCRETE, new Vec(0.1, dist, 0.1)),
new LineColor(Block.LIGHT_BLUE_CONCRETE, new Vec(0.1, 0.1, 3))
)) {
var entity = new DebugRendererEntity(EntityType.BLOCK_DISPLAY, "test");
var meta = (BlockDisplayMeta) entity.getEntityMeta();
var normal = Vec.fromPoint(endPos.sub(startPos)).normalize();
meta.setBlockState(rot.block());
meta.setScale(rot.rot());
meta.setLeftRotation(Quaternion.DEFAULT.rotate(normal).toFloatArray());
entity.setInstance(instance, startPos);
}
var line = new DebugLine("test", startPos, endPos, NamedTextColor.GOLD);
line.setInstance(instance, startPos);
DebugLine.spawn(instance, startPos, endPos, Block.LIME_CONCRETE, "test");
}
}

View File

@ -2,63 +2,52 @@ package ru.dragonestia.msb3.api.entity.debug;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import net.kyori.adventure.text.format.TextColor;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.EntityType;
import net.minestom.server.network.packet.server.play.ParticlePacket;
import net.minestom.server.particle.Particle;
import net.minestom.server.entity.metadata.display.BlockDisplayMeta;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import ru.dragonestia.msb3.api.math.Quaternion;
@Getter
@Log4j2
public class DebugLine extends DebugRendererEntity {
@Getter private Point startPos;
@Getter private Point endPos;
private double dist;
private long lastTick = 0;
private final Particle particle;
private Vec normalWithLength;
private Block block;
public DebugLine(String layerName, Point start, Point end, TextColor color) {
super(EntityType.TEXT_DISPLAY, layerName);
this.startPos = start;
this.endPos = end;
updateDist();
protected DebugLine(String layerName, Vec normalWithLength, Block block) {
super(EntityType.BLOCK_DISPLAY, layerName);
particle = Particle.DUST.withColor(color).withScale(1);
var meta = getMeta();
meta.setBrightnessOverride(15);
meta.setBrightness(15, 15);
updateNormalWithLength(normalWithLength);
updateBlock(block);
}
@Override
public void tick(long time) {
super.tick(time);
if (time - lastTick < 500) return;
lastTick = time;
if (getViewers().isEmpty()) return;
double currentDist = 0;
while ((currentDist += 0.7) < dist) {
var pos = Vec.fromPoint(endPos.sub(startPos))
.normalize()
.mul(currentDist)
.add(startPos);
var pk = new ParticlePacket(particle, pos.x(), pos.y(), pos.z(), 0, 0, 0, 0, 1);
getViewers().forEach(player -> player.sendPacket(pk));
}
public void updateNormalWithLength(Vec newValue) {
normalWithLength = newValue;
var meta = getMeta();
meta.setScale(new Vec(0.1, normalWithLength.length(), 0.1));
meta.setLeftRotation(Quaternion.DEFAULT
.rotate(newValue.normalize())
.toFloatArray());
}
private void updateDist() {
dist = endPos.distance(startPos);
public void updateBlock(Block block) {
getMeta().setBlockState(this.block = block);
}
public void setStartPos(Point startPos) {
this.startPos = startPos;
updateDist();
private BlockDisplayMeta getMeta() {
return (BlockDisplayMeta) getEntityMeta();
}
public void setEndPos(Point endPos) {
this.endPos = endPos;
updateDist();
public static DebugLine spawn(Instance instance, Point start, Point end, Block block, String layerName) {
var entity = new DebugLine(layerName, Vec.fromPoint(end.sub(start)), block);
entity.setInstance(instance, start);
return entity;
}
}