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
1
Merge Requests
1
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
3124cc32
Commit
3124cc32
authored
Apr 03, 2024
by
Jonathan Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add more error handling to save/load of chat data, and ensure UTF-8 encoding
parent
8daf286b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
13 deletions
+22
-13
ChatDataManager.java
src/main/java/com/owlmaddie/ChatDataManager.java
+22
-13
No files found.
src/main/java/com/owlmaddie/ChatDataManager.java
View file @
3124cc32
...
...
@@ -17,10 +17,9 @@ import net.minecraft.util.WorldSavePath;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.File
;
import
java.io.FileReader
;
import
java.io.FileWriter
;
import
java.io.*
;
import
java.lang.reflect.Type
;
import
java.nio.charset.StandardCharsets
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
...
...
@@ -353,30 +352,39 @@ public class ChatDataManager {
// Save chat data to file
public
void
saveChatData
(
MinecraftServer
server
)
{
try
{
File
saveFile
=
new
File
(
server
.
getSavePath
(
WorldSavePath
.
ROOT
).
toFile
(),
"chatdata.json"
);
LOGGER
.
info
(
"Save chat data to "
+
saveFile
.
getAbsolutePath
());
try
(
FileWriter
writer
=
new
FileWriter
(
saveFile
))
{
File
tempFile
=
new
File
(
saveFile
.
getAbsolutePath
()
+
".tmp"
);
LOGGER
.
info
(
"Saving chat data to "
+
saveFile
.
getAbsolutePath
());
try
(
Writer
writer
=
new
OutputStreamWriter
(
new
FileOutputStream
(
tempFile
),
StandardCharsets
.
UTF_8
))
{
GSON
.
toJson
(
this
.
entityChatDataMap
,
writer
);
if
(
saveFile
.
exists
())
{
saveFile
.
delete
();
}
if
(!
tempFile
.
renameTo
(
saveFile
))
{
throw
new
IOException
(
"Failed to rename temporary chat data file to "
+
saveFile
.
getName
());
}
}
catch
(
Exception
e
)
{
// Handle exceptions
LOGGER
.
error
(
"Error saving chat data"
,
e
);
}
}
// Load chat data from file
public
void
loadChatData
(
MinecraftServer
server
)
{
try
{
File
loadFile
=
new
File
(
server
.
getSavePath
(
WorldSavePath
.
ROOT
).
toFile
(),
"chatdata.json"
);
LOGGER
.
info
(
"Load chat data from "
+
loadFile
.
getAbsolutePath
());
LOGGER
.
info
(
"Loading chat data from "
+
loadFile
.
getAbsolutePath
());
if
(
loadFile
.
exists
())
{
try
(
InputStreamReader
reader
=
new
InputStreamReader
(
new
FileInputStream
(
loadFile
),
StandardCharsets
.
UTF_8
))
{
Type
type
=
new
TypeToken
<
HashMap
<
String
,
EntityChatData
>>(){}.
getType
();
try
(
FileReader
reader
=
new
FileReader
(
loadFile
))
{
this
.
entityChatDataMap
=
GSON
.
fromJson
(
reader
,
type
);
}
}
}
catch
(
Exception
e
)
{
// Handle exceptions
LOGGER
.
error
(
"Error loading chat data"
,
e
);
this
.
entityChatDataMap
=
new
HashMap
<>();
}
}
else
{
// Init empty chat data
this
.
entityChatDataMap
=
new
HashMap
<>();
}
}
}
\ 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