diff --git a/build.gradle b/build.gradle index 65cae4f..2ca38c5 100644 --- a/build.gradle +++ b/build.gradle @@ -1,18 +1,25 @@ plugins { - id 'fabric-loom' version '0.12-SNAPSHOT' + id 'fabric-loom' version '1.8-SNAPSHOT' id 'maven-publish' } -sourceCompatibility = JavaVersion.VERSION_17 -targetCompatibility = JavaVersion.VERSION_17 - -archivesBaseName = project.archives_base_name version = project.mod_version group = project.maven_group +base { + archivesName = project.archives_base_name +} + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. +} dependencies { - //to change the versions see the gradle.properties file + // To change the versions see the gradle.properties file minecraft "com.mojang:minecraft:${project.minecraft_version}" mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" @@ -20,51 +27,53 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + // Uncomment the following line to enable the deprecated Fabric API modules. + // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. + + // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" } processResources { - duplicatesStrategy = DuplicatesStrategy.EXCLUDE - - inputs.property "version", project.mod_version + inputs.property "version", project.version filesMatching("fabric.mod.json") { expand "version": project.version } } -// ensure that the encoding is set to UTF-8, no matter what the system default is -// this fixes some edge cases with special characters not displaying correctly -// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html -tasks.withType(JavaCompile) { - options.encoding = "UTF-8" +tasks.withType(JavaCompile).configureEach { + it.options.release = 21 } -// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task -// if it is present. -// If you remove this task, sources will not be generated. -task sourcesJar(type: Jar, dependsOn: classes) { - classifier = "sources" - from sourceSets.main.allSource +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() + + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 } jar { - from "LICENSE" + from("LICENSE") { + rename { "${it}_${project.base.archivesName.get()}"} + } } // configure the maven publication publishing { publications { mavenJava(MavenPublication) { - // add all the jars that should be included when publishing to maven - artifact(remapJar) { - builtBy remapJar - } - artifact(sourcesJar) { - builtBy remapSourcesJar - } - } - gpr(MavenPublication) { - from(components.java) + from components.java } } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } } diff --git a/gradle.properties b/gradle.properties index fe474fb..a7589dd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,14 +8,14 @@ org.gradle.jvmargs=-Xmx1G # To remap the mixin locations: # gradlew migrateMappings --mappings "1.16.1+build.9" -minecraft_version=1.19 -yarn_mappings=1.19+build.1 -loader_version=0.14.6 +minecraft_version=1.21.3 +yarn_mappings=1.21.3+build.2 +loader_version=0.16.9 #Fabric api -fabric_version=0.55.2+1.19 +fabric_version=0.110.0+1.21.3 # Mod Properties -mod_version = 1.19-fabric-1 +mod_version = 1.21.3-fabric-1 maven_group = net.torocraft archives_base_name = torohealth diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8a17ae4..9851827 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https://services.gradle.org/distributions/gradle-7.3.1-bin.zip +distributionUrl=https://services.gradle.org/distributions/gradle-8.10-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/settings.gradle b/settings.gradle index 5b60df3..f91a4fe 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,6 +1,5 @@ pluginManagement { repositories { - jcenter() maven { name = 'Fabric' url = 'https://maven.fabricmc.net/' diff --git a/src/main/java/net/torocraft/torohealth/ToroHealth.java b/src/main/java/net/torocraft/torohealth/ToroHealth.java index 322f6ff..8fea6dd 100644 --- a/src/main/java/net/torocraft/torohealth/ToroHealth.java +++ b/src/main/java/net/torocraft/torohealth/ToroHealth.java @@ -2,9 +2,13 @@ import java.util.Random; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; +import net.torocraft.torohealth.bars.HealthBarRenderer; +import net.torocraft.torohealth.bars.ParticleRenderer; import net.torocraft.torohealth.config.Config; import net.torocraft.torohealth.config.loader.ConfigLoader; import net.torocraft.torohealth.display.Hud; +import net.torocraft.torohealth.mixin.WorldRendererAccessor; import net.torocraft.torohealth.util.RayTrace; public class ToroHealth implements ModInitializer { @@ -24,5 +28,12 @@ public class ToroHealth implements ModInitializer { @Override public void onInitialize() { CONFIG_LOADER.load(); + WorldRenderEvents.LAST.register((context) -> { + var entityVertexConsumers = ((WorldRendererAccessor) context.worldRenderer()) + .getBufferBuilders() + .getEntityVertexConsumers(); + HealthBarRenderer.renderInWorld(context.matrixStack(), entityVertexConsumers, context.camera()); + ParticleRenderer.renderParticles(context.matrixStack(), entityVertexConsumers, context.camera()); + }); } } diff --git a/src/main/java/net/torocraft/torohealth/bars/HealthBarRenderer.java b/src/main/java/net/torocraft/torohealth/bars/HealthBarRenderer.java index 10ec0ca..d7e636d 100644 --- a/src/main/java/net/torocraft/torohealth/bars/HealthBarRenderer.java +++ b/src/main/java/net/torocraft/torohealth/bars/HealthBarRenderer.java @@ -4,30 +4,33 @@ import java.util.ArrayList; import java.util.List; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gl.ShaderProgramKeys; import net.minecraft.client.render.BufferBuilder; +import net.minecraft.client.render.BufferRenderer; import net.minecraft.client.render.Camera; -import net.minecraft.client.render.GameRenderer; import net.minecraft.client.render.Tessellator; +import net.minecraft.client.render.VertexConsumerProvider; import net.minecraft.client.render.VertexFormat; import net.minecraft.client.render.VertexFormats; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.LivingEntity; import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.Matrix4f; +import net.minecraft.util.math.RotationAxis; import net.minecraft.util.math.Vec3d; -import net.minecraft.util.math.Vec3f; import net.torocraft.torohealth.ToroHealth; import net.torocraft.torohealth.config.Config; import net.torocraft.torohealth.config.Config.InWorld; import net.torocraft.torohealth.config.Config.Mode; import net.torocraft.torohealth.util.EntityUtil; import net.torocraft.torohealth.util.EntityUtil.Relation; +import org.joml.Matrix4f; import org.lwjgl.opengl.GL11; public class HealthBarRenderer { - private static final Identifier GUI_BARS_TEXTURES = new Identifier(ToroHealth.MODID + ":textures/gui/bars.png"); + private static final Identifier GUI_BARS_TEXTURES = Identifier.of(ToroHealth.MODID + ":textures/gui/bars.png"); private static final int DARK_GRAY = 0x808080; private static final float FULL_SIZE = 40; @@ -70,7 +73,8 @@ public static void prepareRenderInWorld(LivingEntity entity) { } - public static void renderInWorld(MatrixStack matrix, Camera camera) { + public static void renderInWorld(MatrixStack matrix, + VertexConsumerProvider vertexConsumerProvider, Camera camera) { MinecraftClient client = MinecraftClient.getInstance(); @@ -87,7 +91,7 @@ public static void renderInWorld(MatrixStack matrix, Camera camera) { return; } - RenderSystem.setShader(GameRenderer::getPositionColorShader); + RenderSystem.setShader(ShaderProgramKeys.POSITION_COLOR); RenderSystem.enableDepthTest(); RenderSystem.enableBlend(); RenderSystem.blendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, @@ -98,7 +102,7 @@ public static void renderInWorld(MatrixStack matrix, Camera camera) { boolean sneaking = entity.isInSneakingPose(); float height = entity.getHeight() + 0.6F - (sneaking ? 0.25F : 0.0F); - float tickDelta = client.getTickDelta(); + float tickDelta = client.getRenderTickCounter().getTickDelta(true); double x = MathHelper.lerp((double) tickDelta, entity.prevX, entity.getX()); double y = MathHelper.lerp((double) tickDelta, entity.prevY, entity.getY()); double z = MathHelper.lerp((double) tickDelta, entity.prevZ, entity.getZ()); @@ -110,11 +114,11 @@ public static void renderInWorld(MatrixStack matrix, Camera camera) { matrix.push(); matrix.translate(x - camX, (y + height) - camY, z - camZ); - matrix.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion(-camera.getYaw())); - matrix.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(camera.getPitch())); + matrix.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(-camera.getYaw())); + matrix.multiply(RotationAxis.POSITIVE_X.rotationDegrees(camera.getPitch())); matrix.scale(-scaleToGui, -scaleToGui, scaleToGui); - render(matrix, entity, 0, 0, FULL_SIZE, true); + render(matrix, vertexConsumerProvider, entity, 0, 0, FULL_SIZE, true); matrix.pop(); } @@ -124,8 +128,9 @@ public static void renderInWorld(MatrixStack matrix, Camera camera) { renderedEntities.clear(); } - public static void render(MatrixStack matrix, LivingEntity entity, double x, double y, - float width, boolean inWorld) { + public static void render(MatrixStack matrix, VertexConsumerProvider vertexConsumerProvider, + LivingEntity entity, double x, double y, + float width, boolean inWorld) { Relation relation = EntityUtil.determineRelation(entity); @@ -144,18 +149,20 @@ public static void render(MatrixStack matrix, LivingEntity entity, double x, dou drawBar(m4f, x, y, width, 1, DARK_GRAY, zOffset++, inWorld); drawBar(m4f, x, y, width, percent2, color2, zOffset++, inWorld); drawBar(m4f, x, y, width, percent, color, zOffset, inWorld); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); if (!inWorld) { if (ToroHealth.CONFIG.bar.damageNumberType.equals(Config.NumberType.CUMULATIVE)) { - drawDamageNumber(matrix, state.lastDmgCumulative, x, y, width); + drawDamageNumber(matrix, vertexConsumerProvider, state.lastDmgCumulative, x, y, width); } else if (ToroHealth.CONFIG.bar.damageNumberType.equals(Config.NumberType.LAST)) { - drawDamageNumber(matrix, state.lastDmg, x, y, width); + drawDamageNumber(matrix, vertexConsumerProvider, state.lastDmg, x, y, width); } } } - public static void drawDamageNumber(MatrixStack matrix, int dmg, double x, double y, - float width) { + public static void drawDamageNumber(MatrixStack matrix, + VertexConsumerProvider vertexConsumerProvider, + int dmg, double x, double y, float width) { int i = Math.abs(Math.round(dmg)); if (i == 0) { return; @@ -164,7 +171,10 @@ public static void drawDamageNumber(MatrixStack matrix, int dmg, double x, doubl MinecraftClient minecraft = MinecraftClient.getInstance(); int sw = minecraft.textRenderer.getWidth(s); int color = dmg < 0 ? ToroHealth.CONFIG.particle.healColor : ToroHealth.CONFIG.particle.damageColor; - minecraft.textRenderer.draw(matrix, s, (int) (x + (width / 2) - sw), (int) y + 5, color); + minecraft.textRenderer.draw( + s, (float) (x + (width / 2) - sw), (float) y + 5, color, false, + matrix.peek().getPositionMatrix(), vertexConsumerProvider, + TextRenderer.TextLayerType.NORMAL, 0, 15728880); } private static void drawBar(Matrix4f matrix4f, double x, double y, float width, float percent, @@ -183,7 +193,7 @@ private static void drawBar(Matrix4f matrix4f, double x, double y, float width, float b = (color & 255) / 255.0F; RenderSystem.setShaderColor(r, g, b, 1); - RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShader(ShaderProgramKeys.POSITION_TEX); RenderSystem.setShaderTexture(0, GUI_BARS_TEXTURES); RenderSystem.enableBlend(); @@ -192,16 +202,15 @@ private static void drawBar(Matrix4f matrix4f, double x, double y, float width, float zOffsetAmount = inWorld ? -0.1F : 0.1F; Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder buffer = tessellator.getBuffer(); - buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE); + BufferBuilder buffer = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE); buffer.vertex(matrix4f, (float) (-half + x), (float) y, zOffset * zOffsetAmount) - .texture(u * c, v * c).next(); + .texture(u * c, v * c); buffer.vertex(matrix4f, (float) (-half + x), (float) (h + y), zOffset * zOffsetAmount) - .texture(u * c, (v + vh) * c).next(); + .texture(u * c, (v + vh) * c); buffer.vertex(matrix4f, (float) (-half + size + x), (float) (h + y), zOffset * zOffsetAmount) - .texture((u + uw) * c, (v + vh) * c).next(); + .texture((u + uw) * c, (v + vh) * c); buffer.vertex(matrix4f, (float) (-half + size + x), (float) y, zOffset * zOffsetAmount) - .texture(((u + uw) * c), v * c).next(); - tessellator.draw(); + .texture(((u + uw) * c), v * c); + BufferRenderer.drawWithGlobalProgram(buffer.end()); } } diff --git a/src/main/java/net/torocraft/torohealth/bars/ParticleRenderer.java b/src/main/java/net/torocraft/torohealth/bars/ParticleRenderer.java index b0a83cb..c7b3f12 100644 --- a/src/main/java/net/torocraft/torohealth/bars/ParticleRenderer.java +++ b/src/main/java/net/torocraft/torohealth/bars/ParticleRenderer.java @@ -2,24 +2,25 @@ import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gl.ShaderProgramKeys; import net.minecraft.client.render.Camera; -import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.render.VertexConsumerProvider; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.RotationAxis; import net.minecraft.util.math.Vec3d; -import net.minecraft.util.math.Vec3f; import net.torocraft.torohealth.ToroHealth; import org.lwjgl.opengl.GL11; public class ParticleRenderer { - public static void renderParticles(MatrixStack matrix, Camera camera) { + public static void renderParticles(MatrixStack matrix, VertexConsumerProvider vertexConsumerProvider, Camera camera) { for (BarParticle p : BarStates.PARTICLES) { - renderParticle(matrix, p, camera); + renderParticle(matrix, vertexConsumerProvider, p, camera); } } - private static void renderParticle(MatrixStack matrix, BarParticle particle, Camera camera) { + private static void renderParticle(MatrixStack matrix, VertexConsumerProvider vertexConsumerProvider, BarParticle particle, Camera camera) { double distanceSquared = camera.getPos().squaredDistanceTo(particle.x, particle.y, particle.z); if (distanceSquared > ToroHealth.CONFIG.particle.distanceSquared) { return; @@ -28,7 +29,7 @@ private static void renderParticle(MatrixStack matrix, BarParticle particle, Cam float scaleToGui = 0.025f; MinecraftClient client = MinecraftClient.getInstance(); - float tickDelta = client.getTickDelta(); + float tickDelta = client.getRenderTickCounter().getTickDelta(true); double x = MathHelper.lerp((double) tickDelta, particle.xPrev, particle.x); double y = MathHelper.lerp((double) tickDelta, particle.yPrev, particle.y); @@ -41,17 +42,17 @@ private static void renderParticle(MatrixStack matrix, BarParticle particle, Cam matrix.push(); matrix.translate(x - camX, y - camY, z - camZ); - matrix.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion(-camera.getYaw())); - matrix.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(camera.getPitch())); + matrix.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(-camera.getYaw())); + matrix.multiply(RotationAxis.POSITIVE_X.rotationDegrees(camera.getPitch())); matrix.scale(-scaleToGui, -scaleToGui, scaleToGui); - RenderSystem.setShader(GameRenderer::getPositionColorShader); + RenderSystem.setShader(ShaderProgramKeys.POSITION_COLOR); RenderSystem.enableDepthTest(); RenderSystem.enableBlend(); RenderSystem.blendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO); - HealthBarRenderer.drawDamageNumber(matrix, particle.damage, 0, 0, 10); + HealthBarRenderer.drawDamageNumber(matrix, vertexConsumerProvider, particle.damage, 0, 0, 10); RenderSystem.disableBlend(); diff --git a/src/main/java/net/torocraft/torohealth/display/BarDisplay.java b/src/main/java/net/torocraft/torohealth/display/BarDisplay.java index 376bc2f..ed44572 100644 --- a/src/main/java/net/torocraft/torohealth/display/BarDisplay.java +++ b/src/main/java/net/torocraft/torohealth/display/BarDisplay.java @@ -2,9 +2,9 @@ import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.DrawableHelper; -import net.minecraft.client.render.GameRenderer; -import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.client.gl.ShaderProgramKeys; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.render.RenderLayer; import net.minecraft.entity.LivingEntity; import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; @@ -12,61 +12,59 @@ public class BarDisplay { - private static final Identifier ICON_TEXTURES = new Identifier("textures/gui/icons.png"); + private static final Identifier ARMOR_TEXTURE = Identifier.of("hud/armor_full"); + private static final Identifier HEART_TEXTURE = Identifier.of("hud/heart/full"); private final MinecraftClient mc; - private final DrawableHelper gui; - public BarDisplay(MinecraftClient mc, DrawableHelper gui) { + public BarDisplay(MinecraftClient mc) { this.mc = mc; - this.gui = gui; } private String getEntityName(LivingEntity entity) { return entity.getDisplayName().getString(); } - public void draw(MatrixStack matrix, LivingEntity entity) { + public void draw(DrawContext drawContext, LivingEntity entity) { int xOffset = 0; RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - RenderSystem.setShader(GameRenderer::getPositionTexShader); - RenderSystem.setShaderTexture(0, ICON_TEXTURES); + RenderSystem.setShader(ShaderProgramKeys.POSITION_TEX); RenderSystem.enableBlend(); - HealthBarRenderer.render(matrix, entity, 63, 14, 130, false); + drawContext.draw(vertexConsumerProvider -> { + HealthBarRenderer.render(drawContext.getMatrices(), vertexConsumerProvider, entity, 63, 14, 130, false); + }); String name = getEntityName(entity); int healthMax = MathHelper.ceil(entity.getMaxHealth()); int healthCur = Math.min(MathHelper.ceil(entity.getHealth()), healthMax); String healthText = healthCur + "/" + healthMax; RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - DrawableHelper.drawStringWithShadow(matrix, mc.textRenderer, name, xOffset, (int) 2, 16777215); + drawContext.drawTextWithShadow(mc.textRenderer, name, xOffset, 2, 16777215); - mc.textRenderer.drawWithShadow(matrix, name, xOffset, 2, 16777215); + drawContext.drawTextWithShadow(mc.textRenderer, name, xOffset, 2, 16777215); xOffset += mc.textRenderer.getWidth(name) + 5; - renderHeartIcon(matrix, xOffset, (int) 1); + renderHeartIcon(drawContext, xOffset, (int) 1); xOffset += 10; - mc.textRenderer.drawWithShadow(matrix, healthText, xOffset, 2, 0xe0e0e0); + drawContext.drawTextWithShadow(mc.textRenderer, healthText, xOffset, 2, 0xe0e0e0); xOffset += mc.textRenderer.getWidth(healthText) + 5; int armor = entity.getArmor(); if (armor > 0) { - renderArmorIcon(matrix, xOffset, (int) 1); + renderArmorIcon(drawContext, xOffset, (int) 1); xOffset += 10; - mc.textRenderer.drawWithShadow(matrix, entity.getArmor() + "", xOffset, 2, 0xe0e0e0); + drawContext.drawTextWithShadow(mc.textRenderer, entity.getArmor() + "", xOffset, 2, 0xe0e0e0); } } - private void renderArmorIcon(MatrixStack matrix, int x, int y) { - RenderSystem.setShaderTexture(0, ICON_TEXTURES); - gui.drawTexture(matrix, x, y, 34, 9, 9, 9); + private void renderArmorIcon(DrawContext drawContext, int x, int y) { + drawContext.drawGuiTexture(RenderLayer::getGuiTextured, ARMOR_TEXTURE, x, y, 9, 9); } - private void renderHeartIcon(MatrixStack matrix, int x, int y) { - RenderSystem.setShaderTexture(0, ICON_TEXTURES); - gui.drawTexture(matrix, x, y, 16 + 36, 0, 9, 9); + private void renderHeartIcon(DrawContext drawContext, int x, int y) { + drawContext.drawGuiTexture(RenderLayer::getGuiTextured, HEART_TEXTURE, x, y, 9, 9); } } diff --git a/src/main/java/net/torocraft/torohealth/display/EntityDisplay.java b/src/main/java/net/torocraft/torohealth/display/EntityDisplay.java index 1bb390c..03d45ee 100644 --- a/src/main/java/net/torocraft/torohealth/display/EntityDisplay.java +++ b/src/main/java/net/torocraft/torohealth/display/EntityDisplay.java @@ -2,8 +2,8 @@ import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; import net.minecraft.client.render.DiffuseLighting; -import net.minecraft.client.render.VertexConsumerProvider; import net.minecraft.client.render.entity.EntityRenderDispatcher; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.LivingEntity; @@ -11,8 +11,9 @@ import net.minecraft.entity.passive.ChickenEntity; import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.Quaternion; -import net.minecraft.util.math.Vec3f; +import net.minecraft.util.math.RotationAxis; +import org.joml.Matrix4fStack; +import org.joml.Quaternionf; public class EntityDisplay { @@ -32,10 +33,10 @@ public void setEntity(LivingEntity entity) { updateScale(); } - public void draw(MatrixStack matrix, float scale) { + public void draw(DrawContext drawContext, MatrixStack matrix, float scale) { if (entity != null) { try { - drawEntity(matrix, (int) xOffset, (int) yOffset, entityScale, -80, -20, entity, scale); + drawEntity(drawContext, matrix, (int) xOffset, (int) yOffset, entityScale, -80, -20, entity, scale); } catch (Exception e) { e.printStackTrace(); } @@ -70,22 +71,20 @@ private void updateScale() { /** * copied from InventoryScreen.drawEntity() to expose the matrixStack */ - public static void drawEntity(MatrixStack matrixStack2, int x, int y, int size, float mouseX, - float mouseY, LivingEntity entity, float scale) { + public static void drawEntity(DrawContext drawContext, MatrixStack matrixStack2, int x, int y, int size, + float mouseX, float mouseY, LivingEntity entity, float scale) { float f = (float) Math.atan((double) (mouseX / 40.0F)); float g = (float) Math.atan((double) (mouseY / 40.0F)); - MatrixStack matrixStack = RenderSystem.getModelViewStack(); - matrixStack.push(); - matrixStack.translate((double) x * scale, (double) y * scale, 1050.0D * scale); + Matrix4fStack matrixStack = RenderSystem.getModelViewStack(); + matrixStack.pushMatrix(); + matrixStack.translate(x * scale, y * scale, 1050.0F * scale); matrixStack.scale(1.0F, 1.0F, -1.0F); - RenderSystem.applyModelViewMatrix(); matrixStack2.push(); matrixStack2.translate(0.0D, 0.0D, 1000.0D); matrixStack2.scale((float) size, (float) size, (float) size); - Quaternion quaternion = Vec3f.POSITIVE_Z.getDegreesQuaternion(180.0F); - Quaternion quaternion2 = Vec3f.POSITIVE_X.getDegreesQuaternion(g * 20.0F); - quaternion.hamiltonProduct(quaternion2); - matrixStack2.multiply(quaternion); + Quaternionf quaternion = RotationAxis.POSITIVE_Z.rotationDegrees(180.0F); + Quaternionf quaternion2 = RotationAxis.POSITIVE_X.rotationDegrees(g * 20.0F); + matrixStack2.multiply(quaternion.mul(quaternion2)); float h = entity.bodyYaw; float i = entity.getYaw(); float j = entity.getPitch(); @@ -102,22 +101,18 @@ public static void drawEntity(MatrixStack matrixStack2, int x, int y, int size, quaternion2.conjugate(); entityRenderDispatcher.setRotation(quaternion2); entityRenderDispatcher.setRenderShadows(false); - VertexConsumerProvider.Immediate immediate = - MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers(); - RenderSystem.runAsFancy(() -> { - entityRenderDispatcher.render(entity, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F, matrixStack2, immediate, - 15728880); + drawContext.draw(vertexConsumerProvider -> { + entityRenderDispatcher.render(entity, 0.0D, 0.0D, 0.0D, 1.0F, matrixStack2, + vertexConsumerProvider, 15728880); }); - immediate.draw(); entityRenderDispatcher.setRenderShadows(true); entity.bodyYaw = h; entity.setYaw(i); entity.setPitch(j); entity.prevHeadYaw = k; entity.headYaw = l; - matrixStack.pop(); + matrixStack.popMatrix(); matrixStack2.pop(); - RenderSystem.applyModelViewMatrix(); DiffuseLighting.enableGuiDepthLighting(); } diff --git a/src/main/java/net/torocraft/torohealth/display/Hud.java b/src/main/java/net/torocraft/torohealth/display/Hud.java index 39480da..1d6ef9e 100644 --- a/src/main/java/net/torocraft/torohealth/display/Hud.java +++ b/src/main/java/net/torocraft/torohealth/display/Hud.java @@ -2,7 +2,9 @@ import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.render.RenderLayer; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.LivingEntity; import net.minecraft.text.*; @@ -13,7 +15,7 @@ public class Hud extends Screen { private static final Identifier BACKGROUND_TEXTURE = - new Identifier(ToroHealth.MODID + ":textures/gui/default_skin_basic.png"); + Identifier.of(ToroHealth.MODID + ":textures/gui/default_skin_basic.png"); private EntityDisplay entityDisplay = new EntityDisplay(); private LivingEntity entity; private BarDisplay barDisplay; @@ -23,11 +25,11 @@ public class Hud extends Screen { public Hud() { super(Text.literal("ToroHealth HUD")); this.client = MinecraftClient.getInstance(); - barDisplay = new BarDisplay(MinecraftClient.getInstance(), this); + barDisplay = new BarDisplay(MinecraftClient.getInstance()); } - public void draw(MatrixStack matrix, Config config) { - if (this.client.options.debugEnabled) { + public void draw(DrawContext drawContext, Config config) { + if (this.client.getDebugHud().shouldShowDebugHud()) { return; } this.config = config; @@ -36,7 +38,7 @@ public void draw(MatrixStack matrix, Config config) { } float x = determineX(); float y = determineY(); - draw(matrix, x, y, config.hud.scale); + draw(drawContext, x, y, config.hud.scale); } private float determineX() { @@ -98,36 +100,36 @@ public LivingEntity getEntity() { return entity; } - private void draw(MatrixStack matrix, float x, float y, float scale) { + private void draw(DrawContext drawContext, float x, float y, float scale) { if (entity == null) { return; } - + if (config.hud.onlyWhenHurt && entity.getHealth() >= entity.getMaxHealth()) { return; } + MatrixStack matrix = drawContext.getMatrices(); matrix.push(); matrix.scale(scale, scale, scale); matrix.translate(x - 10, y - 10, 0); if (config.hud.showSkin) { - this.drawSkin(matrix); + this.drawSkin(drawContext); } matrix.translate(10, 10, 0); if (config.hud.showEntity) { - entityDisplay.draw(matrix, scale); + entityDisplay.draw(drawContext, matrix, scale); } matrix.translate(44, 0, 0); if (config.hud.showBar) { - barDisplay.draw(matrix, entity); + barDisplay.draw(drawContext, entity); } matrix.pop(); } - private void drawSkin(MatrixStack matrix) { - RenderSystem.setShaderTexture(0, BACKGROUND_TEXTURE); + private void drawSkin(DrawContext drawContext) { RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); int w = 160, h = 60; - drawTexture(matrix, 0, 0, 0.0f, 0.0f, w, h, w, h); + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, 0, 0, 0.0f, 0.0f, w, h, w, h); } } diff --git a/src/main/java/net/torocraft/torohealth/mixin/InGameHudMixin.java b/src/main/java/net/torocraft/torohealth/mixin/InGameHudMixin.java index 742bbb4..352381b 100644 --- a/src/main/java/net/torocraft/torohealth/mixin/InGameHudMixin.java +++ b/src/main/java/net/torocraft/torohealth/mixin/InGameHudMixin.java @@ -1,7 +1,8 @@ package net.torocraft.torohealth.mixin; +import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.hud.InGameHud; -import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.client.render.RenderTickCounter; import net.torocraft.torohealth.ToroHealth; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -12,8 +13,8 @@ public class InGameHudMixin { @Inject(method = "render", at = @At("RETURN")) - private void render(MatrixStack matrixStack, float partial, CallbackInfo info) { - ToroHealth.HUD.draw(matrixStack, ToroHealth.CONFIG); + private void render(DrawContext drawContext, RenderTickCounter tickCounter, CallbackInfo ci) { + ToroHealth.HUD.draw(drawContext, ToroHealth.CONFIG); } } diff --git a/src/main/java/net/torocraft/torohealth/mixin/PlayerEntityMixin.java b/src/main/java/net/torocraft/torohealth/mixin/PlayerEntityMixin.java index 0e9042c..b2cfca2 100644 --- a/src/main/java/net/torocraft/torohealth/mixin/PlayerEntityMixin.java +++ b/src/main/java/net/torocraft/torohealth/mixin/PlayerEntityMixin.java @@ -21,7 +21,7 @@ protected PlayerEntityMixin(EntityType type, World world @Inject(method = "tick()V", at = @At("HEAD")) private void tick(CallbackInfo info) { - if (!this.world.isClient) { + if (!this.getWorld().isClient) { return; } ToroHealth.HUD.setEntity(ToroHealth.RAYTRACE.getEntityInCrosshair(0, ToroHealth.CONFIG.hud.distance)); diff --git a/src/main/java/net/torocraft/torohealth/mixin/WorldRendererAccessor.java b/src/main/java/net/torocraft/torohealth/mixin/WorldRendererAccessor.java new file mode 100644 index 0000000..371fb56 --- /dev/null +++ b/src/main/java/net/torocraft/torohealth/mixin/WorldRendererAccessor.java @@ -0,0 +1,11 @@ +package net.torocraft.torohealth.mixin; + +import net.minecraft.client.render.BufferBuilderStorage; +import net.minecraft.client.render.WorldRenderer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(WorldRenderer.class) +public interface WorldRendererAccessor { + @Accessor BufferBuilderStorage getBufferBuilders(); +} diff --git a/src/main/java/net/torocraft/torohealth/mixin/WorldRendererMixin.java b/src/main/java/net/torocraft/torohealth/mixin/WorldRendererMixin.java index 8e1fc4f..8963908 100644 --- a/src/main/java/net/torocraft/torohealth/mixin/WorldRendererMixin.java +++ b/src/main/java/net/torocraft/torohealth/mixin/WorldRendererMixin.java @@ -1,19 +1,12 @@ package net.torocraft.torohealth.mixin; -import net.minecraft.client.render.Camera; -import net.minecraft.client.render.GameRenderer; -import net.minecraft.client.render.LightmapTextureManager; import net.minecraft.client.render.VertexConsumerProvider; import net.minecraft.client.render.WorldRenderer; -import net.minecraft.client.render.entity.EntityRenderDispatcher; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; -import net.minecraft.util.math.Matrix4f; import net.torocraft.torohealth.bars.HealthBarRenderer; -import net.torocraft.torohealth.bars.ParticleRenderer; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -21,8 +14,7 @@ @Mixin(WorldRenderer.class) public class WorldRendererMixin { - @Shadow - private EntityRenderDispatcher entityRenderDispatcher; + // @Shadow @Final private BufferBuilderStorage bufferBuilders; @Inject(method = "renderEntity", at = @At(value = "RETURN")) private void renderEntity(Entity entity, double x, double y, double z, float g, @@ -32,12 +24,13 @@ private void renderEntity(Entity entity, double x, double y, double z, float g, } } + /* Replaced by WorldRenderEvents.LAST event listener registered in ToroHealth.java @Inject(method = "render", at = @At(value = "RETURN")) private void render(MatrixStack matrices, float tickDelta, long limitTime, boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer, LightmapTextureManager lightmapTextureManager, Matrix4f matrix, CallbackInfo info) { - HealthBarRenderer.renderInWorld(matrices, camera); - ParticleRenderer.renderParticles(matrices, camera); - } + HealthBarRenderer.renderInWorld(matrices, this.bufferBuilders.getEntityVertexConsumers(), camera); + ParticleRenderer.renderParticles(matrices, this.bufferBuilders.getEntityVertexConsumers(), camera); + } */ } diff --git a/src/main/java/net/torocraft/torohealth/util/EntityUtil.java b/src/main/java/net/torocraft/torohealth/util/EntityUtil.java index ad11d0d..19a62bc 100644 --- a/src/main/java/net/torocraft/torohealth/util/EntityUtil.java +++ b/src/main/java/net/torocraft/torohealth/util/EntityUtil.java @@ -44,13 +44,13 @@ public static Relation determineRelation(Entity entity) { } public static boolean showHealthBar(Entity entity, MinecraftClient client) { - return entity instanceof LivingEntity + return entity instanceof LivingEntity living && !(entity instanceof ArmorStandEntity) && (!entity.isInvisibleTo(client.player) || entity.isGlowing() || entity.isOnFire() - || entity instanceof CreeperEntity && ((CreeperEntity) entity).shouldRenderOverlay() // charged creeper - || StreamSupport.stream(entity.getItemsEquipped().spliterator(), false).anyMatch(is -> !is.isEmpty())) + || entity instanceof CreeperEntity && ((CreeperEntity) entity).isCharged() // charged creeper + || StreamSupport.stream(living.getEquippedItems().spliterator(), false).anyMatch(is -> !is.isEmpty())) && entity != client.player && !entity.isSpectator(); } diff --git a/src/main/java/net/torocraft/torohealth/util/RayTrace.java b/src/main/java/net/torocraft/torohealth/util/RayTrace.java index 6610328..9eda5d9 100644 --- a/src/main/java/net/torocraft/torohealth/util/RayTrace.java +++ b/src/main/java/net/torocraft/torohealth/util/RayTrace.java @@ -24,7 +24,7 @@ import net.minecraft.world.RaycastContext.FluidHandling; public class RayTrace implements BlockView { - private static Predicate isVisible = entity -> !entity.isSpectator() && entity.collides(); + private static Predicate isVisible = entity -> !entity.isSpectator() && entity.canHit(); private static MinecraftClient minecraft = MinecraftClient.getInstance(); @Override @@ -103,8 +103,10 @@ public BlockHitResult raycast(RaycastContext context) { VoxelShape blockShape = c.getBlockShape(block, this, pos); return this.raycastBlock(c.getStart(), c.getEnd(), pos, blockShape, block); }, (c) -> { - Vec3d v = c.getStart().subtract(c.getEnd()); - return BlockHitResult.createMissed(c.getEnd(), Direction.getFacing(v.x, v.y, v.z), new BlockPos(c.getEnd())); + Vec3d end = c.getEnd(); + Vec3d v = c.getStart().subtract(end); + BlockPos endBlockPos = new BlockPos((int) end.x, (int) end.y, (int) end.z); + return BlockHitResult.createMissed(end, Direction.getFacing(v.x, v.y, v.z), endBlockPos); }); } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 16604a7..0d2d3ad 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -27,9 +27,9 @@ "modmenu": "*" }, "depends": { - "fabricloader": ">=0.14.6", + "fabricloader": ">=0.14.21", "fabric": "*", - "minecraft": "1.19.x", - "java": ">=17" + "minecraft": "1.21.x", + "java": ">=21" } } diff --git a/src/main/resources/torohealth.mixins.json b/src/main/resources/torohealth.mixins.json index 4b498d8..cfc24e3 100644 --- a/src/main/resources/torohealth.mixins.json +++ b/src/main/resources/torohealth.mixins.json @@ -7,9 +7,10 @@ "client": [ "InGameHudMixin", "PlayerEntityMixin", - "WorldRendererMixin" + "WorldRendererMixin", + "WorldRendererAccessor" ], "injectors": { "defaultRequire": 1 } -} \ No newline at end of file +}