That's one heck of a commit you've got there...

I may have got a bit behind with version control. A lot behind, in fact. Maybe I'll go back and split this sometime - then again, I probably won't. But hey, at least it's here!
This commit is contained in:
Electroblob77
2019-08-18 00:04:18 +01:00
parent 2680d02304
commit f37812be3e
1564 changed files with 47652 additions and 12984 deletions
@@ -1,9 +1,5 @@
package electroblob.wizardry.util;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import electroblob.wizardry.item.ItemWand;
import electroblob.wizardry.registry.Spells;
import electroblob.wizardry.registry.WizardryItems;
@@ -12,25 +8,29 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
/**
* <i>"Never fear, {@code WandHelper} is here!"</i>
* <p>
* <p></p>
* Much like {@link net.minecraft.enchantment.EnchantmentHelper EnchantmentHelper}, this class has some static methods
* which allow cleaner and more concise interaction with the wand NBT data, which is quite a complex structure. Such
* interaction previously resulted in rather verbose and repetitive code which was hard to read and even harder to
* debug! For example, this class allowed {@link electroblob.wizardry.item.ItemWand ItemWand} to be shortened by about
* 80 lines. In addition, by having all the various null checks and array size checks in one place, the chance of
* accidental errors due to forgetting to check these things is greatly reduced.
* <p>
* <p></p>
* Note that these methods contain no game logic at all; they are purely for interacting with the NBT data. Conversely,
* you should never need to access the wand's NBT data directly when using this class, but the keys are public in the
* unlikely case that this is necessary.
* <p>
* <p></p>
* Also note that none of the methods in this class actually check that the given ItemStack contains an ItemWand; you
* can, for example, pass in a stack of snowballs without causing problems, but that is of course pointless! However, if
* you have your own spell casting item (which doesn't extend ItemWand), this setup means you can still use this class
* to manage its NBT structure.
* <p>
* <p></p>
* All <b>get</b> methods in this class return some kind of default if the passed-in wand stack has no nbt data. See
* individual method descriptions for more details.<br>
* All <b>set</b> methods in this class create a new nbt data for the passed-in wand if it has none, before doing
@@ -46,11 +46,13 @@ public final class WandHelper {
public static final String SPELL_ARRAY_KEY = "spells";
public static final String SELECTED_SPELL_KEY = "selectedSpell";
public static final String COOLDOWN_ARRAY_KEY = "cooldown";
public static final String MAX_COOLDOWN_ARRAY_KEY = "maxCooldown";
public static final String UPGRADES_KEY = "upgrades";
public static final String PROGRESSION_KEY = "progression";
private static final HashMap<Item, String> upgradeMap = new HashMap<Item, String>();
static{
static {
upgradeMap.put(WizardryItems.condenser_upgrade, "condenser");
upgradeMap.put(WizardryItems.storage_upgrade, "storage");
upgradeMap.put(WizardryItems.siphon_upgrade, "siphon");
@@ -59,8 +61,11 @@ public final class WandHelper {
upgradeMap.put(WizardryItems.cooldown_upgrade, "cooldown");
upgradeMap.put(WizardryItems.blast_upgrade, "blast");
upgradeMap.put(WizardryItems.attunement_upgrade, "attunement");
upgradeMap.put(WizardryItems.melee_upgrade, "melee");
}
// =================================================== Spells ===================================================
/**
* Returns an array containing the spells currently bound to the given wand. As of Wizardry 1.1, this array is not
* always the same size; it can be anywhere between 5 and 8 (inclusive) in length. If the wand has no spell data,
@@ -77,7 +82,7 @@ public final class WandHelper {
spells = new Spell[spellIDs.length];
for(int i = 0; i < spellIDs.length; i++){
spells[i] = Spell.get(spellIDs[i]);
spells[i] = Spell.byMetadata(spellIDs[i]);
}
}
@@ -95,7 +100,7 @@ public final class WandHelper {
int[] spellIDs = new int[spells.length];
for(int i = 0; i < spells.length; i++){
spellIDs[i] = spells[i] != null ? spells[i].id() : Spells.none.id();
spellIDs[i] = spells[i] != null ? spells[i].metadata() : Spells.none.metadata();
}
wand.getTagCompound().setIntArray(SPELL_ARRAY_KEY, spellIDs);
@@ -123,9 +128,10 @@ public final class WandHelper {
public static Spell getNextSpell(ItemStack wand){
Spell[] spells = getSpells(wand);
int index = getNextSpellIndex(wand);
if(wand.getTagCompound() != null){
return spells[getNextSpellIndex(wand)];
if(index >= 0 && index < spells.length){
return spells[index];
}
return Spells.none;
@@ -136,9 +142,10 @@ public final class WandHelper {
public static Spell getPreviousSpell(ItemStack wand){
Spell[] spells = getSpells(wand);
int index = getPreviousSpellIndex(wand);
if(wand.getTagCompound() != null){
return spells[getPreviousSpellIndex(wand)];
if(index >= 0 && index < spells.length){
return spells[index];
}
return Spells.none;
@@ -165,6 +172,8 @@ public final class WandHelper {
}
private static int getNextSpellIndex(ItemStack wand){
if(wand.getTagCompound() == null) wand.setTagCompound(new NBTTagCompound());
int numberOfSpells = getSpells(wand).length;
int spellIndex = wand.getTagCompound().getInteger(SELECTED_SPELL_KEY);
@@ -181,6 +190,8 @@ public final class WandHelper {
}
private static int getPreviousSpellIndex(ItemStack wand){
if(wand.getTagCompound() == null) wand.setTagCompound(new NBTTagCompound());
int numberOfSpells = getSpells(wand).length;
int spellIndex = wand.getTagCompound().getInteger(SELECTED_SPELL_KEY);
@@ -194,6 +205,8 @@ public final class WandHelper {
return spellIndex;
}
// ================================================== Cooldowns ==================================================
/**
* Returns an array of the cooldowns for each spell bound to the given wand. As of Wizardry 1.1, this array is not
* always the same size; it can be anywhere between 5 and 8 (inclusive) in length. If the wand has no cooldown data,
@@ -211,7 +224,8 @@ public final class WandHelper {
return cooldowns;
}
/** Sets the given wand's cooldown array. The array can be anywhere between 5 and 8 (inclusive) in length. */
/** Sets the given wand's cooldown array. The array can be anywhere between 5 and 8 (inclusive) in length.
* Unlike {@link WandHelper#setCurrentCooldown(ItemStack, int)}, this will <b>not</b> set the max cooldowns. */
public static void setCooldowns(ItemStack wand, int[] cooldowns){
if(wand.getTagCompound() == null) wand.setTagCompound((new NBTTagCompound()));
@@ -266,7 +280,7 @@ public final class WandHelper {
return cooldowns[getPreviousSpellIndex(wand)];
}
/** Sets the given wand's cooldown for the currently selected spell. */
/** Sets the given wand's cooldown for the currently selected spell. Will also set the maximum cooldown. */
public static void setCurrentCooldown(ItemStack wand, int cooldown){
if(wand.getTagCompound() == null) wand.setTagCompound((new NBTTagCompound()));
@@ -280,8 +294,52 @@ public final class WandHelper {
cooldowns[wand.getTagCompound().getInteger(SELECTED_SPELL_KEY)] = cooldown;
setCooldowns(wand, cooldowns);
int[] maxCooldowns = getMaxCooldowns(wand);
if(maxCooldowns.length == 0) maxCooldowns = new int[getSpells(wand).length];
maxCooldowns[wand.getTagCompound().getInteger(SELECTED_SPELL_KEY)] = cooldown;
setMaxCooldowns(wand, maxCooldowns);
}
/**
* Returns an array of the max cooldowns for each spell bound to the given wand. If the wand has no cooldown data,
* returns an array of length 0.
*/
public static int[] getMaxCooldowns(ItemStack wand){
int[] cooldowns = new int[0];
if(wand.getTagCompound() != null){
return wand.getTagCompound().getIntArray(MAX_COOLDOWN_ARRAY_KEY);
}
return cooldowns;
}
/** Sets the given wand's cooldown array. The array can be anywhere between 5 and 8 (inclusive) in length. */
public static void setMaxCooldowns(ItemStack wand, int[] cooldowns){
if(wand.getTagCompound() == null) wand.setTagCompound((new NBTTagCompound()));
wand.getTagCompound().setIntArray(MAX_COOLDOWN_ARRAY_KEY, cooldowns);
}
/** Returns the given wand's max cooldown for the currently selected spell, or 0 if the wand has no cooldown data. */
public static int getCurrentMaxCooldown(ItemStack wand){
int[] cooldowns = getMaxCooldowns(wand);
if(cooldowns.length == 0) 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)];
}
// ================================================== Upgrades ==================================================
/**
* Returns the number of upgrades of the given type that have been applied to the given wand, or 0 if the wand has
* no upgrade data or the given item is not a valid wand upgrade.
@@ -371,4 +429,28 @@ public final class WandHelper {
throw new IllegalArgumentException("Duplicate wand upgrade identifier: " + identifier);
upgradeMap.put(upgrade, identifier);
}
// ================================================= Progression =================================================
/** Sets the given wand's progression to the given value. */
public static void setProgression(ItemStack wand, int progression){
if(wand.getTagCompound() == null) wand.setTagCompound((new NBTTagCompound()));
wand.getTagCompound().setInteger(PROGRESSION_KEY, progression);
}
/** Returns the progression value for the given wand, or 0 if the wand has no data. */
public static int getProgression(ItemStack wand){
if(wand.getTagCompound() == null) return 0;
return wand.getTagCompound().getInteger(PROGRESSION_KEY);
}
/** Adds the given amount of progression to this wand's progression value. */
public static void addProgression(ItemStack wand, int progression){
setProgression(wand, getProgression(wand) + progression);
}
}