Factions/src/com/massivecraft/factions/util/HealthBarUtil.java

70 lines
2.0 KiB
Java
Raw Normal View History

2012-03-25 21:07:11 +02:00
package com.massivecraft.factions.util;
import java.util.Map.Entry;
2013-04-09 13:15:25 +02:00
import com.massivecraft.factions.ConfServer;
2013-04-10 10:45:47 +02:00
import com.massivecraft.mcore.util.Txt;
2012-03-25 21:07:11 +02:00
public class HealthBarUtil
{
public static String getHealthbar(double healthQuota, int barLength)
{
// Ensure between 0 and 1;
healthQuota = fixQuota(healthQuota);
// What color is the health bar?
String color = getColorFromHealthQuota(healthQuota);
// how much solid should there be?
int solidCount = (int) Math.ceil(barLength * healthQuota);
// The rest is empty
2013-04-09 13:15:25 +02:00
int emptyCount = (int) ((barLength - solidCount) / ConfServer.spoutHealthBarSolidsPerEmpty);
2012-03-25 21:07:11 +02:00
// Create the non-parsed bar
2013-04-10 10:45:47 +02:00
String ret = ConfServer.spoutHealthBarLeft + Txt.repeat(ConfServer.spoutHealthBarSolid, solidCount) + ConfServer.spoutHealthBarBetween + Txt.repeat(ConfServer.spoutHealthBarEmpty, emptyCount) + ConfServer.spoutHealthBarRight;
2012-03-25 21:07:11 +02:00
// Replace color tag
ret = ret.replace("{c}", color);
// Parse amp color codes
2013-04-10 10:45:47 +02:00
ret = Txt.parse(ret);
2012-03-25 21:07:11 +02:00
return ret;
}
public static String getHealthbar(double healthQuota)
{
2013-04-09 13:15:25 +02:00
return getHealthbar(healthQuota, ConfServer.spoutHealthBarWidth);
2012-03-25 21:07:11 +02:00
}
public static double fixQuota(double healthQuota)
{
if (healthQuota > 1)
{
return 1d;
}
else if (healthQuota < 0)
{
return 0d;
}
return healthQuota;
}
public static String getColorFromHealthQuota(double healthQuota)
{
Double currentRoof = null;
String ret = null;
2013-04-09 13:15:25 +02:00
for (Entry<Double, String> entry : ConfServer.spoutHealthBarColorUnderQuota.entrySet())
2012-03-25 21:07:11 +02:00
{
double roof = entry.getKey();
String color = entry.getValue();
if (healthQuota <= roof && (currentRoof == null || roof <= currentRoof))
{
currentRoof = roof;
ret = color;
}
}
return ret;
}
}