Skip to content

Commit

Permalink
1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RedthMC committed Oct 24, 2023
1 parent 6bec19c commit c1fb819
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 120 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ repositories {
dependencies {
// Adds the OneConfig library, so we can develop with it.
modCompileOnly("cc.polyfrost:oneconfig-$platform:0.2.1-alpha+")

modRuntimeOnly("me.djtheredstoner:DevAuth-${if (platform.isFabric) "fabric" else if (platform.isLegacyForge) "forge-legacy" else "forge-latest"}:1.1.2")

// If we are building for legacy forge, includes the launch wrapper with `shade` as we configured earlier.
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/org/polyfrost/polynametag/mixin/RenderAccessor.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Render.class)
public class RenderMixin {
@Inject(method = "renderName(Lnet/minecraft/entity/Entity;DDD)V", at = @At("HEAD"))
private void polyNametag$renderName(Entity entity, double x, double y, double z, CallbackInfo callbackInfo) {
HooksKt.callback(this, entity, x, y, z, callbackInfo);
public abstract class RenderMixin {
@Inject(method = "renderLivingLabel", at = @At("HEAD"), cancellable = true)
private void polyNametag$overrideNametag(Entity entity, String displayName, double x, double y, double z, int range, CallbackInfo callbackInfo) {
HooksKt.overrideNametag(this, entity, displayName, x, y, z, range, callbackInfo);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
package org.polyfrost.polynametag.mixin;

import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.entity.RendererLivingEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import org.polyfrost.polynametag.hooks.HooksKt;
import org.polyfrost.polynametag.config.ModConfig;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(RendererLivingEntity.class)
public class RendererLivingEntityMixin {
@Inject(method = "renderName(Lnet/minecraft/entity/EntityLivingBase;DDD)V", at = @At("HEAD"))
private void polyNametag$renderName(EntityLivingBase entity, double x, double y, double z, CallbackInfo callbackInfo) {
HooksKt.callback(this, entity, x, y, z, callbackInfo);
public abstract class RendererLivingEntityMixin extends Render<EntityLivingBase> {
private RendererLivingEntityMixin(RenderManager renderManager) {
super(renderManager);
}

@Inject(
method = "renderName(Lnet/minecraft/entity/EntityLivingBase;DDD)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/renderer/entity/RendererLivingEntity;getFontRendererFromRenderManager()Lnet/minecraft/client/gui/FontRenderer;"
),
cancellable = true
)
private void polyNametag$overrideSneakingNametag(EntityLivingBase entity, double x, double y, double z, CallbackInfo callbackInfo) {
if (!ModConfig.INSTANCE.enabled) return;
if (entity.isChild()) y -= entity.height / 2.0;
renderLivingLabel(entity, entity.getDisplayName().getFormattedText(), x, y, z, 32);
callbackInfo.cancel();
}

@Redirect(
method = "canRenderName(Lnet/minecraft/entity/EntityLivingBase;)Z",
at = @At(
value = "FIELD",
target = "Lnet/minecraft/client/renderer/entity/RenderManager;livingPlayer:Lnet/minecraft/entity/Entity;"
)
)
private Entity polyNametag$cancelSelfCheck(RenderManager renderManager) {
boolean shouldShowOwnNametag = ModConfig.INSTANCE.enabled && ModConfig.INSTANCE.getShowOwnNametag();
return shouldShowOwnNametag ? null : renderManager.livingPlayer;
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/org/polyfrost/polynametag/PolyNametag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object PolyNametag {
const val VERSION = "@VER@"

@Mod.EventHandler
fun onInit(e: FMLInitializationEvent) {
fun onInit(event: FMLInitializationEvent) {
ModConfig
}
}
18 changes: 6 additions & 12 deletions src/main/kotlin/org/polyfrost/polynametag/config/ModConfig.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package org.polyfrost.polynametag.config

import cc.polyfrost.oneconfig.config.Config
import cc.polyfrost.oneconfig.config.annotations.*
import cc.polyfrost.oneconfig.config.annotations.Color
import cc.polyfrost.oneconfig.config.annotations.Slider
import cc.polyfrost.oneconfig.config.annotations.Switch
import cc.polyfrost.oneconfig.config.core.OneColor
import cc.polyfrost.oneconfig.config.data.InfoType
import cc.polyfrost.oneconfig.config.data.Mod
import cc.polyfrost.oneconfig.config.data.ModType
import org.polyfrost.polynametag.PolyNametag

object ModConfig : Config(Mod(PolyNametag.NAME, ModType.UTIL_QOL), "${PolyNametag.MODID}.json") {

@Info(
text = "Some features may have conflict with Patcher",
type = InfoType.WARNING,
size = 2
)
private var warning = false

@Slider(name = "Height offset", min = -0.5f, max = 0.5f)
var heightOffset = 0f

Expand All @@ -26,14 +20,14 @@ object ModConfig : Config(Mod(PolyNametag.NAME, ModType.UTIL_QOL), "${PolyNameta
@Switch(name = "Text shadow")
var textShadow = false

@Switch(name = "Show self nametag")
var showSelfNametag = true
@Switch(name = "Show own nametag")
var showOwnNametag = true

@Switch(name = "Background")
var background = true

@Color(name = "Background color")
var backgroundColor = OneColor(0f, 0f, 0f, 0.25f)
var backgroundColor = OneColor(0, 0, 0, 63)

init {
initialize()
Expand Down
149 changes: 65 additions & 84 deletions src/main/kotlin/org/polyfrost/polynametag/hooks/Hooks.kt
Original file line number Diff line number Diff line change
@@ -1,125 +1,106 @@
package org.polyfrost.polynametag.hooks

import cc.polyfrost.oneconfig.utils.dsl.mc
import net.minecraft.client.gui.FontRenderer
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.client.renderer.Tessellator
import net.minecraft.client.renderer.entity.Render
import net.minecraft.client.renderer.entity.RendererLivingEntity
import net.minecraft.client.renderer.vertex.DefaultVertexFormats
import net.minecraft.entity.Entity
import net.minecraft.entity.EntityLivingBase
import org.lwjgl.opengl.GL11
import org.polyfrost.polynametag.config.ModConfig
import org.polyfrost.polynametag.mixin.RenderAccessor
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo
import kotlin.math.max

fun nametagTransformation() {
GlStateManager.translate(0f, ModConfig.heightOffset, 0f)
GlStateManager.scale(ModConfig.scale, ModConfig.scale, ModConfig.scale)
}

private const val SCALE_VALUE = 0.02666667f
private const val NAME_TAG_RANGE = 64.0
private const val NAME_TAG_RANGE_SNEAK = 32.0
private const val NAMETAG_SCALE = 0.02666667f

fun callback(renderer: Any, entity: Entity, x: Double, y: Double, z: Double, callbackInfo: CallbackInfo) {
fun overrideNametag(renderer: Any, entity: Entity, displayName: String, x: Double, y: Double, z: Double, range: Int, callbackInfo: CallbackInfo) {
if (!ModConfig.enabled) return
if (renderer !is Render<*>) return

if (!entity.isWithinRange(renderer.renderManager.livingPlayer, range)) return

renderNametag(renderer, entity, displayName, x, y, z)
callbackInfo.cancel()
}

private val Entity.scaledHeight get() = if (this is EntityLivingBase && isChild) height * 0.5f else height

fun renderNametag(renderer: RendererLivingEntity<*>, entity: EntityLivingBase, x: Double, y: Double, z: Double) {
if (!canRenderName(renderer, entity)) return

val displayName: String = entity.displayName.formattedText

var y2 = y + entity.scaledHeight + 0.5

GlStateManager.alphaFunc(GL11.GL_GREATER, 0.1f)
private fun Entity.isWithinRange(entity: Entity, range: Int) = getDistanceSqToEntity(entity) < range * range

private fun renderNametag(renderer: Render<*>, entity: Entity, displayName: String, x: Double, y: Double, z: Double) {
val fontRenderer = renderer.fontRendererFromRenderManager
val textHalfWidth = fontRenderer.getStringWidth(displayName) / 2f
val sneaking = entity.isSneaking
var yAboveHead = y + entity.height + 0.5 + ModConfig.heightOffset
if (sneaking) yAboveHead -= 0.25
val scale = NAMETAG_SCALE * ModConfig.scale
val checkPerspective = if (mc.gameSettings.thirdPersonView == 2) -1 else 1
GL11.glNormal3f(0f, 1f, 0f)
GlStateManager.pushMatrix()
GlStateManager.translate(x, yAboveHead, z)
GlStateManager.rotate(-renderer.renderManager.playerViewY, 0f, 1f, 0f)
GlStateManager.rotate(renderer.renderManager.playerViewX * checkPerspective, 1f, 0f, 0f)
GlStateManager.scale(-scale, -scale, scale)
GlStateManager.disableLighting()

if (entity.isSneaking) {
renderSneaking(renderer, entity, x, y, z, displayName)
} else {
renderNormal(renderer, entity, x, y, z, displayName)
if (!sneaking) {
GlStateManager.disableDepth()
}
}

fun canRenderName(renderer: Render<*>, entity: Entity) =
(renderer as RenderAccessor).invokeCanRenderName(entity) && entity.isWithinDistance(
entity = renderer.renderManager.livingPlayer,
distance = if (entity.isSneaking) NAME_TAG_RANGE_SNEAK else NAME_TAG_RANGE
)

fun Entity.isWithinDistance(entity: Entity, distance: Double) = getDistanceSqToEntity(entity) < distance * distance

fun renderSneaking(renderer: Render<*>, entity: EntityLivingBase, x: Double, y: Double, z: Double, displayName: String) {
val fontRenderer: FontRenderer = renderer.fontRendererFromRenderManager
GlStateManager.pushMatrix()
GlStateManager.translate(x, y + entity.scaledHeight + 0.5, z)
GlStateManager.translate(0.0, -0.25, 0.0)
GL11.glNormal3f(0.0f, 1.0f, 0.0f)
GlStateManager.rotate(-renderer.renderManager.playerViewY, 0.0f, 1.0f, 0.0f)
GlStateManager.rotate(renderer.renderManager.playerViewX, 1.0f, 0.0f, 0.0f)
GlStateManager.scale(-SCALE_VALUE, -SCALE_VALUE, SCALE_VALUE)
GlStateManager.disableLighting()
GlStateManager.depthMask(false)
GlStateManager.enableBlend()
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0)
GlStateManager.disableTexture2D()
drawBackground(fontRenderer.getStringWidth(displayName))
GlStateManager.enableTexture2D()
GlStateManager.depthMask(true)
fontRenderer.drawString(displayName, -fontRenderer.getStringWidth(displayName) / 2, 0, 0x20FFFFFF)
drawBackground(textHalfWidth)
fontRenderer.drawStringWithoutZFighting(displayName, -textHalfWidth, 0f, 0x20FFFFFF)

if (!sneaking) {
GlStateManager.enableDepth()
fontRenderer.drawStringWithoutZFighting(displayName, -textHalfWidth, 0f, 0xFFFFFFFF.toInt())
}

GlStateManager.enableLighting()
GlStateManager.disableBlend()
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f)
GlStateManager.color(1f, 1f, 1f, 1f)
GlStateManager.popMatrix()
}

fun renderNormal(renderer: Render<*>, entity: Entity, x: Double, y: Double, z: Double, displayName: String) {
val distance: Double = entity.getDistanceSqToEntity(renderer.renderManager.livingPlayer)
if (distance > 64 * 64) return // redundant check
private fun drawBackground(textHalfWidth: Float) {
if (!ModConfig.background) return

val fontRenderer: FontRenderer = renderer.fontRendererFromRenderManager
GlStateManager.pushMatrix()
GlStateManager.translate(x, y + entity.scaledHeight + 0.5, z)
GL11.glNormal3f(0.0f, 1.0f, 0.0f)
GlStateManager.rotate(-renderer.renderManager.playerViewY, 0.0f, 1.0f, 0.0f)
GlStateManager.rotate(renderer.renderManager.playerViewX, 1.0f, 0.0f, 0.0f)
GlStateManager.scale(-SCALE_VALUE, -SCALE_VALUE, SCALE_VALUE)
GlStateManager.disableLighting()
GlStateManager.depthMask(false)
GlStateManager.disableDepth() // difference
GlStateManager.enableBlend()
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0)
GlStateManager.disableTexture2D()
drawBackground(fontRenderer.getStringWidth(displayName)) // no depth as same in vanilla
GlStateManager.enableTexture2D()
fontRenderer.drawString(displayName, -fontRenderer.getStringWidth(displayName) / 2, 0, 0x20FFFFFF) // transparent in blocks
GlStateManager.enableDepth() // difference
GlStateManager.depthMask(true)
fontRenderer.drawString(displayName, -fontRenderer.getStringWidth(displayName) / 2, 0, 0xFFFFFFFF.toInt())
GlStateManager.enableLighting()
GlStateManager.disableBlend()
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f)
GlStateManager.popMatrix()
}

fun drawBackground(textWidth: Int) {
GlStateManager.color(0.0f, 0.0f, 0.0f, 0.25f)
with(ModConfig.backgroundColor) {
GlStateManager.color(red / 255f, green / 255f, blue / 255f, alpha / 255f)
}

val halfWidth = textWidth / 2.0 + 1.0
val halfWidth = textHalfWidth + 1.0
val tessellator = Tessellator.getInstance()

with(tessellator.worldRenderer) {
begin(7, DefaultVertexFormats.POSITION)
pos(-halfWidth, -1.0, 0.0).endVertex()
pos(-halfWidth, 8.0, 0.0).endVertex()
pos(halfWidth, 8.0, 0.0).endVertex()
pos(halfWidth, -1.0, 0.0).endVertex()
pos(-halfWidth, -1.0, 0.01).endVertex()
pos(-halfWidth, 8.0, 0.01).endVertex()
pos(halfWidth, 8.0, 0.01).endVertex()
pos(halfWidth, -1.0, 0.01).endVertex()
}

tessellator.draw()

GlStateManager.enableTexture2D()
}

private fun FontRenderer.drawStringWithoutZFighting(text: String, x: Float, y: Float, color: Int): Int {
if (!ModConfig.textShadow) return drawString(text, x, y, color, false)
GlStateManager.pushMatrix()
val shadowX = drawString(text, x + 1f, y + 1f, shadowColor(color), false)
GlStateManager.translate(0f, 0f, -0.01f)
val normalX = drawString(text, x, y, color, false)
GlStateManager.popMatrix()
return max(shadowX, normalX)
}

private fun shadowColor(color: Int): Int {
val shiftedAlpha = color and 0xFF000000.toInt()
val rgbCapped = color and 0xFCFCFC
val quarterRGB = rgbCapped shr 2
return quarterRGB or shiftedAlpha
}
1 change: 0 additions & 1 deletion src/main/resources/mixins.polynametag.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"maxShiftBy": 5
},
"client": [
"RenderAccessor",
"RendererLivingEntityMixin",
"RenderMixin"
],
Expand Down

0 comments on commit c1fb819

Please sign in to comment.