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 {
static HashMap<Integer, byte[]> receivedChunks = new HashMap<>();
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());
buf.writeString(entity.getUuidAsString());
buf.writeString(userLanguageName);
// Send C2S packet
ClientPlayNetworking.send(ServerPackets.PACKET_C2S_GREETING, buf);
......@@ -72,9 +77,14 @@ public class ClientPackets {
}
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());
buf.writeString(entity.getUuidAsString());
buf.writeString(message);
buf.writeString(userLanguageName);
// Send C2S packet
ClientPlayNetworking.send(ServerPackets.PACKET_C2S_SEND_CHAT, buf);
......
......@@ -135,7 +135,7 @@ public class ChatDataManager {
}
// Generate context object
public Map<String, String> getPlayerContext(ServerPlayerEntity player) {
public Map<String, String> getPlayerContext(ServerPlayerEntity player, String userLanguage) {
// Add PLAYER context information
Map<String, String> contextData = new HashMap<>();
contextData.put("player_name", player.getDisplayName().getString());
......@@ -146,6 +146,7 @@ public class ChatDataManager {
contextData.put("player_is_creative", player.isCreative() ? "yes" : "no");
contextData.put("player_is_swimming", player.isSwimming() ? "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 legsArmor = player.getInventory().armor.get(1);
......@@ -209,7 +210,7 @@ public class ChatDataManager {
}
// 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;
if (is_auto_message) {
// Increment an auto-generated message
......@@ -228,7 +229,7 @@ public class ChatDataManager {
}
// Add PLAYER context information
Map<String, String> contextData = getPlayerContext(player);
Map<String, String> contextData = getPlayerContext(player, userLanguage);
// fetch HTTP response from ChatGPT
ChatGPTRequest.fetchMessageFromChatGPT(systemPrompt, contextData, previousMessages, false).thenAccept(output_message -> {
......
......@@ -56,7 +56,7 @@ public class MixinLivingEntity implements LivingEntityInterface {
String directness = isIndirect ? "indirectly" : "directly";
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 {
ChatDataManager chatDataManager = ChatDataManager.getServerInstance();
ChatDataManager.EntityChatData chatData = chatDataManager.getOrCreateChatData(thisEntity.getUuidAsString());
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 {
// Handle packet for Greeting
ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_GREETING, (server, player, handler, buf, responseSender) -> {
UUID entityId = UUID.fromString(buf.readString());
String userLanguage = buf.readString(32767);
// Ensure that the task is synced with the server thread
server.execute(() -> {
......@@ -62,7 +63,7 @@ public class ServerPackets {
if (entity != null) {
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.characterSheet.isEmpty()) {
generate_character(chatData, player, entity);
generate_character(userLanguage, chatData, player, entity);
}
}
});
......@@ -139,6 +140,7 @@ public class ServerPackets {
ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_SEND_CHAT, (server, player, handler, buf, responseSender) -> {
UUID entityId = UUID.fromString(buf.readString());
String message = buf.readString(32767);
String userLanguage = buf.readString(32767);
// Ensure that the task is synced with the server thread
server.execute(() -> {
......@@ -146,9 +148,9 @@ public class ServerPackets {
if (entity != null) {
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.characterSheet.isEmpty()) {
generate_character(chatData, player, entity);
generate_character(userLanguage, chatData, player, entity);
} else {
generate_chat(chatData, player, entity, message, false);
generate_chat(userLanguage, chatData, player, entity, message, false);
}
}
});
......@@ -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)
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();
// Grab random adjective
String randomAdjective = Randomizer.getRandomMessage(Randomizer.RandomType.ADJECTIVE);
String randomFrequency = Randomizer.getRandomMessage(Randomizer.RandomType.FREQUENCY);
......@@ -251,19 +250,20 @@ public class ServerPackets {
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 speaks in '" + userLanguage + "'" );
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)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F);
EntityBehaviorManager.addGoal(entity, talkGoal, GoalPriority.TALK_PLAYER);
// Add new message
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
......
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
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
traits and speaking style / tone).
......
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
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:
- Name: {{entity_name}}
......@@ -27,6 +27,7 @@ Player Character Sheet:
- Creative Mode: {{player_is_creative}}
- Swimming: {{player_is_swimming}}
- On the Ground: {{player_is_on_ground}}
- Language: {{player_language}}
World Info:
- 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