Commit 7fc533f6 by Jonathan Thomas

Fixing crash when displaying message on LeadPlayerGoal, to run the…

Fixing crash when displaying message on LeadPlayerGoal, to run the generateMessage in a scheduler (after 1 tick), and not in the Goal's tick method.
parent 0d1aa055
Pipeline #12708 passed with stages
in 2 minutes 13 seconds
...@@ -263,7 +263,6 @@ public class ChatDataManager { ...@@ -263,7 +263,6 @@ public class ChatDataManager {
float entitySpeedFast = MathHelper.clamp(entitySpeed * 1.3F, 0.5f, 1.3f); float entitySpeedFast = MathHelper.clamp(entitySpeed * 1.3F, 0.5f, 1.3f);
// Apply behaviors (if any) // Apply behaviors (if any)
if (!is_auto_message) {
for (Behavior behavior : result.getBehaviors()) { for (Behavior behavior : result.getBehaviors()) {
LOGGER.info("Behavior: " + behavior.getName() + (behavior.getArgument() != null ? LOGGER.info("Behavior: " + behavior.getName() + (behavior.getArgument() != null ?
", Argument: " + behavior.getArgument() : "")); ", Argument: " + behavior.getArgument() : ""));
...@@ -347,7 +346,6 @@ public class ChatDataManager { ...@@ -347,7 +346,6 @@ public class ChatDataManager {
this.friendship = new_friendship; this.friendship = new_friendship;
} }
} }
}
// Add ASSISTANT message to history // Add ASSISTANT message to history
this.addMessage(result.getOriginalMessage(), ChatSender.ASSISTANT, player.getUuidAsString()); this.addMessage(result.getOriginalMessage(), ChatSender.ASSISTANT, player.getUuidAsString());
......
...@@ -11,8 +11,10 @@ import java.util.concurrent.TimeUnit; ...@@ -11,8 +11,10 @@ import java.util.concurrent.TimeUnit;
*/ */
public class ChatDataSaverScheduler { public class ChatDataSaverScheduler {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private MinecraftServer server = null;
public void startAutoSaveTask(MinecraftServer server, long interval, TimeUnit timeUnit) { public void startAutoSaveTask(MinecraftServer server, long interval, TimeUnit timeUnit) {
this.server = server;
ChatDataAutoSaver saverTask = new ChatDataAutoSaver(server); ChatDataAutoSaver saverTask = new ChatDataAutoSaver(server);
scheduler.scheduleAtFixedRate(saverTask, 1, interval, timeUnit); scheduler.scheduleAtFixedRate(saverTask, 1, interval, timeUnit);
} }
...@@ -20,4 +22,9 @@ public class ChatDataSaverScheduler { ...@@ -20,4 +22,9 @@ public class ChatDataSaverScheduler {
public void stopAutoSaveTask() { public void stopAutoSaveTask() {
scheduler.shutdown(); scheduler.shutdown();
} }
// Schedule a task to run after 1 tick (basically immediately)
public void scheduleTask(Runnable task) {
scheduler.schedule(() -> server.execute(task), 50, TimeUnit.MILLISECONDS);
}
} }
...@@ -61,6 +61,7 @@ public class LeadPlayerGoal extends PlayerBaseGoal { ...@@ -61,6 +61,7 @@ public class LeadPlayerGoal extends PlayerBaseGoal {
foundWaypoint = true; foundWaypoint = true;
LOGGER.info("destination reached"); LOGGER.info("destination reached");
ServerPackets.scheduler.scheduleTask(() -> {
// Prepare a message about the interaction // Prepare a message about the interaction
String arrivedMessage = "<You have arrived at your destination>"; String arrivedMessage = "<You have arrived at your destination>";
...@@ -69,6 +70,7 @@ public class LeadPlayerGoal extends PlayerBaseGoal { ...@@ -69,6 +70,7 @@ public class LeadPlayerGoal extends PlayerBaseGoal {
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("N/A", chatData, (ServerPlayerEntity) this.targetEntity, this.entity, arrivedMessage, true); ServerPackets.generate_chat("N/A", chatData, (ServerPlayerEntity) this.targetEntity, this.entity, arrivedMessage, true);
} }
});
// Stop navigation // Stop navigation
this.entity.getNavigation().stop(); this.entity.getNavigation().stop();
......
...@@ -42,7 +42,7 @@ import java.util.concurrent.TimeUnit; ...@@ -42,7 +42,7 @@ import java.util.concurrent.TimeUnit;
public class ServerPackets { public class ServerPackets {
public static final Logger LOGGER = LoggerFactory.getLogger("creaturechat"); public static final Logger LOGGER = LoggerFactory.getLogger("creaturechat");
public static MinecraftServer serverInstance; public static MinecraftServer serverInstance;
private static ChatDataSaverScheduler scheduler = null; public static ChatDataSaverScheduler scheduler = null;
public static final Identifier PACKET_C2S_GREETING = new Identifier("creaturechat", "packet_c2s_greeting"); public static final Identifier PACKET_C2S_GREETING = new Identifier("creaturechat", "packet_c2s_greeting");
public static final Identifier PACKET_C2S_READ_NEXT = new Identifier("creaturechat", "packet_c2s_read_next"); public static final Identifier PACKET_C2S_READ_NEXT = new Identifier("creaturechat", "packet_c2s_read_next");
public static final Identifier PACKET_C2S_SET_STATUS = new Identifier("creaturechat", "packet_c2s_set_status"); public static final Identifier PACKET_C2S_SET_STATUS = new Identifier("creaturechat", "packet_c2s_set_status");
...@@ -295,10 +295,8 @@ public class ServerPackets { ...@@ -295,10 +295,8 @@ public class ServerPackets {
public static void generate_chat(String userLanguage, 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)
if (!is_auto_message) {
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());
......
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