d0zza Posted February 19, 2017 Share Posted February 19, 2017 (edited) So I'm trying to make a combat script. If another player is in combat with the closest monster to me how do I make my script attack the next closest monster? I've tried making a function to do this: public NPC closestAttackable() { List<NPC> npcs = getNpcs().getAll(); for (i = 0; i < npcs.size(); i++) { if (!npcs.get(i).isUnderAttack() && myPlayer().getArea(7).contains(npcs.get(i))) { return npcs.get(i); } } return null; } but this seems to always return null, any advice? Edited February 19, 2017 by d0zza Quote Link to comment Share on other sites More sharing options...
naaiz Posted February 19, 2017 Share Posted February 19, 2017 I'd personally use a filter for this, not a for loop. something like: npcname = npcs.closest(n -> n.getName().equals("npcname") && areawithnpcs.contains(n) && !n.isHitBarVisible() && n.getHealthPercent() > 0 && !n.isUnderAttack()); if(npcname!= null && !myPlayer().isUnderAttack() && !myPlayer().isMoving()) { npcname.interact("Attack"); } 1 Quote Link to comment Share on other sites More sharing options...
Saiyan Posted February 19, 2017 Share Posted February 19, 2017 Optional<NPC> getAll = getNpcs().getAll().stream().filter(n -> !n.isUnderAttack() && n.getHealthPercent() > 0 && n.getName().equals("X")).findFirst(); if(getAll().get != null) { for(Player player : getPlayers().getAll()) { if(player != myPlayer() && !player.isInteracting(getAll().get()) { getAll().get().interact("Attack") cond sleep } } } probs a bad way to do it but idk if you dont use the optional you can do the same java stream but add .orElse(null) at the end 1 Quote Link to comment Share on other sites More sharing options...
Polymorphism Posted February 20, 2017 Share Posted February 20, 2017 I just set a field variable for the filter such as below private Predicate<NPC> npcFilter = n -> n != null && myPlayer().getArea(6).contains(n) && n.getName().contains("name") && n.hasAction("Attack") && n.isAttackable() && !n.isUnderAttack(); NPC npc = s.npcs.getAll().stream().filter(npcFilter).findFirst().get(); if (npc.interact("Attack")) { new ConditionalSleep(6500, 500) { @Override public boolean condition() throws InterruptedException { return combat.isFighting() || myPlayer().isInteracting(npc); } }.sleep(); //max sleep 6.5s, recheck every 500ms. Stops sleeping if fighting or interacting with the filtered npc } Quote Link to comment Share on other sites More sharing options...
Polymorphism Posted February 20, 2017 Share Posted February 20, 2017 I just set a field variable for the filter such as below private Predicate<NPC> npcFilter = n -> n != null && myPlayer().getArea(6).contains(n) && n.getName().contains("name") && n.hasAction("Attack") && n.isAttackable() && !n.isUnderAttack(); NPC npc = s.npcs.getAll().stream().filter(npcFilter).findFirst().get(); if (npc.interact("Attack")) { new ConditionalSleep(6500, 500) { @Override public boolean condition() throws InterruptedException { return combat.isFighting() || myPlayer().isInteracting(npc); } }.sleep(); //max sleep 6.5s, recheck every 500ms. Stops sleeping if fighting or interacting with the filtered npc } Edit: It'd also be useful to add map#canReach to the Predicate and sort the NPCs in the stream by distance .sorted((n1, n2) -> Integer.compare(s.map.realDistance(n1), s.map.realDistance(n2))) Quote Link to comment Share on other sites More sharing options...
Explv Posted February 20, 2017 Share Posted February 20, 2017 (edited) On 2/19/2017 at 8:35 AM, d0zza said: So I'm trying to make a combat script. If another player is in combat with the closest monster to me how do I make my script attack the next closest monster? I've tried making a function to do this: but this seems to always return null, any advice? Not too sure what everyone else in this thread is smoking, all you need is: NPC cow = getNpcs().closest(npc -> npc.getName().startsWith("Cow") && npc.isAttackable()); The isAttackable() method in the Character class returns true if: It is multiway combat and the character has health > 0 OR The character has health > 0 and is not under attack OR The character is interacting with your player Edited February 20, 2017 by Explv 4 Quote Link to comment Share on other sites More sharing options...
Shudsy Posted February 20, 2017 Share Posted February 20, 2017 (edited) Some of the code suggested here is way over the top lol. Could be done so much more simple like Explv's suggestion Edited February 20, 2017 by Shudsy 2 Quote Link to comment Share on other sites More sharing options...
lrdblk Posted February 21, 2017 Share Posted February 21, 2017 Quote Link to comment Share on other sites More sharing options...