Jump to content

stuck on getInventory.....


Recommended Posts

Posted (edited)
First thing we need is an instance that supplies the methods we need (from MethodProvider). We can do this by extending upon the API type:
final class DefaultAPI extends API {

}

You will be forced to declare an initializeModule method. Don't worry about it, just the devs not following interface segregation wink.png Leave it blank. You could fill it in, but you would be required to call it after instantiating API. 

 

Once you have your API type, create an instance of it in onStart in your Script subclass:

final class MyScript extends Script {
    private API api;

    public void onStart() {
        api = new DefaultAPI();

    }
}

Finally, we need to pass the context of our script to our api instance. We do this by calling exchangeContext on our api instance, passing in the script's bot:

final class MyScript extends Script {
    private API api;

    public void onStart() {
        api = new DefaultAPI();
        api.exchangeContext(getBot());

    }
}

I quoted fixthissite's post. Read it through thoroughly and if you pay attention, you'll make it.

 

Edit: If you did all of this above, here is an example:

import org.osbot.rs07.api.model.Item;
import org.osbot.rs07.script.API;


public class Inventory {

    // ADDED THIS HERE @@@
    private API api;

    public Inventory(API api) {
        this.api = api;
    }
    // @@@@@@@
	
    // WARNING: Untested code
    
    /** Holds the amount of willows we have cut */
    private int willowsCut = 0;
    /** Holds the last inventory that was compared */
    private Item[] lastInv;
    
    /**
     * Simple getter
     * @return the number of willows cut
     */
    public int getWillowsCut() {
        return willowsCut;
    }
    
    /**
     * Compares your last inventory with the current inventory.
     */
    public void compareItems() {
        if (lastInv == null) {
            lastInv = api.getInventory().getItems();
            return; // It will be the same, no need to check
        }
        
        String item = "Willow logs";
        Item[] currInv = getInventory().getItems();
        for (int i = 0; i < 28; i++) {
            // Check for the same thing
            if ((currInv[i] == null && lastInv == null)
                    || currInv[i].getName().equals(lastInv[i].getName()))
                continue; // Its the same, continue
            
            if (currInv[i] != null) {
                // We have an item when we didn't
                if (currInv[i].getName().equals(item))
                    willowsCut++; // It was a willow log, ++
            } else {
                // We don't have an item when we did
                if (lastInv[i].getName().equals(item))
                    willowsCut--; // It was a willow log, --
            }
        }
        lastInv = currInv; // Update the inv
    }
    
    /**
     * Run this when the lastInv needs refreshing, aka after banking
     */
    public void resetItems() {
        lastInv = api.getInventory().getItems();
    }
}

If you want to get inventory in your Inventory class, simply do "api.getInventory()".

Edited by Woody
Posted

 

First thing we need is an instance that supplies the methods we need (from MethodProvider). We can do this by extending upon the API type:
final class DefaultAPI extends API {

}

You will be forced to declare an initializeModule method. Don't worry about it, just the devs not following interface segregation wink.png Leave it blank. You could fill it in, but you would be required to call it after instantiating API. 

 

Once you have your API type, create an instance of it in onStart in your Script subclass:

final class MyScript extends Script {
    private API api;

    public void onStart() {
        api = new DefaultAPI();

    }
}

Finally, we need to pass the context of our script to our api instance. We do this by calling exchangeContext on our api instance, passing in the script's bot:

final class MyScript extends Script {
    private API api;

    public void onStart() {
        api = new DefaultAPI();
        api.exchangeContext(getBot());

    }
}

I quoted fixthissite's post. Read it through thoroughly and if you pay attention, you'll make it.

 

Edit: If you did all of this above, here is an example:

import org.osbot.rs07.api.model.Item;
import org.osbot.rs07.script.API;


public class Inventory {

    // ADDED THIS HERE @@@
    private API api;

    public Inventory(API api) {
        this.api = api;
    }
    // @@@@@@@
	
    // WARNING: Untested code
    
    /** Holds the amount of willows we have cut */
    private int willowsCut = 0;
    /** Holds the last inventory that was compared */
    private Item[] lastInv;
    
    /**
     * Simple getter
     * @return the number of willows cut
     */
    public int getWillowsCut() {
        return willowsCut;
    }
    
    /**
     * Compares your last inventory with the current inventory.
     */
    public void compareItems() {
        if (lastInv == null) {
            lastInv = api.getInventory().getItems();
            return; // It will be the same, no need to check
        }
        
        String item = "Willow logs";
        Item[] currInv = getInventory().getItems();
        for (int i = 0; i < 28; i++) {
            // Check for the same thing
            if ((currInv[i] == null && lastInv == null)
                    || currInv[i].getName().equals(lastInv[i].getName()))
                continue; // Its the same, continue
            
            if (currInv[i] != null) {
                // We have an item when we didn't
                if (currInv[i].getName().equals(item))
                    willowsCut++; // It was a willow log, ++
            } else {
                // We don't have an item when we did
                if (lastInv[i].getName().equals(item))
                    willowsCut--; // It was a willow log, --
            }
        }
        lastInv = currInv; // Update the inv
    }
    
    /**
     * Run this when the lastInv needs refreshing, aka after banking
     */
    public void resetItems() {
        lastInv = api.getInventory().getItems();
    }
}

If you want to get inventory in your Inventory class, simply do "api.getInventory()".

 

how do i call it from the main class it wont let me do it... getting NUlled or error

 

i did 

Inventory inventory = new Inventory();
or
Inventory inventory = new Inventory(null);

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