Jump to content

Script can't properly identify 100% of the time what's attacking me


Recommended Posts

Posted (edited)

Hey all

 

For the last couple of days I've been experimenting and trying to find out how to properly identify who's attacking me. My script is struggling differentiating between Mobs attacking me and Players.

 

Here's my code:

                        //Monster Attacking:			
                        NPC monster = getNpcs().closest(npc -> npc.getName().equals("Skeleton") || npc.getName().equals("Scorpion"));
			if (myPlayer().isUnderAttack() && (monster != null) && monster.isInteracting(myPlayer())) {
                        //stuff
                        }

                        //Player Attacking:
                        //Use to be more complex but after taking out the conditions I had for players
                        //attacking it seamed to work better. So its literally just:
                        }else {
                        //stuff
                        }
                        

                        
Edited by Mushphang
Posted (edited)

Give this a whirl:

public final List<NPC> whoIsPossiblyAttackingMe() {
		return npcs.getAll().stream()
				.filter((npc) -> npc.isInteracting(myPlayer()))
				.collect(java.util.stream.Collectors.toList());
	}

This gets a list of all NPCs interacting with you (e.g., a goblin attacking you, a shop keeper responding to you, a pet following you, etc.) You can then loop through and check each of those npcs for a specific name. For instance, you know that a goblin would only be interacting with you because it's attacking you. However, a guard may have knocked you for failing to pickpocket him. So bare that in mind. :)

Edited by liverare
Posted

Give this a whirl:

public final List<NPC> whoIsPossiblyAttackingMe() {
		return npcs.getAll().stream()
				.filter((npc) -> npc.isInteracting(myPlayer()))
				.collect(java.util.stream.Collectors.toList());
	}

This gets a list of all NPCs interacting with you (e.g., a goblin attacking you, a shop keeper responding to you, a pet following you, etc.) You can then loop through and check each of those npcs for a specific name. For instance, you know that a goblin would only be interacting with you because it's attacking you. However, a guard may have knocked you for failing to pickpocket him. So bare that in mind. smile.png

 

Sorry for the late response! Thank you very much for both of your replies.

 

So far after implementing this all is working well so far! Thank you much. :)

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