[HELP] Thread.sleep(5000);

Support for the latest build of L2J Server, get help here with installations, upgrades, problems.
Do not post bugs reports here, use viewforum.php?f=77 instead.
There is no support for other server builds than the official provided by l2jserver.com
Forum rules
READ NOW: L2j Forums Rules of Conduct
User avatar
valdaron
Posts: 199
Joined: Mon Apr 18, 2011 3:11 pm
Location: Poland

[HELP] Thread.sleep(5000);

Post 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...
Last edited by valdaron on Fri Apr 22, 2011 11:55 am, edited 1 time in total.
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: [HELP]

Post by jurchiks »

... where do you put that?
It makes a thread stop doing anything for 5 seconds, it's not lag.
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
valdaron
Posts: 199
Joined: Mon Apr 18, 2011 3:11 pm
Location: Poland

Re: [HELP]

Post 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.
Last edited by valdaron on Fri Apr 22, 2011 2:43 pm, edited 1 time in total.
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: [HELP]

Post 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).
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
valdaron
Posts: 199
Joined: Mon Apr 18, 2011 3:11 pm
Location: Poland

Re: [HELP]

Post 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...
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

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

Post by jurchiks »

are those scripts in core or are they "extends Quest"???
In any case, you can try to schedule the events using ThreadPoolManager.
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
valdaron
Posts: 199
Joined: Mon Apr 18, 2011 3:11 pm
Location: Poland

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

Post 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.
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

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

Post 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()).
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
valdaron
Posts: 199
Joined: Mon Apr 18, 2011 3:11 pm
Location: Poland

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

Post 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.
User avatar
valdaron
Posts: 199
Joined: Mon Apr 18, 2011 3:11 pm
Location: Poland

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

Post 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. :)
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

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

Post by jurchiks »

thread.sleep freezes the thread, it does not make it do "something else". Figure out what you need/want to do first.
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
valdaron
Posts: 199
Joined: Mon Apr 18, 2011 3:11 pm
Location: Poland

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

Post 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.
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

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

Post 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...
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
plim
Posts: 226
Joined: Mon Sep 01, 2008 3:39 pm
Location: Catalunya, Barcelona

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

Post 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"));    }    }
Image
User avatar
valdaron
Posts: 199
Joined: Mon Apr 18, 2011 3:11 pm
Location: Poland

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

Post 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;} 
Post Reply