Jump to content

Detect Worn Items of Other Players


Kawaii_s

Recommended Posts

Hello.

I'm trying to write a fully functional cwars script, and have it actually defend the flag and run after players. I need it to read all the equipped items of nearby players into a list and iterate through until it finds the one holding the flag, it then needs to grab what gear they are wearing to attack using the combat triangle. 

I'm not quite sure how to read the gear of other players, because it's different than reading your own gear. I spent a lot of time reading the API, but can't find any way of possibly doing this. I was wondering if this is something that can be done with OSbots API and if anyone has any examples/hints of how to do this.

 

What I'm trying to do is something like this, however this code is invalid and halts execution of the client..

for(final Player player : getPlayers().getAll()) {
	if(player.getMethods().getEquipment().getItemInSlot(EquipmentSlot.WEAPON.slot).getName().contains("flag")) {
		//interact and attack player
	}
}

 

Edited by Kawaii_s
Link to comment
Share on other sites

I wrote an API for this.
 

There's 5 functions of interest:

//AppearanceAPI.isDefined(Player, Predicare<ItemDefinition>)
//appearance.findPlayersWielding(Predicare<ItemDefinition>)
//appearance.findPlayersWielding(List<Player>, Predicare<ItemDefinition>)
//appearance.findPlayerWielding(Predicare<ItemDefinition>)
//appearance.findPlayerWielding(List<Player>, Predicare<ItemDefinition>)

Example:

Player randomPlayer123 = null; // TODO fix
		
if (AppearanceAPI.isDefined(randomPlayer123, id -> id.getName().equals("Dragon claws"))) {
	// TODO stuff...
}
List<Player> richPeople = appearance.findPlayersWielding(id -> id.getName().endsWith("spirit shield"));
		
for (Player richPerson : richPeople) {
	// TODO stuff....
}

Source:

package com.liverare.api;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import org.osbot.rs07.api.def.ItemDefinition;
import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.script.API;

/**
 * Appearance API
 * 
 * @author LiveRare
 *
 */
public class AppearanceAPI extends API {

	@Override
	public void initializeModule() {
	}

	/**
	 * Find players wielding specific items
	 * 
	 * Note: rings are not retrieved
	 * 
	 * @param players
	 *            - Players to filter
	 * @param itemDefinitionFilter
	 *            - Filter
	 * @return List of filtered players
	 * @see {@link AppearanceAPI#isDefined(Player, Predicate)}
	 */
	public List<Player> findPlayersWielding(List<Player> players, Predicate<ItemDefinition> itemDefinitionFilter) {
		
		List<Player> results = null;

		if (players != null && !players.isEmpty() && itemDefinitionFilter != null) {

			results = new ArrayList<>();
			
			for (Player player : players) {
				
				if (isDefined(player, itemDefinitionFilter)) {
					
					results.add(player);
				}
			}
		}
		
		return results;
	}
	
	/**
	 * Find players wielding specific items
	 * 
	 * Note: rings are not retrieved
	 * 
	 * @param itemDefinitionFilter
	 *            - Filter
	 * @return List of filtered players
	 * @see {@link AppearanceAPI#findPlayersWielding(List, Predicate)}
	 */
	public List<Player> findPlayersWielding(Predicate<ItemDefinition> itemDefinitionFilter) {
		return findPlayersWielding(players.getAll(), itemDefinitionFilter);
	}
	
	/**
	 * Find first player wielding specific items
	 * 
	 * Note: rings are not retrieved
	 * 
	 * @param players
	 *            - Players to filter
	 * @param itemDefinitionFilter
	 *            - Filter
	 * @return List of filtered players
	 * @see {@link AppearanceAPI#findPlayersWielding(List, Predicate)}
	 */
	public Player findPlayerWielding(List<Player> players, Predicate<ItemDefinition> itemDefinitionFilter) {
		
		Player result = null;
		List<Player> filtered = findPlayersWielding(players, itemDefinitionFilter);
		
		if (filtered != null && !filtered.isEmpty()) {
			
			result = filtered.get(0);
		}
		
		return result;
	}
	
	/**
	 * Find first player wielding specific items
	 * 
	 * Note: rings are not retrieved
	 * 
	 * @param itemDefinitionFilter
	 *            - Filter
	 * @return List of filtered players
	 * @see {@link AppearanceAPI#findPlayerWielding(List, Predicate)}
	 */
	public Player findPlayerWielding(Predicate<ItemDefinition> itemDefinitionFilter) {
		return findPlayerWielding(players.getAll(), itemDefinitionFilter);
	}

	/**
	 * Test whether player is wielding a particular item
	 * 
	 * @param player
	 *            - Player
	 * @param itemDefinitionFilter
	 *            - Item filter
	 * @return <tt>Item found on player</tt>
	 * @see {@link AppearanceAPI#isDefined(int[], Predicate)}
	 */
	public static boolean isDefined(Player player, Predicate<ItemDefinition> itemDefinitionFilter) {
		return isDefined(player.getDefinition().getAppearance(), itemDefinitionFilter);
	}

	/**
	 * Gets item definitions for the items IDs in 'appearance' and tests them
	 * against the ItemDefinition filter
	 * 
	 * @param appearance
	 *            - Item IDs
	 * @param itemDefinitionFilter
	 *            - Filter
	 * @return <tt>Item found</tt>
	 */
	private static boolean isDefined(int[] appearance, Predicate<ItemDefinition> itemDefinitionFilter) {
		
		boolean result = false;
		ItemDefinition itemDefinition = null;
		
		if (appearance != null && appearance.length > 0) {
			
			for (int itemId : appearance) {
				
				if (itemId > 0) {
					
					itemId -= 512;
					
					itemDefinition = ItemDefinition.forId(itemId);
					
					if (itemDefinition != null && itemDefinitionFilter.test(itemDefinition)) {
						
						result = true;
						break;
					}
				}
			}
			
		}
		
		return result;
	}
	
}

Note: this is a custom API, so the gain access to all the methods that start with "appearance", you will have to instantiate a new AppearanceAPI object and exchange the bot's context with it (like so):

	AppearanceAPI appearance;
	
	@Override
	public void onStart() throws InterruptedException {
		appearance = new AppearanceAPI();
		appearance.exchangeContext(bot);
	}

Enjoy.

  • Like 2
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...