A lot of people have wanted this, so I'll release a snippet of how mine works on my own personal server, which is running on the revision 667. This is not complete, and is only a snippet. There are many changes that you would have to make in order to ensure full functionality.
Code:
package org.nova.kshan.content.interfaces;
import org.nova.Constants;
import org.nova.cache.loaders.WidgetDef;
import org.nova.game.entity.Entity;
import org.nova.game.npc.NPC;
import org.nova.game.player.Player;
/**
* Handles the overlay of the OSBuddy Health Bar.
*
* @author K-Shan
*
*/
public class OSBuddyHealthBar {
/**
* Represents the widget ID which contains the OSBuddy health bar.
*/
public static final int WIDGET = 1156;
/**
*
* @param player
* @param entity
* @param hp
*/
public static void showBar(Player player, Entity entity, int hp, int damage) {
if(entity != null && entity instanceof Player)
showPlayerHealth(player, ((Player) entity),damage);
else
showNPCHealth(player, ((NPC) entity), damage);
if(hp < 1) {
player.doAfterDelay(1.5, () -> { closeBar(player); });
player.getData().getRuntimeData().remove("osbuddy_health");
player.getData().getRuntimeData().remove("osbuddy_last_hp");
return;
}
}
/**
*
* @param p
* @param npc
* @param hp
*/
public static void showNPCHealth(Player p, NPC npc, int damage) {
if (!p.interfaces().containsInterface(WIDGET))
p.interfaces().sendTab(2, WIDGET);
p.packets().sendString(npc.getName(), WIDGET, 3);
p.packets().sendString(npc.getHitpoints()+"/"+npc.getMaxHitpoints(), WIDGET, 7);
double calculated = 161.0 * ((double) npc.getHitpoints() / npc.getMaxHitpoints());
int total = (int) Math.floor(calculated);
if(Constants.DEVELOPER_MODE && p.getRights() > 1)
p.sm("Percentage of health bar: "+calculated+": To display: "+total);
int green = 6;
int yellow = 5;
WidgetDef greenBar = WidgetDef.getComponent(WIDGET, green);
WidgetDef yellowBar = WidgetDef.getComponent(WIDGET, yellow);
p.packets().changeWCSize(WIDGET, green, npc.getHitpoints() < 1 ? 0 : total, greenBar.baseHeight);
// add animation here
p.doAfterDelay(0.600, () -> {
p.packets().changeWCSize(WIDGET, yellow, npc.getHitpoints() < 1 ? 0 : total, yellowBar.baseHeight);
});
}
/**
*
* @param p
* @param other
* @param hp
*/
private static void showPlayerHealth(Player p, Player other, int damage) {
if (!p.interfaces().containsInterface(WIDGET))
p.interfaces().sendTab(2, WIDGET);
p.packets().sendString(other.getDisplayName(), WIDGET, 3);
p.packets().sendString(other.getHitpoints()+"/"+other.getMaxHitpoints(), WIDGET, 7);
double calculated = 161.0 * ((double) other.getHitpoints() / other.getMaxHitpoints());
int total = (int) Math.floor(calculated);
if(Constants.DEVELOPER_MODE && p.getRights() > 1)
p.sm("Percentage of health bar: "+calculated+": To display: "+total);
int green = 6;
int yellow = 5;
WidgetDef greenBar = WidgetDef.getComponent(WIDGET, green);
WidgetDef yellowBar = WidgetDef.getComponent(WIDGET, yellow);
p.packets().changeWCSize(WIDGET, green, other.getHitpoints() < 1 ? 0 : total, greenBar.baseHeight);
// add animation here
p.doAfterDelay(0.600, () -> {
p.packets().changeWCSize(WIDGET, yellow, other.getHitpoints() < 1 ? 0 : total, yellowBar.baseHeight);
});
}
/**
*
* @param player
*/
private static void closeBar(Player player) {
player.packets().closeInterface(2);
}
}
... where:
"doAfterDelay" executes whatever is inside of the curly braces after the specified delay, in seconds.
If you have any questions, feel free to ask.