Add random looking feature to lookclose

This commit is contained in:
fullwall 2019-04-26 21:14:15 +08:00
parent 4c7bc91eaa
commit 53efb60b68
5 changed files with 106 additions and 9 deletions

View File

@ -79,6 +79,8 @@ public class Settings {
DEFAULT_PATH_DISTANCE_MARGIN("npc.pathfinding.default-path-distance-margin", 1),
DEFAULT_PATHFINDER_UPDATE_PATH_RATE("npc.pathfinding.update-path-rate", 20),
DEFAULT_PATHFINDING_RANGE("npc.default.pathfinding.range", 25F),
DEFAULT_RANDOM_LOOK_CLOSE("npc.default.look-close.random-look-enabled", false),
DEFAULT_RANDOM_LOOK_DELAY("npc.default.look-close.random-look-delay", 60),
DEFAULT_RANDOM_TALKER("npc.default.random-talker", true),
DEFAULT_REALISTIC_LOOKING("npc.default.realistic-looking", false),
DEFAULT_STATIONARY_TICKS("npc.default.stationary-ticks", -1),

View File

@ -808,16 +808,62 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "lookclose",
usage = "lookclose --(random|r)look [true|false] --(random|r)pitchrange [min,max] --(random|r)yawrange [min,max]",
desc = "Toggle whether a NPC will look when a player is near",
modifiers = { "lookclose", "look", "rotate" },
min = 1,
max = 1,
permission = "citizens.npc.lookclose")
public void lookClose(CommandContext args, CommandSender sender, NPC npc) {
Messaging.sendTr(sender,
npc.getTrait(LookClose.class).toggle() ? Messages.LOOKCLOSE_SET : Messages.LOOKCLOSE_STOPPED,
npc.getName());
public void lookClose(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
boolean toggle = true;
if (args.hasAnyValueFlag("randomlook", "rlook")) {
boolean enableRandomLook = Boolean.parseBoolean(args.getFlag("randomlook", args.getFlag("rlook")));
npc.getTrait(LookClose.class).setRandomLook(enableRandomLook);
Messaging.sendTr(sender,
enableRandomLook ? Messages.LOOKCLOSE_RANDOM_SET : Messages.LOOKCLOSE_RANDOM_STOPPED,
npc.getName());
toggle = false;
}
if (args.hasAnyValueFlag("randomlookdelay", "rlookdelay")) {
int delay = Integer.parseInt(args.getFlag("randomlookdelay", args.getFlag("rlookdelay")));
delay = Math.max(1, delay);
npc.getTrait(LookClose.class).setRandomLookDelay(delay);
Messaging.sendTr(sender, Messages.LOOKCLOSE_RANDOM_DELAY_SET, npc.getName(), delay);
toggle = false;
}
if (args.hasAnyValueFlag("randompitchrange", "rpitchrange")) {
String flag = args.getFlag("randompitchrange", args.getFlag("rpitchrange"));
try {
String[] parts = flag.split(",");
float min = Float.parseFloat(parts[0]), max = Float.parseFloat(parts[1]);
if (min > max)
throw new IllegalArgumentException();
npc.getTrait(LookClose.class).setRandomLookPitchRange(min, max);
} catch (Exception e) {
throw new CommandException(Messaging.tr(Messages.ERROR_SETTING_LOOKCLOSE_RANGE, flag));
}
Messaging.sendTr(sender, Messages.LOOKCLOSE_RANDOM_PITCH_RANGE_SET, npc.getName(), flag);
toggle = false;
}
if (args.hasAnyValueFlag("randomyawrange", "ryawrange")) {
String flag = args.getFlag("randomyawrange", args.getFlag("ryawrange"));
try {
String[] parts = flag.split(",");
float min = Float.parseFloat(parts[0]), max = Float.parseFloat(parts[1]);
if (min > max)
throw new IllegalArgumentException();
npc.getTrait(LookClose.class).setRandomLookYawRange(min, max);
} catch (Exception e) {
throw new CommandException(Messaging.tr(Messages.ERROR_SETTING_LOOKCLOSE_RANGE, flag));
}
Messaging.sendTr(sender, Messages.LOOKCLOSE_RANDOM_YAW_RANGE_SET, npc.getName(), flag);
toggle = false;
}
if (toggle) {
Messaging.sendTr(sender,
npc.getTrait(LookClose.class).toggle() ? Messages.LOOKCLOSE_SET : Messages.LOOKCLOSE_STOPPED,
npc.getName());
}
}
@Command(
@ -1747,7 +1793,7 @@ public class NPCCommands {
if (target != null)
context.addRecipient(target.getEntity());
} else {
Player player = Bukkit.getPlayer(args.getFlag("target"));
Player player = Bukkit.getPlayerExact(args.getFlag("target"));
if (player != null) {
context.addRecipient((Entity) player);
}

View File

@ -3,6 +3,7 @@ package net.citizensnpcs.trait;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -17,7 +18,6 @@ import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.command.CommandConfigurable;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
@ -29,10 +29,19 @@ import net.citizensnpcs.util.Util;
public class LookClose extends Trait implements Toggleable, CommandConfigurable {
@Persist("enabled")
private boolean enabled = Setting.DEFAULT_LOOK_CLOSE.asBoolean();
@Persist
private boolean enableRandomLook = Setting.DEFAULT_RANDOM_LOOK_CLOSE.asBoolean();
private Player lookingAt;
@Persist
private int randomLookDelay = Setting.DEFAULT_RANDOM_LOOK_DELAY.asInt();
@Persist
private float[] randomPitchRange = { -10, 0 };
@Persist
private float[] randomYawRange = { 0, 360 };
private double range = Setting.DEFAULT_LOOK_CLOSE_RANGE.asDouble();
@Persist("realisticlooking")
private boolean realisticLooking = Setting.DEFAULT_REALISTIC_LOOKING.asBoolean();
private int t;
public LookClose() {
super("lookclose");
@ -94,8 +103,8 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
}
@Override
public void load(DataKey key) throws NPCLoadException {
range = key.getDouble("range", range);
public void load(DataKey key) {
range = key.getDouble("range");
}
public void lookClose(boolean lookClose) {
@ -107,6 +116,13 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
lookingAt = null;
}
private void randomLook() {
Random rand = new Random();
float pitch = rand.doubles(randomPitchRange[0], randomPitchRange[1]).iterator().next().floatValue(),
yaw = rand.doubles(randomYawRange[0], randomYawRange[1]).iterator().next().floatValue();
Util.assumePose(npc.getEntity(), yaw, pitch);
}
@Override
public void run() {
if (!enabled || !npc.isSpawned() || npc.getNavigator().isNavigating())
@ -115,6 +131,11 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
if (hasInvalidTarget()) {
findNewTarget();
}
if (lookingAt == null && enableRandomLook && t <= 0) {
randomLook();
t = randomLookDelay;
}
t--;
if (lookingAt != null && canSeeTarget()) {
Util.faceEntity(npc.getEntity(), lookingAt);
if (npc.getEntity().getType().name().toLowerCase().contains("shulker")) {
@ -129,6 +150,22 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
key.setDouble("range", range);
}
public void setRandomLook(boolean enableRandomLook) {
this.enableRandomLook = enableRandomLook;
}
public void setRandomLookDelay(int delay) {
this.randomLookDelay = delay;
}
public void setRandomLookPitchRange(float min, float max) {
this.randomPitchRange = new float[] { min, max };
}
public void setRandomLookYawRange(float min, float max) {
this.randomYawRange = new float[] { min, max };
}
public void setRange(int range) {
this.range = range;
}

View File

@ -73,6 +73,7 @@ public class Messages {
public static final String ERROR_INITALISING_SUB_PLUGIN = "citizens.sub-plugins.error-on-load";
public static final String ERROR_LOADING_ECONOMY = "citizens.economy.error-loading";
public static final String ERROR_SETTING_ENTITY_PERSISTENT = "citizens.nms-errors.error-setting-persistent";
public static final String ERROR_SETTING_LOOKCLOSE_RANGE = "citizens.commands.npc.lookclose.error-random-range";
public static final String ERROR_SPAWNING_CUSTOM_ENTITY = "citizens.nms-errors.spawning-custom-entity";
public static final String ERROR_STOPPING_NETWORK_THREADS = "citizens.nms-errors.stopping-network-threads";
public static final String ERROR_UPDATING_NAVIGATION_WORLD = "citizens.nms-errors.updating-navigation-world";
@ -149,6 +150,11 @@ public class Messages {
public static final String LOAD_UNKNOWN_NPC_TYPE = "citizens.notifications.unknown-npc-type";
public static final String LOADING_SUB_PLUGIN = "citizens.sub-plugins.load";
public static final String LOCALE_NOTIFICATION = "citizens.notifications.locale";
public static final String LOOKCLOSE_RANDOM_DELAY_SET = "citizens.commands.npc.lookclose.random-look-delay-set";
public static final String LOOKCLOSE_RANDOM_PITCH_RANGE_SET = "citizens.commands.npc.lookclose.random-pitch-range-set";
public static final String LOOKCLOSE_RANDOM_SET = "citizens.commands.npc.lookclose.random-set";
public static final String LOOKCLOSE_RANDOM_STOPPED = "citizens.commands.npc.lookclose.random-stopped";
public static final String LOOKCLOSE_RANDOM_YAW_RANGE_SET = "citizens.commands.npc.lookclose.random-yaw-range-set";
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 METADATA_SET = "citizens.commands.npc.metadata.set";

View File

@ -71,6 +71,12 @@ citizens.commands.npc.leashable.stopped=[[{0}]] is no longer leashable.
citizens.commands.npc.llama.strength-set=Llama strength set to [[{0}]].
citizens.commands.npc.llama.color-set=Llama color set to [[{0}]].
citizens.commands.npc.llama.invalid-color=Invalid llama color given. Valid colors are: [[{0}]].
citizens.commands.npc.lookclose.random-look-delay-set=[[{0}]] will now randomly look every [[{1}]] ticks.
citizens.commands.npc.lookclose.random-pitch-range-set=[[{0}]] will now randomly choose a pitch in the range [{1}].
citizens.commands.npc.lookclose.random-yaw-range-set=[[{0}]] will now randomly choose a yaw in the range [{1}].
citizens.commands.npc.lookclose.random-set=[[{0}]] will now randomly look around.
citizens.commands.npc.lookclose.random-stopped=[[{0}]] will no longer randomly look around.
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.stopped=[[{0}]] will no longer rotate when players are nearby.
citizens.commands.npc.metadata.set=[[{0}]] set to [[{1}]].