Hello guys, I have a custom zone and whenever you die in that zone it shows you the timer when you will respawn and after timer reaches 0 it respawns you.This was my first try:
public void onDieInside(L2Character character)
{
if(character instanceof L2PcInstance){
final L2PcInstance player = (L2PcInstance) character;
countdown = 5;
ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new Runnable(){
@Override
public void run(){
if(player.isDead()){
if(countdown >0){
SystemMessage smg = SystemMessage.getSystemMessage(SystemMessageId.RESPAWN_AFTER_S1_SECONDS);
smg.addString(String.valueOf(countdown));
player.sendPacket(SystemMessageId.RESPAWN_AFTER_S1_SECONDS);
countdown--;
}else if(countdown == 0){
player.doRevive();
int x = 174132 + (int)(Math.random() * ((179862 - 174132) + 1));
int y = 112225 + (int)(Math.random() * ((117715 - 112225) + 1));
int z = -7708;
Location loc = new Location(x, y, z);
player.teleToLocation(loc,0);
}
}
}
}
,0,1000);
}
}
It works fine for first time,but then it just get shorter and shorter.I believe it's because thread doesn't stop working after respawning player,so whenever a player dies again the countdown is set to 5,but there is 2threads running and subtracting 1from countdown and then threads just keep increasing. My second try was this method:
public void onDieInside(L2Character character)
{
if(character instanceof L2PcInstance){
final L2PcInstance player = (L2PcInstance) character;
countdown = 5;
while(countdown > 0 && player.isDead()){
ThreadPoolManager.getInstance().scheduleGeneral(new showCountdown(countdown,player), 6000-countdown*1000);
countdown--;
}
ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
@Override
public void run(){
if(player.isDead()){
player.doRevive();
int x = 174132 + (int)(Math.random() * ((179862 - 174132) + 1));
int y = 112225 + (int)(Math.random() * ((117715 - 112225) + 1));
int z = -7708;
Location loc = new Location(x, y, z);
player.teleToLocation(loc,0);
}
}
}, 6000);
}
}
This is showCountdown class:
class showCountdown implements Runnable{
int timer = 0;
L2PcInstance player;
public showCountdown(int countdown,L2PcInstance p){
timer = countdown;
player = p;
}
@Override
public void run(){
SystemMessage smg = SystemMessage.getSystemMessage(SystemMessageId.RESPAWN_AFTER_S1_SECONDS);
smg.addNumber(timer);
player.sendPacket(smg);
}
}
It works just fine,but it seems dumb just to create so many Threads for a simple countdown.
When I have free time, I will update it for version 409.
I might also make some changes, such as allowing you to choose whether you want the berserker at level 1 or 2.
The sale of the pack is valid until 12/31/2024.
The share is prohibited at any time, in any form.
This pack will never be shared by me personally because too many people work on it.
If you bought or will buy the pack not from me, do not expect any claims regarding functionality, answers to questions, help, etc., do not even write))
Question
StealthyS4m
Hello guys,
I have a custom zone and whenever you die in that zone it shows you the timer when you will respawn and after timer reaches 0 it respawns you.This was my first try:
It works fine for first time,but then it just get shorter and shorter.I believe it's because thread doesn't stop working after respawning player,so whenever a player dies again the countdown is set to 5,but there is 2threads running and subtracting 1from countdown and then threads just keep increasing.
My second try was this method:
This is showCountdown class:
It works just fine,but it seems dumb just to create so many Threads for a simple countdown.
Edited by StealthyS4m15 answers to this question
Recommended Posts