Jump to content

[1.12.2] Extract a word in the chat by clicking on it


s0qva

Recommended Posts

Main idea: player opens the chat and clicks (left mouse button) on any word in the chat. Then that selected word has to be processed (how it should be processed does not matter yet).

An example:
- (Player sent a message to the chat): "<Player>: can moderators ban s0qva already??? He is cheating".
- Moderator clicks on the word 's0qva' in the chat message and that selected word is copied (for example the word is copied, as I said before it doesn't matter how it should be processed).

My question: how do I extract a word that a player clicked on?

My attempts: I created a Handler that handles a GuiScreenEvent.MouseInputEvent. Then I got a GuiNewChat object and extracted a line by cordinates that I obtained from Mouse.getX/Y(). It works but I extracted an entire line not a specific word a player clicked on. What should I do next to extract the selected word?

I was thinking about splitting the line and somehow process it or using obtained cordinates to find the selected word in the extracted line, but I do not really believe this will work.

My code:

package com.s0qva.easypunishment.client.handler;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.input.Mouse;

import java.util.Objects;

public class SelectedChatWordEventHandler {
    private static final Logger LOGGER = LogManager.getLogger();
    private static final int LEFT_MOUSE_BUTTON_INDEX = 0;

    @SubscribeEvent
    public void extractSelectedChatWord(GuiScreenEvent.MouseInputEvent event) {
        ITextComponent selectedMessageLine;
        ITextComponent messageToSend;
        String selectedMessage;
        GuiNewChat chatGUI = getGuiNewChat();
        boolean isLeftMouseButton = Mouse.isButtonDown(LEFT_MOUSE_BUTTON_INDEX);
        int xSelectedCord = Mouse.getX();
        int ySelectedCord = Mouse.getY();

        if (!isLeftMouseButton || !chatGUI.getChatOpen()) {
            return;
        }
        LOGGER.info("X: {}, Y: {}", xSelectedCord, ySelectedCord);
        selectedMessageLine = chatGUI.getChatComponent(xSelectedCord, ySelectedCord);

        if (Objects.isNull(selectedMessageLine)) {
            LOGGER.warn("Failed to obtain a chat message");
            return;
        }
        selectedMessage = selectedMessageLine.getUnformattedText();
        LOGGER.info("Obtained message: {}", selectedMessage);
        messageToSend = new TextComponentString("You've selected: " + selectedMessage);
        LOGGER.info("Message to send: {}", messageToSend);
        sendMessage(messageToSend);
    }

    private GuiNewChat getGuiNewChat() {
        return Minecraft.getMinecraft()
                .ingameGUI
                .getChatGUI();
    }

    private void sendMessage(ITextComponent message) {
        Minecraft.getMinecraft()
                .player
                .sendMessage(message);
    }
}

P.S. one more question: why do two or even more messages send to the chat / logs when I click on the message?
 

Edited by s0qva
Link to comment
Share on other sites

  • s0qva changed the title to [1.12.2] Extract a word in the chat by clicking on it

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.