Skip to content

Commit f48fc51

Browse files
unique-nullptrGjum
authored andcommitted
Port: 1.21.11
1 parent 2cf0dfd commit f48fc51

7 files changed

Lines changed: 172 additions & 93 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
22
id "architectury-plugin" version "3.4-SNAPSHOT"
3-
id "dev.architectury.loom" version "1.11-SNAPSHOT" apply false
3+
id "dev.architectury.loom" version "1.13-SNAPSHOT" apply false
44
}
55
architectury {
66
minecraft = rootProject.minecraft_version

common/src/main/java/gjum/minecraft/civ/snitchmod/common/Renderer.java

Lines changed: 147 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import net.minecraft.client.gui.Font;
1010
//import net.minecraft.client.renderer.CoreShaders;
1111
import net.minecraft.client.renderer.MultiBufferSource;
12-
import net.minecraft.client.renderer.RenderType;
12+
import net.minecraft.client.renderer.rendertype.RenderType;
13+
import net.minecraft.client.renderer.rendertype.RenderTypes;
1314
import net.minecraft.client.renderer.ShapeRenderer;
14-
import net.minecraft.core.Direction;
1515
import net.minecraft.network.chat.Component;
1616
import net.minecraft.world.phys.AABB;
1717
import net.minecraft.world.phys.Vec3;
@@ -20,6 +20,7 @@
2020
import org.jetbrains.annotations.NotNull;
2121
import org.joml.Matrix4f;
2222
import org.joml.Matrix4fStack;
23+
import org.joml.Vector3f;
2324
import org.lwjgl.opengl.GL11;
2425

2526
import java.util.ArrayList;
@@ -160,7 +161,7 @@ public static void renderOverlays(PoseStack poseStack) {
160161
if (mc.options.hideGui) return; // F1 mode
161162
// if (mc.options.renderDebug) return; // F3 mode
162163

163-
Vec3 camPos = mc.gameRenderer.getMainCamera().getPosition();
164+
Vec3 camPos = mc.gameRenderer.getMainCamera().position();
164165
Matrix4fStack modelViewStack = RenderSystem.getModelViewStack();
165166
modelViewStack.pushMatrix();
166167
modelViewStack.mul(eventPoseStack.last().pose());
@@ -187,8 +188,6 @@ public static void renderOverlays(PoseStack poseStack) {
187188
renderSnitchFieldPreview(getMod().snitchFieldToPreview);
188189
}
189190

190-
RenderSystem.lineWidth(1.0F);
191-
192191
modelViewStack.popMatrix();
193192
MultiBufferSource.BufferSource buffers = Minecraft.getInstance().renderBuffers().bufferSource();
194193
buffers.endBatch();
@@ -472,114 +471,193 @@ private static void renderPlacementHelper(Snitch snitch) {
472471
}
473472
}
474473

475-
private static void renderBoxOutline(AABB box, Color color, float alpha, float lineWidth) {
476-
//mc.gui.getChat().addMessage(Component.literal("[SnitchMod] renderBoxOutline: " + box));
477-
try (RenderBufferGuard guard = RenderBufferGuard.open()) {
478-
RenderSystem.lineWidth(lineWidth);
479-
VertexConsumer vertexConsumer = guard.bufferSource.getBuffer(RenderType.debugLineStrip(lineWidth));
480-
ShapeRenderer.renderLineBox(
481-
eventPoseStack, vertexConsumer,
482-
box.minX, box.minY, box.minZ,
483-
box.maxX, box.maxY, box.maxZ,
484-
color.r, color.g, color.b, alpha
485-
);
486-
}
487-
}
474+
private static void renderBoxOutline(AABB box, Color color, float alpha, float lineWidth) {
475+
// Convert the requested line width into a world-space thickness.
476+
// Tweak the multiplier if you want thicker/thinner outlines visually.
477+
double t = Math.max(0.01, lineWidth * 0.02);
478+
479+
double minX = box.minX;
480+
double minY = box.minY;
481+
double minZ = box.minZ;
482+
double maxX = box.maxX;
483+
double maxY = box.maxY;
484+
double maxZ = box.maxZ;
485+
486+
// Clamp thickness so it cannot exceed half the box size on any axis.
487+
double maxThicknessX = Math.max(0.0, (maxX - minX) / 2.0);
488+
double maxThicknessY = Math.max(0.0, (maxY - minY) / 2.0);
489+
double maxThicknessZ = Math.max(0.0, (maxZ - minZ) / 2.0);
490+
t = Math.min(t, Math.min(maxThicknessX, Math.min(maxThicknessY, maxThicknessZ)));
491+
492+
if (t <= 0.0) {
493+
return;
494+
}
495+
496+
// Bottom ring
497+
renderFilledBox(new AABB(minX, minY, minZ, maxX, minY + t, minZ + t), color, alpha); // north
498+
renderFilledBox(new AABB(minX, minY, maxZ - t, maxX, minY + t, maxZ), color, alpha); // south
499+
renderFilledBox(new AABB(minX, minY, minZ + t, minX + t, minY + t, maxZ - t), color, alpha); // west
500+
renderFilledBox(new AABB(maxX - t, minY, minZ + t, maxX, minY + t, maxZ - t), color, alpha); // east
501+
502+
// Top ring
503+
renderFilledBox(new AABB(minX, maxY - t, minZ, maxX, maxY, minZ + t), color, alpha); // north
504+
renderFilledBox(new AABB(minX, maxY - t, maxZ - t, maxX, maxY, maxZ), color, alpha); // south
505+
renderFilledBox(new AABB(minX, maxY - t, minZ + t, minX + t, maxY, maxZ - t), color, alpha); // west
506+
renderFilledBox(new AABB(maxX - t, maxY - t, minZ + t, maxX, maxY, maxZ - t), color, alpha); // east
507+
508+
// Vertical edges
509+
renderFilledBox(new AABB(minX, minY + t, minZ, minX + t, maxY - t, minZ + t), color, alpha); // NW
510+
renderFilledBox(new AABB(maxX - t, minY + t, minZ, maxX, maxY - t, minZ + t), color, alpha); // NE
511+
renderFilledBox(new AABB(minX, minY + t, maxZ - t, minX + t, maxY - t, maxZ), color, alpha); // SW
512+
renderFilledBox(new AABB(maxX - t, minY + t, maxZ - t, maxX, maxY - t, maxZ), color, alpha); // SE
513+
}
488514

489515
private static void renderFilledBox(AABB box, Color color, float alpha) {
490516
//mc.gui.getChat().addMessage(Component.literal("[SnitchMod] renderFilledBox: " + box));
491517
try (RenderBufferGuard guard = RenderBufferGuard.open()) {
492-
VertexConsumer vertexConsumer = guard.bufferSource.getBuffer(RenderType.debugQuads());
518+
VertexConsumer vertexConsumer = guard.bufferSource.getBuffer(RenderTypes.debugQuads());
493519
//ShapeRenderer.renderShape(eventPoseStack, vertexConsumer, Shapes.create(box), box.minX, box.minY, box.minZ, color.hex);
494520
float minX = (float) box.minX;
495521
float minY = (float) box.minY;
496522
float minZ = (float) box.minZ;
497523
float maxX = (float) box.maxX;
498524
float maxY = (float) box.maxY;
499525
float maxZ = (float) box.maxZ;
500-
for (Direction direction : Direction.values()) {
501-
ShapeRenderer.renderFace(
502-
eventPoseStack, vertexConsumer, direction,
503-
minX, minY, minZ,
504-
maxX, maxY, maxZ,
505-
color.r, color.g, color.b, alpha
506-
);
507-
}
526+
int alphaInt = Math.round(alpha * 255.0f) & 0xFF;
527+
int colorInt = (alphaInt << 24) | (color.hex & 0x00FFFFFF);
528+
PoseStack.Pose pose = eventPoseStack.last();
529+
530+
// Bottom (-Y)
531+
vertexConsumer.addVertex(pose, minX, minY, minZ).setColor(colorInt);
532+
vertexConsumer.addVertex(pose, maxX, minY, minZ).setColor(colorInt);
533+
vertexConsumer.addVertex(pose, maxX, minY, maxZ).setColor(colorInt);
534+
vertexConsumer.addVertex(pose, minX, minY, maxZ).setColor(colorInt);
535+
536+
// Top (+Y)
537+
vertexConsumer.addVertex(pose, minX, maxY, minZ).setColor(colorInt);
538+
vertexConsumer.addVertex(pose, minX, maxY, maxZ).setColor(colorInt);
539+
vertexConsumer.addVertex(pose, maxX, maxY, maxZ).setColor(colorInt);
540+
vertexConsumer.addVertex(pose, maxX, maxY, minZ).setColor(colorInt);
541+
542+
// North (-Z)
543+
vertexConsumer.addVertex(pose, minX, minY, minZ).setColor(colorInt);
544+
vertexConsumer.addVertex(pose, minX, maxY, minZ).setColor(colorInt);
545+
vertexConsumer.addVertex(pose, maxX, maxY, minZ).setColor(colorInt);
546+
vertexConsumer.addVertex(pose, maxX, minY, minZ).setColor(colorInt);
547+
548+
// South (+Z)
549+
vertexConsumer.addVertex(pose, minX, minY, maxZ).setColor(colorInt);
550+
vertexConsumer.addVertex(pose, maxX, minY, maxZ).setColor(colorInt);
551+
vertexConsumer.addVertex(pose, maxX, maxY, maxZ).setColor(colorInt);
552+
vertexConsumer.addVertex(pose, minX, maxY, maxZ).setColor(colorInt);
553+
554+
// West (-X)
555+
vertexConsumer.addVertex(pose, minX, minY, minZ).setColor(colorInt);
556+
vertexConsumer.addVertex(pose, minX, minY, maxZ).setColor(colorInt);
557+
vertexConsumer.addVertex(pose, minX, maxY, maxZ).setColor(colorInt);
558+
vertexConsumer.addVertex(pose, minX, maxY, minZ).setColor(colorInt);
559+
560+
// East (+X)
561+
vertexConsumer.addVertex(pose, maxX, minY, minZ).setColor(colorInt);
562+
vertexConsumer.addVertex(pose, maxX, maxY, minZ).setColor(colorInt);
563+
vertexConsumer.addVertex(pose, maxX, maxY, maxZ).setColor(colorInt);
564+
vertexConsumer.addVertex(pose, maxX, minY, maxZ).setColor(colorInt);
508565
}
509566
}
510567

511-
private static void renderBoxGuides(AABB box, Color color, float a, float lineWidth) {
512-
boolean initialLineSmooth = GL11.glIsEnabled(GL11.GL_LINE_SMOOTH);
513-
RenderSystem.lineWidth(lineWidth);
514-
515-
float r = color.r;
516-
float g = color.g;
517-
float b = color.b;
518-
Vec3 center = box.getCenter();
519-
float radius = (float) (box.maxX - center.x);
520-
try (RenderBufferGuard guard = RenderBufferGuard.open()) {
521-
PoseStack poseStack = new PoseStack();
522-
PoseStack.Pose pose = poseStack.last();
523-
GL11.glEnable(GL11.GL_LINE_SMOOTH);
524-
VertexConsumer vertexConsumer = guard.bufferSource.getBuffer(RenderType.debugLineStrip(lineWidth));
525-
vertexConsumer.addVertex(pose, (float) center.x + 1, (float) center.y, (float) center.z).setColor(r, g, b, a).setNormal(1, 0, 0);
526-
vertexConsumer.addVertex(pose, (float) center.x + radius, (float) center.y, (float) center.z).setColor(r, g, b, a).setNormal(1, 0, 0);
527-
vertexConsumer.addVertex(pose, (float) center.x - 1, (float) center.y, (float) center.z).setColor(r, g, b, a).setNormal(-1, 0, 0);
528-
vertexConsumer.addVertex(pose, (float) center.x - radius, (float) center.y, (float) center.z).setColor(r, g, b, a).setNormal(-1, 0, 0);
529-
vertexConsumer.addVertex(pose, (float) center.x, (float) center.y + 1, (float) center.z).setColor(r, g, b, a).setNormal(0, 1, 0);
530-
vertexConsumer.addVertex(pose, (float) center.x, (float) center.y + radius, (float) center.z).setColor(r, g, b, a).setNormal(1, 0, 0);
531-
vertexConsumer.addVertex(pose, (float) center.x, (float) center.y - 1, (float) center.z).setColor(r, g, b, a).setNormal(0, -1, 0);
532-
vertexConsumer.addVertex(pose, (float) center.x, (float) center.y - radius, (float) center.z).setColor(r, g, b, a).setNormal(-1, 0, 0);
533-
vertexConsumer.addVertex(pose, (float) center.x, (float) center.y, (float) center.z + 1).setColor(r, g, b, a).setNormal(0, 0, 1);
534-
vertexConsumer.addVertex(pose, (float) center.x, (float) center.y, (float) center.z + radius).setColor(r, g, b, a).setNormal(0, 0, 1);
535-
vertexConsumer.addVertex(pose, (float) center.x, (float) center.y, (float) center.z - 1).setColor(r, g, b, a).setNormal(0, 0, -1);
536-
vertexConsumer.addVertex(pose, (float) center.x, (float) center.y, (float) center.z - radius).setColor(r, g, b, a).setNormal(0, 0, -1);
537-
}
538-
finally {
539-
if (!initialLineSmooth) {
540-
GL11.glDisable(GL11.GL_LINE_SMOOTH);
541-
}
542-
}
543-
}
568+
private static void renderBoxGuides(AABB box, Color color, float alpha, float lineWidth) {
569+
Vec3 center = box.getCenter();
570+
double radius = box.maxX - center.x;
571+
572+
// Convert requested line width into a world-space thickness.
573+
double t = Math.max(0.01, lineWidth * 0.02);
574+
double halfT = t / 2.0;
575+
576+
double cx = center.x;
577+
double cy = center.y;
578+
double cz = center.z;
579+
580+
// +X guide
581+
renderFilledBox(
582+
new AABB(cx + 1.0, cy - halfT, cz - halfT,
583+
cx + radius, cy + halfT, cz + halfT),
584+
color, alpha
585+
);
586+
587+
// -X guide
588+
renderFilledBox(
589+
new AABB(cx - radius, cy - halfT, cz - halfT,
590+
cx - 1.0, cy + halfT, cz + halfT),
591+
color, alpha
592+
);
593+
594+
// +Y guide
595+
renderFilledBox(
596+
new AABB(cx - halfT, cy + 1.0, cz - halfT,
597+
cx + halfT, cy + radius, cz + halfT),
598+
color, alpha
599+
);
600+
601+
// -Y guide
602+
renderFilledBox(
603+
new AABB(cx - halfT, cy - radius, cz - halfT,
604+
cx + halfT, cy - 1.0, cz + halfT),
605+
color, alpha
606+
);
607+
608+
// +Z guide
609+
renderFilledBox(
610+
new AABB(cx - halfT, cy - halfT, cz + 1.0,
611+
cx + halfT, cy + halfT, cz + radius),
612+
color, alpha
613+
);
614+
615+
// -Z guide
616+
renderFilledBox(
617+
new AABB(cx - halfT, cy - halfT, cz - radius,
618+
cx + halfT, cy + halfT, cz - 1.0),
619+
color, alpha
620+
);
621+
}
544622

545623
/**
546624
* middle center of text is at `pos` before moving it down the screen by `offset`
547625
*/
548626
private static void renderTextFacingCamera(Component text, Vec3 pos, float offset, float scale, int colorAlphaHex) {
549627
// Create a new pose stack for proper 3D positioning
550628
PoseStack poseStack = new PoseStack();
551-
629+
552630
// Translate to the world position
553631
poseStack.translate(pos.x, pos.y, pos.z);
554-
632+
555633
// Make text face the camera
556634
poseStack.mulPose(mc.gameRenderer.getMainCamera().rotation());
557-
635+
558636
// Calculate scale based on distance
559637
scale *= 0.005f * (mc.player.position().distanceTo(pos) / 2.4);
560638
scale = Math.clamp(scale, 0.015f, 0.15f);
561-
639+
562640
// Apply scaling (negative Y to flip text right-side up)
563641
poseStack.scale(scale, -scale, scale);
564-
642+
565643
// Calculate text positioning
566644
float w = mc.font.width(text);
567645
float x = -w / 2f;
568646
float y = -(.5f - offset) * (mc.font.lineHeight + 2); // +2 for background padding, -1 for default line spacing
569647
boolean shadow = false;
570-
648+
571649
// Get the final transformation matrix
572650
Matrix4f matrix = poseStack.last().pose();
573-
651+
574652
// Background settings - make it more transparent to see text better
575653
float bgOpacity = Minecraft.getInstance().options.getBackgroundOpacity(0.25f);
576654
int bgColor = (int) (bgOpacity * 255.0f) << 24;
577-
655+
578656
// Ensure text has full alpha if not already set
579657
if ((colorAlphaHex & 0xFF000000) == 0) {
580658
colorAlphaHex |= 0xFF000000; // Add full alpha if missing
581659
}
582-
660+
583661
// Use immediate mode rendering with proper depth handling
584662
try (RenderBufferGuard guard = RenderBufferGuard.open(false, true, false)) {
585663
mc.font.drawInBatch(text, x, y, colorAlphaHex, shadow, matrix, guard.bufferSource, Font.DisplayMode.NORMAL, bgColor, 15728880);

common/src/main/java/gjum/minecraft/civ/snitchmod/common/SnitchMod.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import net.minecraft.client.Minecraft;
1414
import net.minecraft.client.multiplayer.ServerData;
1515
import net.minecraft.network.chat.Component;
16+
import net.minecraft.resources.Identifier;
1617
import net.minecraft.world.item.ItemStack;
1718
import net.minecraft.world.phys.Vec3;
1819
import org.jetbrains.annotations.Nullable;
@@ -28,54 +29,56 @@
2829

2930
public abstract class SnitchMod {
3031
private final static Minecraft mc = Minecraft.getInstance();
32+
public static final KeyMapping.Category SNITCHMOD_CATEGORY
33+
= KeyMapping.Category.register(Identifier.fromNamespaceAndPath("snitchmod", "category"));
3134

3235
protected static final KeyMapping openGuiKey = new KeyMapping(
3336
"key.snitchmod.openGui",
3437
InputConstants.Type.KEYSYM,
3538
GLFW.GLFW_KEY_L,
36-
"category.snitchmod"
39+
SNITCHMOD_CATEGORY
3740
);
3841

3942
protected static final KeyMapping toggleOverlayKey = new KeyMapping(
4043
"key.snitchmod.toggleOverlay",
4144
InputConstants.Type.KEYSYM,
4245
GLFW.GLFW_KEY_O,
43-
"category.snitchmod"
46+
SNITCHMOD_CATEGORY
4447
);
4548

4649
protected static final KeyMapping togglePlacementKey = new KeyMapping(
4750
"key.snitchmod.togglePlacement",
4851
InputConstants.Type.KEYSYM,
4952
GLFW.GLFW_KEY_P,
50-
"category.snitchmod"
53+
SNITCHMOD_CATEGORY
5154
);
5255

5356
protected static final KeyMapping previewSnitchFieldKey = new KeyMapping(
5457
"key.snitchmod.togglePreviewSnitchFieldKey",
5558
InputConstants.Type.KEYSYM,
5659
GLFW.GLFW_KEY_N,
57-
"category.snitchmod"
60+
SNITCHMOD_CATEGORY
5861
);
5962

6063
protected static final KeyMapping jalistAutoKey = new KeyMapping(
6164
"key.snitchmod.jalistAuto",
6265
InputConstants.Type.KEYSYM,
6366
GLFW.GLFW_KEY_J,
64-
"category.snitchmod"
67+
SNITCHMOD_CATEGORY
6568
);
6669

6770
protected static final KeyMapping toggleSnitchGoneStatusKey = new KeyMapping(
6871
"key.snitchmod.toggleSnitchGoneStatusKey",
6972
InputConstants.Type.KEYSYM,
7073
GLFW.GLFW_KEY_DELETE,
71-
"category.snitchmod"
74+
SNITCHMOD_CATEGORY
7275
);
7376

7477
protected static final KeyMapping toggleSnitchFieldRenderKey = new KeyMapping(
7578
"key.snitchmod.toggleSnitchFieldRenderKey",
7679
InputConstants.Type.KEYSYM,
7780
GLFW.GLFW_KEY_T,
78-
"category.snitchmod"
81+
SNITCHMOD_CATEGORY
7982
);
8083

8184
private static SnitchMod INSTANCE;
@@ -107,7 +110,7 @@ public SnitchMod() {
107110

108111
public String getCurrentWorld() {
109112
if (mc.level == null) return null;
110-
String dimension = mc.level.dimension().location().getPath();
113+
String dimension = mc.level.dimension().identifier().getPath();
111114
// civ server world names as they occur in snitches are different from dimension names
112115
return switch (dimension) {
113116
case "overworld" -> "world";

0 commit comments

Comments
 (0)