• 0
Sign in to follow this  
StarSCreams

Help How to check the time has passed into switch kill in a row

Question

StarSCreams    0

hi,

I have this code and try to verify the time has passed when case occurred

Example in l2pcinstance:
 

private int assasins = 0;

switch(assasins) {
  case 2:
    Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed two times" + pk.getTarget().getName());
    break;
  case 3:
    Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed three times" + pk.getTarget().getName());
  default:
}

 

i need verify if the next kill (case 3) ocurred  < 10 seconds. if the time passed make a different announcements..

 

i think how see the code with my idea...

private int assasins = 0;
switch(assasins) {
  case 2:
    	Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed two times" + pk.getTarget().getName());
		ThreadPoolManager.getInstance().schedule(assasins = 3 , 10 * 1000);
    break;
  case 3:
    Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed" + pk.getTarget().getName());
		ThreadPoolManager.getInstance().schedule(assasins = 4 , 10 * 1000);
    break;
  default:
}

 

sorry my bad english :3

Share this post


Link to post
Share on other sites

34 answers to this question

  • 0
SweeTs    10

No no no. You have to create a new long variable, set the time using system current time + 10sec on kill, then on the switch compare the time if your variable > current time. 

Edited by SweeTs
  • Upvote 1

Share this post


Link to post
Share on other sites
  • 0
StarSCreams    0

thanks sweets i try this..

private int assasins = 0;
switch(assasins) {
	case 1:
	//Here obtain time when case 1 ocurred
	long firstkill = System.currentTimeMillis();
  case 2:
    	long checktime = System.currentTimeMillis() - firstkill;
		if(checktime < 10000) {
			Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed two times" + pk.getTarget().getName()); 
			} else {
				Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed" + pk.getTarget().getName());
		}
    break;
  case 3:
   .........
    break;
  default:
}

but i get an error, in case 2 "The local variable firstkill may not have been initialized"

How to fix this? i try the fix but not obtain solution hahaha thanks!

Share this post


Link to post
Share on other sites
  • 0
SweeTs    10

Put the long out of the method. Like the assassins, under it. 

Edited by SweeTs

Share this post


Link to post
Share on other sites
  • 0
StarSCreams    0

i tested and not work.. every kill obtain first case only, never update or increment the variable "assasins", i try implement this code in fandc files like mobius.. anye help me?

 

private int assasins = 0;

protected void doPKPVPManage(Creature....
                             
                             long firstkill = System.currentTimeMillis() + 10 * 1000;
                             switch(assasins) {
                               case 1:
                               Announcements.getInstance().announceToAll("Test First Kill VAR:" + assasins);
                               break;
                               case 2:
                               if(System.currentTimeMillis() - firstkill > 10000) {
                                 Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed" + pk.getTarget().getName());
                               } else { 
                                 Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed two times in a row!" + pk.getTarget().getName()); 
                               }
                               break;
                               case 3:
                               if(System.currentTimeMillis() - firstkill > 20000) {
                                 Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed" + pk.getTarget().getName()); 
                               } else { 
                                 Announcements.getInstance().announceToAll("Player " + pk.getName() + " has killed three times in a row!" + pk.getTarget().getName()); 
                               }
                               break;
                               case 4: 
                               ...... 
                               break; 
                               default: 
                             }

 

Out Put only first case

Edited by StarSCreams
add

Share this post


Link to post
Share on other sites
  • 0
melron    24

this is happening because the variable firstkill is inside the kill method. Put it after assasins... 

generally follow this :

private long firstkill = 0L;

 

the firstkill variable must be used after the end of the switch.

switch(something)
{
	case 1:
		...
	brake;
	case 2:
		...
	break;
}

firstkill = System.currentTimeMillis() + (10*1000); //or instantly 10000 

the only thing you have to do is to check the the current time - firstkill  is > 10000 then 10 seconds  are passed

p.s Edited 

example in case 1:

 

switch(assasins)
{
  case 1:
    if (System.currentTimeMillis() - firstkill > 10000)
        10 seconds passed
    else
        not passed yet
  	break;
  cases X...
 	 ....
 	 ....
}

firstkill = System.currentTimeMillis() + (10 * 1000);

 

Edited by melron

Share this post


Link to post
Share on other sites
  • 0
StarSCreams    0

hey melron, thanks for you reply, you fix one of my problems (How to check if 10 seconds passed), but my other problem not fix..

 

i make the clean code to check it work, but never increment the variable..

Example:

private int assasins = 0;

protected void doPKPVPManage(Creature....

			.....
			//Line: @@ -4286,6 +4319,37 @@

			//Here my code check if pvpkill and update +1 kills to db
				pk.setPvpKills(pk.getPvpKills() + 1);
				
				assasins++;
				
				switch(assasins) {
					case 1:
						Announcements.getInstance().announceToAll("FIRST KILL VARIABLE: " + assasins);
					break;
					case 2:
						Announcements.getInstance().announceToAll("SECOND KILL VARIABLE: " + assasins);
					break;
					case 3:
						Announcements.getInstance().announceToAll("THIRD KILL VARIABLE: " + assasins);
					break;
					case 4:
						Announcements.getInstance().announceToAll("FOUR KILL VARIABLE: " + assasins);
					break;
					default:
							Announcements.getInstance().announceToAll("DEFAULT KILL++ VARIABLE: " + assasins);
					break;
				}

 

Ingame, i made more than 10 kills only show FIRST ANNOUNCEMENT u.u

 

Thanks for help me!

Edited by StarSCreams

Share this post


Link to post
Share on other sites
  • 0
melron    24

be sure that your variable assasins is a global variable. the code you posted seems running fine without problems

Edited by melron

Share this post


Link to post
Share on other sites
  • 0
StarSCreams    0
12 minutes ago, melron said:

be sure that your variable assasins is a global variable. the code you posted seems running fine without problems

how to check if my variable is global? i add to my variable static to make a global..

public static int assasins = 0;

 

but obtain same result, only show first kill

Edited by StarSCreams

Share this post


Link to post
Share on other sites
  • 0
melron    24

no you dont need it as static you need it instanceid ... just put the variable out of any method. if you still have problems add me in skype

Edited by melron

Share this post


Link to post
Share on other sites
  • 0
StarSCreams    0
25 minutes ago, melron said:

no you dont need it as static you need it instanceied ... just put the variable out of any method. if you still have problems add me in skype

thanks for you support, i found the problem.. i have put ondie character class:  assasins = 0;

but set 0 when any character dies not the killer for stop kills in a row.. i try find the fix now hahaha thanks!!!

Edited by StarSCreams

Share this post


Link to post
Share on other sites
  • 0
StarSCreams    0

hi, i try to fix my last problem but i can't...

 

i add assasins = 0; in protected void onDeath(Creature killer) .... to stop killingspree

and not increment assasins++; in protected void doPKPVPManage(Creature killer)

how to check if killer is dead? or how to fix? i upload my Player.java in paste bin if any can help me, search: //killingspree to see if my code is correct..

https://pastebin.com/hGXgnsUV

 

Thanks all!

Share this post


Link to post
Share on other sites
  • 0
SweeTs    10

From the beginning, what's your whole idea? If time < 10s from previous kill send other message than default? Counting from first kill, or 2nd - > 3rd send other message as well? 

Share this post


Link to post
Share on other sites
  • 0
StarSCreams    0
7 hours ago, SweeTs said:

From the beginning, what's your whole idea? If time < 10s from previous kill send other message than default? Counting from first kill, or 2nd - > 3rd send other message as well? 

the messages and kills works, but when i add assassins = 0; in... protected void onDeath(Creature killer)..., don't work anymore. i add that to stop killing spree
to create this I followed this example: http://www.maxcheaters.com/topic/107756-sharefreya-killing-spree/

 

I hope you understand me i am from argentine and i do not know how to express myself very well in english u.u

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.