Jump to content

Putting conditionalsleep in a method


The Hero of Time

Recommended Posts

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);
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
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...