Jump to content

Putting conditionalsleep in a method


Recommended Posts

Posted

so basically, i want to put conditionalsleep in a method, so that i dont have to paste all those lines of codes everytime

 

this is what i got. i know it doesn't work, but i was wondering if someone could explain why it doesn't work.

 

 

This works

new ConditionalSleep(7000)
{
    @Override
    public boolean condition() throws InterruptedException
    {
        return ctx.getInventory().getAmount("Plank") > amountOfPlankInInventory;
    }          
}.sleep();

This doesn't

public void condSleep(int timeout,boolean statement)
{
    new ConditionalSleep(timeout)
    {
        @Override
        public boolean condition() throws InterruptedException
        {
                return statement;
        }          
    }.sleep();
}
 
condSleep(7000,ctx.getInventory().getAmount("Plank") > amountOfPlankInInventory);
Posted

Using lambda would make your first method a lot cleaner.

 

For the second method you could change the boolean from the statement to Callable<Boolean>. The thing you are doing wrong is that currently the statement will never change. The value is assigned when the method is called and will never change.
Anyway more info here: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html

Posted (edited)
public void condSleep(int timeout,boolean statement)
{
    new ConditionalSleep(timeout)
    {
        @Override
        public boolean condition() throws InterruptedException
        {
                return statement;
        }          
    }.sleep();
}
 
condSleep(7000,ctx.getInventory().getAmount("Plank") > amountOfPlankInInventory);

The method receives a boolean 'statement', which is evaluated to either true or false at the time of the method call. It's not going to magically change during the execution of the conditional sleep. So either you sleep the full time, or not at all.

Edited by FrostBug

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
  • Recently Browsing   0 members

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