Add /npc lookclose --targetnpcs

This commit is contained in:
fullwall 2022-12-27 14:22:27 +08:00
parent 4590193673
commit b8a608ec5c
4 changed files with 37 additions and 12 deletions

View File

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

View File

@ -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<UUID, PacketRotationSession> 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<Player> getNearbyPlayers() {
List<Player> options = Lists.newArrayList();
for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(NPC_LOCATION, range)) {
if (player == lookingAt || CitizensAPI.getNPCRegistry().getNPC(player) != null) {
Iterable<Player> 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;

View File

@ -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";

View File

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