Modularized stats

This commit is contained in:
Adubbz 2013-12-10 10:22:24 +11:00
parent 71f5ab9758
commit 6a1bb5fc22
10 changed files with 211 additions and 87 deletions

View file

@ -1,6 +1,7 @@
package tan;
import net.minecraftforge.common.MinecraftForge;
import tan.core.TANPlayerStats;
import tan.handler.ConnectionHandler;
import tan.handler.RenderOverlayEventHandler;
import tan.handler.TickHandlerServer;
@ -30,7 +31,7 @@ public class ToughAsNails
@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
TANPlayerStats.init();
}
@EventHandler

View file

@ -0,0 +1,37 @@
package tan.api;
import java.util.ArrayList;
import java.util.Collection;
public class PlayerStatRegistry
{
public static ArrayList<TANStat> tanStatList = new ArrayList<TANStat>();
public static void registerStat(TANStat stat)
{
tanStatList.add(stat);
}
public static <Type extends TANStat> String getStatName(Class<Type> clazz)
{
return getStatObject(clazz).getStatName();
}
public static <Type extends TANStat> TANStat getStatObject(Class<Type> clazz)
{
return (TANStat)findObjectOfType(tanStatList, clazz);
}
public static <Type> Type findObjectOfType(Collection<?> arrayList, Class<Type> clazz)
{
for (Object object : arrayList)
{
if (object != null && object.getClass() == clazz)
{
return clazz.cast(object);
}
}
return null;
}
}

View file

@ -0,0 +1,46 @@
package tan.api;
import tan.network.PacketTypeHandler;
import tan.network.packet.PacketSendStats;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
public abstract class TANStat
{
public World world;
public EntityPlayerMP player;
public NBTTagCompound tanData;
public abstract void update();
public abstract void setDefaults();
public abstract String getStatName();
public static void updatePlayerData(NBTTagCompound tanData, EntityPlayerMP player)
{
player.getEntityData().setCompoundTag("ToughAsNails", tanData);
PacketDispatcher.sendPacketToPlayer(PacketTypeHandler.populatePacket(new PacketSendStats(tanData)), (Player)player);
}
public void setDefaultInt(String key, int value)
{
if (!tanData.hasKey(key))
{
tanData.setInteger(key, value);
updatePlayerData(tanData, player);
}
}
public void setDefaultFloat(String key, float value)
{
if (!tanData.hasKey(key))
{
tanData.setFloat(key, value);
updatePlayerData(tanData, player);
}
}
}

View file

@ -0,0 +1,14 @@
package tan.api;
public class TemperatureSource
{
public TemperatureSource(int id, int meta, float temperature, float rate)
{
}
public TemperatureSource(int id, float temperature, float rate)
{
}
}

View file

@ -1,66 +1,17 @@
package tan.core;
import java.util.HashMap;
import tan.network.PacketTypeHandler;
import tan.network.packet.PacketSendStats;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import tan.api.PlayerStatRegistry;
import tan.stats.TemperatureStat;
public class TANPlayerStats
{
public static void update(World world, EntityPlayerMP player)
public static void init()
{
NBTTagCompound tanData = player.getEntityData().getCompoundTag("ToughAsNails");
setDefaults(tanData, player);
float originalTemperature = tanData.getFloat("Temp");
float temperature = originalTemperature;
if (world.rand.nextInt(25) == 0)
{
temperature--;
}
if (temperature != originalTemperature)
{
tanData.setFloat("Temp", MathHelper.clamp_float(temperature, -50F, 50F));
updatePlayerData(tanData, player);
}
registerStats();
}
public static void updatePlayerData(NBTTagCompound tanData, EntityPlayerMP player)
private static void registerStats()
{
player.getEntityData().setCompoundTag("ToughAsNails", tanData);
PacketDispatcher.sendPacketToPlayer(PacketTypeHandler.populatePacket(new PacketSendStats(tanData)), (Player)player);
}
public static void setDefaults(NBTTagCompound tanData, EntityPlayerMP player)
{
setDefaultFloat(tanData, player, "Temp", 20F);
}
private static void setDefaultInt(NBTTagCompound tanData, EntityPlayerMP player, String key, int value)
{
if (!tanData.hasKey(key))
{
tanData.setInteger(key, value);
updatePlayerData(tanData, player);
}
}
private static void setDefaultFloat(NBTTagCompound tanData, EntityPlayerMP player, String key, float value)
{
if (!tanData.hasKey(key))
{
tanData.setFloat(key, value);
updatePlayerData(tanData, player);
}
PlayerStatRegistry.registerStat(new TemperatureStat());
}
}

View file

@ -12,6 +12,8 @@ import net.minecraftforge.event.ForgeSubscribe;
import org.lwjgl.opengl.GL11;
import tan.api.PlayerStatRegistry;
import tan.stats.TemperatureStat;
import cpw.mods.fml.client.FMLClientHandler;
public class RenderOverlayEventHandler
@ -31,36 +33,42 @@ public class RenderOverlayEventHandler
{
NBTTagCompound tanData = minecraft.thePlayer.getEntityData().getCompoundTag("ToughAsNails");
int temperature = MathHelper.floor_float(tanData.getFloat("Temp"));
renderTemperature(scaledRes, minecraft, fontRenderer, tanData);
int temperatureXPos = scaledRes.getScaledWidth() / 2 - 8;
int temperatureYPos = scaledRes.getScaledHeight() - 52;
minecraft.mcProfiler.startSection("temperatureBall");
{
this.drawTexturedModalRect(temperatureXPos, temperatureYPos, 16, 0, 16, 16);
GL11.glColor4f(1.0F, 1.0F, 1.0F, temperature / 100F + 0.5F);
this.drawTexturedModalRect(temperatureXPos, temperatureYPos, 0, 0, 16, 16);
}
minecraft.mcProfiler.endSection();
minecraft.mcProfiler.startSection("temperatureLevel");
{
GL11.glPushMatrix();
{
String text = temperature + "°C";
GL11.glTranslatef((float)(temperatureXPos - (fontRenderer.getStringWidth(text) / 2) + 12), (float)(temperatureYPos + 6), 0.0F);
GL11.glScalef(0.65F, 0.65F, 0.0F);
drawStringWithBorder(fontRenderer, text, 0, 0, 0, 16777215);
}
GL11.glPopMatrix();
}
minecraft.mcProfiler.endSection();
}
bindTexture(new ResourceLocation("minecraft:textures/gui/icons.png"));
}
private void renderTemperature(ScaledResolution scaledRes, Minecraft minecraft, FontRenderer fontRenderer, NBTTagCompound tanData)
{
int temperature = MathHelper.floor_float(tanData.getFloat(PlayerStatRegistry.getStatName(TemperatureStat.class)));
int temperatureXPos = scaledRes.getScaledWidth() / 2 - 8;
int temperatureYPos = scaledRes.getScaledHeight() - 52;
minecraft.mcProfiler.startSection("temperatureBall");
{
this.drawTexturedModalRect(temperatureXPos, temperatureYPos, 16, 0, 16, 16);
GL11.glColor4f(1.0F, 1.0F, 1.0F, temperature / 100F + 0.5F);
this.drawTexturedModalRect(temperatureXPos, temperatureYPos, 0, 0, 16, 16);
}
minecraft.mcProfiler.endSection();
minecraft.mcProfiler.startSection("temperatureLevel");
{
GL11.glPushMatrix();
{
String text = temperature + "°C";
GL11.glTranslatef((float)(temperatureXPos - (fontRenderer.getStringWidth(text) / 2) + 12), (float)(temperatureYPos + 6), 0.0F);
GL11.glScalef(0.65F, 0.65F, 0.0F);
drawStringWithBorder(fontRenderer, text, 0, 0, 0, 16777215);
}
GL11.glPopMatrix();
}
}
public static void bindTexture(ResourceLocation resourceLocation)
{

View file

@ -0,0 +1,28 @@
package tan.handler;
import java.util.ArrayList;
import java.util.Collection;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import tan.api.TANStat;
import tan.api.PlayerStatRegistry;
public class TANPlayerStatHandler
{
public static void update(World world, EntityPlayerMP player)
{
NBTTagCompound tanData = player.getEntityData().getCompoundTag("ToughAsNails");
for (TANStat stat : PlayerStatRegistry.tanStatList)
{
stat.tanData = tanData;
stat.world = world;
stat.player = player;
stat.setDefaults();
stat.update();
}
}
}

View file

@ -2,7 +2,6 @@ package tan.handler;
import java.util.EnumSet;
import tan.core.TANPlayerStats;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
@ -38,6 +37,6 @@ public class TickHandlerServer implements ITickHandler
public static void onPlayerTick(World world, EntityPlayerMP player)
{
TANPlayerStats.update(world, player);
TANPlayerStatHandler.update(world, player);
}
}

View file

@ -7,7 +7,9 @@ import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import tan.api.PlayerStatRegistry;
import tan.network.PacketTypeHandler;
import tan.stats.TemperatureStat;
import cpw.mods.fml.common.network.Player;
public class PacketSendStats extends PacketTAN
@ -23,7 +25,7 @@ public class PacketSendStats extends PacketTAN
{
super(PacketTypeHandler.sendStats);
temperature = tanCompound.getFloat("Temp");
temperature = tanCompound.getFloat(PlayerStatRegistry.getStatName(TemperatureStat.class));
}
@Override
@ -45,9 +47,7 @@ public class PacketSendStats extends PacketTAN
NBTTagCompound tanCompound = entityPlayer.getEntityData().getCompoundTag("ToughAsNails");
tanCompound.setFloat("Temp", temperature);
System.out.println(temperature);
tanCompound.setFloat(PlayerStatRegistry.getStatName(TemperatureStat.class), temperature);
entityPlayer.getEntityData().setCompoundTag("ToughAsNails", tanCompound);
}

View file

@ -0,0 +1,40 @@
package tan.stats;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import tan.api.TANStat;
public class TemperatureStat extends TANStat
{
@Override
public void update()
{
float originalTemperature = tanData.getFloat(getStatName());
float temperature = originalTemperature;
if (world.rand.nextInt(25) == 0)
{
temperature--;
}
if (temperature != originalTemperature)
{
tanData.setFloat(getStatName(), MathHelper.clamp_float(temperature, -50F, 50F));
updatePlayerData(tanData, player);
}
}
@Override
public void setDefaults()
{
setDefaultFloat(getStatName(), 37F);
}
@Override
public String getStatName()
{
return "Temp";
}
}