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);
}
}

View file

@ -6,15 +6,35 @@ import java.util.Map.Entry;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import WayofTime.bloodmagic.api.livingArmour.LivingArmourHandler;
import WayofTime.bloodmagic.api.livingArmour.LivingArmourUpgrade;
import WayofTime.bloodmagic.api.livingArmour.StatTracker;
import WayofTime.bloodmagic.api.livingArmour.StatTrackerRegistry;
public class LivingArmour {
public HashMap<String, StatTracker> trackerMap = new HashMap();
public HashMap<String, LivingArmourUpgrade> upgradeMap = new HashMap();
/**
* Ticks the upgrades and stat trackers, passing in the world and player as
* well as the LivingArmour
*
* @param world
* @param player
*/
public void onTick(World world, EntityPlayer player) {
for (Entry<String, LivingArmourUpgrade> entry : upgradeMap.entrySet()) {
LivingArmourUpgrade upgrade = entry.getValue();
if (upgrade == null) {
continue;
}
upgrade.onTick(world, player, this);
}
for (Entry<String, StatTracker> entry : trackerMap.entrySet()) {
StatTracker tracker = entry.getValue();
@ -27,7 +47,18 @@ public class LivingArmour {
}
public void readFromNBT(NBTTagCompound tag) {
for (Class<? extends StatTracker> clazz : StatTrackerRegistry.trackers) {
NBTTagList upgradeTags = tag.getTagList("upgrades", 10);
if (upgradeTags != null) {
for (int i = 0; i < upgradeTags.tagCount(); i++) {
NBTTagCompound upgradeTag = upgradeTags.getCompoundTagAt(i);
String key = upgradeTag.getString("key");
int level = upgradeTag.getInteger("level");
NBTTagCompound nbtTag = upgradeTag.getCompoundTag("upgrade");
LivingArmourHandler.generateUpgradeFromKey(key, level, nbtTag);
}
}
for (Class<? extends StatTracker> clazz : LivingArmourHandler.trackers) {
try {
Constructor<?> ctor = clazz.getConstructor();
Object obj = ctor.newInstance();
@ -48,6 +79,24 @@ public class LivingArmour {
}
public void writeToNBT(NBTTagCompound tag, boolean forceWrite) {
NBTTagList tags = new NBTTagList();
for (Entry<String, LivingArmourUpgrade> entry : upgradeMap.entrySet()) {
NBTTagCompound upgradeTag = new NBTTagCompound();
LivingArmourUpgrade upgrade = entry.getValue();
NBTTagCompound nbtTag = new NBTTagCompound();
upgrade.writeToNBT(nbtTag);
upgradeTag.setString("key", upgrade.getUniqueIdentifier());
upgradeTag.setInteger("level", upgrade.getUpgradeLevel());
upgradeTag.setTag("upgrade", nbtTag);
tags.appendTag(upgradeTag);
}
tag.setTag("upgrades", tags);
for (Entry<String, StatTracker> entry : trackerMap.entrySet()) {
StatTracker tracker = entry.getValue();