Skip to content

Commit

Permalink
Add debug utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeRNG committed Apr 7, 2023
1 parent 9fb7445 commit bae3e61
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/main/java/dev/dfonline/codeclient/dev/Debug/Debug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package dev.dfonline.codeclient.dev.Debug;

import dev.dfonline.codeclient.CodeClient;
import dev.dfonline.codeclient.OverlayManager;
import dev.dfonline.codeclient.location.Plot;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.debug.DebugRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.network.listener.PacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.OverlayMessageS2CPacket;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.Vec3d;

import java.util.List;

public class Debug {
public static boolean active = false;

public static Variables variables = new Variables();
public static Double CPU = null;
private static Variable variable;

public static <T extends PacketListener> boolean handlePacket(Packet<T> packet) {
if(packet instanceof OverlayMessageS2CPacket overlay) {
String message = overlay.getMessage().getString();
String[] args = message.split(" ");
if(args.length > 0 && args[0].equals("ccdbug")) {
if(args.length > 1) {
if(args[1].equals("hello")) {
active = true;
if(args.length > 3 && CodeClient.location instanceof Plot plot) {
plot.setOrigin(Integer.parseInt(args[2]), Integer.parseInt(args[3]));
}
}
if(args.length > 2 && args[1].equals("var")) {
if(args[2].equals("set")) {
variable = new Variable(message.replaceFirst("^ccdbug var set ",""));
}
if(args[2].equals("type")) {
Variable.ValueType type = Variable.ValueType.valueTypeMap.get(message.replaceFirst("^ccdbug var type ",""));
variable.type = type;
if(type == Variable.ValueType.Dead) {
variables.addOrUpdate(variable);
variable = null;
}
}
if(args[2].equals("value")) {
variable.value = message.replaceFirst("^ccdbug var value ", "");
variables.addOrUpdate(variable);
variable = null;
}
}
}
// updateDisplay();
return true;
}
if(message.matches("^CPU Usage: \\[▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮] \\([\\d\\.]+%\\)$")) {
CPU = Double.parseDouble(message.replaceAll("(^CPU Usage: \\[▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮▮] \\(|%\\)$)",""));
return true;
}
}
return false;
}

public static void updateDisplay() {
OverlayManager.setOverlayText();
OverlayManager.addOverlayText(Text.literal("CCDBUG").formatted(Formatting.YELLOW,Formatting.BOLD));
if(CPU != null) {
OverlayManager.addOverlayText(Text.literal("CPU Usage: ").formatted(Formatting.GOLD).append(Text.literal(String.valueOf(CPU)).formatted(Formatting.AQUA)));
}
OverlayManager.addOverlayText(Text.empty());
List<Variable> variableList = List.copyOf(variables.variables);
for (Variable variable: variableList) {
MutableText text = Text.literal(variable.type.name).fillStyle(Style.EMPTY.withColor(variable.type.color)).append(" ").append(Text.literal(variable.name).formatted(Formatting.YELLOW));
if(variable.value != null) {
text.append(" ").append(Text.literal(variable.value).formatted(Formatting.AQUA));
}
OverlayManager.addOverlayText(text);
}
}

public static void tick() {
if(active && CodeClient.location instanceof Plot) {
updateDisplay();
}
else {
active = false;
variables.clear();
CPU = null;
}
}

public static void render(MatrixStack matrices, VertexConsumerProvider.Immediate vertexConsumers) {
for (Variable variable: variables.variables) {
if(variable.type == Variable.ValueType.Loc && CodeClient.location instanceof Plot plot) {
try {
String[] posmaker = variable.value.replaceAll("^\\[|,|]$","").split(" ");
Vec3d pos = new Vec3d(
Double.parseDouble(posmaker[0]),
Double.parseDouble(posmaker[1]),
Double.parseDouble(posmaker[2]))
.add(plot.getX(),0, plot.getZ());
DebugRenderer.drawString(matrices,vertexConsumers, variable.name, pos.x,pos.y,pos.z, 0xFFFFFF, 0.02F, true, 0, true);
} catch (Exception ignored) {}
}
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/dev/dfonline/codeclient/dev/Debug/Variable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dev.dfonline.codeclient.dev.Debug;

import net.minecraft.text.TextColor;

import java.util.HashMap;
import java.util.Map;

import dev.dfonline.codeclient.actiondump.Argument.Type;

public class Variable {
public ValueType type;
public String name;
public String value = null;

Variable(String name) {
this.name = name;
}

public static enum ValueType {
Dead("Dead", Type.NONE.color),
Num("Number", Type.NUMBER.color),
Txt("Text", Type.TEXT.color),
Loc("Location", Type.LOCATION.color),
Item("Item", Type.ITEM.color),
List("List", Type.LIST.color),
Pot("Potion Effect", Type.POTION.color),
Snd("Sound", Type.SOUND.color),
Pfx("Particle", Type.PARTICLE.color),
Vec("Vector", Type.VECTOR.color),
Dict("Dictionary", Type.DICT.color);

public String name;
public TextColor color;
ValueType(String name, TextColor color) {
this.name = name;
this.color = color;
}

public static Map<String, ValueType> valueTypeMap;
static {
HashMap<String, ValueType> map = new HashMap<>();
for (ValueType type: values()) {
map.put(type.name, type);
}
valueTypeMap = Map.copyOf(map);
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/dev/dfonline/codeclient/dev/Debug/Variables.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.dfonline.codeclient.dev.Debug;

import java.util.ArrayList;
import java.util.Objects;

public class Variables {
public ArrayList<Variable> variables = new ArrayList<>();
public Variables() {}

public void addOrUpdate(Variable variable) {
for (int i = 0; i < variables.size(); i++) {
if(Objects.equals(variables.get(i).name, variable.name)) {
variables.set(i, variable);
return;
}
}
variables.add(variable);
}

public void clear() {
variables.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.dfonline.codeclient.CodeClient;
import dev.dfonline.codeclient.Event;
import dev.dfonline.codeclient.dev.Debug.Debug;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.listener.PacketListener;
Expand All @@ -16,6 +17,7 @@ public class MClientConnection {
private static <T extends PacketListener> void handlePacket(Packet<T> packet, net.minecraft.network.listener.PacketListener listener, CallbackInfo ci) {
if(CodeClient.handlePacket(packet)) ci.cancel();
if(CodeClient.currentAction.onReceivePacket(packet)) ci.cancel();
if(Debug.handlePacket(packet)) ci.cancel();
Event.handlePacket(packet);
// if(ChestPeeker.onPacket(packet)) ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.dfonline.codeclient.mixin;

import dev.dfonline.codeclient.CodeClient;
import dev.dfonline.codeclient.dev.Debug.Debug;
import dev.dfonline.codeclient.dev.NoClip;
import dev.dfonline.codeclient.action.impl.MoveToSpawn;
import dev.dfonline.codeclient.location.Dev;
Expand Down Expand Up @@ -30,6 +31,7 @@ private void tick(CallbackInfo ci) {
CodeClient.onTick();
CodeClient.currentAction.onTick();
if(NoClip.ignoresWalls()) MC.player.noClip = true;
Debug.tick();
// ChestPeeker.tick();
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/dev/dfonline/codeclient/mixin/MDebugRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.dfonline.codeclient.mixin;

import dev.dfonline.codeclient.dev.Debug.Debug;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.debug.DebugRenderer;
import net.minecraft.client.util.math.MatrixStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(DebugRenderer.class)
public class MDebugRenderer {
@Inject(method = "render", at = @At("HEAD"))
private void onRender(MatrixStack matrices, VertexConsumerProvider.Immediate vertexConsumers, double cameraX, double cameraY, double cameraZ, CallbackInfo ci) {
Debug.render(matrices, vertexConsumers);
}
}
1 change: 1 addition & 0 deletions src/main/resources/CodeClient.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"MClientPlayerEntity",
"MClientPlayerInteractionManager",
"MClientPlayNetworkHandler",
"MDebugRenderer",
"MEntity",
"MInGameHud",
"MInGameOverlayRenderer",
Expand Down

0 comments on commit bae3e61

Please sign in to comment.