Skip to content

Commit 5b0ccd3

Browse files
committed
Fix FlatFile data truncation on read failure and float XP parsing
Signed-off-by: Cyrne1_7208 <127477265+cyrne1-7208@users.noreply.github.com>
1 parent fee990b commit 5b0ccd3

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,11 @@ public int purgePowerlessUsers() {
263263
purgedUsers++;
264264
}
265265
}
266+
writeStringToFileSafely(writer.toString());
266267
} catch (IOException e) {
267268
logger.severe("Exception while reading " + usersFilePath
268269
+ " (Are you sure you formatted it correctly?)" + e);
269270
}
270-
271-
writeStringToFileSafely(writer.toString());
272271
}
273272

274273
logger.info("Purged " + purgedUsers + " users from the database.");
@@ -289,7 +288,7 @@ public void purgeOldUsers() {
289288
}
290289

291290
String[] data = row.fields();
292-
if (data.length <= UUID_INDEX) {
291+
if (data.length <= OVERHAUL_LAST_LOGIN) {
293292
// Not enough fields; preserve line
294293
return line;
295294
}
@@ -777,22 +776,23 @@ public boolean saveUserUUID(String userName, UUID uuid) {
777776
logger.severe("Database entry is invalid.");
778777
// still append original line
779778
} else {
780-
line = line.replace(character[UUID_INDEX], uuid.toString());
779+
character[UUID_INDEX] = uuid.toString();
780+
line = org.apache.commons.lang3.StringUtils.join(character, ":") + ":";
781781
worked = true;
782782
}
783783
}
784784

785785
entriesWritten++;
786786
writer.append(line).append(LINE_ENDING);
787787
}
788+
789+
LogUtils.debug(logger,
790+
entriesWritten + " entries written while saving UUID for " + userName);
791+
writeStringToFileSafely(writer.toString());
788792
} catch (Exception e) {
789793
logger.severe("Exception while reading " + usersFilePath
790794
+ " (Are you sure you formatted it correctly?)" + e);
791795
}
792-
793-
LogUtils.debug(logger,
794-
entriesWritten + " entries written while saving UUID for " + userName);
795-
writeStringToFileSafely(writer.toString());
796796
}
797797

798798
return worked;
@@ -1197,7 +1197,7 @@ private void tryLoadSkillFloatValuesFromRawData(@NotNull Map<PrimarySkillType, F
11971197
@NotNull String[] character, @NotNull PrimarySkillType primarySkillType, int index,
11981198
@NotNull String userName) {
11991199
try {
1200-
float valueFromString = Integer.parseInt(character[index]);
1200+
float valueFromString = Float.parseFloat(character[index]);
12011201
skillMap.put(primarySkillType, valueFromString);
12021202
} catch (NumberFormatException e) {
12031203
skillMap.put(primarySkillType, 0F);
@@ -1281,12 +1281,11 @@ private void rewriteUsersFile(@NotNull Function<String, String> lineMapper) {
12811281
writer.append(mapped).append(LINE_ENDING);
12821282
}
12831283
}
1284+
writeStringToFileSafely(writer.toString());
12841285
} catch (IOException e) {
12851286
logger.severe("Exception while reading " + usersFilePath
12861287
+ " (Are you sure you formatted it correctly?)" + e);
12871288
}
1288-
1289-
writeStringToFileSafely(writer.toString());
12901289
}
12911290
}
12921291

‎src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,24 @@ void saveUserUuidWithShortEntryDoesNotModifyDataAndReturnsFalse() throws IOExcep
534534
}
535535
}
536536

537+
@Test
538+
void loadPlayerProfileWithFloatingPointXpLoadsAccurately() throws IOException {
539+
// Given
540+
var databaseManager = new FlatFileDatabaseManager(
541+
new File(getTemporaryUserFilePath()), logger, PURGE_TIME, 0, true);
542+
543+
// Replace mining XP (field index 4) with a floating point number "10.5"
544+
String floatXpLine = normalDatabaseData[0].replace(":10:2:", ":10.5:2:");
545+
replaceDataInFile(databaseManager, new String[]{floatXpLine});
546+
547+
// When
548+
var profile = databaseManager.loadPlayerProfile("nossr50");
549+
550+
// Then
551+
assertTrue(profile.isLoaded());
552+
assertEquals(10.5f, profile.getSkillXpLevelRaw(PrimarySkillType.MINING), 0.001f);
553+
}
554+
537555
@Test
538556
void convertUsersCopiesAllNonCommentLinesToDestination() throws IOException {
539557
// Given

0 commit comments

Comments
 (0)