Jump to content

Acting based on inventory changing?


slazter

Recommended Posts

Okay so a few days ago, i tried to make a timer based on my player moving or animating, it worked fine, also, thx for the snippet @dreameo

So today i was thinking that it would be fun to try to add some kind of inventory check to see if inventory has changed, in that case i don't want the Idle timer to go of aswell.. I read the forums and someone said to cache the inventory (I don't know what that is, i read up on it and it said, to save some kind of temporary data in the memory to more easily acess it?) I don't know how to do that though, so any input regarding that would be very helpful, but apart from that i tried to implement some kind of check, before looting and then after, then comparing them to eachother, which i would assume would work but didn't. So i'll post my code down below, and would be super glad for any help regarding how to solve this problem. 

 

public final int onLoop() throws InterruptedException {
        GroundItem toLoot = getGroundItems().closest(i->i.getName().contains(x) && getMap().canReach(i) && i.isVisible());
        if(toLoot!=null){
            int slotCount = getInventory().getEmptySlotCount();
            log("Slotcount 1: " +slotCount);
            if(toLoot.interact("Take")){
                Sleep.sleepUntil(()-> toLoot==null || !toLoot.exists(),5000);
                int slotCount2=getInventory().getEmptySlotCount();
                log("Slotcount 2: "+ slotCount2);
            }
        }
    

    if(idleFor(20000)){
        log("Idle for to long");
        getWalking().walk(Banks.EDGEVILLE);
    }


    return random(150, 250);
}

private boolean idleFor(long millis){
    if(myPlayer().isAnimating() || myPlayer().isMoving() || slotCount!=slotCount2) {
        timeSinceAction = System.currentTimeMillis();
        log("Not counting");
    }
    else if(!myPlayer().isMoving()) {
        timeSinceIdle = System.currentTimeMillis();
        log("Counting");
    }
    //return true,
    return timeSinceAction + millis < timeSinceIdle;
}
Link to comment
Share on other sites

Credits:

Spoiler

Comes from another botting site, user is: daxmagex

 

Modified source for osbot:

Spoiler

public interface InventoryListener {

    public void inventoryItemGained(int itemID, int count);
    public void inventoryItemLost(int itemID, int count);
}

 

Spoiler


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

import java.util.ArrayList;
import java.util.HashMap;

public class InventoryObserver extends Thread{

    private ArrayList<InventoryListener> listeners;
    private MethodProvider methodProvider;

    public InventoryObserver(MethodProvider methodProvider){
        listeners = new ArrayList<>();
        this.methodProvider = methodProvider;
    }

    @Override
    public void run(){

      // initial inventory
        HashMap<Integer, Integer> map = inventoryHashMap();


        while (true){
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

          // inventory of 'updated map'
            HashMap<Integer, Integer> updatedMap = inventoryHashMap();

          // iterate over updated map
            for(Integer i : updatedMap.keySet())
            {
              // gets the count of each item
              
                int initialCount = map.containsKey(i) ?  map.get(i) : 0;
                int finalCount = updatedMap.get(i);
              
			// compares item count of map and updatedMap
                if(finalCount > initialCount)
                {
                  // if item count is higher in updated map then current map,
                  // then item count increased
                    addTrigger(i, finalCount - initialCount);
                }
                else if (initialCount > finalCount)
                {
                  // if item count is not higher in updated map then current map,
                  // then item count decreased
                    subtractedTrigger(i, initialCount - finalCount);
                }

                map.remove(i);
            }

          // previously we compared items between both inventories
          // but this case covers if the item occoured in current map but not uppdated map (i.e: item was dropped, banked...)
            for (Integer i: map.keySet())
            {
              // if updated map does not contain instance of item, then we no longer have it
                if (!updatedMap.containsKey(i))
                    subtractedTrigger(i, map.get(i));
            }

            map = updatedMap;
        }
    }

  // gets items from inventory
    public HashMap<Integer, Integer> inventoryHashMap(){

        HashMap<Integer, Integer> map = new HashMap<>();

        for(Item item : methodProvider.getInventory().getItems())
        {
            if(item == null)
                continue;

            map.put(item.getId(), item.getAmount());
        }

        return map;
    }

    public void addListener(InventoryListener listener){
        this.listeners.add(listener);
    }

    public void addTrigger(int id, int count){
        for(InventoryListener i : listeners)
        {
            i.inventoryItemGained(id, count);
        }
    }

    public void subtractedTrigger(int id, int count){
        for(InventoryListener i : listeners)
        {
            i.inventoryItemLost(id, count);
        }
    }

}

 

 

Implementation: 

Spoiler

public class Main extends Script implements InventoryListener {

    @Override
    public void onStart(){
        InventoryObserver inventoryObserver = new InventoryObserver(this);

        inventoryObserver.addListener(this);
        inventoryObserver.start();
    }

    @Override
    public int onLoop() throws InterruptedException {
        return 0;
    }

    @Override
    public void inventoryItemGained(int itemID, int count) {
        log("Gained item: " + itemID +" in the amount of: " + count);
    }

    @Override
    public void inventoryItemLost(int itemID, int count) {
        log("Lost item: " + itemID +" in the amount of: " + count);
    }
}

 

 

Edited by dreameo
  • Like 2
Link to comment
Share on other sites

6 hours ago, dreameo said:

Credits:

  Reveal hidden contents

Comes from another botting site, user is: daxmagex

 

Modified source for osbot:

  Reveal hidden contents


public interface InventoryListener {

    public void inventoryItemGained(int itemID, int count);
    public void inventoryItemLost(int itemID, int count);
}

 

  Reveal hidden contents



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

import java.util.ArrayList;
import java.util.HashMap;

public class InventoryObserver extends Thread{

    private ArrayList<InventoryListener> listeners;
    private MethodProvider methodProvider;

    public InventoryObserver(MethodProvider methodProvider){
        listeners = new ArrayList<>();
        this.methodProvider = methodProvider;
    }

    @Override
    public void run(){

      // initial inventory
        HashMap<Integer, Integer> map = inventoryHashMap();


        while (true){
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

          // inventory of 'updated map'
            HashMap<Integer, Integer> updatedMap = inventoryHashMap();

          // iterate over updated map
            for(Integer i : updatedMap.keySet())
            {
              // gets the count of each item
              
                int initialCount = map.containsKey(i) ?  map.get(i) : 0;
                int finalCount = updatedMap.get(i);
              
			// compares item count of map and updatedMap
                if(finalCount > initialCount)
                {
                  // if item count is higher in updated map then current map,
                  // then item count increased
                    addTrigger(i, finalCount - initialCount);
                }
                else if (initialCount > finalCount)
                {
                  // if item count is not higher in updated map then current map,
                  // then item count decreased
                    subtractedTrigger(i, initialCount - finalCount);
                }

                map.remove(i);
            }

          // previously we compared items between both inventories
          // but this case covers if the item occoured in current map but not uppdated map (i.e: item was dropped, banked...)
            for (Integer i: map.keySet())
            {
              // if updated map does not contain instance of item, then we no longer have it
                if (!updatedMap.containsKey(i))
                    subtractedTrigger(i, map.get(i));
            }

            map = updatedMap;
        }
    }

  // gets items from inventory
    public HashMap<Integer, Integer> inventoryHashMap(){

        HashMap<Integer, Integer> map = new HashMap<>();

        for(Item item : methodProvider.getInventory().getItems())
        {
            if(item == null)
                continue;

            map.put(item.getId(), item.getAmount());
        }

        return map;
    }

    public void addListener(InventoryListener listener){
        this.listeners.add(listener);
    }

    public void addTrigger(int id, int count){
        for(InventoryListener i : listeners)
        {
            i.inventoryItemGained(id, count);
        }
    }

    public void subtractedTrigger(int id, int count){
        for(InventoryListener i : listeners)
        {
            i.inventoryItemLost(id, count);
        }
    }

}

 

 

Implementation: 

  Reveal hidden contents


public class Main extends Script implements InventoryListener {

    @Override
    public void onStart(){
        InventoryObserver inventoryObserver = new InventoryObserver(this);

        inventoryObserver.addListener(this);
        inventoryObserver.start();
    }

    @Override
    public int onLoop() throws InterruptedException {
        return 0;
    }

    @Override
    public void inventoryItemGained(int itemID, int count) {
        log("Gained item: " + itemID +" in the amount of: " + count);
    }

    @Override
    public void inventoryItemLost(int itemID, int count) {
        log("Lost item: " + itemID +" in the amount of: " + count);
    }
}

 

 

Holy shit bro, your like a wizard, keep poping up with all sorts of magic! Thank you very much for this! :D

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