Search in sources :

Example 11 with OrderedText

use of net.minecraft.text.OrderedText in project EdenClient by HahaOO7.

the class AntiSpam method onChat.

private void onChat(AddChatMessageCallback.ChatAddEvent event) {
    if (!enabled)
        return;
    List<ChatHudLine<OrderedText>> chatLines = event.getChatLines();
    var chatText = event.getChatText();
    if (chatText == null)
        return;
    if (chatLines.isEmpty())
        return;
    class JustGiveMeTheStringVisitor implements CharacterVisitor {

        final StringBuilder sb = new StringBuilder();

        @Override
        public boolean accept(int index, Style style, int codePoint) {
            sb.appendCodePoint(codePoint);
            return true;
        }

        @Override
        public String toString() {
            return sb.toString();
        }
    }
    ChatHud chat = MC.inGameHud.getChatHud();
    int maxTextLength = MathHelper.floor(chat.getWidth() / chat.getChatScale());
    List<OrderedText> newLines = ChatMessages.breakRenderedChatMessageLines(chatText, maxTextLength, MC.textRenderer);
    int spamCounter = 1;
    int matchingLines = 0;
    for (int i = chatLines.size() - 1; i >= 0; i--) {
        JustGiveMeTheStringVisitor oldLineVS = new JustGiveMeTheStringVisitor();
        chatLines.get(i).getText().accept(oldLineVS);
        String oldLine = oldLineVS.toString();
        if (matchingLines <= newLines.size() - 1) {
            JustGiveMeTheStringVisitor newLineVS = new JustGiveMeTheStringVisitor();
            newLines.get(matchingLines).accept(newLineVS);
            String newLine = newLineVS.toString();
            if (matchingLines < newLines.size() - 1) {
                if (oldLine.equals(newLine))
                    matchingLines++;
                else
                    matchingLines = 0;
                continue;
            }
            if (!oldLine.startsWith(newLine)) {
                matchingLines = 0;
                continue;
            }
            if (i > 0 && matchingLines == newLines.size() - 1) {
                JustGiveMeTheStringVisitor nextOldLineVS = new JustGiveMeTheStringVisitor();
                chatLines.get(i - 1).getText().accept(nextOldLineVS);
                String nextOldLine = nextOldLineVS.toString();
                String twoLines = oldLine + nextOldLine;
                String addedText = twoLines.substring(newLine.length());
                if (addedText.startsWith(" [x") && addedText.endsWith("]")) {
                    String oldSpamCounter = addedText.substring(3, addedText.length() - 1);
                    if (MathUtils.isInteger(oldSpamCounter)) {
                        spamCounter += Integer.parseInt(oldSpamCounter);
                        matchingLines++;
                        continue;
                    }
                }
            }
            if (oldLine.length() == newLine.length())
                spamCounter++;
            else {
                String addedText = oldLine.substring(newLine.length());
                if (!addedText.startsWith(" [x") || !addedText.endsWith("]")) {
                    matchingLines = 0;
                    continue;
                }
                String oldSpamCounter = addedText.substring(3, addedText.length() - 1);
                if (!MathUtils.isInteger(oldSpamCounter)) {
                    matchingLines = 0;
                    continue;
                }
                spamCounter += Integer.parseInt(oldSpamCounter);
            }
        }
        if (i + matchingLines >= i) {
            chatLines.subList(i, i + matchingLines + 1).clear();
        }
        matchingLines = 0;
    }
    if (spamCounter > 1) {
        chatText = new LiteralText("").append(chatText).append(new LiteralText(" [x" + spamCounter + "]"));
    }
    event.setChatText(chatText);
}
Also used : ChatHudLine(net.minecraft.client.gui.hud.ChatHudLine) CharacterVisitor(net.minecraft.text.CharacterVisitor) OrderedText(net.minecraft.text.OrderedText) Style(net.minecraft.text.Style) ChatHud(net.minecraft.client.gui.hud.ChatHud) LiteralText(net.minecraft.text.LiteralText)

Example 12 with OrderedText

use of net.minecraft.text.OrderedText in project AdvancedChatBox by DarkKronicle.

the class ChatSuggestorGui method showUsages.

private void showUsages(Formatting formatting) {
    CommandContextBuilder<CommandSource> commandContextBuilder = this.suggestor.getParse().getContext();
    SuggestionContext<CommandSource> suggestionContext = commandContextBuilder.findSuggestionContext(this.textField.getCursor());
    Map<CommandNode<CommandSource>, String> map = this.client.player.networkHandler.getCommandDispatcher().getSmartUsage(suggestionContext.parent, this.client.player.networkHandler.getCommandSource());
    List<OrderedText> list = new ArrayList<>();
    int i = 0;
    Style style = Style.EMPTY.withColor(formatting);
    for (Map.Entry<CommandNode<CommandSource>, String> commandNodeStringEntry : map.entrySet()) {
        if (!(commandNodeStringEntry.getKey() instanceof LiteralCommandNode)) {
            list.add(OrderedText.styledForwardsVisitedString(commandNodeStringEntry.getValue(), style));
            i = Math.max(i, this.textRenderer.getWidth(commandNodeStringEntry.getValue()));
        }
    }
    if (!list.isEmpty()) {
        this.messages.addAll(list);
        this.x = MathHelper.clamp(this.textField.getCharacterX(suggestionContext.startPos), 0, this.textField.getCharacterX(0) + this.textField.getInnerWidth() - i);
        this.width = i;
    }
}
Also used : LiteralCommandNode(com.mojang.brigadier.tree.LiteralCommandNode) CommandNode(com.mojang.brigadier.tree.CommandNode) ArrayList(java.util.ArrayList) CommandSource(net.minecraft.command.CommandSource) LiteralCommandNode(com.mojang.brigadier.tree.LiteralCommandNode) OrderedText(net.minecraft.text.OrderedText) Style(net.minecraft.text.Style) Map(java.util.Map)

Example 13 with OrderedText

use of net.minecraft.text.OrderedText in project Moonfix by Kingdom-of-Moon.

the class SignBlockEntityRendererMixin method renderModel.

// sign scaling
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/model/ModelPart;render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;II)V", shift = At.Shift.BEFORE), method = "render(Lnet/minecraft/block/entity/SignBlockEntity;FLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;II)V", locals = LocalCapture.CAPTURE_FAILHARD)
private void renderModel(SignBlockEntity signBlockEntity, float f, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i, int j, CallbackInfo ci, BlockState blockState, float g, SignType signType, SignBlockEntityRenderer.SignModel signModel, SpriteIdentifier h, VertexConsumer vertexConsumer) {
    if (!(boolean) Config.SIGN_SCALE.value)
        return;
    // text len
    float max = 90;
    OrderedText[] texts = signBlockEntity.updateSign(MinecraftClient.getInstance().shouldFilterText(), Text::asOrderedText);
    for (OrderedText text : texts) max = Math.max(this.textRenderer.getWidth(text), max);
    max = (max + 6) / 96f;
    if (max <= 1.01f)
        return;
    // stick
    boolean wasStickVisible = signModel.stick.visible;
    signModel.stick.visible = false;
    // scale
    matrixStack.push();
    matrixStack.scale(max, 1.01f, 1.01f);
    // render
    signModel.root.render(matrixStack, vertexConsumer, i, j);
    matrixStack.pop();
    signModel.stick.visible = wasStickVisible;
}
Also used : OrderedText(net.minecraft.text.OrderedText) OrderedText(net.minecraft.text.OrderedText) Text(net.minecraft.text.Text) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

OrderedText (net.minecraft.text.OrderedText)13 Text (net.minecraft.text.Text)3 TranslatableText (net.minecraft.text.TranslatableText)3 Inject (org.spongepowered.asm.mixin.injection.Inject)3 ArrayList (java.util.ArrayList)2 GameRenderer (net.minecraft.client.render.GameRenderer)2 Style (net.minecraft.text.Style)2 CommandNode (com.mojang.brigadier.tree.CommandNode)1 LiteralCommandNode (com.mojang.brigadier.tree.LiteralCommandNode)1 Color (java.awt.Color)1 Map (java.util.Map)1 OptionImpact (me.jellysquid.mods.sodium.client.gui.options.OptionImpact)1 Dim2i (me.jellysquid.mods.sodium.client.util.Dim2i)1 StringCharacterVisitor (meteordevelopment.meteorclient.utils.misc.text.StringCharacterVisitor)1 MinecraftClient (net.minecraft.client.MinecraftClient)1 ChatHud (net.minecraft.client.gui.hud.ChatHud)1 ChatHudLine (net.minecraft.client.gui.hud.ChatHudLine)1 TooltipComponent (net.minecraft.client.gui.tooltip.TooltipComponent)1 CommandSource (net.minecraft.command.CommandSource)1 CharacterVisitor (net.minecraft.text.CharacterVisitor)1