Commit 5b24b76c by Jonathan Thomas

Adding client language into both prompts, so entities will attempt to speak in…

Adding client language into both prompts, so entities will attempt to speak in the player's language (if valid and possible).
parent dedb924a
Pipeline #12079 passed with stage
in 21 seconds
...@@ -31,8 +31,13 @@ public class ClientPackets { ...@@ -31,8 +31,13 @@ public class ClientPackets {
static HashMap<Integer, byte[]> receivedChunks = new HashMap<>(); static HashMap<Integer, byte[]> receivedChunks = new HashMap<>();
public static void sendGenerateGreeting(Entity entity) { public static void sendGenerateGreeting(Entity entity) {
// Get user language
String userLanguageCode = MinecraftClient.getInstance().getLanguageManager().getLanguage();
String userLanguageName = MinecraftClient.getInstance().getLanguageManager().getLanguage(userLanguageCode).getDisplayText().getLiteralString();
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeString(entity.getUuidAsString()); buf.writeString(entity.getUuidAsString());
buf.writeString(userLanguageName);
// Send C2S packet // Send C2S packet
ClientPlayNetworking.send(ServerPackets.PACKET_C2S_GREETING, buf); ClientPlayNetworking.send(ServerPackets.PACKET_C2S_GREETING, buf);
...@@ -72,9 +77,14 @@ public class ClientPackets { ...@@ -72,9 +77,14 @@ public class ClientPackets {
} }
public static void sendChat(Entity entity, String message) { public static void sendChat(Entity entity, String message) {
// Get user language
String userLanguageCode = MinecraftClient.getInstance().getLanguageManager().getLanguage();
String userLanguageName = MinecraftClient.getInstance().getLanguageManager().getLanguage(userLanguageCode).getDisplayText().getLiteralString();
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeString(entity.getUuidAsString()); buf.writeString(entity.getUuidAsString());
buf.writeString(message); buf.writeString(message);
buf.writeString(userLanguageName);
// Send C2S packet // Send C2S packet
ClientPlayNetworking.send(ServerPackets.PACKET_C2S_SEND_CHAT, buf); ClientPlayNetworking.send(ServerPackets.PACKET_C2S_SEND_CHAT, buf);
......
...@@ -135,7 +135,7 @@ public class ChatDataManager { ...@@ -135,7 +135,7 @@ public class ChatDataManager {
} }
// Generate context object // Generate context object
public Map<String, String> getPlayerContext(ServerPlayerEntity player) { public Map<String, String> getPlayerContext(ServerPlayerEntity player, String userLanguage) {
// Add PLAYER context information // Add PLAYER context information
Map<String, String> contextData = new HashMap<>(); Map<String, String> contextData = new HashMap<>();
contextData.put("player_name", player.getDisplayName().getString()); contextData.put("player_name", player.getDisplayName().getString());
...@@ -146,6 +146,7 @@ public class ChatDataManager { ...@@ -146,6 +146,7 @@ public class ChatDataManager {
contextData.put("player_is_creative", player.isCreative() ? "yes" : "no"); contextData.put("player_is_creative", player.isCreative() ? "yes" : "no");
contextData.put("player_is_swimming", player.isSwimming() ? "yes" : "no"); contextData.put("player_is_swimming", player.isSwimming() ? "yes" : "no");
contextData.put("player_is_on_ground", player.isOnGround() ? "yes" : "no"); contextData.put("player_is_on_ground", player.isOnGround() ? "yes" : "no");
contextData.put("player_language", userLanguage);
ItemStack feetArmor = player.getInventory().armor.get(0); ItemStack feetArmor = player.getInventory().armor.get(0);
ItemStack legsArmor = player.getInventory().armor.get(1); ItemStack legsArmor = player.getInventory().armor.get(1);
...@@ -209,7 +210,7 @@ public class ChatDataManager { ...@@ -209,7 +210,7 @@ public class ChatDataManager {
} }
// Generate greeting // Generate greeting
public void generateMessage(ServerPlayerEntity player, String systemPrompt, String userMessage, boolean is_auto_message) { public void generateMessage(String userLanguage, ServerPlayerEntity player, String systemPrompt, String userMessage, boolean is_auto_message) {
this.status = ChatStatus.PENDING; this.status = ChatStatus.PENDING;
if (is_auto_message) { if (is_auto_message) {
// Increment an auto-generated message // Increment an auto-generated message
...@@ -228,7 +229,7 @@ public class ChatDataManager { ...@@ -228,7 +229,7 @@ public class ChatDataManager {
} }
// Add PLAYER context information // Add PLAYER context information
Map<String, String> contextData = getPlayerContext(player); Map<String, String> contextData = getPlayerContext(player, userLanguage);
// fetch HTTP response from ChatGPT // fetch HTTP response from ChatGPT
ChatGPTRequest.fetchMessageFromChatGPT(systemPrompt, contextData, previousMessages, false).thenAccept(output_message -> { ChatGPTRequest.fetchMessageFromChatGPT(systemPrompt, contextData, previousMessages, false).thenAccept(output_message -> {
......
...@@ -56,7 +56,7 @@ public class MixinLivingEntity implements LivingEntityInterface { ...@@ -56,7 +56,7 @@ public class MixinLivingEntity implements LivingEntityInterface {
String directness = isIndirect ? "indirectly" : "directly"; String directness = isIndirect ? "indirectly" : "directly";
String attackedMessage = "<" + player.getName().getString() + " attacked you " + directness + " with " + weaponName + ">"; String attackedMessage = "<" + player.getName().getString() + " attacked you " + directness + " with " + weaponName + ">";
ServerPackets.generate_chat(chatData, player, (MobEntity)thisEntity, attackedMessage, true); ServerPackets.generate_chat("N/A", chatData, player, (MobEntity)thisEntity, attackedMessage, true);
} }
} }
} }
......
...@@ -37,7 +37,7 @@ public class MixinMobEntity { ...@@ -37,7 +37,7 @@ public class MixinMobEntity {
ChatDataManager chatDataManager = ChatDataManager.getServerInstance(); ChatDataManager chatDataManager = ChatDataManager.getServerInstance();
ChatDataManager.EntityChatData chatData = chatDataManager.getOrCreateChatData(thisEntity.getUuidAsString()); ChatDataManager.EntityChatData chatData = chatDataManager.getOrCreateChatData(thisEntity.getUuidAsString());
if (!chatData.characterSheet.isEmpty() && chatData.auto_generated < chatDataManager.MAX_AUTOGENERATE_RESPONSES) { if (!chatData.characterSheet.isEmpty() && chatData.auto_generated < chatDataManager.MAX_AUTOGENERATE_RESPONSES) {
ServerPackets.generate_chat(chatData, serverPlayer, thisEntity, giveItemMessage, true); ServerPackets.generate_chat("N/A", chatData, serverPlayer, thisEntity, giveItemMessage, true);
} }
} }
} }
......
...@@ -55,6 +55,7 @@ public class ServerPackets { ...@@ -55,6 +55,7 @@ public class ServerPackets {
// Handle packet for Greeting // Handle packet for Greeting
ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_GREETING, (server, player, handler, buf, responseSender) -> { ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_GREETING, (server, player, handler, buf, responseSender) -> {
UUID entityId = UUID.fromString(buf.readString()); UUID entityId = UUID.fromString(buf.readString());
String userLanguage = buf.readString(32767);
// Ensure that the task is synced with the server thread // Ensure that the task is synced with the server thread
server.execute(() -> { server.execute(() -> {
...@@ -62,7 +63,7 @@ public class ServerPackets { ...@@ -62,7 +63,7 @@ public class ServerPackets {
if (entity != null) { if (entity != null) {
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString()); ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.characterSheet.isEmpty()) { if (chatData.characterSheet.isEmpty()) {
generate_character(chatData, player, entity); generate_character(userLanguage, chatData, player, entity);
} }
} }
}); });
...@@ -139,6 +140,7 @@ public class ServerPackets { ...@@ -139,6 +140,7 @@ public class ServerPackets {
ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_SEND_CHAT, (server, player, handler, buf, responseSender) -> { ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_SEND_CHAT, (server, player, handler, buf, responseSender) -> {
UUID entityId = UUID.fromString(buf.readString()); UUID entityId = UUID.fromString(buf.readString());
String message = buf.readString(32767); String message = buf.readString(32767);
String userLanguage = buf.readString(32767);
// Ensure that the task is synced with the server thread // Ensure that the task is synced with the server thread
server.execute(() -> { server.execute(() -> {
...@@ -146,9 +148,9 @@ public class ServerPackets { ...@@ -146,9 +148,9 @@ public class ServerPackets {
if (entity != null) { if (entity != null) {
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString()); ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.characterSheet.isEmpty()) { if (chatData.characterSheet.isEmpty()) {
generate_character(chatData, player, entity); generate_character(userLanguage, chatData, player, entity);
} else { } else {
generate_chat(chatData, player, entity, message, false); generate_chat(userLanguage, chatData, player, entity, message, false);
} }
} }
}); });
...@@ -230,14 +232,11 @@ public class ServerPackets { ...@@ -230,14 +232,11 @@ public class ServerPackets {
} }
public static void generate_character(ChatDataManager.EntityChatData chatData, ServerPlayerEntity player, MobEntity entity) { public static void generate_character(String userLanguage, ChatDataManager.EntityChatData chatData, ServerPlayerEntity player, MobEntity entity) {
// Set talk to player goal (prevent entity from walking off) // Set talk to player goal (prevent entity from walking off)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F); TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F);
EntityBehaviorManager.addGoal(entity, talkGoal, GoalPriority.TALK_PLAYER); 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();
// Grab random adjective // Grab random adjective
String randomAdjective = Randomizer.getRandomMessage(Randomizer.RandomType.ADJECTIVE); String randomAdjective = Randomizer.getRandomMessage(Randomizer.RandomType.ADJECTIVE);
String randomFrequency = Randomizer.getRandomMessage(Randomizer.RandomType.FREQUENCY); String randomFrequency = Randomizer.getRandomMessage(Randomizer.RandomType.FREQUENCY);
...@@ -251,19 +250,20 @@ public class ServerPackets { ...@@ -251,19 +250,20 @@ public class ServerPackets {
userMessageBuilder.append("whose name starts with the letter '").append(Randomizer.RandomLetter()).append("' "); userMessageBuilder.append("whose name starts with the letter '").append(Randomizer.RandomLetter()).append("' ");
userMessageBuilder.append("and uses ").append(Randomizer.RandomNumber(4) + 1).append(" syllables "); userMessageBuilder.append("and uses ").append(Randomizer.RandomNumber(4) + 1).append(" syllables ");
} }
userMessageBuilder.append("and speaks in '" + userLanguage + "'" );
LOGGER.info(userMessageBuilder.toString()); LOGGER.info(userMessageBuilder.toString());
chatData.generateMessage(player, "system-character", userMessageBuilder.toString(), false); chatData.generateMessage(userLanguage, player, "system-character", userMessageBuilder.toString(), false);
} }
public static void generate_chat(ChatDataManager.EntityChatData chatData, ServerPlayerEntity player, MobEntity entity, String message, boolean is_auto_message) { public static void generate_chat(String userLanguage, ChatDataManager.EntityChatData chatData, ServerPlayerEntity player, MobEntity entity, String message, boolean is_auto_message) {
// Set talk to player goal (prevent entity from walking off) // Set talk to player goal (prevent entity from walking off)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F); TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F);
EntityBehaviorManager.addGoal(entity, talkGoal, GoalPriority.TALK_PLAYER); EntityBehaviorManager.addGoal(entity, talkGoal, GoalPriority.TALK_PLAYER);
// Add new message // Add new message
LOGGER.info("Player message received: " + message + " | Entity: " + entity.getType().toString()); LOGGER.info("Player message received: " + message + " | Entity: " + entity.getType().toString());
chatData.generateMessage(player, "system-chat", message, is_auto_message); chatData.generateMessage(userLanguage, player, "system-chat", message, is_auto_message);
} }
// Send new message to all connected players // Send new message to all connected players
......
You are a RPG dungeon master, crafting a new character sheet in the following format. Be very creative with each new You are a RPG dungeon master, crafting a new character sheet in the following format. Be very creative with each new
character. These characters will be inhabiting and adventuring in an epic fantasy which takes place in character. These characters will be inhabiting and adventuring in an epic fantasy which takes place in
Minecraft. Please limit traits and background to a few choices and keep them very short and concise. Please follow the output Minecraft. Please limit traits and background to a few choices and keep them very short and concise. Please follow the output
format below (including - dashes), and DO NOT output any intro text. format below (including - dashes), and DO NOT output any intro text. If a language is mentioned, generate the
"Short Greeting" entirely in that language.
Be extremely creative! Include a short initial greeting (as spoken by the character using their personality Be extremely creative! Include a short initial greeting (as spoken by the character using their personality
traits and speaking style / tone). traits and speaking style / tone).
......
Please respond directly to the player, as if the response was written by the following Minecraft entity. Please respond directly to the player, as if the response was written by the following Minecraft entity.
Please do NOT break the 4th wall and leverage the entity's character sheet below as much as Please do NOT break the 4th wall and leverage the entity's character sheet below as much as
possible. Try to keep response to 1 to 2 sentences (very brief). Include behaviors at the end of the message possible. Try to keep response to 1 to 2 sentences (very brief). Include behaviors at the end of the message
when relevant. when relevant. IMPORTANT: Always generate responses in player's language (if valid).
Entity Character Sheet: Entity Character Sheet:
- Name: {{entity_name}} - Name: {{entity_name}}
...@@ -27,6 +27,7 @@ Player Character Sheet: ...@@ -27,6 +27,7 @@ Player Character Sheet:
- Creative Mode: {{player_is_creative}} - Creative Mode: {{player_is_creative}}
- Swimming: {{player_is_swimming}} - Swimming: {{player_is_swimming}}
- On the Ground: {{player_is_on_ground}} - On the Ground: {{player_is_on_ground}}
- Language: {{player_language}}
World Info: World Info:
- Biome: {{player_biome}} - Biome: {{player_biome}}
......
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