Skip to content

Commit

Permalink
better coffin debug
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Aug 29, 2024
1 parent 34f29a2 commit 4fe849a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void onInitializeClient() {
CoffinDebugRenderer coffinDebugRenderer = new CoffinDebugRenderer(client);

ClientPlayNetworking.registerGlobalReceiver(CoffinDebugPacket.PACKET_TYPE, (packet, ctx) -> {
coffinDebugRenderer.addConnection(packet.entityId(), packet.tickCount(), packet.entityPos(), packet.coffinPos());
coffinDebugRenderer.addConnection(packet.entityId(), packet.coffinPos(), packet.lastInteractionTime(), packet.gameTime());
});

ClientPlayNetworking.registerGlobalReceiver(CoffinRemoveDebugPacket.PACKET_TYPE, (packet, ctx) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public void appendCoffinSpawnAttributes(Entity entity, Level level, BlockPos pos
}
if (entity instanceof EntityCoffinInterface entityInterface) {
entityInterface.trailierTales$setCoffinData(
new EntityCoffinData(pos, this.uuid)
new EntityCoffinData(pos, this.uuid, level.getGameTime())
);
}
if (entity instanceof Apparition apparition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.VisibleForDebug;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.player.Player;
Expand All @@ -21,10 +22,12 @@
public class EntityCoffinData {
private final BlockPos pos;
private final UUID coffinUUID;
private long lastInteractionAt;

public EntityCoffinData(BlockPos pos, UUID coffinUUID) {
public EntityCoffinData(BlockPos pos, UUID coffinUUID, long lastInteractionAt) {
this.pos = pos;
this.coffinUUID = coffinUUID;
this.lastInteractionAt = lastInteractionAt;
}

public BlockPos getPos() {
Expand All @@ -47,7 +50,7 @@ public void tick(LivingEntity entity, @NotNull Level level) {
if (FrozenLibConfig.IS_DEBUG && level instanceof ServerLevel serverLevel) {
FrozenNetworking.sendPacketToAllPlayers(
serverLevel,
new CoffinDebugPacket(entity.getId(), entity.tickCount, entity.getEyePosition(), this.pos.getCenter())
new CoffinDebugPacket(entity.getId(), this.lastInteractionAt, this.pos, serverLevel.getGameTime())
);
}
if (entity instanceof Mob mob) {
Expand All @@ -71,12 +74,31 @@ public Optional<CoffinSpawner> getSpawner(@NotNull Level level) {
return Optional.empty();
}

@VisibleForDebug
public Optional<CoffinSpawner> getSpawnerIgnoringUUID(@NotNull Level level) {
if (level.isLoaded(this.getPos())) {
if (level.getBlockEntity(this.getPos()) instanceof CoffinBlockEntity coffinBlockEntity) {
return Optional.of(coffinBlockEntity.getCoffinSpawner());
}
}
return Optional.empty();
}

public long lastInteraction() {
return this.lastInteractionAt;
}

public void updateLastInteraction(long newTime) {
this.lastInteractionAt = newTime;
}

public void saveCompoundTag(@NotNull CompoundTag tag) {
CompoundTag coffinDataTag = new CompoundTag();
coffinDataTag.putInt("X", this.pos.getX());
coffinDataTag.putInt("Y", this.pos.getY());
coffinDataTag.putInt("Z", this.pos.getZ());
coffinDataTag.putUUID("CoffinUUID", this.coffinUUID);
coffinDataTag.putLong("LastInteractionAt", this.lastInteractionAt);
tag.put("TrailierTales_CoffinData", coffinDataTag);
}

Expand All @@ -89,7 +111,8 @@ public void saveCompoundTag(@NotNull CompoundTag tag) {
coffinDataTag.getInt("Z")
);
UUID coffinUUID = coffinDataTag.getUUID("CoffinUUID");
return new EntityCoffinData(pos, coffinUUID);
long lastInteractionAt = coffinDataTag.getLong("LastInteractionAt");
return new EntityCoffinData(pos, coffinUUID, lastInteractionAt);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import com.google.common.collect.Maps;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import java.util.Map;
import java.util.UUID;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.frozenblock.lib.debug.client.impl.DebugRenderManager;
import net.frozenblock.trailiertales.block.entity.coffin.impl.EntityCoffinData;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
Expand All @@ -27,10 +28,10 @@ public class CoffinDebugRenderer implements DebugRenderer.SimpleDebugRenderer {
private static final int TEXT_COLOR = FastColor.ARGB32.color(255, 255, 255, 255);
private final Minecraft minecraft;
private final IntArrayList scheduledRemovals = new IntArrayList();
private final Map<Integer, Pair<Vec3, Vec3>> connections = Maps.newHashMap();
private final Map<Integer, Integer> tickCounts = Maps.newHashMap();
private final Map<Integer, EntityCoffinData> connections = Maps.newHashMap();
@Nullable
private Integer lastLookedAtId;
private long gameTime;

public CoffinDebugRenderer(Minecraft client) {
this.minecraft = client;
Expand All @@ -43,18 +44,11 @@ private void clearRemovedEntities() {
Entity entity = this.minecraft.level.getEntity(id);
return entity == null || entity.isRemoved();
});

this.tickCounts.entrySet().removeIf(entry -> {
int id = entry.getKey();
if (scheduledRemovals.contains(id)) return true;
Entity entity = this.minecraft.level.getEntity(id);
return entity == null || entity.isRemoved();
});
}

public void addConnection(int entityId, int tickCount, Vec3 vec3, Vec3 target) {
this.connections.put(entityId, Pair.of(vec3, target));
this.tickCounts.put(entityId, tickCount);
public void addConnection(int entityId, BlockPos coffinPos, long lastInteractionTime, long gameTime) {
this.connections.put(entityId, new EntityCoffinData(coffinPos, UUID.randomUUID(), lastInteractionTime));
this.gameTime = gameTime;
}

public void scheduleRemoval(int entityId) {
Expand All @@ -64,7 +58,6 @@ public void scheduleRemoval(int entityId) {
@Override
public void clear() {
this.connections.clear();
this.tickCounts.clear();
this.scheduledRemovals.clear();
this.lastLookedAtId = null;
}
Expand All @@ -78,27 +71,36 @@ public void render(PoseStack matrices, @NotNull MultiBufferSource vertexConsumer
this.updateLastLookedAtUuid();
}

for (Map.Entry<Integer, Pair<Vec3, Vec3>> connectionInfo : this.connections.entrySet()) {
Pair<Vec3, Vec3> connection = connectionInfo.getValue();
for (Map.Entry<Integer, EntityCoffinData> connectionInfo : this.connections.entrySet()) {
EntityCoffinData coffinData = connectionInfo.getValue();
boolean selected = false;
Integer id = connectionInfo.getKey();
if (id != null && id.equals(this.lastLookedAtId)) {
selected = true;
highlightPos(matrices, vertexConsumers, BlockPos.containing(connection.getSecond()));
if (id != null) {
Entity entity = this.minecraft.level.getEntity(id);
if (entity != null) {
Vec3 entityTextPos = entity.getEyePosition(DebugRenderManager.PARTIAL_TICK);
renderTextOverPos(matrices, vertexConsumers, entity.getDisplayName().getString(), entityTextPos, 3, TEXT_COLOR);
renderTextOverPos(matrices, vertexConsumers, "TickCount: " + this.tickCounts.get(id), entityTextPos, 2, TEXT_COLOR);
if (id.equals(this.lastLookedAtId)) {
selected = true;
highlightPos(matrices, vertexConsumers, coffinData.getPos());
Vec3 entityTextPos = entity.getEyePosition(DebugRenderManager.PARTIAL_TICK);
renderTextOverPos(matrices, vertexConsumers, entity.getDisplayName().getString(), entityTextPos, 3, TEXT_COLOR);
renderTextOverPos(
matrices,
vertexConsumers,
"Last Interaction: " + (this.gameTime - coffinData.lastInteraction()),
entityTextPos,
2,
TEXT_COLOR
);
}
drawLine(
matrices,
vertexConsumers,
cameraX, cameraY, cameraZ,
entity.getEyePosition(DebugRenderManager.PARTIAL_TICK), Vec3.atCenterOf(coffinData.getPos()),
selected ? SELECTED_CONNECTION_COLOR : CONNECTION_COLOR
);
}
}
drawLine(
matrices,
vertexConsumers,
cameraX, cameraY, cameraZ,
connection.getFirst(), connection.getSecond(),
selected ? SELECTED_CONNECTION_COLOR : CONNECTION_COLOR
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package net.frozenblock.trailiertales.networking.packet;

import net.frozenblock.trailiertales.TrailierConstants;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;

public record CoffinDebugPacket(Integer entityId, Integer tickCount, Vec3 entityPos, Vec3 coffinPos) implements CustomPacketPayload {
public record CoffinDebugPacket(int entityId, long lastInteractionTime, BlockPos coffinPos, long gameTime) implements CustomPacketPayload {
public static final Type<CoffinDebugPacket> PACKET_TYPE = new Type<>(
TrailierConstants.id("debug_coffin")
);

public static final StreamCodec<FriendlyByteBuf, CoffinDebugPacket> CODEC = StreamCodec.ofMember(CoffinDebugPacket::write, CoffinDebugPacket::new);

public CoffinDebugPacket(@NotNull FriendlyByteBuf buf) {
this(buf.readVarInt(), buf.readInt(), buf.readVec3(), buf.readVec3());
this(buf.readVarInt(), buf.readLong(), buf.readBlockPos(), buf.readLong());
}

public void write(@NotNull FriendlyByteBuf buf) {
buf.writeVarInt(this.entityId);
buf.writeInt(this.tickCount);
buf.writeVec3(this.entityPos);
buf.writeVec3(this.coffinPos);
buf.writeLong(this.lastInteractionTime);
buf.writeBlockPos(this.coffinPos);
buf.writeLong(this.gameTime);
}

@NotNull
Expand Down

0 comments on commit 4fe849a

Please sign in to comment.