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.
⏳ L2Elixir Open Beta goes live in less than 4 hours!
This Saturday, November 15th at 21:00 (UTC +2), the gates open for our biggest testing phase!
🔥 Don’t miss the first 30 minutes — exclusive rewards await!
A special NPC, “The Judge”, will appear in Giran, offering unique bonuses to early participants:
🏅 Open Beta Rewards:
- The first 2 players who talk to The Judge → Premium Account for Launch
- Another 2 random players who interact → Premium Account
- Everyone who speaks to the NPC within the first 30 minutes → Legendary Starter Pack for all characters on launch day (Nov 28th, 2025)
📌 The NPC will spawn exactly at 21:00 (UTC+2).
⏱️ Follow the countdown on our website — the hype is real!
Create your account & download the Updater to be ready!
🔗 https://l2elixir.org/connect/
💬 Discord: https://discord.gg/5ydPHvhbxs
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:
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.
Edited by StealthyS4m15 answers to this question
Recommended Posts