From b23136d56851ac8b5f6e54e9dba7567fbe7072ca Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Fri, 17 Apr 2020 23:02:45 +0100 Subject: [PATCH] Add method for checking if a spell property exists --- .../java/electroblob/wizardry/spell/Spell.java | 15 +++++++++++++-- .../wizardry/util/SpellProperties.java | 14 +++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/electroblob/wizardry/spell/Spell.java b/src/main/java/electroblob/wizardry/spell/Spell.java index ffac262b..3547010b 100644 --- a/src/main/java/electroblob/wizardry/spell/Spell.java +++ b/src/main/java/electroblob/wizardry/spell/Spell.java @@ -606,16 +606,27 @@ public abstract class Spell extends IForgeRegistryEntry.Impl implements C return properties.cooldown; } + /** + * Returns whether a property has been defined for the given identifier. + * @param identifier The JSON identifier to check for. + * @return True if a property has been defined for the given identifier, false if not. + */ + public final boolean hasProperty(String identifier){ + return properties.hasBaseValue(identifier); + } + /** * Returns the base value specified in JSON for the given identifier. This may be used from within the spell * class, or from elsewhere (entities, items, etc.) via the spell's instance. * * @param identifier The JSON identifier for the required property. This must have been defined using - * {@link Spell#addProperties(String...)} or an exception will be thrown. + * {@link Spell#addProperties(String...)} or an exception will be thrown. To check if an + * identifier exists, use {@link Spell#hasProperty(String)}. * @return The base value of the property, as a {@code Number} object. Internally this is handled as a float, but * it is passed through as a {@code Number} to avoid casting. Be careful with rounding when extracting integer * values! The JSON parser cannot guarantee that the property file has an integer value. - * @throws IllegalArgumentException if no property was defined with the given identifier. */ + * @throws IllegalArgumentException if no property was defined with the given identifier. + */ public final Number getProperty(String identifier){ return properties.getBaseValue(identifier); } diff --git a/src/main/java/electroblob/wizardry/util/SpellProperties.java b/src/main/java/electroblob/wizardry/util/SpellProperties.java index de12eb52..535c60a4 100644 --- a/src/main/java/electroblob/wizardry/util/SpellProperties.java +++ b/src/main/java/electroblob/wizardry/util/SpellProperties.java @@ -224,11 +224,23 @@ public final class SpellProperties { } /** - * Returns the base value for this spell that corresponds to the given identifier. + * Returns whether a base value was defined with the given identifier. + * @param identifier The string identifier to check for. + * @return True if a base value was defined with the given identifier, false otherwise. + */ + public boolean hasBaseValue(String identifier){ + return baseValues.containsKey(identifier); + } + + /** + * Returns the base value for this spell that corresponds to the given identifier. To check whether an identifier + * exists, use {@link SpellProperties#hasBaseValue(String)}. * @param identifier The string identifier to fetch the base value for. * @return The base value, as a {@code Number}. * @throws IllegalArgumentException if no base value was defined with the given identifier. */ + // Better to throw an exception than make this nullable because the vast majority of uses are for retrieving + // specific spells' properties that are known to exist, and IntelliJ would scream at us for not checking public Number getBaseValue(String identifier){ if(!baseValues.containsKey(identifier)){ throw new IllegalArgumentException("Base value with identifier '" + identifier + "' is not defined.");