Commit 6b84a1af by Jonathan Thomas

Replace INT entityId with UUID, which is persistent and does not change between sessions.

parent 00c2e3c3
Pipeline #11865 passed with stage
in 22 seconds
...@@ -20,6 +20,7 @@ import java.lang.reflect.Type; ...@@ -20,6 +20,7 @@ import java.lang.reflect.Type;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -43,7 +44,7 @@ public class ClickHandler { ...@@ -43,7 +44,7 @@ public class ClickHandler {
// Client-side packet handler, message sync // Client-side packet handler, message sync
ClientPlayNetworking.registerGlobalReceiver(ModInit.PACKET_S2C_MESSAGE, (client, handler, buffer, responseSender) -> { ClientPlayNetworking.registerGlobalReceiver(ModInit.PACKET_S2C_MESSAGE, (client, handler, buffer, responseSender) -> {
// Read the data from the server packet // Read the data from the server packet
int entityId = buffer.readInt(); UUID entityId = UUID.fromString(buffer.readString());
String message = buffer.readString(32767); String message = buffer.readString(32767);
int line = buffer.readInt(); int line = buffer.readInt();
String status_name = buffer.readString(32767); String status_name = buffer.readString(32767);
...@@ -51,10 +52,10 @@ public class ClickHandler { ...@@ -51,10 +52,10 @@ public class ClickHandler {
// Update the chat data manager on the client-side // Update the chat data manager on the client-side
client.execute(() -> { // Make sure to run on the client thread client.execute(() -> { // Make sure to run on the client thread
Entity entity = client.world.getEntityById(entityId); Entity entity = ClientEntityFinder.getEntityByUUID(client.world, entityId);
if (entity != null) { if (entity != null) {
ChatDataManager chatDataManager = ChatDataManager.getClientInstance(); ChatDataManager chatDataManager = ChatDataManager.getClientInstance();
ChatDataManager.EntityChatData chatData = chatDataManager.getOrCreateChatData(entityId); ChatDataManager.EntityChatData chatData = chatDataManager.getOrCreateChatData(entity.getUuidAsString());
if (!message.isEmpty()) { if (!message.isEmpty()) {
chatData.currentMessage = message; chatData.currentMessage = message;
} }
...@@ -75,7 +76,7 @@ public class ClickHandler { ...@@ -75,7 +76,7 @@ public class ClickHandler {
Gson GSON = new Gson(); Gson GSON = new Gson();
client.execute(() -> { // Make sure to run on the client thread client.execute(() -> { // Make sure to run on the client thread
// Parse JSON and override client chat data // Parse JSON and override client chat data
Type type = new TypeToken<HashMap<Integer, ChatDataManager.EntityChatData>>(){}.getType(); Type type = new TypeToken<HashMap<String, ChatDataManager.EntityChatData>>(){}.getType();
ChatDataManager.getClientInstance().entityChatDataMap = GSON.fromJson(chatDataJSON, type); ChatDataManager.getClientInstance().entityChatDataMap = GSON.fromJson(chatDataJSON, type);
}); });
}); });
...@@ -149,7 +150,7 @@ public class ClickHandler { ...@@ -149,7 +150,7 @@ public class ClickHandler {
// Handle the click for the closest entity after the loop // Handle the click for the closest entity after the loop
if (closestEntity != null) { if (closestEntity != null) {
// Look-up conversation // Look-up conversation
ChatDataManager.EntityChatData chatData = ChatDataManager.getClientInstance().getOrCreateChatData(closestEntity.getId()); ChatDataManager.EntityChatData chatData = ChatDataManager.getClientInstance().getOrCreateChatData(closestEntity.getUuidAsString());
if (chatData.status == ChatDataManager.ChatStatus.NONE) { if (chatData.status == ChatDataManager.ChatStatus.NONE) {
// Start conversation // Start conversation
......
package com.owlmaddie;
import net.minecraft.entity.Entity;
import net.minecraft.client.world.ClientWorld;
import java.util.UUID;
// Find Client Entity from UUID
public class ClientEntityFinder {
public static Entity getEntityByUUID(ClientWorld world, UUID uuid) {
for (Entity entity : world.getEntities()) {
if (entity.getUuid().equals(uuid)) {
return entity;
}
}
return null; // Entity not found
}
}
...@@ -173,7 +173,7 @@ public class ClientInit implements ClientModInitializer { ...@@ -173,7 +173,7 @@ public class ClientInit implements ClientModInitializer {
} }
// Look-up greeting (if any) // Look-up greeting (if any)
ChatDataManager.EntityChatData chatData = ChatDataManager.getClientInstance().getOrCreateChatData(entity.getId()); ChatDataManager.EntityChatData chatData = ChatDataManager.getClientInstance().getOrCreateChatData(entity.getUuidAsString());
List<String> lines = chatData.getWrappedLines(); List<String> lines = chatData.getWrappedLines();
// Set the range of lines to display // Set the range of lines to display
......
...@@ -13,7 +13,7 @@ public class ModPackets { ...@@ -13,7 +13,7 @@ public class ModPackets {
public static void sendGenerateGreeting(Entity entity) { public static void sendGenerateGreeting(Entity entity) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeInt(entity.getId()); buf.writeString(entity.getUuidAsString());
// Send C2S packet // Send C2S packet
ClientPlayNetworking.send(ModInit.PACKET_C2S_GREETING, buf); ClientPlayNetworking.send(ModInit.PACKET_C2S_GREETING, buf);
...@@ -21,7 +21,7 @@ public class ModPackets { ...@@ -21,7 +21,7 @@ public class ModPackets {
public static void sendUpdateLineNumber(Entity entity, Integer lineNumber) { public static void sendUpdateLineNumber(Entity entity, Integer lineNumber) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeInt(entity.getId()); buf.writeString(entity.getUuidAsString());
buf.writeInt(lineNumber); buf.writeInt(lineNumber);
// Send C2S packet // Send C2S packet
...@@ -30,7 +30,7 @@ public class ModPackets { ...@@ -30,7 +30,7 @@ public class ModPackets {
public static void sendStartChat(Entity entity) { public static void sendStartChat(Entity entity) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeInt(entity.getId()); buf.writeString(entity.getUuidAsString());
// Send C2S packet // Send C2S packet
ClientPlayNetworking.send(ModInit.PACKET_C2S_START_CHAT, buf); ClientPlayNetworking.send(ModInit.PACKET_C2S_START_CHAT, buf);
...@@ -38,7 +38,7 @@ public class ModPackets { ...@@ -38,7 +38,7 @@ public class ModPackets {
public static void sendChat(Entity entity, String message) { public static void sendChat(Entity entity, String message) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeInt(entity.getId()); buf.writeString(entity.getUuidAsString());
buf.writeString(message); buf.writeString(message);
// Send C2S packet // Send C2S packet
......
...@@ -6,10 +6,7 @@ import net.minecraft.item.ItemStack; ...@@ -6,10 +6,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.WorldSavePath; import net.minecraft.util.WorldSavePath;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.google.gson.Gson; import com.google.gson.Gson;
...@@ -46,7 +43,7 @@ public class ChatDataManager { ...@@ -46,7 +43,7 @@ public class ChatDataManager {
} }
// HashMap to associate unique entity IDs with their chat data // HashMap to associate unique entity IDs with their chat data
public HashMap<Integer, EntityChatData> entityChatDataMap; public HashMap<String, EntityChatData> entityChatDataMap;
public static class ChatMessage { public static class ChatMessage {
public String message; public String message;
...@@ -60,7 +57,7 @@ public class ChatDataManager { ...@@ -60,7 +57,7 @@ public class ChatDataManager {
// Inner class to hold entity-specific data // Inner class to hold entity-specific data
public static class EntityChatData { public static class EntityChatData {
public int entityId; public String entityId;
public String currentMessage; public String currentMessage;
public int currentLineNumber; public int currentLineNumber;
public ChatStatus status; public ChatStatus status;
...@@ -68,7 +65,7 @@ public class ChatDataManager { ...@@ -68,7 +65,7 @@ public class ChatDataManager {
public String characterSheet; public String characterSheet;
public ChatSender sender; public ChatSender sender;
public EntityChatData(int entityId) { public EntityChatData(String entityId) {
this.entityId = entityId; this.entityId = entityId;
this.currentMessage = ""; this.currentMessage = "";
this.currentLineNumber = 0; this.currentLineNumber = 0;
...@@ -80,7 +77,7 @@ public class ChatDataManager { ...@@ -80,7 +77,7 @@ public class ChatDataManager {
// Light version with no 'previousMessages' attribute // Light version with no 'previousMessages' attribute
public class EntityChatDataLight { public class EntityChatDataLight {
public int entityId; public String entityId;
public String currentMessage; public String currentMessage;
public int currentLineNumber; public int currentLineNumber;
public ChatStatus status; public ChatStatus status;
...@@ -138,7 +135,7 @@ public class ChatDataManager { ...@@ -138,7 +135,7 @@ public class ChatDataManager {
contextData.put("world_time", String.format("%02d:%02d", hours, minutes)); contextData.put("world_time", String.format("%02d:%02d", hours, minutes));
// Get Entity details // Get Entity details
Entity entity = player.getServerWorld().getEntityById(entityId); Entity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), UUID.fromString(entityId));
if (entity.getCustomName() == null) { if (entity.getCustomName() == null) {
contextData.put("entity_name", "Un-named"); contextData.put("entity_name", "Un-named");
} else { } else {
...@@ -243,7 +240,7 @@ public class ChatDataManager { ...@@ -243,7 +240,7 @@ public class ChatDataManager {
} }
// Retrieve chat data for a specific entity, or create it if it doesn't exist // Retrieve chat data for a specific entity, or create it if it doesn't exist
public EntityChatData getOrCreateChatData(int entityId) { public EntityChatData getOrCreateChatData(String entityId) {
return entityChatDataMap.computeIfAbsent(entityId, k -> new EntityChatData(entityId)); return entityChatDataMap.computeIfAbsent(entityId, k -> new EntityChatData(entityId));
} }
...@@ -284,7 +281,7 @@ public class ChatDataManager { ...@@ -284,7 +281,7 @@ public class ChatDataManager {
public String GetLightChatData() { public String GetLightChatData() {
try { try {
// Create "light" version of entire chat data HashMap // Create "light" version of entire chat data HashMap
HashMap<Integer, EntityChatData.EntityChatDataLight> lightVersionMap = new HashMap<>(); HashMap<String, EntityChatData.EntityChatDataLight> lightVersionMap = new HashMap<>();
this.entityChatDataMap.forEach((id, entityChatData) -> lightVersionMap.put(id, entityChatData.toLightVersion())); this.entityChatDataMap.forEach((id, entityChatData) -> lightVersionMap.put(id, entityChatData.toLightVersion()));
// Convert light chat data to JSON string // Convert light chat data to JSON string
...@@ -314,7 +311,7 @@ public class ChatDataManager { ...@@ -314,7 +311,7 @@ public class ChatDataManager {
File loadFile = new File(server.getSavePath(WorldSavePath.ROOT).toFile(), "chatdata.json"); File loadFile = new File(server.getSavePath(WorldSavePath.ROOT).toFile(), "chatdata.json");
LOGGER.info("Load chat data from " + loadFile.getAbsolutePath()); LOGGER.info("Load chat data from " + loadFile.getAbsolutePath());
if (loadFile.exists()) { if (loadFile.exists()) {
Type type = new TypeToken<HashMap<Integer, EntityChatData>>(){}.getType(); Type type = new TypeToken<HashMap<String, EntityChatData>>(){}.getType();
try (FileReader reader = new FileReader(loadFile)) { try (FileReader reader = new FileReader(loadFile)) {
this.entityChatDataMap = GSON.fromJson(reader, type); this.entityChatDataMap = GSON.fromJson(reader, type);
} }
......
...@@ -17,6 +17,8 @@ import net.minecraft.util.Identifier; ...@@ -17,6 +17,8 @@ import net.minecraft.util.Identifier;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.UUID;
public class ModInit implements ModInitializer { public class ModInit implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt"); public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
...@@ -36,16 +38,16 @@ public class ModInit implements ModInitializer { ...@@ -36,16 +38,16 @@ public class ModInit implements ModInitializer {
// 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) -> {
int entityId = buf.readInt(); UUID entityId = UUID.fromString(buf.readString());
// Ensure that the task is synced with the server thread // Ensure that the task is synced with the server thread
server.execute(() -> { server.execute(() -> {
Entity entity = player.getServerWorld().getEntityById(entityId); Entity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) { if (entity != null) {
// Slow entity // Slow entity
SlowEntity((LivingEntity) entity, 3.5F); SlowEntity((LivingEntity) entity, 3.5F);
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entityId); ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.status == ChatDataManager.ChatStatus.NONE || if (chatData.status == ChatDataManager.ChatStatus.NONE ||
chatData.status == ChatDataManager.ChatStatus.END) { chatData.status == ChatDataManager.ChatStatus.END) {
// Only generate a new greeting if not already doing so // Only generate a new greeting if not already doing so
...@@ -60,17 +62,17 @@ public class ModInit implements ModInitializer { ...@@ -60,17 +62,17 @@ public class ModInit implements ModInitializer {
// Handle packet for reading lines of message // Handle packet for reading lines of message
ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_READ_NEXT, (server, player, handler, buf, responseSender) -> { ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_READ_NEXT, (server, player, handler, buf, responseSender) -> {
int entityId = buf.readInt(); UUID entityId = UUID.fromString(buf.readString());
int lineNumber = buf.readInt(); int lineNumber = buf.readInt();
// Ensure that the task is synced with the server thread // Ensure that the task is synced with the server thread
server.execute(() -> { server.execute(() -> {
Entity entity = player.getServerWorld().getEntityById(entityId); Entity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) { if (entity != null) {
// Slow entity // Slow entity
SlowEntity((LivingEntity) entity, 3.5F); SlowEntity((LivingEntity) entity, 3.5F);
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entityId); ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.status == ChatDataManager.ChatStatus.DISPLAY) { if (chatData.status == ChatDataManager.ChatStatus.DISPLAY) {
// Only set line number if status allows // Only set line number if status allows
LOGGER.info("Increment read lines to " + lineNumber + " for: " + entity.getType().toString()); LOGGER.info("Increment read lines to " + lineNumber + " for: " + entity.getType().toString());
...@@ -82,11 +84,11 @@ public class ModInit implements ModInitializer { ...@@ -82,11 +84,11 @@ public class ModInit implements ModInitializer {
// Handle packet for Start Chat // Handle packet for Start Chat
ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_START_CHAT, (server, player, handler, buf, responseSender) -> { ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_START_CHAT, (server, player, handler, buf, responseSender) -> {
int entityId = buf.readInt(); UUID entityId = UUID.fromString(buf.readString());
// Ensure that the task is synced with the server thread // Ensure that the task is synced with the server thread
server.execute(() -> { server.execute(() -> {
Entity entity = player.getServerWorld().getEntityById(entityId); Entity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) { if (entity != null) {
// Slow entity, so it does NOT walk away during player typing // Slow entity, so it does NOT walk away during player typing
SlowEntity((LivingEntity) entity, 7F); SlowEntity((LivingEntity) entity, 7F);
...@@ -96,17 +98,17 @@ public class ModInit implements ModInitializer { ...@@ -96,17 +98,17 @@ public class ModInit implements ModInitializer {
// Handle packet for new chat message // Handle packet for new chat message
ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_SEND_CHAT, (server, player, handler, buf, responseSender) -> { ServerPlayNetworking.registerGlobalReceiver(PACKET_C2S_SEND_CHAT, (server, player, handler, buf, responseSender) -> {
int entityId = buf.readInt(); UUID entityId = UUID.fromString(buf.readString());
String message = buf.readString(32767); String message = 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(() -> {
Entity entity = player.getServerWorld().getEntityById(entityId); Entity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) { if (entity != null) {
// Slow entity // Slow entity
SlowEntity((LivingEntity) entity, 3.5F); SlowEntity((LivingEntity) entity, 3.5F);
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entityId); ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.status == ChatDataManager.ChatStatus.END) { if (chatData.status == ChatDataManager.ChatStatus.END) {
// Add new message // Add new message
LOGGER.info("Add new message (" + message + ") to Entity: " + entity.getType().toString()); LOGGER.info("Add new message (" + message + ") to Entity: " + entity.getType().toString());
...@@ -152,12 +154,13 @@ public class ModInit implements ModInitializer { ...@@ -152,12 +154,13 @@ public class ModInit implements ModInitializer {
// Send new message to all connected players // Send new message to all connected players
public static void BroadcastPacketMessage(ChatDataManager.EntityChatData chatData) { public static void BroadcastPacketMessage(ChatDataManager.EntityChatData chatData) {
for (ServerWorld world : serverInstance.getWorlds()) { for (ServerWorld world : serverInstance.getWorlds()) {
Entity entity = world.getEntityById(chatData.entityId); UUID entityId = UUID.fromString(chatData.entityId);
Entity entity = ServerEntityFinder.getEntityByUUID(world, entityId);
if (entity != null) { if (entity != null) {
PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer());
// Write the entity's chat updated data // Write the entity's chat updated data
buffer.writeInt(entity.getId()); buffer.writeString(entity.getUuidAsString());
buffer.writeString(chatData.currentMessage); buffer.writeString(chatData.currentMessage);
buffer.writeInt(chatData.currentLineNumber); buffer.writeInt(chatData.currentLineNumber);
buffer.writeString(chatData.status.toString()); buffer.writeString(chatData.status.toString());
......
package com.owlmaddie;
import net.minecraft.entity.Entity;
import net.minecraft.server.world.ServerWorld;
import java.util.UUID;
// Find Server Entity from UUID
public class ServerEntityFinder {
public static Entity getEntityByUUID(ServerWorld world, UUID uuid) {
for (Entity entity : world.iterateEntities()) {
if (entity.getUuid().equals(uuid)) {
return entity;
}
}
return null; // Entity not found
}
}
\ No newline at end of file
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