Converting Process() To Events
Purpose: Converting process() to events.
Difficulty: 2
Tested On: Palidino, but can be applied to any base with minimal changes
Credits: Mystic Flow for helping me learn, and Graham for creating the Event Manager.
In Palidino76>rs2>players>Player.java search for Process().
Inside your process, you will probably see a lot of junk like so:
Code:
if (deadItemDelay <= 0 && needItemDelay) {
NpcDrops ND = new NpcDrops();
ND.npcDrop(this);
needItemDelay = false;
}
if (disconnected[0]) {
if (pTrade.getPartner() != null) {
pTrade.declineTrade();
}
disconnected[1] = true;
} if (combatDelay > 0) {
combatDelay--;
}
if (npcDelay > 0) {
npcDelay--;
}
if (teleDelay > 0) {
teleDelay--;
}
if (isDead) {
deathDelay--;
applyDead();
}
if (ghostTimer > 0) {
ghostTimer--;
}
if (eatDelay > 0) {
eatDelay--;
}
if (magicDelay > 0) {
magicDelay--;
}
if (drinkDelay > 0) {
drinkDelay--;
}
if (skulledDelay > 0) {
skulledDelay--;
skulledUpdateReq = true;
}
if (jumpDelay > 0) {
jumpDelay--;
jumpUpdateReq = true;
}
if (freezeDelay > 0) {
freezeDelay--;
stopMovement(this);
} else if (freezeDelay == 0) {// your allowed to walk, what more you want.
}
if (runEnergyDelay > 0) {
runEnergyDelay--;
} else {
if (runEnergy < 100) {
runEnergy++;
runEnergyUpdateReq = true;
}
runEnergyDelay = 4;
}
What this code is doing is continually looping all this every 600ms. (Or whatever your timer might be)
Event Manager, created by Graham reduces lagg by taking these codes and placing them in line, then activating them when called. After the event is completed, it is stopped and returned to its position to be called again when requested.
So lets take this part in the process for example:
Code:
if (statRestoreDelay > 0) {
statRestoreDelay--;
}
Rather than placing this in the process(), we are going to create a void, like this:
Code:
public void statRestore(final Player p) {
EventManager.getSingleton().addEvent(2400, new Event() {
public void execute(EventContainer c) {
if (statRestoreDelay > 0) {
statRestoreDelay--;
} else {
c.stop();
}
}
});
}
So here is how it works:
EventManager.getSingleton().addEvent(2400, new Event() { adds this event to te event manager, to prepare it for use.
public void execute(EventContainer c) { The content inside that void (if (statRestoreDelay > 0) {) Tells the event manager what needs to be done when the void is called for.
And finally, c.stop(); tells the event manager that the execution of the event is done, and can be stopped.
There! You can now convert all your process() items into events! This will drastically help your server to reduce the lagg.
Remember to add your import to player.java also :D
Code:
import palidino76.rs2.EventManager.*;