|
| 1 | +/* |
| 2 | + * Copyright 2024 Lambda |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.lambda.mixin.render; |
| 19 | + |
| 20 | +import com.lambda.Lambda; |
| 21 | +import com.lambda.graphics.renderer.gui.font.FontRenderer; |
| 22 | +import com.lambda.graphics.renderer.gui.font.LambdaEmoji; |
| 23 | +import com.lambda.module.modules.client.LambdaMoji; |
| 24 | +import com.lambda.util.math.Vec2d; |
| 25 | +import net.minecraft.client.font.TextRenderer; |
| 26 | +import net.minecraft.client.render.VertexConsumerProvider; |
| 27 | +import net.minecraft.text.OrderedText; |
| 28 | +import net.minecraft.text.Text; |
| 29 | +import org.joml.Matrix4f; |
| 30 | +import org.spongepowered.asm.mixin.Mixin; |
| 31 | +import org.spongepowered.asm.mixin.Overwrite; |
| 32 | +import org.spongepowered.asm.mixin.Shadow; |
| 33 | +import org.spongepowered.asm.mixin.Unique; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +@Mixin(TextRenderer.class) |
| 38 | +public abstract class TextRendererMixin { |
| 39 | + @Shadow |
| 40 | + protected abstract int drawInternal(String text, float x, float y, int color, boolean shadow, Matrix4f matrix, VertexConsumerProvider vertexConsumers, TextRenderer.TextLayerType layerType, int backgroundColor, int light, boolean mirror); |
| 41 | + |
| 42 | + @Shadow |
| 43 | + protected abstract int drawInternal(OrderedText text, float x, float y, int color, boolean shadow, Matrix4f matrix, VertexConsumerProvider vertexConsumerProvider, TextRenderer.TextLayerType layerType, int backgroundColor, int light); |
| 44 | + |
| 45 | + /** |
| 46 | + * @author Edouard127 |
| 47 | + * @reason xx |
| 48 | + */ |
| 49 | + @Overwrite |
| 50 | + public int draw( |
| 51 | + String text, |
| 52 | + float x, |
| 53 | + float y, |
| 54 | + int color, |
| 55 | + boolean shadow, |
| 56 | + Matrix4f matrix, |
| 57 | + VertexConsumerProvider vertexConsumers, |
| 58 | + TextRenderer.TextLayerType layerType, |
| 59 | + int backgroundColor, |
| 60 | + int light, |
| 61 | + boolean rightToLeft |
| 62 | + ) { |
| 63 | + if (LambdaMoji.INSTANCE.isDisabled()) return this.drawInternal(text, x, y, color, shadow, matrix, vertexConsumers, layerType, backgroundColor, light, rightToLeft); |
| 64 | + |
| 65 | + return this.drawInternal(neoLambda$parseEmojisAndRender(text, x, y), x, y, color, shadow, matrix, vertexConsumers, layerType, backgroundColor, light, rightToLeft); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @author Edouard127 |
| 70 | + * @reason xx |
| 71 | + */ |
| 72 | + @Overwrite |
| 73 | + public int draw( |
| 74 | + OrderedText text, |
| 75 | + float x, |
| 76 | + float y, |
| 77 | + int color, |
| 78 | + boolean shadow, |
| 79 | + Matrix4f matrix, |
| 80 | + VertexConsumerProvider vertexConsumers, |
| 81 | + TextRenderer.TextLayerType layerType, |
| 82 | + int backgroundColor, |
| 83 | + int light |
| 84 | + ) { |
| 85 | + if (LambdaMoji.INSTANCE.isDisabled()) return this.drawInternal(text, x, y, color, shadow, matrix, vertexConsumers, layerType, backgroundColor, light); |
| 86 | + |
| 87 | + StringBuilder builder = new StringBuilder(); |
| 88 | + text.accept((index, style, c) -> { |
| 89 | + builder.appendCodePoint(c); |
| 90 | + return true; |
| 91 | + }); |
| 92 | + |
| 93 | + return this.drawInternal( |
| 94 | + Text.literal(neoLambda$parseEmojisAndRender(builder.toString(), x, y)).asOrderedText(), |
| 95 | + x, y, color, shadow, matrix, vertexConsumers, layerType, backgroundColor, light |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + @Unique |
| 100 | + private String neoLambda$parseEmojisAndRender(String raw, float x, float y) { |
| 101 | + List<String> emojis = LambdaEmoji.Twemoji.parse(raw); |
| 102 | + |
| 103 | + for (String emoji : emojis) { |
| 104 | + String constructed = ":" + emoji + ":"; |
| 105 | + int index = raw.indexOf(constructed); |
| 106 | + |
| 107 | + if (LambdaEmoji.Twemoji.get(emoji) == null || |
| 108 | + index == -1) continue; |
| 109 | + |
| 110 | + int height = Lambda.getMc().textRenderer.fontHeight; |
| 111 | + int width = Lambda.getMc().textRenderer.getWidth(raw.substring(0, index)); |
| 112 | + |
| 113 | + LambdaMoji.INSTANCE.push(constructed, new Vec2d(x + width, y + (float) height / 2)); |
| 114 | + |
| 115 | + // Replace the emoji with whitespaces depending on the player's settings |
| 116 | + raw = raw.replaceFirst(constructed, neoLambda$getReplacement()); |
| 117 | + } |
| 118 | + |
| 119 | + return raw; |
| 120 | + } |
| 121 | + |
| 122 | + @Unique |
| 123 | + private String neoLambda$getReplacement() { |
| 124 | + int emojiWidth = (int) (((double) Lambda.getMc().textRenderer.fontHeight / 2 / Lambda.getMc().textRenderer.getWidth(" ")) * LambdaMoji.INSTANCE.getScale()); |
| 125 | + return " ".repeat(emojiWidth); |
| 126 | + } |
| 127 | +} |
0 commit comments