Commit bfe50738 by Jonathan Thomas

Prevent spamming of chat generation. Introduced chat status, and now render…

Prevent spamming of chat generation. Introduced chat status, and now render dotdot button when chat is pending.
parent 21fb4bbe
Pipeline #11652 passed with stage
in 20 seconds
......@@ -76,9 +76,9 @@ public class ClientInit implements ClientModInitializer {
Tessellator.getInstance().draw();
}
private void drawStartIcon(MatrixStack matrices, Entity entity, float x, float y, float width, float height) {
private void drawIcon(String ui_icon_name, MatrixStack matrices, Entity entity, float x, float y, float width, float height) {
// Draw button icon
Identifier button_texture = textures.Get("ui", "button-chat");
Identifier button_texture = textures.Get("ui", ui_icon_name);
RenderSystem.setShaderTexture(0, button_texture);
RenderSystem.enableDepthTest();
RenderSystem.enableBlend();
......@@ -238,9 +238,13 @@ public class ClientInit implements ClientModInitializer {
matrices.translate(0F, -scaledTextHeight + -textHeaderHeight + -textFooterHeight, 0F);
// Check if conversation has started
if (chatData.currentMessage.isEmpty()) {
// Draw 'start' button
drawStartIcon(matrices, entity, -16, textHeaderHeight, 32, 17);
if (chatData.status == ChatDataManager.ChatStatus.NONE) {
// Draw 'start chat' button
drawIcon("button-chat", matrices, entity, -16, textHeaderHeight, 32, 17);
} else if (chatData.status == ChatDataManager.ChatStatus.PENDING) {
// Draw 'pending' button
drawIcon("button-dotdot", matrices, entity, -16, textHeaderHeight, 32, 17);
} else {
// Draw text background (no smaller than 50F tall)
......
......@@ -10,6 +10,13 @@ public class ChatDataManager {
private static final ChatDataManager INSTANCE = new ChatDataManager();
public static int MAX_CHAR_PER_LINE = 22;
public enum ChatStatus {
NONE, // No chat status yet
PENDING, // Chat is pending (e.g., awaiting response or processing)
DISPLAY, // Chat is currently being displayed
END // Chat has ended or been dismissed
}
// HashMap to associate unique entity IDs with their chat data
private HashMap<Integer, EntityChatData> entityChatDataMap;
......@@ -17,6 +24,7 @@ public class ChatDataManager {
public static class EntityChatData {
public String currentMessage;
public int currentLineNumber;
public ChatStatus status;
public List<String> previousMessages;
public String characterSheet;
......@@ -25,10 +33,12 @@ public class ChatDataManager {
this.currentLineNumber = 0;
this.previousMessages = new ArrayList<>();
this.characterSheet = "";
this.status = ChatStatus.NONE;
}
// Generate greeting
public void generateGreeting() {
this.status = ChatStatus.PENDING;
ChatGPTRequest.fetchGreetingFromChatGPT().thenAccept(greeting -> {
if (greeting != null) {
this.addMessage(greeting);
......@@ -45,6 +55,7 @@ public class ChatDataManager {
// Set line number of displayed text
this.currentLineNumber = 0;
this.status = ChatStatus.DISPLAY;
}
// Get wrapped lines
......@@ -56,6 +67,9 @@ public class ChatDataManager {
public void setLineNumber(Integer lineNumber) {
// Update displayed starting line # (between 0 and # of lines)
this.currentLineNumber = Math.min(Math.max(lineNumber, 0), this.getWrappedLines().size());
if (lineNumber == this.getWrappedLines().size()) {
this.status = ChatStatus.END;
}
}
}
......
......@@ -36,9 +36,13 @@ public class ModInit implements ModInitializer {
// Slow entity
SlowEntity((LivingEntity) entity, 3.5F);
LOGGER.info("Generate greeting for: " + entity.getType().toString());
ChatDataManager.EntityChatData chatData = ChatDataManager.getInstance().getOrCreateChatData(entityId);
chatData.generateGreeting();
if (chatData.status == ChatDataManager.ChatStatus.NONE ||
chatData.status == ChatDataManager.ChatStatus.END) {
// Only generate a new greeting if not already doing so
LOGGER.info("Generate greeting for: " + entity.getType().toString());
chatData.generateGreeting();
}
}
});
});
......@@ -57,9 +61,12 @@ public class ModInit implements ModInitializer {
// Slow entity
SlowEntity((LivingEntity) entity, 3.5F);
LOGGER.info("Increment read lines to " + lineNumber + " for: " + entity.getType().toString());
ChatDataManager.EntityChatData chatData = ChatDataManager.getInstance().getOrCreateChatData(entityId);
chatData.setLineNumber(lineNumber);
if (chatData.status == ChatDataManager.ChatStatus.DISPLAY) {
// Only set line number if status allows
LOGGER.info("Increment read lines to " + lineNumber + " for: " + entity.getType().toString());
chatData.setLineNumber(lineNumber);
}
}
});
});
......@@ -80,6 +87,7 @@ public class ModInit implements ModInitializer {
// Slow the entity temporarily (so they don't run away)
// Apply a slowness effect with a high amplifier for a short duration
// (Amplifier value must be between 0 and 127)
LOGGER.info("Apply SLOWNESS status effect to: " + entity.getType().toString());
float TPS = 20F; // ticks per second
StatusEffectInstance slowness = new StatusEffectInstance(StatusEffects.SLOWNESS, Math.round(numSeconds * TPS),
127, false, false);
......
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