Jump to content

How to constantly check if under attacked?


The Legman

Recommended Posts

Hi

I'm basically writing a task based air orber script - which I am going to be adding anti-PK support (so if my player is under attacked by a skull player OR I can be attacked by a nearby skulled player, teleport out and hop worlds)

 

My issue is where do I put this code? I want the script to be constantly checking but considering my script will be running through tasks I don't want to have the same code pasted throughout repeatedly. Is there anwhere i can perhaps put it in a onLoop in the main class or something?

 

Thanks

Link to comment
Share on other sites

If you're using task based stuff, you probably have an abstract class for your task classes. So you can create a concrete method there which checks if you're attacked. Can add it into you're "check if task can be run" method or anywhere really.

Another option is to put it in onLoop.

Can do whatever suits you best really.

Link to comment
Share on other sites

7 minutes ago, nosepicker said:

If you're using task based stuff, you probably have an abstract class for your task classes. So you can create a concrete method there which checks if you're attacked. Can add it into you're "check if task can be run" method or anywhere really.

Another option is to put it in onLoop.

Can do whatever suits you best really.

Would having it in the onLoop make it check constantly, or only after all the tasks have cleared?

Link to comment
Share on other sites

I would do it like this (pseudo code):

public abstract class Task {
	abstract boolean validate();
	abstract boolean process();

	void run() {
		if (validate) {
			if (!checkPK)
				process();
		}
	}

	boolean checkPK() {
		if (IM_ATTACKED) 
			GO_RUN_AWAY_OR_SOMETHING;
		return true;
	}
}
Main Script class

ArrayList<Task> taskList = new ArrayList<>();
onStart() {
	taskList.add(new DoSomethingClass());
  	taskList.add(new DoSomethingElseClass());
}
  
  onLoop() {
  	taskList.forEach(t -> t.run());
  }
}
  

Task classes:
class DoSomethingClass extends Task {
  boolean validate() {
  	if (I_CAN_DO_IT)
      return true;
  }
  
  void proccess() {
    JUMP_ROPE:
  }
}

 

  • Like 1
Link to comment
Share on other sites

1 hour ago, nosepicker said:

I would do it like this (pseudo code):


public abstract class Task {
	abstract boolean validate();
	abstract boolean process();

	void run() {
		if (validate) {
			if (!checkPK)
				process();
		}
	}

	boolean checkPK() {
		if (IM_ATTACKED) 
			GO_RUN_AWAY_OR_SOMETHING;
		return true;
	}
}

Main Script class

ArrayList<Task> taskList = new ArrayList<>();
onStart() {
	taskList.add(new DoSomethingClass());
  	taskList.add(new DoSomethingElseClass());
}
  
  onLoop() {
  	taskList.forEach(t -> t.run());
  }
}
  

Task classes:
class DoSomethingClass extends Task {
  boolean validate() {
  	if (I_CAN_DO_IT)
      return true;
  }
  
  void proccess() {
    JUMP_ROPE:
  }
}

 

So if it's in a onLoop() in main class.java, it should always check?

Link to comment
Share on other sites

4 hours ago, The Legman said:

Would having it in the onLoop make it check constantly, or only after all the tasks have cleared?

In your main task, the Boolean you use to activate your task, add a check there. Example

boolean hasmaterials{ 

run code}

vs 

boolean hasmaterials&&!myplayer.isunderattack{

run code}

that should let you stop what your doing when under attack, you’ll then need to just tell to wheat to do when under attack.

Link to comment
Share on other sites

Q: How to constantly check if under attacked?

A: isUnderAttackByAnotherPlayer


		if (isUnderAttackByAnotherPlayer()) {
			
			// run away
			
		} else {
			
			// make air orbs
		}
		

Source:

public boolean isUnderAttackByAnotherPlayer() {
		return players.getAll().stream()
				.filter(this::isNotMe)
				.filter(this::isInteractingWithMe)
				.anyMatch(this::isSkulled);
	}
	
	private boolean isNotMe(Player p) {
		return !p.equals(myPlayer());
	}
	
	private boolean isInteractingWithMe(Player p) {
		return myPlayer().equals(p.getInteracting());
	}
	
	private boolean isSkulled(Player p) {
		return p.getSkullIcon() >= 0;
	}

Note: this checks for players that...

  • are not you - because you have to filter yourself out of the list too
  • are interacting with you - this can include following, trading, or attacking; anything where one player's focus is directed towards you
  • are skulled - I found a thread which supposedly has ids, because Player#getSkullIcon returns an number. 

However, this does not actually know whether a player is attacking you. For all the bot knows, a skulled player could be trying to trade with you.

Edited by liverare
  • Like 1
Link to comment
Share on other sites

8 minutes ago, liverare said:

Q: How to constantly check if under attacked?

A: isUnderAttackByAnotherPlayer



		if (isUnderAttackByAnotherPlayer()) {
			
			// run away
			
		} else {
			
			// make air orbs
		}
		

Source:


public boolean isUnderAttackByAnotherPlayer() {
		return players.getAll().stream()
				.filter(this::isNotMe)
				.filter(this::isInteractingWithMe)
				.anyMatch(this::isSkulled);
	}
	
	private boolean isNotMe(Player p) {
		return !p.equals(myPlayer());
	}
	
	private boolean isInteractingWithMe(Player p) {
		return myPlayer().equals(p.getInteracting());
	}
	
	private boolean isSkulled(Player p) {
		return p.getSkullIcon() >= 0;
	}

Note: this checks for players that...

  • are not you - because you have to filter yourself out of the list too
  • are interacting with you - this can include following, trading, or attacking; anything where one player's focus is directed towards you
  • are skulled - I found a thread which supposedly has ids, because Player#getSkullIcon returns an number. 

However, this does not actually know whether a player is attacking you. For all the bot knows, a skulled player could be trying to trade with you.

Thanks! :)

I was looking for where I can actually place the code, such as the onLoop in the main class.

I need it somewhere where it always checks - so say if it's half way through the MakeAirOrbs.java class it breaks out of the class.

 

I will probally put

 

else if in

Link to comment
Share on other sites

15 minutes ago, liverare said:

Q: How to constantly check if under attacked?

A: isUnderAttackByAnotherPlayer



		if (isUnderAttackByAnotherPlayer()) {
			
			// run away
			
		} else {
			
			// make air orbs
		}
		

Source:


public boolean isUnderAttackByAnotherPlayer() {
		return players.getAll().stream()
				.filter(this::isNotMe)
				.filter(this::isInteractingWithMe)
				.anyMatch(this::isSkulled);
	}
	
	private boolean isNotMe(Player p) {
		return !p.equals(myPlayer());
	}
	
	private boolean isInteractingWithMe(Player p) {
		return myPlayer().equals(p.getInteracting());
	}
	
	private boolean isSkulled(Player p) {
		return p.getSkullIcon() >= 0;
	}

Note: this checks for players that...

  • are not you - because you have to filter yourself out of the list too
  • are interacting with you - this can include following, trading, or attacking; anything where one player's focus is directed towards you
  • are skulled - I found a thread which supposedly has ids, because Player#getSkullIcon returns an number. 

However, this does not actually know whether a player is attacking you. For all the bot knows, a skulled player could be trying to trade with you.

onLoop.PNG.2e966a32ebee923378562df4e2e9c591.PNG This currently how I build my scripts. The issue is when it runs through a task, such as normalRocks ( a completely different script btw), it will never interrupt that task while it's mid way done - which is what I want it to do. Thus I will end up trying to put that code in the onLoop. Thanks!

Link to comment
Share on other sites

12 minutes ago, Viston said:

@The Legman You can perhaps make a standalone thread for that.

That was my main question for this thread but asked it poorly :o

6 minutes ago, Apaec said:

You seem to be confused about how the script is running - don't use code that you don't understand!

However you've structured your source, it should be continuously looping via. onLoop, so take advantage of that.

I got it now. :) Thanks for the support everyone!

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...