From 8551dc407be8913778b59f9e68049ba09b2c83c3 Mon Sep 17 00:00:00 2001 From: Electroblob77 <35599699+Electroblob77@users.noreply.github.com> Date: Mon, 2 Sep 2019 19:03:08 +0100 Subject: [PATCH] Add array range checks to all cooldown methods that don't have them, fixes #183 I'm concerned this may just be convering up the problem, but without being able to recreate it there's little else I can do. --- .../electroblob/wizardry/util/WandHelper.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/java/electroblob/wizardry/util/WandHelper.java b/src/main/java/electroblob/wizardry/util/WandHelper.java index 07ab31c3..415076ab 100644 --- a/src/main/java/electroblob/wizardry/util/WandHelper.java +++ b/src/main/java/electroblob/wizardry/util/WandHelper.java @@ -253,9 +253,11 @@ public final class WandHelper { int[] cooldowns = getCooldowns(wand); - if(cooldowns.length == 0) return 0; + int selectedSpell = wand.getTagCompound().getInteger(SELECTED_SPELL_KEY); + + if(cooldowns.length <= selectedSpell) return 0; // Don't need to check if the tag compound is null since the above check is equivalent. - return cooldowns[wand.getTagCompound().getInteger(SELECTED_SPELL_KEY)]; + return cooldowns[selectedSpell]; } /** Returns the given wand's cooldown for the spell after the currently selected spell, or 0 if the wand has no @@ -264,9 +266,11 @@ public final class WandHelper { int[] cooldowns = getCooldowns(wand); - if(cooldowns.length == 0) return 0; + int nextSpell = getNextSpellIndex(wand); + + if(cooldowns.length <= nextSpell) return 0; // Don't need to check if the tag compound is null since the above check is equivalent. - return cooldowns[getNextSpellIndex(wand)]; + return cooldowns[nextSpell]; } /** Returns the given wand's cooldown for the spell before the currently selected spell, or 0 if the wand has no @@ -275,9 +279,11 @@ public final class WandHelper { int[] cooldowns = getCooldowns(wand); - if(cooldowns.length == 0) return 0; + int previousSpell = getPreviousSpellIndex(wand); + + if(cooldowns.length <= previousSpell) return 0; // Don't need to check if the tag compound is null since the above check is equivalent. - return cooldowns[getPreviousSpellIndex(wand)]; + return cooldowns[previousSpell]; } /** Sets the given wand's cooldown for the currently selected spell. Will also set the maximum cooldown. */ @@ -287,19 +293,24 @@ public final class WandHelper { int[] cooldowns = getCooldowns(wand); + int selectedSpell = wand.getTagCompound().getInteger(SELECTED_SPELL_KEY); + int spellCount = getSpells(wand).length; + + if(spellCount <= selectedSpell) return; // Probably shouldn't happen + // The length of the spells array must be greater than 0 since this method can only be called if a spell is // cast, which is impossible if there are no spells. - if(cooldowns.length == 0) cooldowns = new int[getSpells(wand).length]; + if(cooldowns.length <= selectedSpell) cooldowns = new int[spellCount]; - cooldowns[wand.getTagCompound().getInteger(SELECTED_SPELL_KEY)] = cooldown; + cooldowns[selectedSpell] = cooldown; setCooldowns(wand, cooldowns); int[] maxCooldowns = getMaxCooldowns(wand); - if(maxCooldowns.length == 0) maxCooldowns = new int[getSpells(wand).length]; + if(maxCooldowns.length <= selectedSpell) maxCooldowns = new int[spellCount]; - maxCooldowns[wand.getTagCompound().getInteger(SELECTED_SPELL_KEY)] = cooldown; + maxCooldowns[selectedSpell] = cooldown; setMaxCooldowns(wand, maxCooldowns); }