Tweaks to exception throwing/catching for PlayerData

This commit is contained in:
nossr50 2021-03-30 11:48:34 -07:00
parent 1de9c2610b
commit ed807342cc
5 changed files with 31 additions and 15 deletions

View File

@ -1339,7 +1339,7 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
//Build Data //Build Data
return playerDataBuilder.build(); return playerDataBuilder.build();
} catch (NullArgumentException e) { } catch (Exception e) {
mcMMO.p.getLogger().severe("Critical failure when trying to construct persistent player data!"); mcMMO.p.getLogger().severe("Critical failure when trying to construct persistent player data!");
e.printStackTrace(); e.printStackTrace();
return null; return null;

View File

@ -5,6 +5,7 @@ import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.experience.MMOExperienceBarManager; import com.gmail.nossr50.util.experience.MMOExperienceBarManager;
import com.neetgames.mcmmo.UniqueDataType; import com.neetgames.mcmmo.UniqueDataType;
import com.neetgames.mcmmo.exceptions.UnexpectedValueException;
import com.neetgames.mcmmo.skill.SkillBossBarState; import com.neetgames.mcmmo.skill.SkillBossBarState;
import org.apache.commons.lang.NullArgumentException; import org.apache.commons.lang.NullArgumentException;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -59,7 +60,7 @@ public class MMODataBuilder {
return new PlayerData(playerUUID, playerName); return new PlayerData(playerUUID, playerName);
} }
public PlayerData build() throws NullArgumentException { public PlayerData build() throws UnexpectedValueException, NullPointerException, NullArgumentException {
if(playerUUID == null) if(playerUUID == null)
throw new NullArgumentException("playerUUID"); throw new NullArgumentException("playerUUID");

View File

@ -25,7 +25,7 @@ public class PlayerData {
/* Player Stuff */ /* Player Stuff */
private @NotNull String playerName; private @NotNull String playerName;
private final @Nullable UUID playerUUID; private final @Nullable UUID playerUUID; //TODO: T&C See if this is ever actually null, and if it is maybe we shouldn't allow it to be
/* Records */ /* Records */
private long lastLogin; private long lastLogin;
@ -120,7 +120,7 @@ public class PlayerData {
@NotNull Map<PrimarySkillType, SkillBossBarState> barStateMap, @NotNull Map<PrimarySkillType, SkillBossBarState> barStateMap,
int scoreboardTipsShown, int scoreboardTipsShown,
long lastLogin, long lastLogin,
boolean leaderBoardExclusion) throws Exception { boolean leaderBoardExclusion) throws UnexpectedValueException, NullPointerException {
/* /*
* Skills Data * Skills Data
@ -155,18 +155,29 @@ public class PlayerData {
* @throws UnexpectedValueException when values are outside of expected norms * @throws UnexpectedValueException when values are outside of expected norms
* @throws Exception when values are outside of expected norms * @throws Exception when values are outside of expected norms
*/ */
private void validateRootSkillMap(Map<PrimarySkillType, ? extends Number> map) throws UnexpectedValueException, Exception { private void validateRootSkillMap(Map<PrimarySkillType, ? extends Number> map) throws UnexpectedValueException, NullPointerException {
//TODO: Check for missing/unregistered //TODO: Check for missing/unregistered
Validator<Number> validator = new Validator<>(); Validator<Number> positiveValidator = new Validator<>();
Validator<Number> nullValidator = new Validator<>();
validator.addRule(new PositiveIntegerRule<>()); positiveValidator.addRule(new PositiveIntegerRule<>());
validator.addRule(new NonNullRule<>()); nullValidator.addRule(new NonNullRule<>());
for(PrimarySkillType primarySkillType : PrimarySkillType.values()) { for(PrimarySkillType primarySkillType : PrimarySkillType.values()) {
if(primarySkillType.isChildSkill()) if(primarySkillType.isChildSkill())
continue; continue;
validator.validate(map.get(primarySkillType)); try {
positiveValidator.validate(map.get(primarySkillType));
} catch (Exception e) {
throw new UnexpectedValueException();
}
try {
nullValidator.validate(map.get(primarySkillType));
} catch (Exception e) {
throw new NullPointerException();
}
} }
} }
@ -177,7 +188,7 @@ public class PlayerData {
* @throws UnexpectedValueException when values are outside of expected norms * @throws UnexpectedValueException when values are outside of expected norms
* @throws Exception when values are outside of expected norms * @throws Exception when values are outside of expected norms
*/ */
private void validateSuperSkillMap(Map<? extends SuperAbilityType, ? extends Number> map) throws UnexpectedValueException, Exception { private void validateSuperSkillMap(Map<? extends SuperAbilityType, ? extends Number> map) throws UnexpectedValueException, NullPointerException {
//TODO: Check for missing/unregistered //TODO: Check for missing/unregistered
Validator<Number> validator = new Validator<>(); Validator<Number> validator = new Validator<>();
@ -185,7 +196,11 @@ public class PlayerData {
validator.addRule(new NonNullRule<>()); validator.addRule(new NonNullRule<>());
for(SuperAbilityType superSkill : SuperAbilityType.values()) { for(SuperAbilityType superSkill : SuperAbilityType.values()) {
try {
validator.validate(map.get(superSkill)); validator.validate(map.get(superSkill));
} catch (Exception e) {
throw new UnexpectedValueException();
}
} }
} }

View File

@ -2,7 +2,7 @@ package com.gmail.nossr50.datatypes.validation;
public class NonNullRule<T> extends Rule<T> { public class NonNullRule<T> extends Rule<T> {
@Override @Override
public void applyRule(T object) throws Exception { public void applyRule(T object) throws NullPointerException {
if(object == null) if(object == null)
throw new NullPointerException(); throw new NullPointerException();
} }

View File

@ -1,10 +1,10 @@
package com.gmail.nossr50.datatypes.validation; package com.gmail.nossr50.datatypes.validation;
import com.gmail.nossr50.api.exceptions.UnexpectedValueException; import com.neetgames.mcmmo.exceptions.UnexpectedValueException;
public class PositiveIntegerRule<T extends Number> extends Rule<T> { public class PositiveIntegerRule<T extends Number> extends Rule<T> {
@Override @Override
public void applyRule(T number) throws Exception { public void applyRule(T number) throws UnexpectedValueException {
if(number.intValue() < 0) if(number.intValue() < 0)
throw new UnexpectedValueException(); throw new UnexpectedValueException();
} }