Jump to content

Checking if a door is closed and open if it's closed


Recommended Posts

Posted (edited)

If you are using the code for one specific door at a specific location you could create a class to hold the name and position, such as this: 

package com.scripts.mrsdefnerd.dnfishcooker.utils;

import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.RS2Object;

import com.scripts.mrsdefnerd.dnfishcooker.core.Main;

public abstract class ObstacleObject {
	
	private String name;
	private Position location;
	public ObstacleObject(String name, Position location) {
		this.name = name;
		this.location = location;
	}
	
	public final String getName() {
		return name;
	}
	
	public final Position getLocation() {
		return location;
	}
	
	public final RS2Object get(Script ctx) {
		for (RS2Object object : ctx.closestObjectListForName(getName()))
			if (object != null && object.getPosition().equals(getLocation()))
				return object;
		return null;
	}
	
	public abstract boolean validate(Script ctx);

}

Declare this somewhere (I guess you can just declare it at the top of the class):

ObstacleObject door; 

Then you can do something like this in you startup method:

door = new ObstacleObject("Door", new Position(2816, 3438, 0)) {

		@Override
		public boolean validate(Script ctx) {
			return this.get(ctx) != null && !this.get(ctx).getDefinition().getActions()[1].toLowerCase().contains("close");
		}
		
	}

What the above code does is that it overrides the validate method to your custom prefference (it will check if the object at that position is not null, and make sure that the object does not have an action that contains close). 

 

EDIT: The "Door" is the name of the object, the new Position() is the position of the object, and the validate is the requirements for the object to retrieved (This way you can support most objects)

 

This is probably a bit overdone, but if you have to handle alot of different objects I guess its a good way to do it.

 

To use the code simply do (I am assuming you are calling the door class from the class that extends script):

RS2Object doorObject = door.get(this);
Edited by Mrsdefnerd
Posted

Yep, but its not really needed if you know what objects you are interacting with (lets say a specific door)

	public final RS2Object get(Script ctx) {
		for (RS2Object object : ctx.closestObjectListForName(getName()))
			if (object != null && object.getPosition().equals(getLocation()))
				return object;
		return null;
	}
	

Just a little enhancement by me:

public WallObject getWallObjectOnPosition(Position position, String... names) {
        try {

            if (position == null) {
                return null;
            }
            if (client.getCurrentRegion() == null || client.getCurrentRegion().instance == null || client.getCurrentRegion().instance.getTiles() == null) {
                return null;
            }

            int localX = position.getLocalX(bot);
            int localY = position.getLocalY(bot);
            if (localX <= 104 && localY <= 104 && localX >= 0 && localY >= 0) {
                if (client.getCurrentRegion().instance.getTiles()[position.getZ()][localX][localY] == null) {
                    return null;
                }
                XWallObject wallObject = client.getCurrentRegion().instance.getTiles()[position.getZ()][localX][localY].getWallObject();
                WallObject wrap = WallObject.wrap(wallObject, position.getZ());

                if (names.length == 0 || Arrays.asList(names).contains(wrap.getDefinition().getName())) {
                    return wallObject == null ? null : wrap;
                }

            }
        } catch (Exception ignored) {
        }
        return null;
    }
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...