[Code Style] Avoiding double checking in TaskScripts

Hello. I’m trying to teach myself Java and I’m using script writing as a learing book so to say. I’ve currently hit a small hurdle. I’m trying to write code where:

if(valid logs in viewport) { run one task }
else { run other task }

So currently I have 2 validate() functions that do pretty much the same, but with opposite results. My exact code is:

public boolean validate() {
    return validLogs().isEmpty();
}
private ArrayList<Pickable> validLogs() {
    ArrayList<Pickable> logsToLight = new ArrayList<>();
    PositionableQueryResults<Pickable> logs = Pickables.newQuery().names("Logs").filter(s -> s.distance() <= 6).results();
    for (Pickable log : logs) {
        if (!isBlocking(log.getPosition())) {
            logsToLight.add(log);
        }
    }
    return logsToLight;
}
private boolean isBlocking(Position position) {
    SceneObject scene = SceneObjects.getFirstAt(position);
    return (scene != null && Arrays.asList(FiremakingConstants.BLOCKING_SCENES).contains(scene.getName()));
}

The second task, contains exactly the same code, of course the boolean in validate() is inverted using !. Which allows the task to run. Any pointers on how could I avoid writing this messy redundant code? If you have any more resources on this coding-style type of stuff, they are also more than welcome.

Thanks in advance.

Update:
I resolved my issue, by creating a new class, which queries the environment for what I need and saves it for later use (like caching? if you will). Anyway, in this process, my account got banned :laughing: .

Link to the full project and my “solution” to this issue is linked here below, enjoy:

If you have any feedback, criticism, everything is more than welcome.

Don’t cache game objects, its bad mkay

If I’m understanding you, the second task can simply return true. Assuming you are using TaskScript and you submit the first task before the second one. The script execution engine visits each task in the order they are submitted, so if A.validate is true then B.validate wouldn’t even be executed.

submit(A, B)
[A -> validLogs().isEmpty(), B -> true]

“Anyway, in this process, my account got banned”

I cannot even begin to express how relatable that statement is.

First scene:
TaskExecutor:
Node 1 valid?=true; activate Task A.
Loop 2:
Node 1 valid?=false; Node 1 valid?=false; activate Task B.
This is where I would use breaking. As soon as you know node1 isn’t getting activated, pull it out of the script loop.

Second scene:
TaskExecutor:
Node 1 valid?=false;
Node 2 valid?=true activate Task B.
However in this script we know node1&2 valid method returns the same for each, so if node 1 is false, we can presume node 2 to be true.

What i would do here is implement something in your Task called SkillChaining.