Commit 53000c90 by Jonathan Thomas

- Added max char length as a static property

- Added additional truncate logic, to ensure all messages are limited to 512 characters
- Added new PlayerMessageManager to keep track of currently visible player messages, to soon be displayed temporarily
parent 458845e4
Pipeline #11992 passed with stage
in 18 seconds
......@@ -3,10 +3,11 @@ package com.owlmaddie;
import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.ui.BubbleRenderer;
import com.owlmaddie.ui.ClickHandler;
import com.owlmaddie.ui.PlayerMessageManager;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
/**
* The {@code ClientInit} class initializes this mod in the client and defines all hooks into the
......@@ -19,6 +20,7 @@ public class ClientInit implements ClientModInitializer {
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
tickCounter++;
PlayerMessageManager.tickUpdate();
});
ClickHandler.register();
......
package com.owlmaddie.ui;
import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.network.ModPackets;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
......@@ -38,7 +39,7 @@ public class ChatScreen extends Screen {
// Initialize the text field
textField = new TextFieldWidget(textRenderer, textFieldX, textFieldY, textFieldWidth, textFieldHeight, Text.literal("Chat Input"));
textField.setMaxLength(512);
textField.setMaxLength(ChatDataManager.MAX_CHAR_IN_USER_MESSAGE);
textField.setDrawsBackground(true);
textField.setText("");
this.addDrawableChild(textField);
......
......@@ -78,6 +78,11 @@ public class ClickHandler {
chatData.status = ChatDataManager.ChatStatus.valueOf(status_name);
chatData.sender = ChatDataManager.ChatSender.valueOf(sender_name);
chatData.friendship = friendship;
if (chatData.sender == ChatDataManager.ChatSender.USER) {
// Add player message to queue for rendering
PlayerMessageManager.addMessage(UUID.fromString(chatData.playerId), chatData.currentMessage, ChatDataManager.TICKS_TO_DISPLAY_USER_MESSAGE);
}
}
});
});
......
package com.owlmaddie.ui;
import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.chat.LineWrapper;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* The {@code PlayerMessage} class provides a player message object, which keeps track of how
* many ticks to remain visible, and the message to display. Similar to an EntityChatData, but
* much simpler.
*/
public class PlayerMessage {
public String currentMessage;
public int currentLineNumber;
public AtomicInteger tickCountdown;
public PlayerMessage(String messageText, int ticks) {
this.currentMessage = messageText;
this.currentLineNumber = 0;
this.tickCountdown = new AtomicInteger(ticks);
}
public List<String> getWrappedLines() {
return LineWrapper.wrapLines(this.currentMessage, ChatDataManager.MAX_CHAR_PER_LINE);
}
public boolean isEndOfMessage() {
int totalLines = this.getWrappedLines().size();
// Check if the current line number plus DISPLAY_NUM_LINES covers or exceeds the total number of lines
return currentLineNumber + ChatDataManager.DISPLAY_NUM_LINES >= totalLines;
}
}
\ No newline at end of file
package com.owlmaddie.ui;
import com.owlmaddie.chat.ChatDataManager;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
/**
* The {@code PlayerMessageManager} class keeps track of currently visible player messages. These are temporary,
* and only stored when they need to be rendered.
*/
public class PlayerMessageManager {
private static final ConcurrentHashMap<UUID, PlayerMessage> messages = new ConcurrentHashMap<>();
public static void addMessage(UUID playerUUID, String messageText, int ticks) {
messages.put(playerUUID, new PlayerMessage(messageText, ticks));
}
public static void tickUpdate() {
messages.forEach((uuid, playerMessage) -> {
if (playerMessage.tickCountdown.decrementAndGet() <= 0) {
// Move to next line or remove the message
nextLineOrRemove(uuid, playerMessage);
}
});
}
private static void nextLineOrRemove(UUID uuid, PlayerMessage playerMessage) {
// Logic to move to the next line or remove the message
if (!playerMessage.isEndOfMessage()) {
// Check if more lines are available
playerMessage.currentLineNumber += ChatDataManager.DISPLAY_NUM_LINES;
playerMessage.tickCountdown.set(ChatDataManager.TICKS_TO_DISPLAY_USER_MESSAGE);
} else {
messages.remove(uuid);
}
}
}
......@@ -41,6 +41,8 @@ public class ChatDataManager {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
public static int MAX_CHAR_PER_LINE = 20;
public static int DISPLAY_NUM_LINES = 3;
public static int MAX_CHAR_IN_USER_MESSAGE = 512;
public static int TICKS_TO_DISPLAY_USER_MESSAGE = 90;
public QuestJson quest = null;
private static final Gson GSON = new Gson();
......@@ -302,11 +304,14 @@ public class ChatDataManager {
// Add a message to the history and update the current message
public void addMessage(String message, ChatSender messageSender, String playerId) {
// Truncate message (prevent crazy long messages... just in case)
String truncatedMessage = message.substring(0, Math.min(message.length(), MAX_CHAR_IN_USER_MESSAGE));
// Add message to history
previousMessages.add(new ChatMessage(message, messageSender));
previousMessages.add(new ChatMessage(truncatedMessage, messageSender));
// Set new message and reset line number of displayed text
currentMessage = message;
currentMessage = truncatedMessage;
currentLineNumber = 0;
if (messageSender == ChatSender.ASSISTANT) {
// Show new generated message
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment