Jump to content

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


Mushphang

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

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