Added a custom renderer for health bars

This commit is contained in:
Thomas Guimbretiere committed 2015-01-05 15:27:55 +01:00
1 parent a05c6aa6d7
commit 8483573146
3 files changed
+76 -27

No files matched your search

@@ -6,6 +6,7 @@ import mcp.mobius.waila.gui.truetyper.TrueTypeFont;
import mcp.mobius.waila.handlers.HUDHandlerBlocks;
import mcp.mobius.waila.handlers.HUDHandlerEntities;
import mcp.mobius.waila.handlers.VanillaTooltipHandler;
import mcp.mobius.waila.overlay.tooltiprenderers.TTRenderHealth;
//import mcp.mobius.waila.handlers.SummaryProviderDefault;
import mcp.mobius.waila.server.ProxyServer;
import net.minecraft.block.Block;
@@ -52,7 +53,9 @@ public class ProxyClient extends ProxyServer {
//ModuleRegistrar.instance().registerShortDataProvider(new SummaryProviderDefault(), Item.class);
ModuleRegistrar.instance().addConfig("General", "general.showhp");
ModuleRegistrar.instance().addConfig("General", "general.showcrop");
ModuleRegistrar.instance().addConfig("General", "general.showcrop");
ModuleRegistrar.instance().registerTooltipRenderer("waila.health", new TTRenderHealth());
}
@Override
@@ -52,32 +52,7 @@ public class HUDHandlerEntities implements IWailaEntityProvider {
currenttip.add(String.format("HP : " + WHITE + "%.0f" + GRAY + " / " + WHITE + "%.0f", ((EntityLivingBase)entity).getHealth(), ((EntityLivingBase)entity).getMaxHealth()));
else{
for (int i = 0; i < MathHelper.floor_float(health); i++){
hptip += HEART;
if(hptip.length() % (nhearts * HEART.length()) == 0){
currenttip.add(hptip);
hptip = "";
}
}
if (((EntityLivingBase)entity).getHealth() > MathHelper.floor_float(health) * 2.0f){
hptip += HHEART;
if(hptip.length() % (nhearts * HEART.length()) == 0){
currenttip.add(hptip);
hptip = "";
}
}
for (int i = 0; i < MathHelper.floor_float(maxhp - health); i++){
hptip += EHEART;
if(hptip.length() % (nhearts * HEART.length()) == 0){
currenttip.add(hptip);
hptip = "";
}
}
if (!hptip.equals(""))
currenttip.add(hptip);
currenttip.add(getRenderString("waila.health", String.valueOf(nhearts), String.valueOf(health), String.valueOf(maxhp)));
}
}
@@ -0,0 +1,71 @@
package mcp.mobius.waila.overlay.tooltiprenderers;
import static mcp.mobius.waila.api.SpecialChars.WailaIcon;
import static mcp.mobius.waila.api.SpecialChars.WailaStyle;
import java.awt.Dimension;
import net.minecraft.util.MathHelper;
import mcp.mobius.waila.api.IWailaCommonAccessor;
import mcp.mobius.waila.api.IWailaTooltipRenderer;
import mcp.mobius.waila.overlay.IconUI;
import mcp.mobius.waila.overlay.OverlayRenderer;
/**
* Custom renderer for health bars.
* Syntax : {waila.health, nheartperline, health, maxhealth}
*
*/
public class TTRenderHealth implements IWailaTooltipRenderer {
@Override
public Dimension getSize(String[] params, IWailaCommonAccessor accessor) {
float maxhearts = Float.valueOf(params[0]);
float health = Float.valueOf(params[1]);
float maxhealth = Float.valueOf(params[2]);
int heartsPerLine = (int)(Math.min(maxhearts, Math.ceil(maxhealth)));
int nlines = (int)(Math.ceil(maxhealth/maxhearts));
return new Dimension(8 * heartsPerLine, 10 * nlines - 2);
}
@Override
public void draw(String[] params, IWailaCommonAccessor accessor) {
float maxhearts = Float.valueOf(params[0]);
float health = Float.valueOf(params[1]);
float maxhealth = Float.valueOf(params[2]);
int nhearts = MathHelper.ceiling_float_int(maxhealth);
int heartsPerLine = (int)(Math.min(maxhearts, Math.ceil(maxhealth)));
int offsetX = 0;
int offsetY = 0;
for (int iheart = 1; iheart <= nhearts; iheart++){
if (iheart <= MathHelper.floor_float(health)){
OverlayRenderer.renderIcon(offsetX, offsetY, 8, 8, IconUI.HEART);
offsetX += 8;
}
if ((iheart > health) && (iheart < health + 1)){
OverlayRenderer.renderIcon(offsetX, offsetY, 8, 8, IconUI.HHEART);
offsetX += 8;
}
if (iheart >= health + 1){
OverlayRenderer.renderIcon(offsetX, offsetY, 8, 8, IconUI.EHEART);
offsetX += 8;
}
if (iheart%heartsPerLine == 0){
offsetY += 10;
offsetX = 0;
}
}
}
}