Fix null-pointer crash in island reset event.

This commit is contained in:
BONNe 2019-02-08 21:54:08 +02:00
parent 55578ab88c
commit dff3608aa3
2 changed files with 41 additions and 17 deletions

View File

@ -1,6 +1,8 @@
package world.bentobox.challenges; package world.bentobox.challenges;
import org.eclipse.jdt.annotation.NonNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
@ -322,9 +324,20 @@ public class ChallengesManager
* *
* @param user - user to add * @param user - user to add
*/ */
private void addPlayer(User user) private void addPlayer(@NonNull User user)
{ {
if (this.playerCacheData.containsKey(user.getUniqueId())) this.addPlayer(user.getUniqueId());
}
/**
* Load player from database into the cache or create new player data
*
* @param userID - userID to add
*/
private void addPlayer(@NonNull UUID userID)
{
if (this.playerCacheData.containsKey(userID))
{ {
return; return;
} }
@ -332,20 +345,20 @@ public class ChallengesManager
// The player is not in the cache // The player is not in the cache
// Check if the player exists in the database // Check if the player exists in the database
if (this.playersDatabase.objectExists(user.getUniqueId().toString())) if (this.playersDatabase.objectExists(userID.toString()))
{ {
// Load player from database // Load player from database
ChallengesPlayerData data = this.playersDatabase.loadObject(user.getUniqueId().toString()); ChallengesPlayerData data = this.playersDatabase.loadObject(userID.toString());
// Store in cache // Store in cache
this.playerCacheData.put(user.getUniqueId(), data); this.playerCacheData.put(userID, data);
} }
else else
{ {
// Create the player data // Create the player data
ChallengesPlayerData pd = new ChallengesPlayerData(user.getUniqueId().toString()); ChallengesPlayerData pd = new ChallengesPlayerData(userID.toString());
this.playersDatabase.saveObject(pd); this.playersDatabase.saveObject(pd);
// Add to cache // Add to cache
this.playerCacheData.put(user.getUniqueId(), pd); this.playerCacheData.put(userID, pd);
} }
} }
@ -563,7 +576,7 @@ public class ChallengesManager
* @param user - user * @param user - user
* @param challenge - challenge * @param challenge - challenge
*/ */
public void resetChallenge(User user, Challenge challenge) public void resetChallenge(@NonNull User user, @NonNull Challenge challenge)
{ {
this.addPlayer(user); this.addPlayer(user);
this.playerCacheData.get(user.getUniqueId()).setChallengeTimes(challenge.getUniqueId(), 0); this.playerCacheData.get(user.getUniqueId()).setChallengeTimes(challenge.getUniqueId(), 0);
@ -572,6 +585,20 @@ public class ChallengesManager
} }
/**
* Resets all the challenges for user in world
*
* @param userID - island owner's UUID
* @param world - world
*/
public void resetAllChallenges(@NonNull UUID userID, @NonNull World world)
{
this.addPlayer(userID);
this.playerCacheData.get(userID).reset(world);
// Save
this.savePlayer(userID);
}
/** /**
* Resets all the challenges for user in world * Resets all the challenges for user in world
@ -579,12 +606,9 @@ public class ChallengesManager
* @param user - island owner's UUID * @param user - island owner's UUID
* @param world - world * @param world - world
*/ */
public void resetAllChallenges(User user, World world) public void resetAllChallenges(@NonNull User user, @NonNull World world)
{ {
this.addPlayer(user); this.resetAllChallenges(user.getUniqueId(), world);
this.playerCacheData.get(user.getUniqueId()).reset(world);
// Save
this.savePlayer(user.getUniqueId());
} }
@ -594,7 +618,7 @@ public class ChallengesManager
* @param user User who need to be checked. * @param user User who need to be checked.
* @return true, if level is already completed. * @return true, if level is already completed.
*/ */
public boolean isLevelCompleted(User user, ChallengeLevel level) public boolean isLevelCompleted(@NonNull User user, @NonNull ChallengeLevel level)
{ {
this.addPlayer(user); this.addPlayer(user);
return this.playerCacheData.get(user.getUniqueId()).isLevelDone(level.getUniqueId()); return this.playerCacheData.get(user.getUniqueId()).isLevelDone(level.getUniqueId());
@ -607,7 +631,7 @@ public class ChallengesManager
* @param user User who need to be checked. * @param user User who need to be checked.
* @return true, if all challenges are done, otherwise false. * @return true, if all challenges are done, otherwise false.
*/ */
public boolean validateLevelCompletion(User user, ChallengeLevel level) public boolean validateLevelCompletion(@NonNull User user, @NonNull ChallengeLevel level)
{ {
this.addPlayer(user); this.addPlayer(user);
ChallengesPlayerData playerData = this.playerCacheData.get(user.getUniqueId()); ChallengesPlayerData playerData = this.playerCacheData.get(user.getUniqueId());
@ -622,7 +646,7 @@ public class ChallengesManager
* @param level Level that must be completed. * @param level Level that must be completed.
* @param user User who complete level. * @param user User who complete level.
*/ */
public void setLevelComplete(User user, ChallengeLevel level) public void setLevelComplete(@NonNull User user, @NonNull ChallengeLevel level)
{ {
this.addPlayer(user); this.addPlayer(user);
this.playerCacheData.get(user.getUniqueId()).addCompletedLevel(level.getUniqueId()); this.playerCacheData.get(user.getUniqueId()).addCompletedLevel(level.getUniqueId());

View File

@ -28,7 +28,7 @@ public class ResetListener implements Listener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onIslandReset(IslandEvent e) { public void onIslandReset(IslandEvent e) {
if (e.getReason().equals(Reason.CREATED) || (addon.getChallengesSettings().isResetChallenges() && e.getReason().equals(Reason.RESETTED))) { if (e.getReason().equals(Reason.CREATED) || (addon.getChallengesSettings().isResetChallenges() && e.getReason().equals(Reason.RESETTED))) {
addon.getChallengesManager().resetAllChallenges(User.getInstance(e.getOwner()), e.getLocation().getWorld()); addon.getChallengesManager().resetAllChallenges(e.getOwner(), e.getLocation().getWorld());
} }
} }
} }