Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
CreatureChat
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
Public
CreatureChat
Commits
6b84a1af
Commit
6b84a1af
authored
Mar 23, 2024
by
Jonathan Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace INT entityId with UUID, which is persistent and does not change between sessions.
parent
00c2e3c3
Pipeline
#11865
passed with stage
in 22 seconds
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
71 additions
and
35 deletions
+71
-35
ClickHandler.java
src/client/java/com/owlmaddie/ClickHandler.java
+6
-5
ClientEntityFinder.java
src/client/java/com/owlmaddie/ClientEntityFinder.java
+17
-0
ClientInit.java
src/client/java/com/owlmaddie/ClientInit.java
+1
-1
ModPackets.java
src/client/java/com/owlmaddie/ModPackets.java
+4
-4
ChatDataManager.java
src/main/java/com/owlmaddie/ChatDataManager.java
+9
-12
ModInit.java
src/main/java/com/owlmaddie/ModInit.java
+16
-13
ServerEntityFinder.java
src/main/java/com/owlmaddie/ServerEntityFinder.java
+18
-0
No files found.
src/client/java/com/owlmaddie/ClickHandler.java
View file @
6b84a1af
...
...
@@ -20,6 +20,7 @@ import java.lang.reflect.Type;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.UUID
;
import
java.util.stream.Collectors
;
...
...
@@ -43,7 +44,7 @@ public class ClickHandler {
// Client-side packet handler, message sync
ClientPlayNetworking
.
registerGlobalReceiver
(
ModInit
.
PACKET_S2C_MESSAGE
,
(
client
,
handler
,
buffer
,
responseSender
)
->
{
// Read the data from the server packet
int
entityId
=
buffer
.
readInt
(
);
UUID
entityId
=
UUID
.
fromString
(
buffer
.
readString
()
);
String
message
=
buffer
.
readString
(
32767
);
int
line
=
buffer
.
readInt
();
String
status_name
=
buffer
.
readString
(
32767
);
...
...
@@ -51,10 +52,10 @@ public class ClickHandler {
// Update the chat data manager on the client-side
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
)
{
ChatDataManager
chatDataManager
=
ChatDataManager
.
getClientInstance
();
ChatDataManager
.
EntityChatData
chatData
=
chatDataManager
.
getOrCreateChatData
(
entity
Id
);
ChatDataManager
.
EntityChatData
chatData
=
chatDataManager
.
getOrCreateChatData
(
entity
.
getUuidAsString
()
);
if
(!
message
.
isEmpty
())
{
chatData
.
currentMessage
=
message
;
}
...
...
@@ -75,7 +76,7 @@ public class ClickHandler {
Gson
GSON
=
new
Gson
();
client
.
execute
(()
->
{
// Make sure to run on the client thread
// 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
);
});
});
...
...
@@ -149,7 +150,7 @@ public class ClickHandler {
// Handle the click for the closest entity after the loop
if
(
closestEntity
!=
null
)
{
// Look-up conversation
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getClientInstance
().
getOrCreateChatData
(
closestEntity
.
get
Id
());
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getClientInstance
().
getOrCreateChatData
(
closestEntity
.
get
UuidAsString
());
if
(
chatData
.
status
==
ChatDataManager
.
ChatStatus
.
NONE
)
{
// Start conversation
...
...
src/client/java/com/owlmaddie/ClientEntityFinder.java
0 → 100644
View file @
6b84a1af
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
}
}
src/client/java/com/owlmaddie/ClientInit.java
View file @
6b84a1af
...
...
@@ -173,7 +173,7 @@ public class ClientInit implements ClientModInitializer {
}
// Look-up greeting (if any)
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getClientInstance
().
getOrCreateChatData
(
entity
.
get
Id
());
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getClientInstance
().
getOrCreateChatData
(
entity
.
get
UuidAsString
());
List
<
String
>
lines
=
chatData
.
getWrappedLines
();
// Set the range of lines to display
...
...
src/client/java/com/owlmaddie/ModPackets.java
View file @
6b84a1af
...
...
@@ -13,7 +13,7 @@ public class ModPackets {
public
static
void
sendGenerateGreeting
(
Entity
entity
)
{
PacketByteBuf
buf
=
new
PacketByteBuf
(
Unpooled
.
buffer
());
buf
.
write
Int
(
entity
.
getId
());
buf
.
write
String
(
entity
.
getUuidAsString
());
// Send C2S packet
ClientPlayNetworking
.
send
(
ModInit
.
PACKET_C2S_GREETING
,
buf
);
...
...
@@ -21,7 +21,7 @@ public class ModPackets {
public
static
void
sendUpdateLineNumber
(
Entity
entity
,
Integer
lineNumber
)
{
PacketByteBuf
buf
=
new
PacketByteBuf
(
Unpooled
.
buffer
());
buf
.
write
Int
(
entity
.
getId
());
buf
.
write
String
(
entity
.
getUuidAsString
());
buf
.
writeInt
(
lineNumber
);
// Send C2S packet
...
...
@@ -30,7 +30,7 @@ public class ModPackets {
public
static
void
sendStartChat
(
Entity
entity
)
{
PacketByteBuf
buf
=
new
PacketByteBuf
(
Unpooled
.
buffer
());
buf
.
write
Int
(
entity
.
getId
());
buf
.
write
String
(
entity
.
getUuidAsString
());
// Send C2S packet
ClientPlayNetworking
.
send
(
ModInit
.
PACKET_C2S_START_CHAT
,
buf
);
...
...
@@ -38,7 +38,7 @@ public class ModPackets {
public
static
void
sendChat
(
Entity
entity
,
String
message
)
{
PacketByteBuf
buf
=
new
PacketByteBuf
(
Unpooled
.
buffer
());
buf
.
write
Int
(
entity
.
getId
());
buf
.
write
String
(
entity
.
getUuidAsString
());
buf
.
writeString
(
message
);
// Send C2S packet
...
...
src/main/java/com/owlmaddie/ChatDataManager.java
View file @
6b84a1af
...
...
@@ -6,10 +6,7 @@ import net.minecraft.item.ItemStack;
import
net.minecraft.server.network.ServerPlayerEntity
;
import
net.minecraft.util.WorldSavePath
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
com.google.gson.Gson
;
...
...
@@ -46,7 +43,7 @@ public class ChatDataManager {
}
// 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
String
message
;
...
...
@@ -60,7 +57,7 @@ public class ChatDataManager {
// Inner class to hold entity-specific data
public
static
class
EntityChatData
{
public
int
entityId
;
public
String
entityId
;
public
String
currentMessage
;
public
int
currentLineNumber
;
public
ChatStatus
status
;
...
...
@@ -68,7 +65,7 @@ public class ChatDataManager {
public
String
characterSheet
;
public
ChatSender
sender
;
public
EntityChatData
(
int
entityId
)
{
public
EntityChatData
(
String
entityId
)
{
this
.
entityId
=
entityId
;
this
.
currentMessage
=
""
;
this
.
currentLineNumber
=
0
;
...
...
@@ -80,7 +77,7 @@ public class ChatDataManager {
// Light version with no 'previousMessages' attribute
public
class
EntityChatDataLight
{
public
int
entityId
;
public
String
entityId
;
public
String
currentMessage
;
public
int
currentLineNumber
;
public
ChatStatus
status
;
...
...
@@ -138,7 +135,7 @@ public class ChatDataManager {
contextData
.
put
(
"world_time"
,
String
.
format
(
"%02d:%02d"
,
hours
,
minutes
));
// Get Entity details
Entity
entity
=
player
.
getServerWorld
().
getEntityById
(
entityId
);
Entity
entity
=
ServerEntityFinder
.
getEntityByUUID
(
player
.
getServerWorld
(),
UUID
.
fromString
(
entityId
)
);
if
(
entity
.
getCustomName
()
==
null
)
{
contextData
.
put
(
"entity_name"
,
"Un-named"
);
}
else
{
...
...
@@ -243,7 +240,7 @@ public class ChatDataManager {
}
// 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
));
}
...
...
@@ -284,7 +281,7 @@ public class ChatDataManager {
public
String
GetLightChatData
()
{
try
{
// 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
()));
// Convert light chat data to JSON string
...
...
@@ -314,7 +311,7 @@ public class ChatDataManager {
File
loadFile
=
new
File
(
server
.
getSavePath
(
WorldSavePath
.
ROOT
).
toFile
(),
"chatdata.json"
);
LOGGER
.
info
(
"Load chat data from "
+
loadFile
.
getAbsolutePath
());
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
))
{
this
.
entityChatDataMap
=
GSON
.
fromJson
(
reader
,
type
);
}
...
...
src/main/java/com/owlmaddie/ModInit.java
View file @
6b84a1af
...
...
@@ -17,6 +17,8 @@ import net.minecraft.util.Identifier;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.UUID
;
public
class
ModInit
implements
ModInitializer
{
public
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
"mobgpt"
);
...
...
@@ -36,16 +38,16 @@ public class ModInit implements ModInitializer {
// Handle packet for Greeting
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
server
.
execute
(()
->
{
Entity
entity
=
player
.
getServerWorld
().
getEntityById
(
entityId
);
Entity
entity
=
ServerEntityFinder
.
getEntityByUUID
(
player
.
getServerWorld
(),
entityId
);
if
(
entity
!=
null
)
{
// Slow entity
SlowEntity
((
LivingEntity
)
entity
,
3.5
F
);
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getServerInstance
().
getOrCreateChatData
(
entity
Id
);
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getServerInstance
().
getOrCreateChatData
(
entity
.
getUuidAsString
()
);
if
(
chatData
.
status
==
ChatDataManager
.
ChatStatus
.
NONE
||
chatData
.
status
==
ChatDataManager
.
ChatStatus
.
END
)
{
// Only generate a new greeting if not already doing so
...
...
@@ -60,17 +62,17 @@ public class ModInit implements ModInitializer {
// Handle packet for reading lines of message
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
();
// Ensure that the task is synced with the server thread
server
.
execute
(()
->
{
Entity
entity
=
player
.
getServerWorld
().
getEntityById
(
entityId
);
Entity
entity
=
ServerEntityFinder
.
getEntityByUUID
(
player
.
getServerWorld
(),
entityId
);
if
(
entity
!=
null
)
{
// Slow entity
SlowEntity
((
LivingEntity
)
entity
,
3.5
F
);
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getServerInstance
().
getOrCreateChatData
(
entity
Id
);
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getServerInstance
().
getOrCreateChatData
(
entity
.
getUuidAsString
()
);
if
(
chatData
.
status
==
ChatDataManager
.
ChatStatus
.
DISPLAY
)
{
// Only set line number if status allows
LOGGER
.
info
(
"Increment read lines to "
+
lineNumber
+
" for: "
+
entity
.
getType
().
toString
());
...
...
@@ -82,11 +84,11 @@ public class ModInit implements ModInitializer {
// Handle packet for Start Chat
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
server
.
execute
(()
->
{
Entity
entity
=
player
.
getServerWorld
().
getEntityById
(
entityId
);
Entity
entity
=
ServerEntityFinder
.
getEntityByUUID
(
player
.
getServerWorld
(),
entityId
);
if
(
entity
!=
null
)
{
// Slow entity, so it does NOT walk away during player typing
SlowEntity
((
LivingEntity
)
entity
,
7
F
);
...
...
@@ -96,17 +98,17 @@ public class ModInit implements ModInitializer {
// Handle packet for new chat message
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
);
// Ensure that the task is synced with the server thread
server
.
execute
(()
->
{
Entity
entity
=
player
.
getServerWorld
().
getEntityById
(
entityId
);
Entity
entity
=
ServerEntityFinder
.
getEntityByUUID
(
player
.
getServerWorld
(),
entityId
);
if
(
entity
!=
null
)
{
// Slow entity
SlowEntity
((
LivingEntity
)
entity
,
3.5
F
);
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getServerInstance
().
getOrCreateChatData
(
entity
Id
);
ChatDataManager
.
EntityChatData
chatData
=
ChatDataManager
.
getServerInstance
().
getOrCreateChatData
(
entity
.
getUuidAsString
()
);
if
(
chatData
.
status
==
ChatDataManager
.
ChatStatus
.
END
)
{
// Add new message
LOGGER
.
info
(
"Add new message ("
+
message
+
") to Entity: "
+
entity
.
getType
().
toString
());
...
...
@@ -152,12 +154,13 @@ public class ModInit implements ModInitializer {
// Send new message to all connected players
public
static
void
BroadcastPacketMessage
(
ChatDataManager
.
EntityChatData
chatData
)
{
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
)
{
PacketByteBuf
buffer
=
new
PacketByteBuf
(
Unpooled
.
buffer
());
// Write the entity's chat updated data
buffer
.
write
Int
(
entity
.
getId
());
buffer
.
write
String
(
entity
.
getUuidAsString
());
buffer
.
writeString
(
chatData
.
currentMessage
);
buffer
.
writeInt
(
chatData
.
currentLineNumber
);
buffer
.
writeString
(
chatData
.
status
.
toString
());
...
...
src/main/java/com/owlmaddie/ServerEntityFinder.java
0 → 100644
View file @
6b84a1af
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment