Jump to content

(Snippet) [Entitype] How to collect a list of any Entity sub-class type


Recommended Posts

Posted (edited)

Allows you to group multiple entity types in one list which may or may not allow for more efficient overall traversal cost / filtering.

 

Allows you to access sub-list of RS2Object sub-types such as Wall Decoration, which may allow for more efficient filtering.

 

Examples:

List<Entity> wallRelated = Entitype.collect(this, Entitype.OBJECT_WALL_DECORATION, Entitype.OBJECT_WALL_OBJECT);

List<Entity> playersAndGroundItems = Entitype.collect(this, Entitype.PLAYER, Entitype.GROUND_ITEM);

List<Entity> all = Entitype.collect(this, Entitype.ALL);
		List<Entity> entities = Entitype.collect(ctx, Entitype.OBJECT_ALL, Entitype.NPC, Entitype.GROUND_ITEM);
		if(!entities.isEmpty()) {
			Entity target = entities.get(random.nextInt(entities.size()));
			target.examine();
		}

Snippet:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

import org.osbot.rs07.api.model.Character;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.GroundDecoration;
import org.osbot.rs07.api.model.InteractableObject;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.model.WallDecoration;
import org.osbot.rs07.api.model.WallObject;
import org.osbot.rs07.script.MethodProvider;

public class Entitype {
	
	public static final int OBJECT_GROUND_DECORATION = 0;
	public static final int OBJECT_INTERACTABLE_OBJECT = 1;
	public static final int OBJECT_WALL_DECORATION = 2; 
	public static final int OBJECT_WALL_OBJECT = 3;
	public static final int OBJECT_ALL = 4;
	public static final int CHARACTER = 5;
	public static final int NPC = 6;
	public static final int PLAYER = 7;
	public static final int GROUND_ITEM = 8;
	public static final int ALL = 9;
	
	public static List<Entity> collect(MethodProvider ctx, int... types) {
		
		List<Entity> result = new ArrayList<>();
		
		List<Entity> objectsGroundDecoration = new ArrayList<>();
		List<Entity> objectsInteractableObject = new ArrayList<>();
		List<Entity> objectsWallDecoration = new ArrayList<>();
		List<Entity> objectsWallObject = new ArrayList<>();
		
		if(IntStream.of(types).anyMatch(x -> x < OBJECT_ALL)) {
			for (RS2Object o : ctx.getObjects().getAll()) {
				Class<?> clazz = o.getClass();
				if(clazz.equals(GroundDecoration.class)) {
					objectsGroundDecoration.add(o);
				}
				else if(clazz.equals(InteractableObject.class)) {
					objectsInteractableObject.add(o);
				}
				else if(clazz.equals(WallDecoration.class)) {
					objectsWallDecoration.add(o);
				}
				else if(clazz.equals(WallObject.class)) {
					objectsWallObject.add(o);
				}
			}
		}

		for (int type : types) {
			switch (type) {
			case ALL:
				List<Entity> all = new ArrayList<>();
				all.addAll(ctx.getObjects().getAll());
				all.addAll(ctx.getNpcs().getAll());
				all.addAll(ctx.getPlayers().getAll());
				result.addAll(all);
				break;
			case OBJECT_ALL:
				result.addAll(ctx.getObjects().getAll());
				break;
			case OBJECT_GROUND_DECORATION:
				result.addAll(objectsGroundDecoration);
				break;
			case OBJECT_INTERACTABLE_OBJECT:
				result.addAll(objectsInteractableObject);
				break;
			case OBJECT_WALL_DECORATION:
				result.addAll(objectsWallDecoration);
				break;
			case OBJECT_WALL_OBJECT:
				result.addAll(objectsWallObject);
				break;
			case CHARACTER:
				List<Character<?>> characters = new ArrayList<>();
				characters.addAll(ctx.getNpcs().getAll());
				characters.addAll(ctx.getPlayers().getAll());
				result.addAll(characters);
				break;
			case NPC:
				result.addAll(ctx.getNpcs().getAll());
				break;
			case PLAYER:
				result.addAll(ctx.getPlayers().getAll());
				break;
			case GROUND_ITEM:
				result.addAll(ctx.getGroundItems().getAll());
				break;
			}
			
		}
		return result;
	}
	
}
Edited by Botre
  • Like 2

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