The Legman Posted November 23, 2017 Share Posted November 23, 2017 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 Quote Link to comment Share on other sites More sharing options...
Butters Posted November 23, 2017 Share Posted November 23, 2017 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. Quote Link to comment Share on other sites More sharing options...
The Legman Posted November 23, 2017 Author Share Posted November 23, 2017 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? Quote Link to comment Share on other sites More sharing options...
Butters Posted November 23, 2017 Share Posted November 23, 2017 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: } } 1 Quote Link to comment Share on other sites More sharing options...
The Legman Posted November 23, 2017 Author Share Posted November 23, 2017 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? Quote Link to comment Share on other sites More sharing options...
Butters Posted November 23, 2017 Share Posted November 23, 2017 It will check on each loop iteration, yes. Though if you're task runs forever on each loop iteration, it might not check for 1 minute, 2 minutes or however long. Quote Link to comment Share on other sites More sharing options...
RONTAG Posted November 23, 2017 Share Posted November 23, 2017 44 minutes ago, nosepicker said: It will check on each loop iteration, yes. Though if you're task runs forever on each loop iteration, it might not check for 1 minute, 2 minutes or however long. Quote Link to comment Share on other sites More sharing options...
sonda Posted November 23, 2017 Share Posted November 23, 2017 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. Quote Link to comment Share on other sites More sharing options...
liverare Posted November 24, 2017 Share Posted November 24, 2017 (edited) 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 November 24, 2017 by liverare 1 Quote Link to comment Share on other sites More sharing options...
The Legman Posted November 24, 2017 Author Share Posted November 24, 2017 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 Quote Link to comment Share on other sites More sharing options...
The Legman Posted November 24, 2017 Author Share Posted November 24, 2017 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. 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! Quote Link to comment Share on other sites More sharing options...
Viston Posted November 24, 2017 Share Posted November 24, 2017 @The Legman You can perhaps make a standalone thread for that. Quote Link to comment Share on other sites More sharing options...
Apaec Posted November 24, 2017 Share Posted November 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
The Legman Posted November 24, 2017 Author Share Posted November 24, 2017 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 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! 1 Quote Link to comment Share on other sites More sharing options...
Sysm Posted November 25, 2017 Share Posted November 25, 2017 4 hours ago, The Legman said: That was my main question for this thread but asked it poorly I got it now. Thanks for the support everyone! YEET Quote Link to comment Share on other sites More sharing options...