From 9b367985f88076c2d840ce3c5e1b3a85bf70b0c4 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Mon, 22 Mar 2021 20:17:41 +0000 Subject: [PATCH] Added warning and checks for invalid enchantments, effects, potion effects, default tiers. --- .../com/willfp/ecoarmor/effects/Effects.java | 2 + .../com/willfp/ecoarmor/sets/ArmorSet.java | 46 ++++++++++++++++--- .../com/willfp/ecoarmor/sets/ArmorSets.java | 2 + .../com/willfp/ecoarmor/upgrades/Tiers.java | 1 + 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java index 606c28b..da69a12 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/effects/Effects.java @@ -20,6 +20,7 @@ import com.willfp.ecoarmor.effects.effects.TridentDamageMultiplier; import com.willfp.ecoarmor.effects.effects.WarpChance; import lombok.experimental.UtilityClass; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; @@ -53,6 +54,7 @@ public class Effects { * @param name The name to query. * @return The matching effect, or null if not found. */ + @Nullable public static Effect getByName(@NotNull final String name) { return BY_NAME.get(name); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSet.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSet.java index c6c6767..8d048ca 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSet.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSet.java @@ -14,9 +14,11 @@ import com.willfp.ecoarmor.effects.Effect; import com.willfp.ecoarmor.effects.Effects; import com.willfp.ecoarmor.sets.meta.ArmorSlot; import com.willfp.ecoarmor.sets.util.ArmorUtils; +import com.willfp.ecoarmor.upgrades.Tier; import com.willfp.ecoarmor.upgrades.Tiers; import lombok.AccessLevel; import lombok.Getter; +import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -128,7 +130,11 @@ public class ArmorSet { String key = split[0].trim(); String value = split[1].trim(); Condition condition = Conditions.getByName(key); - conditions.put(condition, ArmorUtils.getConditionValue(value, condition)); + if (condition == null) { + Bukkit.getLogger().warning("Invalid condition specified in " + this.name); + } else { + conditions.put(condition, ArmorUtils.getConditionValue(value, condition)); + } } for (String definedKey : this.getConfig().getStrings("set-bonus")) { @@ -136,7 +142,11 @@ public class ArmorSet { String key = split[0].trim(); String value = split[1].trim(); Effect effect = Effects.getByName(key); - effects.put(effect, ArmorUtils.getEffectValue(value, effect)); + if (effect == null) { + Bukkit.getLogger().warning("Invalid effect specified in " + this.name); + } else { + effects.put(effect, ArmorUtils.getEffectValue(value, effect)); + } } for (String definedKey : this.getConfig().getStrings("advanced-set-bonus")) { @@ -144,7 +154,11 @@ public class ArmorSet { String key = split[0].trim(); String value = split[1].trim(); Effect effect = Effects.getByName(key); - advancedEffects.put(effect, ArmorUtils.getEffectValue(value, effect)); + if (effect == null) { + Bukkit.getLogger().warning("Invalid advanced effect specified in " + this.name); + } else { + advancedEffects.put(effect, ArmorUtils.getEffectValue(value, effect)); + } } for (String definedKey : this.getConfig().getStrings("potion-effects")) { @@ -152,7 +166,11 @@ public class ArmorSet { String key = split[0].trim(); String value = split[1].trim(); PotionEffectType type = PotionEffectType.getByName(key.toUpperCase()); - potionEffects.put(type, Integer.parseInt(value)); + if (type == null) { + Bukkit.getLogger().warning("Invalid potion effect specified in " + this.name); + } else { + potionEffects.put(type, Integer.parseInt(value)); + } } for (String definedKey : this.getConfig().getStrings("advanced-potion-effects")) { @@ -160,7 +178,11 @@ public class ArmorSet { String key = split[0].trim(); String value = split[1].trim(); PotionEffectType type = PotionEffectType.getByName(key.toUpperCase()); - advancedPotionEffects.put(type, Integer.parseInt(value)); + if (type == null) { + Bukkit.getLogger().warning("Invalid advanced potion effect specified in " + this.name); + } else { + advancedPotionEffects.put(type, Integer.parseInt(value)); + } } for (ArmorSlot slot : ArmorSlot.values()) { @@ -226,7 +248,11 @@ public class ArmorSet { String key = split[0].trim(); String value = split[1].trim(); Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(key)); - enchants.put(enchantment, Integer.valueOf(value)); + if (enchantment == null) { + Bukkit.getLogger().warning("Invalid enchantment specified in " + this.name + " " + pieceName); + } else { + enchants.put(enchantment, Integer.valueOf(value)); + } } assert material != null; @@ -294,7 +320,13 @@ public class ArmorSet { itemStack.setItemMeta(meta); ArmorUtils.setAdvanced(itemStack, advanced); - ArmorUtils.setTier(itemStack, Tiers.getByName(this.getConfig().getString(pieceName + ".default-tier"))); + Tier defaultTier = Tiers.getByName(this.getConfig().getString(pieceName + ".default-tier")); + if (defaultTier == null) { + Bukkit.getLogger().warning("Default tier specified in " + this.name + " " + pieceName + " is invalid! Defaulting to 'default'"); + ArmorUtils.setTier(itemStack, Tiers.DEFAULT); + } else { + ArmorUtils.setTier(itemStack, defaultTier); + } if (advanced) { RecipeParts.registerRecipePart(PLUGIN.getNamespacedKeyFactory().create("set_" + name.toLowerCase() + "_" + pieceName + "_advanced"), new ComplexRecipePart(test -> { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSets.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSets.java index 3a77654..135cbae 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSets.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/sets/ArmorSets.java @@ -11,6 +11,7 @@ import com.willfp.ecoarmor.config.CustomConfig; import lombok.experimental.UtilityClass; import org.bukkit.configuration.file.YamlConfiguration; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; @@ -51,6 +52,7 @@ public class ArmorSets { * @param name The name to search for. * @return The matching {@link ArmorSet}, or null if not found. */ + @Nullable public static ArmorSet getByName(@NotNull final String name) { return BY_NAME.get(name); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/upgrades/Tiers.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/upgrades/Tiers.java index c636476..a8bf972 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/upgrades/Tiers.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoarmor/upgrades/Tiers.java @@ -55,6 +55,7 @@ public class Tiers { * @param name The name to search for. * @return The matching {@link Tiers}, or null if not found. */ + @Nullable public static Tier getByName(@Nullable final String name) { return BY_NAME.get(name); }