Commit a2ed5ee5 by Jonathan Thomas

- Always entity render name, and fallback to "N/A" if no name is avaialble

- Clear previous messages, if character sheet is empty (i.e. test mode)
- Refactor out generate_chat() and generate_character() so they can be called from different packets more easily
- Don't allow chats with entities that have no character sheet generated - keep trying to generate a new character sheet (needed for test mode)
- Don't ever set "N/A" as an actual customName
parent fd5da028
Pipeline #11990 passed with stage
in 20 seconds
......@@ -186,18 +186,21 @@ public class BubbleRenderer {
private static void drawEntityName(MobEntity entity, Matrix4f matrix, VertexConsumerProvider immediate,
int fullBright, float yOffset) {
if (entity.getCustomName() != null) {
TextRenderer fontRenderer = MinecraftClient.getInstance().textRenderer;
String lineText = entity.getCustomName().getLiteralString();
// Ensure the string is no longer than X characters
if (lineText.length() > 14) {
lineText = lineText.substring(0, 14) + "...";
// Get custom name (if any)
String nameText = "N/A";
if (entity.getCustomName() != null) {
nameText = entity.getCustomName().getLiteralString();
}
fontRenderer.draw(lineText, -fontRenderer.getWidth(lineText) / 2f, yOffset, 0xffffff,
false, matrix, immediate, TextLayerType.NORMAL, 0, fullBright);
// Truncate long names
if (nameText.length() > 14) {
nameText = nameText.substring(0, 14) + "...";
}
fontRenderer.draw(nameText, -fontRenderer.getWidth(nameText) / 2f, yOffset, 0xffffff,
false, matrix, immediate, TextLayerType.NORMAL, 0, fullBright);
}
public static void drawTextAboveEntities(WorldRenderContext context, long tick, float partialTicks) {
......
......@@ -66,28 +66,9 @@ public class ModInit implements ModInitializer {
server.execute(() -> {
MobEntity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) {
// Set talk to player goal (prevent entity from walking off)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F);
EntityBehaviorManager.addGoal(entity, talkGoal, GoalPriority.TALK_PLAYER);
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.status == ChatDataManager.ChatStatus.NONE) {
// Only generate a new greeting if not already doing so
String player_biome = player.getWorld().getBiome(player.getBlockPos()).getKey().get().getValue().getPath();
StringBuilder userMessageBuilder = new StringBuilder();
userMessageBuilder.append("Please generate a new character ");
if (entity.getCustomName() != null) {
userMessageBuilder.append("named '").append(entity.getCustomName().getLiteralString()).append("' ");
} else {
userMessageBuilder.append("whose name starts with the letter '").append(RandomUtils.RandomLetter()).append("' ");
userMessageBuilder.append("and which uses ").append(RandomUtils.RandomNumber(4) + 1).append(" syllables ");
}
userMessageBuilder.append("of type '").append(entity.getType().getUntranslatedName().toLowerCase(Locale.ROOT)).append("' ");
userMessageBuilder.append("who lives near the ").append(player_biome).append(".");
LOGGER.info(userMessageBuilder.toString());
chatData.generateMessage(player, "system-character", userMessageBuilder.toString());
if (chatData.characterSheet.isEmpty()) {
generate_character(chatData, player, entity);
}
}
});
......@@ -157,14 +138,12 @@ public class ModInit implements ModInitializer {
server.execute(() -> {
MobEntity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) {
// Set talk to player goal (prevent entity from walking off)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F);
EntityBehaviorManager.addGoal(entity, talkGoal, GoalPriority.TALK_PLAYER);
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
// Add new message
LOGGER.info("Add new message (" + message + ") to Entity: " + entity.getType().toString());
chatData.generateMessage(player, "system-chat", message);
if (chatData.characterSheet.isEmpty()) {
generate_character(chatData, player, entity);
} else {
generate_chat(chatData, player, entity, message);
}
}
});
});
......@@ -246,15 +225,48 @@ public class ModInit implements ModInitializer {
LOGGER.info("MobGPT Initialized!");
}
public static void generate_character(ChatDataManager.EntityChatData chatData, ServerPlayerEntity player, MobEntity entity) {
// Set talk to player goal (prevent entity from walking off)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F);
EntityBehaviorManager.addGoal(entity, talkGoal, GoalPriority.TALK_PLAYER);
// Only generate a new greeting if not already doing so
String player_biome = player.getWorld().getBiome(player.getBlockPos()).getKey().get().getValue().getPath();
StringBuilder userMessageBuilder = new StringBuilder();
userMessageBuilder.append("Please generate a new character ");
if (entity.getCustomName() != null && !entity.getCustomName().getLiteralString().equals("N/A")) {
userMessageBuilder.append("named '").append(entity.getCustomName().getLiteralString()).append("' ");
} else {
userMessageBuilder.append("whose name starts with the letter '").append(RandomUtils.RandomLetter()).append("' ");
userMessageBuilder.append("and which uses ").append(RandomUtils.RandomNumber(4) + 1).append(" syllables ");
}
userMessageBuilder.append("of type '").append(entity.getType().getUntranslatedName().toLowerCase(Locale.ROOT)).append("' ");
userMessageBuilder.append("who lives near the ").append(player_biome).append(".");
LOGGER.info(userMessageBuilder.toString());
chatData.generateMessage(player, "system-character", userMessageBuilder.toString());
}
public static void generate_chat(ChatDataManager.EntityChatData chatData, ServerPlayerEntity player, MobEntity entity, String message) {
// Set talk to player goal (prevent entity from walking off)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F);
EntityBehaviorManager.addGoal(entity, talkGoal, GoalPriority.TALK_PLAYER);
// Add new message
LOGGER.info("Add new message (" + message + ") to Entity: " + entity.getType().toString());
chatData.generateMessage(player, "system-chat", message);
}
// Send new message to all connected players
public static void BroadcastPacketMessage(ChatDataManager.EntityChatData chatData) {
for (ServerWorld world : serverInstance.getWorlds()) {
UUID entityId = UUID.fromString(chatData.entityId);
MobEntity entity = ServerEntityFinder.getEntityByUUID(world, entityId);
if (entity != null) {
// Set custom name (if none)
if (entity.getCustomName() == null && chatData.status != ChatDataManager.ChatStatus.PENDING) {
// Set custom name (if null)
String characterName = chatData.getCharacterProp("name");
if (!characterName.isEmpty() && !characterName.equals("N/A") && entity.getCustomName() == null) {
LOGGER.info("Setting entity name to " + characterName + " for " + chatData.entityId);
entity.setCustomName(Text.literal(characterName));
entity.setCustomNameVisible(true);
......
......@@ -286,6 +286,11 @@ public class ChatDataManager {
// Error / No Chat Message (Failure)
String randomErrorMessage = ParsedMessage.getRandomErrorMessage();
this.addMessage(randomErrorMessage, ChatSender.ASSISTANT);
// Clear history (if no character sheet was generated)
if (characterSheet.isEmpty()) {
previousMessages.clear();
}
}
// Broadcast to all players
......
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