Description: This tutorial will show you how to process an NPC's HP before sending it to the client, so it shows what percentage of HP they have remaining, instead of looping in lots of around 250. Difficulty: Not even 2/10
Assumed Knowledge: How to read, how to Copy and Paste.
Tested Server: shockerscape
Files/Classes Modified: NPC.java, misc.java
Procedure
Step 1 - Find your collection of Miscellaneous Methods:
Open up your misc.java. On the first or second line you will see the class declaration:
Step 2 - Add the method for working out the HP Percentage:
Insert the following below the class declaration shown above.
Code
public static int getCurrentHP(int i, int i1, int i2) {
double x = (double)i / (double)i1;
return (int)Math.round(x*i2);
}
It should now look like this:
Code
public class misc {
public static int getCurrentHP(int i, int i1, int i2) {
double x = (double)i / (double)i1;
return (int)Math.round(x*i2);
}
Save and close the file.
Step 3 - Find the Append Hit Update Method in your NPC Class:
Open npc.java. Scroll down until you see (or search for):
Code
protected void appendHitUpdate(stream str) {
Scroll down through the method until you see the following:
Code
str.writeByteS(HP); // Their current hp, for HP bar
str.writeByteC(MaxHP); // Their max hp, for HP bar
Replace that with:
[code]//HP Bar Fix by shockz
str.writeByteS(misc.getCurrentHP(HP, MaxHP, 100));
str.writeByteC(100);[code]
NOTE: Be careful not to scroll into 'appendHitUpdate2()', as it uses different methods to send data and may cause client crashes if incorrectly modified
Step 4 - Save, Compile, and (re)start your Server:
I'm not going to tell you how to Save, COmpile, and restart. You can figure that out yourself.
Overview
Basically, before this fix, any NPC with HP over around 255 will appear to have its HP bar refilled when it hits 0. Now, Your server will send the percentage of its HP.
For example - If an NPC had 270/500 HP left - which is around 55% - It will show the HP as 55/100 instead of 270/550, which looks exactly as it should (Just over half full), and stops the HP bar jumping back to full when the NPCs hp drops below around 255.
If you use, or even just read this tutorial - Please comment.
Credits: 100% to Myself shockz.