Mostly finished the LivingArmour framework, which includes the upgrades and stat trackers.

This commit is contained in:
WayofTime 2015-12-03 10:18:05 -05:00
parent 13d9cb4b5a
commit eaa6226861
5 changed files with 184 additions and 15 deletions

View file

@ -0,0 +1,61 @@
package WayofTime.bloodmagic.api.livingArmour;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.minecraft.nbt.NBTTagCompound;
public class LivingArmourHandler {
public static List<Class<? extends StatTracker>> trackers = new ArrayList();
public static HashMap<String, Class<? extends LivingArmourUpgrade>> upgradeMap = new HashMap();
public static HashMap<String, Constructor<? extends LivingArmourUpgrade>> upgradeConstructorMap = new HashMap();
public static void registerStatTracker(Class<? extends StatTracker> tracker) {
trackers.add(tracker);
}
/**
* Registers a LivingArmourUpgrade using its unique identifier and class.
* This is done to more easily load upgrades
*
* @param upgrade
*/
public static void registerArmourUpgrade(LivingArmourUpgrade upgrade) {
Class<? extends LivingArmourUpgrade> clazz = upgrade.getClass();
upgradeMap.put(upgrade.getUniqueIdentifier(), clazz);
try {
Constructor<? extends LivingArmourUpgrade> ctor = clazz.getConstructor(int.class);
if (ctor == null) {
// TODO: This is bad - add something to the log
} else {
upgradeConstructorMap.put(upgrade.getUniqueIdentifier(), ctor);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static LivingArmourUpgrade generateUpgradeFromKey(String key, int level) {
return generateUpgradeFromKey(key, level, null);
}
public static LivingArmourUpgrade generateUpgradeFromKey(String key, int level, NBTTagCompound tag) {
Constructor<? extends LivingArmourUpgrade> ctor = upgradeConstructorMap.get(key);
if (ctor != null) {
try {
LivingArmourUpgrade upgrade = ctor.newInstance(level);
if (upgrade != null && tag != null) {
upgrade.readFromNBT(tag);
}
return upgrade;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}

View file

@ -0,0 +1,53 @@
package WayofTime.bloodmagic.api.livingArmour;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import WayofTime.bloodmagic.livingArmour.LivingArmour;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
public abstract class LivingArmourUpgrade {
protected int level = 0; // Upgrade level 0 is the first upgrade. Upgrade
// goes from 0 to getMaxTier() - 1.
/**
* The LivingArmourUpgrade must have a constructor that has a single integer
* parameter. Upgrades may have other constructors, but must have one of
* these.
*
* @param level
* The level of the upgrade
*/
public LivingArmourUpgrade(int level) {
this.level = level;
}
public int getUpgradeLevel()
{
return this.level;
}
public abstract String getUniqueIdentifier();
/**
*
* @return
*/
public abstract int getMaxTier();
public abstract int getCostOfUpgrade();
public void onTick(World world, EntityPlayer player, LivingArmour livingArmour){}
public Multimap<String, AttributeModifier> getAttributeModifiers() {
return HashMultimap.<String, AttributeModifier> create();
}
public abstract void writeToNBT(NBTTagCompound tag);
public abstract void readFromNBT(NBTTagCompound tag);
}

View file

@ -11,13 +11,31 @@ public abstract class StatTracker {
public abstract String getUniqueIdentifier();
/**
* When called the StatTracker should reset all of its data, including
* upgrades.
*/
public abstract void resetTracker();
public abstract void readFromNBT(NBTTagCompound tag);
public abstract void writeToNBT(NBTTagCompound tag);
public abstract void onTick(World world, EntityPlayer player, LivingArmour livingArmour);
/**
* Called each tick to update the tracker's information. Called in
* LivingArmour
*
* @param world
* World the player is in
* @param player
* The player that has the armour equipped
* @param livingArmour
* The equipped LivingArmour
* @return True if there is a new upgrade unlocked this tick.
*/
public abstract boolean onTick(World world, EntityPlayer player, LivingArmour livingArmour);
public abstract LivingArmourUpgrade[] getUpgrades();
public final boolean isDirty() {
return isDirty;

View file

@ -1,12 +0,0 @@
package WayofTime.bloodmagic.api.livingArmour;
import java.util.ArrayList;
import java.util.List;
public class StatTrackerRegistry {
public static List<Class<? extends StatTracker>> trackers = new ArrayList();
public static void registerStatTracker(Class<? extends StatTracker> tracker) {
trackers.add(tracker);
}
}