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.
L2LIVE.PRO- Dynamic Mid-rates Essence Seven Signs
GRAND OPENING - July 5, 20:00 GMT+3 (EEST)
TEST SERVER IS OPEN - COME AND CHECK IT OUT TODAY!
Join our community and be part of it at:
https://www.l2live.pro
https://discord.gg/k3NMgR4Dmu
Server description
* EXP/SP: Dynamic (x1- x100 based on your level, *before* Sayha and EXP buffs
* Adena: x50 / Item Drop: x10 / Fishing EXP increased / Attribute EXP increased
* Simplified gameplay to stay in the loop while not spending hours and hours farming
* Starter Pack containing very useful items for beginners
* MP replenishing potions with auto-consumption
* No overpowered donations
L2LIVE shop
* All spellbook coupons, pet spellbook coupons and master books are sold via Game Assistant
* Additionally you can buy SP pouches, enchanted talismans, pet training guides and various other consumables for Adena and L-Coin
* More items such as cloaks, more talismans, agathions, belts, pendants, enchantment scrolls of various grades, evolution stones, etc will be added! Shop server as a shortcut, and all retail-like ways of earning items are still here!
L-Coins
* Drops with small change and in random amounts from Lv60+ monsters
* All raidbosses drop random amount of L-Coin Pouches generating up to 420 Lcoin per unit.
**Grand Olympiad and Events**
* Grand Olympiad is held week day
* Format is 1v1, unlimited weekly fights
* Heroes are declared weekly at Sunday
* There are three automated events - TvT, CTF and Deathmatch, running at evenings
* Orc Fortress, Battle with Balok, Keber Hunter, Archievements Box, Daily Gift Calendar provisional events are active too
Custom user commands
* .offlineplay command, your character will keep playing till death or server restart
* .offlineshop command, keeps your shop sitting until all items are purchased
* .apon / .apoff - enable/disable HP/MP autoconsume
And lots of other small improvements are waiting for you!
Join our community and be part of it at:
https://www.l2live.pro
https://discord.gg/k3NMgR4Dmu
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 StealthyS4mLink to comment
Share on other sites
15 answers to this question
Recommended Posts