From b8a608ec5c6fdb932d7063b5ac0be89b9020295a Mon Sep 17 00:00:00 2001 From: fullwall Date: Tue, 27 Dec 2022 14:22:27 +0800 Subject: [PATCH] Add /npc lookclose --targetnpcs --- .../citizensnpcs/commands/NPCCommands.java | 13 ++++++-- .../net/citizensnpcs/trait/LookClose.java | 32 +++++++++++++------ .../java/net/citizensnpcs/util/Messages.java | 2 ++ .../src/main/resources/messages_en.properties | 2 ++ 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 0e94ab2ff..9757d19fa 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -1331,7 +1331,7 @@ public class NPCCommands { @Command( aliases = { "npc" }, - usage = "lookclose --range [range] -r[ealistic looking] --randomlook [true|false] --randomswitchtargets [true|false] --randompitchrange [min,max] --randomyawrange [min,max] --disablewhennavigating [true|false]", + usage = "lookclose --range [range] -r[ealistic looking] --randomlook [true|false] --randomswitchtargets [true|false] --randompitchrange [min,max] --randomyawrange [min,max] --disablewhennavigating [true|false] --targetnpcs [true|false]", desc = "Toggle whether a NPC will look when a player is near", modifiers = { "lookclose", "look" }, min = 1, @@ -1342,8 +1342,8 @@ public class NPCCommands { @Flag({ "randomlook", "rlook" }) Boolean randomlook, @Flag("range") Double range, @Flag("randomlookdelay") Integer randomLookDelay, @Flag("randomyawrange") String randomYaw, @Flag("randompitchrange") String randomPitch, @Flag("randomswitchtargets") Boolean randomSwitchTargets, - @Flag("disablewhennavigating") Boolean disableWhenNavigating, @Flag("perplayer") Boolean perPlayer) - throws CommandException { + @Flag("disablewhennavigating") Boolean disableWhenNavigating, @Flag("perplayer") Boolean perPlayer, + @Flag("targetnpcs") Boolean targetNPCs) throws CommandException { boolean toggle = true; LookClose trait = npc.getOrAddTrait(LookClose.class); if (randomlook != null) { @@ -1364,6 +1364,13 @@ public class NPCCommands { : Messages.LOOKCLOSE_RANDOM_TARGET_SWITCH_DISABLED, npc.getName()); toggle = false; } + if (targetNPCs != null) { + trait.setTargetNPCs(targetNPCs); + Messaging.sendTr(sender, + targetNPCs ? Messages.LOOKCLOSE_TARGET_NPCS_SET : Messages.LOOKCLOSE_TARGET_NPCS_UNSET, + npc.getName()); + toggle = false; + } if (disableWhenNavigating != null) { trait.setDisableWhileNavigating(disableWhenNavigating); Messaging.sendTr(sender, disableWhenNavigating ? Messages.LOOKCLOSE_DISABLE_WHEN_NAVIGATING diff --git a/main/src/main/java/net/citizensnpcs/trait/LookClose.java b/main/src/main/java/net/citizensnpcs/trait/LookClose.java index 17255a837..5c7376e42 100644 --- a/main/src/main/java/net/citizensnpcs/trait/LookClose.java +++ b/main/src/main/java/net/citizensnpcs/trait/LookClose.java @@ -4,10 +4,12 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; +import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.metadata.MetadataValue; @@ -57,6 +59,8 @@ public class LookClose extends Trait implements Toggleable { private boolean realisticLooking = Setting.DEFAULT_REALISTIC_LOOKING.asBoolean(); private final Map sessions = Maps.newHashMapWithExpectedSize(4); private int t; + @Persist("targetnpcs") + private boolean targetNPCs; public LookClose() { super("lookclose"); @@ -130,12 +134,9 @@ public class LookClose extends Trait implements Toggleable { } } else { double min = range; - for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(NPC_LOCATION, range)) { - Location location = player.getLocation(CACHE_LOCATION); - if (location.getWorld() != NPC_LOCATION.getWorld()) - continue; - double dist = location.distance(NPC_LOCATION); - if (dist > min || CitizensAPI.getNPCRegistry().getNPC(player) != null || isInvisible(player)) + for (Player player : getNearbyPlayers()) { + double dist = player.getLocation(CACHE_LOCATION).distance(NPC_LOCATION); + if (dist > min) continue; min = dist; lookingAt = player; @@ -154,12 +155,17 @@ public class LookClose extends Trait implements Toggleable { private List getNearbyPlayers() { List options = Lists.newArrayList(); - for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(NPC_LOCATION, range)) { - if (player == lookingAt || CitizensAPI.getNPCRegistry().getNPC(player) != null) { + Iterable nearby = targetNPCs + ? npc.getEntity().getNearbyEntities(range, range, range).stream() + .filter(e -> e.getType() == EntityType.PLAYER && e.getWorld() == NPC_LOCATION.getWorld()) + .map(e -> (Player) e).collect(Collectors.toList()) + : CitizensAPI.getLocationLookup().getNearbyPlayers(NPC_LOCATION, range); + for (Player player : nearby) { + if (player == lookingAt || (!targetNPCs && CitizensAPI.getNPCRegistry().getNPC(player) != null)) continue; - } if (player.getLocation().getWorld() != NPC_LOCATION.getWorld() || isInvisible(player)) continue; + options.add(player); } return options; @@ -337,6 +343,14 @@ public class LookClose extends Trait implements Toggleable { this.realisticLooking = realistic; } + public void setTargetNPCs(boolean target) { + this.targetNPCs = target; + } + + public boolean targetNPCs() { + return targetNPCs; + } + @Override public boolean toggle() { enabled = !enabled; diff --git a/main/src/main/java/net/citizensnpcs/util/Messages.java b/main/src/main/java/net/citizensnpcs/util/Messages.java index d8eb531cf..6bb3742cf 100644 --- a/main/src/main/java/net/citizensnpcs/util/Messages.java +++ b/main/src/main/java/net/citizensnpcs/util/Messages.java @@ -225,6 +225,8 @@ public class Messages { public static final String LOOKCLOSE_REALISTIC_LOOK_UNSET = "citizens.commands.npc.lookclose.rl-unset"; public static final String LOOKCLOSE_SET = "citizens.commands.npc.lookclose.set"; public static final String LOOKCLOSE_STOPPED = "citizens.commands.npc.lookclose.stopped"; + public static final String LOOKCLOSE_TARGET_NPCS_SET = "citizens.commands.npc.lookclose.target-npcs-set"; + public static final String LOOKCLOSE_TARGET_NPCS_UNSET = "citizens.commands.npc.lookclose.target-npcs-unset"; public static final String METADATA_SET = "citizens.commands.npc.metadata.set"; public static final String METADATA_UNSET = "citizens.commands.npc.metadata.unset"; public static final String METRICS_ERROR_NOTIFICATION = "citizens.notifications.metrics-load-error"; diff --git a/main/src/main/resources/messages_en.properties b/main/src/main/resources/messages_en.properties index 9ba746eef..1347f3856 100644 --- a/main/src/main/resources/messages_en.properties +++ b/main/src/main/resources/messages_en.properties @@ -153,6 +153,8 @@ citizens.commands.npc.lookclose.perplayer-unset=[[{0}]] will no longer use per p citizens.commands.npc.lookclose.perplayer-set=[[{0}]] will now use per player looking. citizens.commands.npc.lookclose.error-random-range=Invalid range [[{0}]]. Use the format `min,max`. citizens.commands.npc.lookclose.set=[[{0}]] will now rotate when players are nearby. +citizens.commands.npc.lookclose.target-npcs-set=[[{0}]] will now look at NPCs. +citizens.commands.npc.lookclose.target-npcs-unset=[[{0}]] will no longer look at NPCs. citizens.commands.npc.lookclose.enable-when-navigating=[[{0}]] will now look close when navigating. citizens.commands.npc.lookclose.disable-when-navigating=[[{0}]] will no longer look close when navigating. citizens.commands.npc.lookclose.random-target-switch-enabled=[[{0}]] will now randomly switch targets depending on the random look delay.