Add line of sight check to lookclose

This commit is contained in:
fullwall 2021-01-25 21:30:07 +08:00
parent 60db622b68
commit ac106a6c50
2 changed files with 19 additions and 11 deletions

View File

@ -4,20 +4,21 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; 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.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import com.google.common.base.Preconditions; 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 { public class CitizensSpeechFactory implements SpeechFactory {
Map<String, Class<? extends VocalChord>> registered = new HashMap<String, Class<? extends VocalChord>>(); Map<String, Class<? extends VocalChord>> registered = new HashMap<String, Class<? extends VocalChord>>();
@Override @Override
public VocalChord getVocalChord(Class<? extends VocalChord> clazz) { public VocalChord getVocalChord(Class<? extends VocalChord> clazz) {
Preconditions.checkNotNull(clazz, "class cannot be null");
// Return a new instance of the VocalChord specified // Return a new instance of the VocalChord specified
try { try {
return clazz.newInstance(); return clazz.newInstance();
@ -31,8 +32,9 @@ public class CitizensSpeechFactory implements SpeechFactory {
@Override @Override
public VocalChord getVocalChord(String name) { public VocalChord getVocalChord(String name) {
Preconditions.checkNotNull(name, "name cannot be null");
// Check if VocalChord name is a registered type // Check if VocalChord name is a registered type
if (isRegistered(name)) if (isRegistered(name)) {
// Return a new instance of the VocalChord specified // Return a new instance of the VocalChord specified
try { try {
return registered.get(name.toLowerCase()).newInstance(); return registered.get(name.toLowerCase()).newInstance();
@ -41,6 +43,7 @@ public class CitizensSpeechFactory implements SpeechFactory {
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
return null; return null;
} }
@ -56,6 +59,7 @@ public class CitizensSpeechFactory implements SpeechFactory {
@Override @Override
public boolean isRegistered(String name) { public boolean isRegistered(String name) {
Preconditions.checkNotNull(name, "name cannot be null");
return registered.containsKey(name.toLowerCase()); return registered.containsKey(name.toLowerCase());
} }

View File

@ -49,14 +49,18 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
super("lookclose"); 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)} * Returns whether the target can be seen. Will use realistic line of sight if {@link #setRealisticLooking(boolean)}
* is true. * is true.
*/ */
public boolean canSeeTarget() { public boolean canSeeTarget() {
return realisticLooking && npc.getEntity() instanceof LivingEntity return canSee(lookingAt);
? ((LivingEntity) npc.getEntity()).hasLineOfSight(lookingAt)
: lookingAt != null && lookingAt.isValid();
} }
@Override @Override
@ -120,7 +124,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
private boolean isInvisible(Player player) { private boolean isInvisible(Player player) {
return player.getGameMode() == GameMode.SPECTATOR || player.hasPotionEffect(PotionEffectType.INVISIBILITY) return player.getGameMode() == GameMode.SPECTATOR || player.hasPotionEffect(PotionEffectType.INVISIBILITY)
|| isPluginVanished(player); || isPluginVanished(player) || !canSee(player);
} }
private boolean isPluginVanished(Player player) { private boolean isPluginVanished(Player player) {
@ -138,8 +142,8 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
private boolean isValid(Player entity) { private boolean isValid(Player entity) {
return entity.isOnline() && entity.isValid() && entity.getWorld() == npc.getEntity().getWorld() 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 @Override