From f1ff179b983f66d55328c1bb515330fdaa09c7da Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Mon, 2 Sep 2019 17:05:21 +0100 Subject: [PATCH] Check if spell properties are initialised when setting lightning hammer item attribute modifiers, fixes #219 --- .../electroblob/wizardry/item/ItemLightningHammer.java | 4 +++- src/main/java/electroblob/wizardry/spell/Spell.java | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/electroblob/wizardry/item/ItemLightningHammer.java b/src/main/java/electroblob/wizardry/item/ItemLightningHammer.java index 0b7d40cd..33ecab67 100644 --- a/src/main/java/electroblob/wizardry/item/ItemLightningHammer.java +++ b/src/main/java/electroblob/wizardry/item/ItemLightningHammer.java @@ -81,7 +81,9 @@ public class ItemLightningHammer extends Item implements IConjuredItem { Multimap multimap = super.getItemAttributeModifiers(slot); if(slot == EntityEquipmentSlot.MAINHAND){ - multimap.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", Spells.lightning_hammer.getProperty(Spell.DIRECT_DAMAGE).floatValue(), WizardryUtilities.Operations.ADD)); + float attackDamage = Spells.lightning_hammer.arePropertiesInitialised() ? + Spells.lightning_hammer.getProperty(Spell.DIRECT_DAMAGE).floatValue() : 10; // Fallback for search tree init, value doesn't really matter + multimap.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", attackDamage, WizardryUtilities.Operations.ADD)); multimap.put(SharedMonsterAttributes.ATTACK_SPEED.getName(), new AttributeModifier(ATTACK_SPEED_MODIFIER, "Weapon modifier", ATTACK_SPEED, WizardryUtilities.Operations.ADD)); multimap.put(SharedMonsterAttributes.MOVEMENT_SPEED.getName(), new AttributeModifier(MOVEMENT_SPEED_MODIFIER, "Weapon modifier", MOVEMENT_SPEED_REDUCTION, WizardryUtilities.Operations.MULTIPLY_FLAT)); } diff --git a/src/main/java/electroblob/wizardry/spell/Spell.java b/src/main/java/electroblob/wizardry/spell/Spell.java index 8ed6516e..3a264eac 100644 --- a/src/main/java/electroblob/wizardry/spell/Spell.java +++ b/src/main/java/electroblob/wizardry/spell/Spell.java @@ -221,7 +221,7 @@ public abstract class Spell extends IForgeRegistryEntry.Impl implements C // Conversely, general spell classes ONLY EVER define the properties they ACTUALLY USE. public final Spell addProperties(String... keys){ - if(properties != null) throw new IllegalStateException("Tried to add spell properties after they were initialised"); + if(arePropertiesInitialised()) throw new IllegalStateException("Tried to add spell properties after they were initialised"); for(String key : keys) if(propertyKeys.contains(key)) Wizardry.logger.warn("Tried to add a duplicate property key '" + key + "' to spell " + this.getRegistryName()); @@ -236,11 +236,17 @@ public abstract class Spell extends IForgeRegistryEntry.Impl implements C return propertyKeys.toArray(new String[0]); } + /** Returns true if this spell's properties have been initialised, false if not. Check this if you're attempting + * to access them from code that could be called before wizardry's {@code init()} method (e.g. item attributes). */ + public final boolean arePropertiesInitialised(){ + return properties != null; + } + /** Sets this spell's properties to the given {@link SpellProperties} object, but only if it doesn't already * have one. This prevents spell properties from being changed after initialisation. */ public void setProperties(@Nonnull SpellProperties properties){ - if(this.properties == null){ + if(!arePropertiesInitialised()){ this.properties = properties; }else{ Wizardry.logger.info("A mod attempted to set a spell's properties, but they were already initialised.");