Skip to content

Commit

Permalink
Fix issue with corrupted world names after upgrading from 1.13 to 1.14.
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Jun 20, 2019
1 parent 4527216 commit b2f2c4a
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,77 @@ public ChallengesManager(ChallengesAddon addon)
this.playerCacheData = new HashMap<>();

this.load();

// TODO: Remove this code after some time, as this is just a protective code against invalid world names.
if (Bukkit.getBukkitVersion().startsWith("1.14"))
{
Set<Challenge> updatedChallenges = new HashSet<>();

this.challengeCacheData.values().forEach(challengeObject -> {
if (challengeObject.getUniqueId().matches(".*[A-Z]+.*"))
{
challengeObject.setUniqueId(challengeObject.getUniqueId().toLowerCase());
challengeObject.setLevel(challengeObject.getLevel().toLowerCase());

updatedChallenges.add(challengeObject);

this.addon.logWarning("Challenge addon fixed your data for Challenge " +
challengeObject.getUniqueId() +
". 1.14 does not allow to use capital letters in world names.");
}
});

Set<ChallengeLevel> updatedLevels = new HashSet<>();

this.levelCacheData.values().forEach(levelObject -> {
if (levelObject.getUniqueId().matches(".*[A-Z]+.*"))
{
levelObject.setUniqueId(levelObject.getUniqueId().toLowerCase());
levelObject.setWorld(levelObject.getWorld().toLowerCase());

Set<String> correctNames = levelObject.getChallenges().stream().
map(String::toLowerCase).
collect(Collectors.toSet());

levelObject.setChallenges(correctNames);

updatedLevels.add(levelObject);

this.addon.logWarning("Challenge addon fixed your data for Challenge Level " +
levelObject.getUniqueId() +
". 1.14 does not allow to use capital letters in world names.");
}
});

// As at least one challenge or level was corrupted we must update all player data objects!
if (!updatedLevels.isEmpty() || !updatedChallenges.isEmpty())
{
List<ChallengesPlayerData> playerDataList = this.playersDatabase.loadObjects();

playerDataList.forEach(challengesPlayerData -> {

Map<String, Integer> fixedChallengeStatus = new HashMap<>();
challengesPlayerData.getChallengeStatus().forEach((challenge, count) ->
fixedChallengeStatus.put(challenge.toLowerCase(), count));
challengesPlayerData.setChallengeStatus(fixedChallengeStatus);

Map<String, Long> fixedChallengeTimestamp = new HashMap<>();
challengesPlayerData.getChallengesTimestamp().forEach((challenge, count) ->
fixedChallengeTimestamp.put(challenge.toLowerCase(), count));
challengesPlayerData.setChallengesTimestamp(fixedChallengeTimestamp);

Set<String> fixedLevelsDone = new HashSet<>();
challengesPlayerData.getLevelsDone().forEach(level -> fixedLevelsDone.add(level.toLowerCase()));
challengesPlayerData.setLevelsDone(fixedLevelsDone);

this.playersDatabase.saveObject(challengesPlayerData);

this.addon.logWarning("Challenge addon fixed your data for PlayerData " +
challengesPlayerData.getUniqueId() +
". 1.14 does not allow to use capital letters in world names.");
});
}
}
}


Expand Down

0 comments on commit b2f2c4a

Please sign in to comment.