Skip to content

Commit

Permalink
NEXTGEN: Fixed NameProtect module making names misaligned. (CCBlueX#1169
Browse files Browse the repository at this point in the history
)

* Closes CCBlueX#510

I've added support for hiding the user's friends too, along with both options having name color change support.

Plus during my tests, I had a slight difficulty friend clicking players because they were out of range, so I made an option that can modify the pick-up range so nobody suffers.
  • Loading branch information
mems01 authored Jul 26, 2023
1 parent 008ad4a commit 8c8c761
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.text.CharacterVisitor;
import net.minecraft.text.OrderedText;
import net.minecraft.text.StringVisitable;
import org.joml.Matrix4f;
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.Redirect;

import java.util.Optional;

@Mixin(TextRenderer.class)
public abstract class MixinTextRenderer implements IMixinGameRenderer {
@Shadow protected abstract float drawLayer(String text, float x, float y, int color, boolean shadow, Matrix4f matrix, VertexConsumerProvider vertexConsumerProvider, TextRenderer.TextLayerType layerType, int underlineColor, int light);
@Shadow
protected abstract float drawLayer(String text, float x, float y, int color, boolean shadow, Matrix4f matrix, VertexConsumerProvider vertexConsumerProvider, TextRenderer.TextLayerType layerType, int underlineColor, int light);

@Redirect(method = "drawInternal(Ljava/lang/String;FFIZLorg/joml/Matrix4f;Lnet/minecraft/client/render/VertexConsumerProvider;Lnet/minecraft/client/font/TextRenderer$TextLayerType;IIZ)I", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/font/TextRenderer;drawLayer(Ljava/lang/String;FFIZLorg/joml/Matrix4f;Lnet/minecraft/client/render/VertexConsumerProvider;Lnet/minecraft/client/font/TextRenderer$TextLayerType;II)F"))
private float injectNameProtectA(TextRenderer textRenderer, String text, float x, float y, int color, boolean shadow, Matrix4f matrix, VertexConsumerProvider vertexConsumerProvider, TextRenderer.TextLayerType layerType, int underlineColor, int light) {
Expand All @@ -46,6 +50,16 @@ private float injectNameProtectWidthA(TextHandler textHandler, String text) {
return textHandler.getWidth(ModuleNameProtect.INSTANCE.replace(text));
}

@Redirect(method = "getWidth(Lnet/minecraft/text/StringVisitable;)I", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/font/TextHandler;getWidth(Lnet/minecraft/text/StringVisitable;)F"))
private float injectNameProtectWidthB(TextHandler instance, StringVisitable text) {
StringBuilder stringBuilder = new StringBuilder();
text.visit((asString -> {
stringBuilder.append(asString);
return Optional.empty();
}));
return instance.getWidth(ModuleNameProtect.INSTANCE.replace(stringBuilder.toString()));
}

@Redirect(method = "drawLayer(Lnet/minecraft/text/OrderedText;FFIZLorg/joml/Matrix4f;Lnet/minecraft/client/render/VertexConsumerProvider;Lnet/minecraft/client/font/TextRenderer$TextLayerType;II)F", at = @At(value = "INVOKE", target = "Lnet/minecraft/text/OrderedText;accept(Lnet/minecraft/text/CharacterVisitor;)Z"))
private boolean injectNameProtectB(OrderedText orderedText, CharacterVisitor visitor) {
if (ModuleNameProtect.INSTANCE.getEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import net.ccbluex.liquidbounce.event.repeatable
import net.ccbluex.liquidbounce.features.misc.FriendManager
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.utils.aiming.facingEnemy
import net.ccbluex.liquidbounce.utils.aiming.raytraceEntity
import net.ccbluex.liquidbounce.utils.client.notification
import net.ccbluex.liquidbounce.utils.entity.rotation
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.text.Text
import net.minecraft.util.hit.EntityHitResult

/**
* FriendClicker module
Expand All @@ -37,14 +39,22 @@ import net.minecraft.util.hit.EntityHitResult

object ModuleFriendClicker : Module("FriendClicker", Category.MISC) {

private val pickUpRange by float("PickUpRange", 3.0f, 1f..100f)

private var clicked = false

val repeatable = repeatable {
val crosshair = mc.crosshairTarget
val rotation = player.rotation

val entity = (raytraceEntity(pickUpRange.toDouble(), rotation) { it is PlayerEntity }
?: return@repeatable) as PlayerEntity

val facesEnemy = facingEnemy(entity, rotation, pickUpRange.toDouble(), wallsRange = 0.0)

val pickup = mc.options.pickItemKey.isPressed

if (crosshair is EntityHitResult && crosshair.entity is PlayerEntity && pickup && !clicked) {
val name = (crosshair.entity as PlayerEntity).gameProfile.name
if (facesEnemy && pickup && !clicked) {
val name = entity.entityName

if (FriendManager.isFriend(name)) {
FriendManager.friends.remove(FriendManager.Friend(name, null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@

package net.ccbluex.liquidbounce.features.module.modules.misc

import net.ccbluex.liquidbounce.config.ToggleableConfigurable
import net.ccbluex.liquidbounce.event.GameRenderEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.misc.FriendManager
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.render.engine.Color4b
import net.ccbluex.liquidbounce.render.utils.rainbow
import net.minecraft.text.CharacterVisitor
import net.minecraft.text.OrderedText
import net.minecraft.text.Style
import net.minecraft.util.Formatting
import net.minecraft.text.TextColor

/**
* NameProtect module
Expand All @@ -37,14 +41,35 @@ import net.minecraft.util.Formatting
object ModuleNameProtect : Module("NameProtect", Category.MISC) {

val replacement by text("Replacement", "You")
val replaceFriendNames by boolean("ObfuscateFriends", true)

val color by color("Color", Color4b.WHITE)
val colorRainbow by boolean("Rainbow", false)

object ReplaceFriendNames : ToggleableConfigurable(this, "ObfuscateFriends", true) {
val color by color("Color", Color4b(255, 179, 72, 255))
val colorRainbow by boolean("Rainbow", false)
}

init {
tree(ReplaceFriendNames)
}

val replacements = ArrayList<ReplacementMapping>()

val renderEventHandler = handler<GameRenderEvent> {
replacements.clear()

replacements.add(ReplacementMapping(mc.session.username, this.replacement))
if (ReplaceFriendNames.enabled) {
FriendManager.friends.withIndex().forEach { (id, friend) ->
val color4b = if (ReplaceFriendNames.colorRainbow) rainbow() else ReplaceFriendNames.color

replacements.add(ReplacementMapping(friend.name, "Friend $id", color4b))
}
}

val color4b = if (colorRainbow) rainbow() else color

replacements.add(ReplacementMapping(mc.session.username, this.replacement, color4b))

// Prevent shorter names being replaced before longer names
replacements.sortByDescending { it.originalName.length }
Expand Down Expand Up @@ -129,7 +154,7 @@ object ModuleNameProtect : Module("NameProtect", Category.MISC) {
return charsIndex - index
}

class ReplacementMapping(val originalName: String, val replacement: String)
class ReplacementMapping(val originalName: String, val replacement: String, val color4b: Color4b)

class NameProtectOrderedText(original: OrderedText) : OrderedText {
private val mappedCharacters = ArrayList<MappedCharacter>()
Expand Down Expand Up @@ -160,23 +185,18 @@ object ModuleNameProtect : Module("NameProtect", Category.MISC) {
for ((replacementIdx, c) in replacement.originalName.toCharArray().withIndex()) {
val origIndex = index + replacementIdx

if (originalCharacters.lastIndex < origIndex || originalCharacters[origIndex].codePoint != c.toInt()) {
if (originalCharacters.lastIndex < origIndex || originalCharacters[origIndex].codePoint != c.code) {
canReplace = false
break
}
}

if (canReplace) {
this.mappedCharacters.addAll(
replacement.replacement.map {
MappedCharacter(
originalChar.style.withColor(
Formatting.RED
),
it.toInt()
)
}
)
this.mappedCharacters.addAll(replacement.replacement.map {
MappedCharacter(
originalChar.style.withColor(TextColor.parse(replacement.color4b.toHex())), it.code
)
})
index += replacement.originalName.length
return@run
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ data class Color4b(val r: Int, val g: Int, val b: Int, val a: Int) {
buffer.put(idx + 3, a.toByte())
}

fun toHex(): String {
val hex = StringBuilder("#")

hex.append(componentToHex(r))
hex.append(componentToHex(g))
hex.append(componentToHex(b))

return hex.toString().uppercase()
}

private fun componentToHex(c: Int): String {
val hexString = Integer.toHexString(c)
return if (hexString.length == 1) "0$hexString" else hexString
}

fun red(red: Int) = Color4b(red, this.g, this.b, this.a)

Expand Down Expand Up @@ -233,7 +247,5 @@ enum class PrimitiveType(val verticesPerPrimitive: Int, val mode: Int) {
/**
* Line loop; 1 vertices per primitive
*/
LineLoop(1, GL11.GL_LINE_LOOP),
LineStrip(1, GL11.GL_LINE_STRIP),
Points(1, GL11.GL_POINTS)
LineLoop(1, GL11.GL_LINE_LOOP), LineStrip(1, GL11.GL_LINE_STRIP), Points(1, GL11.GL_POINTS)
}

0 comments on commit 8c8c761

Please sign in to comment.