Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
RedthMC committed Oct 23, 2023
1 parent 0973f87 commit 6f4d591
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
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((RenderAccessor) this, entity, x, y, z, callbackInfo);
HooksKt.callback(this, entity, x, y, z, callbackInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
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((RenderAccessor) this, entity, x, y, z, callbackInfo);
HooksKt.callback(this, entity, x, y, z, callbackInfo);
}
}
65 changes: 29 additions & 36 deletions src/main/kotlin/org/polyfrost/polynametag/hooks/Hooks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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.EntityLiving
import net.minecraft.entity.EntityLivingBase
import org.lwjgl.opengl.GL11
import org.polyfrost.polynametag.config.ModConfig
import org.polyfrost.polynametag.mixin.RenderAccessor
Expand All @@ -18,96 +18,89 @@ fun nametagTransformation() {
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

fun callback(renderer: RenderAccessor, entity: Entity, x: Double, y: Double, z: Double, callbackInfo: CallbackInfo) {
fun callback(renderer: Any, entity: Entity, x: Double, y: Double, z: Double, callbackInfo: CallbackInfo) {
if (!ModConfig.enabled) return

callbackInfo.cancel()
}

fun renderNametag(renderer: RendererLivingEntity<EntityLiving>, entity: EntityLiving, x: Double, y: Double, z: Double) {
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
GlStateManager.alphaFunc(516, 0.1f)

var y2 = y + entity.scaledHeight + 0.5

GlStateManager.alphaFunc(GL11.GL_GREATER, 0.1f)

if (entity.isSneaking) {
renderSneaking(renderer, entity, x, y, z, displayName)
} else {
renderNormal(renderer, entity, x, y - if (entity.isChild) (entity.height / 2.0f).toDouble() else 0.0, z, displayName)
renderNormal(renderer, entity, x, y, z, displayName)
}
}

fun canRenderName(renderer: Render<out Entity>, entity: Entity) =
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: RendererLivingEntity<EntityLiving>, entity: EntityLiving, x: Double, y: Double, z: Double, displayName: String) {
val fontrenderer: FontRenderer = renderer.fontRendererFromRenderManager
fun renderSneaking(renderer: Render<*>, entity: EntityLivingBase, x: Double, y: Double, z: Double, displayName: String) {
val fontRenderer: FontRenderer = renderer.fontRendererFromRenderManager
GlStateManager.pushMatrix()
GlStateManager.translate(x.toFloat(), y.toFloat() + entity.height + 0.5f - if (entity.isChild) entity.height / 2.0f else 0.0f, z.toFloat())
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(-0.02666667f, -0.02666667f, 0.02666667f)
GlStateManager.translate(0.0f, 9.374999f, 0.0f)
GlStateManager.scale(-SCALE_VALUE, -SCALE_VALUE, SCALE_VALUE)
GlStateManager.disableLighting()
GlStateManager.depthMask(false)
GlStateManager.enableBlend()
GlStateManager.disableTexture2D()
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0)
drawBackground(fontrenderer.getStringWidth(displayName))
GlStateManager.disableTexture2D()
drawBackground(fontRenderer.getStringWidth(displayName))
GlStateManager.enableTexture2D()
GlStateManager.depthMask(true)
fontrenderer.drawString(displayName, -fontrenderer.getStringWidth(displayName) / 2, 0, 553648127)
fontRenderer.drawString(displayName, -fontRenderer.getStringWidth(displayName) / 2, 0, 553648127)
GlStateManager.enableLighting()
GlStateManager.disableBlend()
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f)
GlStateManager.popMatrix()
}

fun renderNormal(renderer: RendererLivingEntity<EntityLiving>, entity: EntityLiving, x: Double, y: Double, z: Double, displayName: String) {
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

val fontRenderer: FontRenderer = renderer.fontRendererFromRenderManager
val f = 1.6f
val g = 0.016666668f * f
GlStateManager.pushMatrix()
GlStateManager.translate(x.toFloat() + 0.0f, y.toFloat() + entity.height + 0.5f, z.toFloat())
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(-g, -g, g)
GlStateManager.scale(-SCALE_VALUE, -SCALE_VALUE, SCALE_VALUE)
GlStateManager.disableLighting()
GlStateManager.depthMask(false)
GlStateManager.disableDepth()
GlStateManager.disableDepth() // added
GlStateManager.enableBlend()
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0)
val tessellator = Tessellator.getInstance()
val worldRenderer = tessellator.worldRenderer
var i = 0
if (displayName == "deadmau5") {
i = -10
}
val j = fontRenderer.getStringWidth(displayName) / 2
GlStateManager.disableTexture2D()
worldRenderer.begin(7, DefaultVertexFormats.POSITION_COLOR)
worldRenderer.pos((-j - 1).toDouble(), (-1 + i).toDouble(), 0.0).color(0.0f, 0.0f, 0.0f, 0.25f).endVertex()
worldRenderer.pos((-j - 1).toDouble(), (8 + i).toDouble(), 0.0).color(0.0f, 0.0f, 0.0f, 0.25f).endVertex()
worldRenderer.pos((j + 1).toDouble(), (8 + i).toDouble(), 0.0).color(0.0f, 0.0f, 0.0f, 0.25f).endVertex()
worldRenderer.pos((j + 1).toDouble(), (-1 + i).toDouble(), 0.0).color(0.0f, 0.0f, 0.0f, 0.25f).endVertex()
tessellator.draw()
drawBackground(fontRenderer.getStringWidth(displayName)) // no depth in vanilla
GlStateManager.enableTexture2D()
fontRenderer.drawString(displayName, -fontRenderer.getStringWidth(displayName) / 2, i, 553648127)
GlStateManager.enableDepth()
fontRenderer.drawString(displayName, -fontRenderer.getStringWidth(displayName) / 2, 0, 553648127)
GlStateManager.enableDepth() // added
GlStateManager.depthMask(true)
fontRenderer.drawString(displayName, -fontRenderer.getStringWidth(displayName) / 2, i, -1)
fontRenderer.drawString(displayName, -fontRenderer.getStringWidth(displayName) / 2, 0, -1)
GlStateManager.enableLighting()
GlStateManager.disableBlend()
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f)
Expand Down

0 comments on commit 6f4d591

Please sign in to comment.