Created framework for LivingArmour and the handling. Upgrade handling is not done.

This commit is contained in:
WayofTime 2015-12-02 21:30:54 -05:00
parent 42d6c4b59b
commit a3b12cb7e2
3 changed files with 129 additions and 0 deletions

View file

@ -0,0 +1,33 @@
package WayofTime.bloodmagic.api.livingArmour;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import WayofTime.bloodmagic.livingArmour.LivingArmour;
public abstract class StatTracker {
private boolean isDirty = false;
public abstract String getUniqueIdentifier();
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);
public final boolean isDirty() {
return isDirty;
}
public final void markDirty() {
this.isDirty = true;
}
public final void resetDirty() {
this.isDirty = false;
}
}

View file

@ -0,0 +1,12 @@
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

@ -0,0 +1,84 @@
package WayofTime.bloodmagic.livingArmour;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map.Entry;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import WayofTime.bloodmagic.api.livingArmour.StatTracker;
import WayofTime.bloodmagic.api.livingArmour.StatTrackerRegistry;
public class LivingArmour {
public HashMap<String, StatTracker> trackerMap = new HashMap();
public void onTick(World world, EntityPlayer player) {
for (Entry<String, StatTracker> entry : trackerMap.entrySet()) {
StatTracker tracker = entry.getValue();
if (tracker == null) {
continue;
}
tracker.onTick(world, player, this);
}
}
public void readFromNBT(NBTTagCompound tag) {
for (Class<? extends StatTracker> clazz : StatTrackerRegistry.trackers) {
try {
Constructor<?> ctor = clazz.getConstructor();
Object obj = ctor.newInstance();
if (!(obj instanceof StatTracker)) {
// ?????
}
StatTracker tracker = (StatTracker) obj;
String key = tracker.getUniqueIdentifier();
NBTTagCompound trackerTag = tag.getCompoundTag(key);
if (!trackerTag.hasNoTags()) {
tracker.readFromNBT(trackerTag);
}
trackerMap.put(key, tracker);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void writeToNBT(NBTTagCompound tag, boolean forceWrite) {
for (Entry<String, StatTracker> entry : trackerMap.entrySet()) {
StatTracker tracker = entry.getValue();
if (tracker == null) {
continue;
}
String key = tracker.getUniqueIdentifier();
if (forceWrite || tracker.isDirty()) {
NBTTagCompound trackerTag = new NBTTagCompound();
tracker.writeToNBT(trackerTag);
tag.setTag(key, trackerTag);
tracker.resetDirty();
}
}
}
/**
* Writes the LivingArmour to the NBTTag. This will only write the trackers
* that are dirty.
*
* @param tag
*/
public void writeDirtyToNBT(NBTTagCompound tag) {
writeToNBT(tag, false);
}
public void writeToNBT(NBTTagCompound tag) {
writeToNBT(tag, true);
}
}