Jump to content

Rune Essence Portal Interaction (NPC and Object). SOLVED


Lexhanatin

Recommended Posts

Previous thread: 

So it seems as if the portal in the rune essence mine changes types from object to NPC to whatever. I'm having trouble implementing a solution for this.

I've used some of the ideas explored in the previous thread, but my understanding is still weak.

I originally wanted to filter through both objects and NPCs but Chris told me this wasn't possible.

Instead he suggested for me to add both types to a list then filter through them.

// Is this the correct way of creating and adding to a list?

// Create
List <Object> portals = new ArrayList<>();

//Add
portals.add(getObjects().getAll().stream().filter(object -> object.getName().contains("Portal")).findAny());

//Add
portals.add(getNpcs().getAll().stream().filter(object -> object.getName().contains("Portal")).findAny());

I'm not sure how to interact with the list of type Object. I was thinking of  iterating through the list checking if the object was present, then interacting with it, but you can't interact unless the list is <Entity> not <Object>. If the list is <Entity> it's difficult to add to it...

Basically... I have no idea what I'm doing, and I need some more assistance :).

It should be common knowledge now, but in case it's forgotten, I take a while to learn, so pls be patient :).

EDIT

Read the 4th post for the solution.

Edited by Lexhanatin
Solved
Link to comment
Share on other sites


		NPC portalNpc = null;
		RS2Object portalObj = null;
		int portalNpcDist = Integer.MAX_VALUE;
		int portalObjDist = Integer.MAX_VALUE;
		Entity portalEntity;
		
		// Find objects
		portalNpc = npcs.closest("Portal");
		portalObj = objects.closest("Portal");
		
		// Check if either NPC or OBJ is valid
		if (portalNpc != null || portalObj != null) {
			
			// Calculate distance to NPC
			if (portalNpc != null) {
				portalNpcDist = map.distance(portalNpc);
			}
			
			// Calculate distance to OBJ
			if (portalObj != null) {
				portalObjDist = map.distance(portalObj);
			}
			
			// Check distance
			// Note: NPC or OBJ may be null
			if (portalNpcDist < portalObjDist) {
				// NPC is closer
				portalEntity = portalNpc;
			} else {
				// OBJ is same distance/closer
				portalEntity = portalObj;
			}
			
			// Finally, check portal
			if (portalEntity != null) {
				// Interact
				if (portalEntity.interact("Exit")) {
					// TODO: Conditional sleep
				}
			}
		}

 

Link to comment
Share on other sites

I found the problem in @liverare solution.

Simply the portals name isn't "Portal". Extra tags are added to the name preventing the below code from working.

portalNpc = npcs.closest("Portal");
portalObj = objects.closest("Portal");

Instead partial matching should be used instead.

portalNpc = getNpcs().closestThatContains("Portal");
portalObj = getObjects().closestThatContains("Portal");

This will always find a portal.

 

The second problem in the solution was the implementation of distance.

The current instance of portal would be of type NPC or Object, but when comparing distances, another instance was deemed closer. This would prevent interaction.

A possible reason for this could be that the current portal instance was being compared to a previous instances distance. I'm not sure.

I fixed this by taking out the distance comparison. Below is the find product.

private void bank() throws InterruptedException { 
    log("Bank method");
    if (getObjects().closest("Rune Essence") != null) {
      log("Rune Essence Exists");
      portalNpc = getNpcs().closestThatContains("Portal");
      portalObj = getObjects().closestThatContains("Portal");

      if (portalNpc != null || portalObj != null) {
        log("Entity not null");
        if (portalNpc != null) {
          log("Portal is NPC");
          portalEntity = portalNpc;
        }

        if (portalObj != null) {
          log("Portal is Object");
          portalEntity = portalObj;
        }
        if (portalEntity != null) {
          log("Portal Interaction");
          if (portalEntity.interact("Exit", "Use")) {
            new ConditionalSleep(8000) {
              @Override
                public boolean condition() throws InterruptedException {
                return !myPlayer().isMoving();
              }
            }.sleep();
          }
        }
      }
      else {
        log ("Entity null");
      }
    }

 

I would like to thank everyone for helping me solve this problem over the week. Hopefully this thread will help people with similar problems in the future.

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