Number unexpectedly changing to 0

Support for the latest build of L2J Server, get help here with installations, upgrades, problems.
Do not post bugs reports here, use viewforum.php?f=77 instead.
There is no support for other server builds than the official provided by l2jserver.com
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
TotalChaos
Posts: 4
Joined: Wed Jun 13, 2007 5:54 am

Number unexpectedly changing to 0

Post by TotalChaos »

If you want to receive support we need this info to help you properly.
» Find Revision
L2J Revision 3208:
L2JDP Revision 6295:

Hey all, I'm hoping this is the right place to post this question.

I'm experimenting a bit with titles. Essentially, the title shows the player's HP as a percent (currenthp/maxhp). It's setup to update every time the player's hp changes. My problem is, as the player is receiving damage, the number in the title always becomes 0, and only when they're receiving damage. Here's the code (my addition noted from between "//mod" and "//end mod"):

net.sf.l2j.gameserver.model.actor.status.CharStatus.java

Code: Select all

 public final void setCurrentHp(double newHp, boolean broadcastPacket)    {        // Get the Max HP of the L2Character        double maxHp = getActiveChar().getStat().getMaxHp();         synchronized (this)        {            if (getActiveChar().isDead()) return;        	if (newHp >= maxHp)            {                // Set the RegenActive flag to false                _currentHp = maxHp;                _flagsRegenActive &= ~REGEN_FLAG_HP;                 // Stop the HP/MP/CP Regeneration task                if (_flagsRegenActive == 0)                    stopHpMpRegeneration();            }            else            {                // Set the RegenActive flag to true                _currentHp = newHp;                _flagsRegenActive |= REGEN_FLAG_HP;                 // Start the HP/MP/CP Regeneration task with Medium priority                startHpMpRegeneration();            }        }         if (getActiveChar() instanceof L2PcInstance)        {               //Mod               double percent = (newHp/maxHp)*100;        	getActiveChar().sendMessage("DEBUGGING: percent - " + (int) percent);        	getActiveChar().setTitle("» " + (int) percent + "% «");        	Broadcast.toSelfAndKnownPlayers(getActiveChar(), new UserInfo((L2PcInstance) getActiveChar()));               //End mod        	if (getCurrentHp() <= maxHp * .3)            {                QuestState qs = ((L2PcInstance) getActiveChar()).getQuestState("255_Tutorial");                if (qs != null)                    qs.getQuest().notifyEvent("CE45", null, ((L2PcInstance) getActiveChar()));            }        }         // Send the Server->Client packet StatusUpdate with current HP and MP to all other L2PcInstance to inform        if (broadcastPacket)            getActiveChar().broadcastStatusUpdate();    } 
Now here's the weird part... The number displayed from sendMessage is ALWAYS correct and never 0, while at the same exact time the number displayed in the title IS 0.. But ONLY when its updated due to the character receiving damage..

I've tried not casting "percent" to int and making an int variable assigning it the int casted value of "percent" and using that variable for the title in place of "percent" but had the same results.

I've been stuck on this for hours, any help would be appreciated! :wink:

- Austin
Thunderb0lt
Posts: 69
Joined: Wed Apr 22, 2009 5:32 pm

Re: Number unexpectedly changing to 0

Post by Thunderb0lt »

I think this is because the result of newHP/maxHP will be double but 100 is int. So Java will implicit cast the result of newHP/maxHP to int discarding the fractional part. So if the player receives damage the quotient will be less then 1 (eg. 0.99). If this is casted to int it will be 0 and 0 times 100 is still 0.

Try this:

Code: Select all

double percent = (newHp/maxHp)*100[b].[/b];
This will force Java to handle 100 as double and percent being calculated correctly.
TotalChaos
Posts: 4
Joined: Wed Jun 13, 2007 5:54 am

Re: Number unexpectedly changing to 0

Post by TotalChaos »

Ahh yes, I had overlooked that (probably because it was 3am :P ) ... Thanks for pointing that out, I've changed the code.

However, it is still becoming 0 when the player is being attacked. The function I put my code in "setCurrentHp" appears to be the root function that ultimately handles all hp changes (as far as I've been able to figure). I know it is called by the hp/mp/cp regen task "RegenTask" and as my hp heals due to regeneration it updates fine, it is never zero. I thought maybe it had something to do with the fact the hp was being reduced not increased, but when I use hp consuming skills like body to mind it updates fine. So for the moment, I'm still stuck :?

Thankyou for the help though! :)

- Austin
TotalChaos
Posts: 4
Joined: Wed Jun 13, 2007 5:54 am

Re: Number unexpectedly changing to 0

Post by TotalChaos »

Sorry about the double post..

As a work around I've placed the same code in the L2PcInstance.java file in the function "reduceCurrentHp" and in "RegenTask" in CharStatus.java ... This works well enough, though I don't think it'll update the title for every change in hp, but its close enough.

So it seems it has something to do with being placed in "setCurrentHp" in CharStatus.java, if anyone has any other ideas on what it could be it would still be appreciated!

- Austin
_DS_
L2j Veteran
L2j Veteran
Posts: 3437
Joined: Wed Apr 30, 2008 8:53 am
Location: Russia

Re: Number unexpectedly changing to 0

Post by _DS_ »

double percent = ((double)newHp/maxHp)*100.;
Commiter of the shit
public static final int PI = 3.1415926535897932384626433832795;
Post Reply