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
...@@ -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,14 +61,16 @@ public class LeadPlayerGoal extends PlayerBaseGoal { ...@@ -61,14 +61,16 @@ public class LeadPlayerGoal extends PlayerBaseGoal {
foundWaypoint = true; foundWaypoint = true;
LOGGER.info("destination reached"); LOGGER.info("destination reached");
// Prepare a message about the interaction ServerPackets.scheduler.scheduleTask(() -> {
String arrivedMessage = "<You have arrived at your destination>"; // Prepare a message about the interaction
String arrivedMessage = "<You have arrived at your destination>";
ChatDataManager chatDataManager = ChatDataManager.getServerInstance();
ChatDataManager.EntityChatData chatData = chatDataManager.getOrCreateChatData(this.entity.getUuidAsString()); ChatDataManager chatDataManager = ChatDataManager.getServerInstance();
if (!chatData.characterSheet.isEmpty() && chatData.auto_generated < chatDataManager.MAX_AUTOGENERATE_RESPONSES) { ChatDataManager.EntityChatData chatData = chatDataManager.getOrCreateChatData(this.entity.getUuidAsString());
ServerPackets.generate_chat("N/A", chatData, (ServerPlayerEntity) this.targetEntity, this.entity, arrivedMessage, true); if (!chatData.characterSheet.isEmpty() && chatData.auto_generated < chatDataManager.MAX_AUTOGENERATE_RESPONSES) {
} 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