Commit 7c8267cd by Jonathan Thomas

Set customName of an entity after the initial greeting has been generated (and…

Set customName of an entity after the initial greeting has been generated (and only if the customName is not already set)
parent 6b84a1af
Pipeline #11866 passed with stage
in 22 seconds
......@@ -97,17 +97,17 @@ public class ChatDataManager {
return light;
}
public static String extractGreeting(String inputText) {
// Regex pattern to match the "Short Greeting" line and capture the greeting text
Pattern pattern = Pattern.compile("- Short Greeting: \"?([^\"]+)\"?");
Matcher matcher = pattern.matcher(inputText);
public String getCharacterProp(String propertyName) {
// Create a case-insensitive regex pattern to match the property name and capture its value
Pattern pattern = Pattern.compile("-\\s*" + Pattern.quote(propertyName) + ":\\s*(.+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(characterSheet);
if (matcher.find()) {
// Return the captured group (greeting text), removing any quotes
return matcher.group(1).replace("\"", "");
// Return the captured value, trimmed of any excess whitespace
return matcher.group(1).trim().replace("\"", "");
}
return "Umm... hello... ugh..."; // Return a default string if no match is found
return "N/A";
}
// Generate context object
......@@ -161,7 +161,7 @@ public class ChatDataManager {
if (output_message != null && systemPrompt == "system-character") {
// Add NEW CHARACTER sheet & greeting
this.characterSheet = output_message;
String shortGreeting = extractGreeting(output_message);
String shortGreeting = getCharacterProp("short greeting");
this.addMessage(shortGreeting.replace("\n", " "), ChatSender.ASSISTANT);
} else if (output_message != null && systemPrompt == "system-chat") {
......
......@@ -14,6 +14,7 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -157,6 +158,14 @@ public class ModInit implements ModInitializer {
UUID entityId = UUID.fromString(chatData.entityId);
Entity entity = ServerEntityFinder.getEntityByUUID(world, entityId);
if (entity != null) {
// Set custom name (if none)
if (entity.getCustomName() == null && chatData.status != ChatDataManager.ChatStatus.PENDING) {
String characterName = chatData.getCharacterProp("name");
LOGGER.info("Setting entity name to " + characterName + " for " + chatData.entityId);
entity.setCustomName(Text.literal(characterName));
entity.setCustomNameVisible(true);
}
PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer());
// Write the entity's chat updated data
......
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