Page 1 of 2

[HELP] Thread.sleep(5000);

Posted: Fri Apr 22, 2011 11:18 am
by valdaron
L2J Revision Number: 4566
L2JDP Revision Number: 7981

Hello.

Why this script lag server? My character etc. After 5 seconds i can move finally. Before it's mean with Gracia Final it wasn't a problem. Now when I want delay something I get lag. Anybody knows what's happend?

Code: Select all

try{Thread.sleep(5000);}catch (Exception e){}
PS Sorry for my bad english...

Re: [HELP]

Posted: Fri Apr 22, 2011 11:19 am
by jurchiks
... where do you put that?
It makes a thread stop doing anything for 5 seconds, it's not lag.

Re: [HELP]

Posted: Fri Apr 22, 2011 11:23 am
by valdaron
I want to put between NPC conversation for example:

Code: Select all

target.broadcastPacket(new CreatureSay(target.getObjectId(), Say2.ALL, target.getName(),"Hello"));try{     Thread.sleep(5000); // 5 sek}catch (InterruptedException e1){}//and after 5 seconds npc saying again something...target.broadcastPacket(new CreatureSay(target.getObjectId(), Say2.ALL, target.getName(),"I said... Hello!!!"));
Before this code doesn't block my character.

Re: [HELP]

Posted: Fri Apr 22, 2011 11:38 am
by jurchiks
well, you can schedule an event that after 5 seconds sends the second packet...
Thread.sleep is not a good choice in that place, you have to return something in that event.
That is, assuming you're putting this in an event script (extends Quest).

Re: [HELP]

Posted: Fri Apr 22, 2011 11:48 am
by valdaron
So what I have to do to delay NPC conversation. We have only thread.sleep or something else?

Like I said I have a lot scripts with thread.sleep I had live server and everything was OK. I had musicians playing music with 6 threads.sleep about 5 minut delay in never ending loop (while (true)) and no lag, no problems.

Now only one thread with 5 sec delay and all stops... Weird...

Re: [HELP] Thread.sleep(5000);

Posted: Fri Apr 22, 2011 12:17 pm
by jurchiks
are those scripts in core or are they "extends Quest"???
In any case, you can try to schedule the events using ThreadPoolManager.

Re: [HELP] Thread.sleep(5000);

Posted: Fri Apr 22, 2011 12:38 pm
by valdaron
Script was in L2Npc.java but now all scripts like support blessing are in DP handlers. So I made new java file with public boolean useBypass(String command, L2PcInstance player, L2Character target) and I paste my script without any errors and warning in gameserver window. All script works but thread.sleep freeze my char on 5 sek.

Re: [HELP] Thread.sleep(5000);

Posted: Fri Apr 22, 2011 12:48 pm
by jurchiks
most probably because useBypass call expects an answer before continuing doing its stuff, i.e. it's not just blabla.useBypass, it's if (blabla.useBypass()).

Re: [HELP] Thread.sleep(5000);

Posted: Fri Apr 22, 2011 1:19 pm
by valdaron
Ok. So far i don't know what to do... I'm going to look for similar case in l2j java scripts... :|

Thanks jurchiks for help.

Re: [HELP] Thread.sleep(5000);

Posted: Wed Apr 27, 2011 10:40 am
by valdaron
I've tested with people script with thread.sleep and i observed that freeze only character who activate link. Not all world.
Maybe there is a way to focus this thread on NPC not on character.
Any Idea?

Code: Select all

target.broadcastPacket(new CreatureSay(target.getObjectId(), Say2.ALL, target.getName(),"Hello"));------HERE I NEED SOMETHING TO FOCUS THIS ALL THREAD ON NPC------try{     Thread.sleep(5000); // 5 sek}catch (InterruptedException e1){}//and after 5 seconds npc saying again something...target.broadcastPacket(new CreatureSay(target.getObjectId(), Say2.ALL, target.getName(),"I said... Hello!!!"));
If I'm wrong... tell me. :)

Re: [HELP] Thread.sleep(5000);

Posted: Wed Apr 27, 2011 11:31 am
by jurchiks
thread.sleep freezes the thread, it does not make it do "something else". Figure out what you need/want to do first.

Re: [HELP] Thread.sleep(5000);

Posted: Wed Apr 27, 2011 11:40 am
by valdaron
I want to freeze NPC not character. NPC saying words, not me. I push the button in chat window to start script. The rest of the script should concern NPC.

Re: [HELP] Thread.sleep(5000);

Posted: Wed Apr 27, 2011 11:46 am
by jurchiks
I think the character is not allowed to do anything until the bypass is being processed.
If you want a delayed action, use threadpool and a private method.
Also, if the player is not saying it, then who is "target"? Naming the NPC as "target" makes me think it could change at any given moment...

Re: [HELP] Thread.sleep(5000);

Posted: Wed Apr 27, 2011 1:17 pm
by plim
Using the thread.sleep method is not a good option... Playing with threads causes these problems you've described, and there is no clean way to solve it. It's much better to use a runnable class like someone has already said. Here you have an example of what you should do to make it work.

Code: Select all

... //Send the first packet normallytarget.broadcastPacket(new CreatureSay(target.getObjectId(), Say2.ALL, target.getName(),"Hello")); //Schedule the next CreatureSay after 5 secsThreadPoolManager.getInstance().scheduleGeneral(new NpcTalk(new CreatureSay(target.getObjectId(), Say2.ALL, target.getName(),"I said... Hello!!!"), target), 5000); ... //Runnable class that implements what you needprivate class NpcTalk implements Runnable{    private CreatureSay _packet;    private L2PcInstance _target;     public NpcTalk(CreatureSay cs, L2PcInstance target)    {        _packet = cs;        _target = target;    }     @Override    public void run()    {        _target.broadcastPacket(new CreatureSay(_target.getObjectId(), Say2.ALL, _target.getName(),"Hello"));    }    }

Re: [HELP] Thread.sleep(5000);

Posted: Wed Apr 27, 2011 1:29 pm
by valdaron
This is a code of my script.
Target is NPC whos saying Hello.

Code: Select all

 public boolean useBypass(String command, L2PcInstance player, L2Character target)    {        if(player.getInventory().getItemByItemId(1700) != null)        {            try            {                target = (L2Npc) player.getTarget();                target.broadcastPacket(new CreatureSay(target.getObjectId(), Say2.ALL, target.getName(),"Hello"));                Thread.sleep(5000);//5 sec                target.broadcastPacket(new CreatureSay(target.getObjectId(), Say2.ALL, target.getName(),"I said... Hello!!!"));                        }            catch (Exception e)            {                _log.warning("error: " + e);            }               }               return false;               }                public String[] getBypassList(){    return COMMANDS;}