Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Death Handling


    Defiled

    Recommended Posts

    Hello,

    This snippet gets your items from the spot you died at and equips the items you had equipped.

    Get Items that you are equipping (execute in the onStart method):

    Credit for suggestion: @Nex
    Credit for the getItems() code: @NovaGTX

    	private HashMap<EquipmentSlot, Item> slots = new HashMap<EquipmentSlot, Item>();
          
    	private void getItems() {
    		for(EquipmentSlot i : EquipmentSlot.values()) {
    			if(getEquipment().getItemInSlot(i.getSlot()) != null) {
    				slots.put(i, getEquipment().getItemInSlot(i.getSlot()));
    			}
    		}
    		
    	}
    

    Get Death Location:

    private static Tile deathTile;
    deathTile = getLocalPlayer().getTile();

    Know when the player dies:

    if(getLocalPlayer().getHealthPercent() == 0) {

    Death Method (Executed when player dies):

    	private void death() {
    		if(deathTile.getArea(2).contains(getLocalPlayer())) {
    			GroundItem[] deathItems = getGroundItems().getGroundItems(deathTile);
    			if(deathItems != null) {
    			for(GroundItem deathItem : deathItems) {
    				if(deathItem != null) {
    					deathItem.interact("Take");
    					sleepUntil(()->!deathItem.exists(), 2000);
    				}
    			}
    			}
    			if(getTabs().isOpen(Tab.INVENTORY)) {
    			if(!getEquipment().containsAll(slots.values())) {
    				for(Item equippedItem : slots.values()) {
    					if(getInventory().contains(equippedItem) && equippedItem != null) {
    						String action = null;
    						if(equippedItem.hasAction("Equip")) {
    							action = "Equip";
    						} else if(equippedItem.hasAction("Wear")) {
    							action = "Wear";
    						} else if(equippedItem.hasAction("Wield")) {
    							action = "Wield";
    						}
    						if(action != null) {
    							getInventory().interact(equippedItem.getName(), action);
    						}
    					}
    				}
    			}
    			} else {
    				getTabs().open(Tab.INVENTORY);
    			}
    			
    		} else {
    			if(getWalking().shouldWalk()) {
    				getWalking().walk(deathTile);
    			}
    		}
    	}

     

    There it is! :)

    Link to comment
    Share on other sites

    35 minutes ago, Pseudo said:

    Good job on the contribution pal, but that first method in particular could be written far better. 

    Thank you!

    I agree, I could add all the slots to an array then loop through them to check if they are empty or not.. OR just simply minimize the size of it ... 

    suggestions are welcome!

    Link to comment
    Share on other sites

    5 hours ago, ItsDefiled said:

    Thank you!

    I agree, I could add all the slots to an array then loop through them to check if they are empty or not.. OR just simply minimize the size of it ... 

    suggestions are welcome!

    Would suggest a hashmap for this

    Link to comment
    Share on other sites

    Suggested edit for your first function.

    	private HashMap<EquipmentSlot, Item> slots = new HashMap<EquipmentSlot, Item>();
          
    	private void getItems() {
    		for(EquipmentSlot i : EquipmentSlot.values()) {
    			if(getEquipment().getItemInSlot(i.getSlot()) != null) {
    				slots.put(i, getEquipment().getItemInSlot(i.getSlot()));
    			}
    		}
    		
    	}
    Link to comment
    Share on other sites

    59 minutes ago, NovaGTX said:

    Suggested edit for your first function.

    
    	private HashMap<EquipmentSlot, Item> slots = new HashMap<EquipmentSlot, Item>();
          
    	private void getItems() {
    		for(EquipmentSlot i : EquipmentSlot.values()) {
    			if(getEquipment().getItemInSlot(i.getSlot()) != null) {
    				slots.put(i, getEquipment().getItemInSlot(i.getSlot()));
    			}
    		}
    		
    	}

    Exactly how I do it in a couple scripts I've made. Iterative process do I dont have to write 20 if statements for each slot.  Quality snippet @NovaGTX

    Link to comment
    Share on other sites

    Also you should change this
     

    if(getInventory().contains(equippedItem) && equippedItem != null) {
    						String action = null;
    						if(equippedItem.hasAction("Equip")) {
    							action = "Equip";
    						} else if(equippedItem.hasAction("Wear")) {
    							action = "Wear";
    						} else if(equippedItem.hasAction("Wield")) {
    							action = "Wield";
    						}
    						if(action != null) {
    							getInventory().interact(equippedItem.getName(), action);
    						}
    }

    to a utilize a lambda expression:

    for(Item equippedItem : slots.values()) {
    			if(getInventory().get(item -> item != null && item.equals(equippedItem)).interact()) {
    				//Equipped item
    			}
    		}


    Each of the actions you listed are left click, you can just interact with the object to equip it regardless of which it is. You could make it check each action but it's not as clean and not 100% necessary.

    Link to comment
    Share on other sites

    10 hours ago, NovaGTX said:

    Also you should change this
     

    
    if(getInventory().contains(equippedItem) && equippedItem != null) {
    						String action = null;
    						if(equippedItem.hasAction("Equip")) {
    							action = "Equip";
    						} else if(equippedItem.hasAction("Wear")) {
    							action = "Wear";
    						} else if(equippedItem.hasAction("Wield")) {
    							action = "Wield";
    						}
    						if(action != null) {
    							getInventory().interact(equippedItem.getName(), action);
    						}
    }

    to a utilize a lambda statement:

    
    for(Item equippedItem : slots.values()) {
    			if(getInventory().get(item -> item != null && item.equals(equippedItem)).interact()) {
    				//Equipped item
    			}
    		}


    Each of the actions you listed are left click, you can just interact with the object to equip it regardless of which it is. You could make it check each action but it's not as clean and not 100% necessary.

    Yeah, uhm i wrote the last code in regards with an older version where itll pick up all the items and interact with the items that have the actions wield, equip, and wear.. didnt update it when i changed the way i went around redoing it because it just worked fine.

    Thank you for the edit, I will edit it in once i reach my pc.

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.