From ac106a6c5080b202d552b35e5a5af16fd2299836 Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 25 Jan 2021 21:30:07 +0800 Subject: [PATCH] Add line of sight check to lookclose --- .../npc/ai/speech/CitizensSpeechFactory.java | 14 +++++++++----- .../java/net/citizensnpcs/trait/LookClose.java | 16 ++++++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/npc/ai/speech/CitizensSpeechFactory.java b/main/src/main/java/net/citizensnpcs/npc/ai/speech/CitizensSpeechFactory.java index 4e6b9c3bf..cda13cd02 100644 --- a/main/src/main/java/net/citizensnpcs/npc/ai/speech/CitizensSpeechFactory.java +++ b/main/src/main/java/net/citizensnpcs/npc/ai/speech/CitizensSpeechFactory.java @@ -4,20 +4,21 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; -import net.citizensnpcs.api.ai.speech.SpeechFactory; -import net.citizensnpcs.api.ai.speech.Talkable; -import net.citizensnpcs.api.ai.speech.VocalChord; - import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import com.google.common.base.Preconditions; +import net.citizensnpcs.api.ai.speech.SpeechFactory; +import net.citizensnpcs.api.ai.speech.Talkable; +import net.citizensnpcs.api.ai.speech.VocalChord; + public class CitizensSpeechFactory implements SpeechFactory { Map> registered = new HashMap>(); @Override public VocalChord getVocalChord(Class clazz) { + Preconditions.checkNotNull(clazz, "class cannot be null"); // Return a new instance of the VocalChord specified try { return clazz.newInstance(); @@ -31,8 +32,9 @@ public class CitizensSpeechFactory implements SpeechFactory { @Override public VocalChord getVocalChord(String name) { + Preconditions.checkNotNull(name, "name cannot be null"); // Check if VocalChord name is a registered type - if (isRegistered(name)) + if (isRegistered(name)) { // Return a new instance of the VocalChord specified try { return registered.get(name.toLowerCase()).newInstance(); @@ -41,6 +43,7 @@ public class CitizensSpeechFactory implements SpeechFactory { } catch (IllegalAccessException e) { e.printStackTrace(); } + } return null; } @@ -56,6 +59,7 @@ public class CitizensSpeechFactory implements SpeechFactory { @Override public boolean isRegistered(String name) { + Preconditions.checkNotNull(name, "name cannot be null"); return registered.containsKey(name.toLowerCase()); } diff --git a/main/src/main/java/net/citizensnpcs/trait/LookClose.java b/main/src/main/java/net/citizensnpcs/trait/LookClose.java index cd080b03a..c3d2f5661 100644 --- a/main/src/main/java/net/citizensnpcs/trait/LookClose.java +++ b/main/src/main/java/net/citizensnpcs/trait/LookClose.java @@ -49,14 +49,18 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable super("lookclose"); } + private boolean canSee(Player player) { + return realisticLooking && npc.getEntity() instanceof LivingEntity + ? ((LivingEntity) npc.getEntity()).hasLineOfSight(player) + : player != null && player.isValid(); + } + /** * Returns whether the target can be seen. Will use realistic line of sight if {@link #setRealisticLooking(boolean)} * is true. */ public boolean canSeeTarget() { - return realisticLooking && npc.getEntity() instanceof LivingEntity - ? ((LivingEntity) npc.getEntity()).hasLineOfSight(lookingAt) - : lookingAt != null && lookingAt.isValid(); + return canSee(lookingAt); } @Override @@ -120,7 +124,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable private boolean isInvisible(Player player) { return player.getGameMode() == GameMode.SPECTATOR || player.hasPotionEffect(PotionEffectType.INVISIBILITY) - || isPluginVanished(player); + || isPluginVanished(player) || !canSee(player); } private boolean isPluginVanished(Player player) { @@ -138,8 +142,8 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable private boolean isValid(Player entity) { return entity.isOnline() && entity.isValid() && entity.getWorld() == npc.getEntity().getWorld() - && !isInvisible(entity) - && entity.getLocation(PLAYER_LOCATION).distanceSquared(NPC_LOCATION) < range * range; + && entity.getLocation(PLAYER_LOCATION).distanceSquared(NPC_LOCATION) < range * range + && !isInvisible(entity); } @Override