From ed807342cc0f4629374657fe6a315c178e6139ee Mon Sep 17 00:00:00 2001 From: nossr50 Date: Tue, 30 Mar 2021 11:48:34 -0700 Subject: [PATCH] Tweaks to exception throwing/catching for PlayerData --- .../database/FlatFileDatabaseManager.java | 2 +- .../datatypes/player/MMODataBuilder.java | 3 +- .../nossr50/datatypes/player/PlayerData.java | 35 +++++++++++++------ .../datatypes/validation/NonNullRule.java | 2 +- .../validation/PositiveIntegerRule.java | 4 +-- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java index 9b2dc42f3..452f793ad 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java @@ -1339,7 +1339,7 @@ public final class FlatFileDatabaseManager implements DatabaseManager { //Build Data return playerDataBuilder.build(); - } catch (NullArgumentException e) { + } catch (Exception e) { mcMMO.p.getLogger().severe("Critical failure when trying to construct persistent player data!"); e.printStackTrace(); return null; diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/MMODataBuilder.java b/src/main/java/com/gmail/nossr50/datatypes/player/MMODataBuilder.java index ad64ea1d7..7b03d5bc5 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/MMODataBuilder.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/MMODataBuilder.java @@ -5,6 +5,7 @@ import com.gmail.nossr50.datatypes.skills.SuperAbilityType; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.util.experience.MMOExperienceBarManager; import com.neetgames.mcmmo.UniqueDataType; +import com.neetgames.mcmmo.exceptions.UnexpectedValueException; import com.neetgames.mcmmo.skill.SkillBossBarState; import org.apache.commons.lang.NullArgumentException; import org.bukkit.OfflinePlayer; @@ -59,7 +60,7 @@ public class MMODataBuilder { return new PlayerData(playerUUID, playerName); } - public PlayerData build() throws NullArgumentException { + public PlayerData build() throws UnexpectedValueException, NullPointerException, NullArgumentException { if(playerUUID == null) throw new NullArgumentException("playerUUID"); diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/PlayerData.java b/src/main/java/com/gmail/nossr50/datatypes/player/PlayerData.java index 6e4d30387..cd6ce9784 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/PlayerData.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/PlayerData.java @@ -25,7 +25,7 @@ public class PlayerData { /* Player Stuff */ 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 */ private long lastLogin; @@ -120,7 +120,7 @@ public class PlayerData { @NotNull Map barStateMap, int scoreboardTipsShown, long lastLogin, - boolean leaderBoardExclusion) throws Exception { + boolean leaderBoardExclusion) throws UnexpectedValueException, NullPointerException { /* * Skills Data @@ -155,18 +155,29 @@ public class PlayerData { * @throws UnexpectedValueException when values are outside of expected norms * @throws Exception when values are outside of expected norms */ - private void validateRootSkillMap(Map map) throws UnexpectedValueException, Exception { + private void validateRootSkillMap(Map map) throws UnexpectedValueException, NullPointerException { //TODO: Check for missing/unregistered - Validator validator = new Validator<>(); + Validator positiveValidator = new Validator<>(); + Validator nullValidator = new Validator<>(); - validator.addRule(new PositiveIntegerRule<>()); - validator.addRule(new NonNullRule<>()); + positiveValidator.addRule(new PositiveIntegerRule<>()); + nullValidator.addRule(new NonNullRule<>()); for(PrimarySkillType primarySkillType : PrimarySkillType.values()) { if(primarySkillType.isChildSkill()) 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 Exception when values are outside of expected norms */ - private void validateSuperSkillMap(Map map) throws UnexpectedValueException, Exception { + private void validateSuperSkillMap(Map map) throws UnexpectedValueException, NullPointerException { //TODO: Check for missing/unregistered Validator validator = new Validator<>(); @@ -185,7 +196,11 @@ public class PlayerData { validator.addRule(new NonNullRule<>()); for(SuperAbilityType superSkill : SuperAbilityType.values()) { - validator.validate(map.get(superSkill)); + try { + validator.validate(map.get(superSkill)); + } catch (Exception e) { + throw new UnexpectedValueException(); + } } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/validation/NonNullRule.java b/src/main/java/com/gmail/nossr50/datatypes/validation/NonNullRule.java index 5495763b8..0e0ef230a 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/validation/NonNullRule.java +++ b/src/main/java/com/gmail/nossr50/datatypes/validation/NonNullRule.java @@ -2,7 +2,7 @@ package com.gmail.nossr50.datatypes.validation; public class NonNullRule extends Rule { @Override - public void applyRule(T object) throws Exception { + public void applyRule(T object) throws NullPointerException { if(object == null) throw new NullPointerException(); } diff --git a/src/main/java/com/gmail/nossr50/datatypes/validation/PositiveIntegerRule.java b/src/main/java/com/gmail/nossr50/datatypes/validation/PositiveIntegerRule.java index c4348943c..86fd089b4 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/validation/PositiveIntegerRule.java +++ b/src/main/java/com/gmail/nossr50/datatypes/validation/PositiveIntegerRule.java @@ -1,10 +1,10 @@ package com.gmail.nossr50.datatypes.validation; -import com.gmail.nossr50.api.exceptions.UnexpectedValueException; +import com.neetgames.mcmmo.exceptions.UnexpectedValueException; public class PositiveIntegerRule extends Rule { @Override - public void applyRule(T number) throws Exception { + public void applyRule(T number) throws UnexpectedValueException { if(number.intValue() < 0) throw new UnexpectedValueException(); }