Skip to content

Commit

Permalink
Add default poses
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jun 29, 2020
1 parent c7ab1a8 commit 0293ac1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
13 changes: 9 additions & 4 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1337,9 +1337,9 @@ public void playerlist(CommandContext args, CommandSender sender, NPC npc) {

@Command(
aliases = { "npc" },
usage = "pose (--save [name]|--assume [name]|--remove [name]) (-a)",
usage = "pose (--save [name] (-d)|--assume [name]|--remove [name]|--default [name]) (-a)",
desc = "Changes/Saves/Lists NPC's head pose(s)",
flags = "a",
flags = "ad",
modifiers = { "pose" },
min = 1,
max = 2,
Expand All @@ -1353,10 +1353,16 @@ public void pose(CommandContext args, CommandSender sender, NPC npc) throws Comm
if (args.getSenderLocation() == null)
throw new ServerCommandException();

if (trait.addPose(args.getFlag("save"), args.getSenderLocation())) {
if (trait.addPose(args.getFlag("save"), args.getSenderLocation(), args.hasFlag('d'))) {
Messaging.sendTr(sender, Messages.POSE_ADDED);
} else
throw new CommandException(Messages.POSE_ALREADY_EXISTS, args.getFlag("save"));
} else if (args.hasValueFlag("default")) {
String pose = args.getFlag("default");
if (!trait.hasPose(pose))
throw new CommandException(Messages.POSE_MISSING, pose);
trait.setDefaultPose(pose);
Messaging.sendTr(sender, Messages.DEFAULT_POSE_SET, pose);
} else if (args.hasValueFlag("assume")) {
String pose = args.getFlag("assume");
if (pose.isEmpty())
Expand All @@ -1376,7 +1382,6 @@ public void pose(CommandContext args, CommandSender sender, NPC npc) throws Comm
trait.describe(sender, args.getInteger(1, 1));
}

// Assume Player's pose
if (!args.hasFlag('a'))
return;
if (args.getSenderLocation() == null)
Expand Down
7 changes: 0 additions & 7 deletions main/src/main/java/net/citizensnpcs/trait/LookClose.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,6 @@ public void run() {
if (npc.getNavigator().isNavigating() && Setting.DISABLE_LOOKCLOSE_WHILE_NAVIGATING.asBoolean()) {
return;
}
// TODO: remove in a later version, defaults weren't saving properly
if (randomPitchRange == null || randomPitchRange.length != 2) {
randomPitchRange = new float[] { -10, 0 };
}
if (randomYawRange == null || randomYawRange.length != 2) {
randomYawRange = new float[] { 0, 360 };
}
npc.getEntity().getLocation(NPC_LOCATION);
if (hasInvalidTarget()) {
findNewTarget();
Expand Down
38 changes: 31 additions & 7 deletions main/src/main/java/net/citizensnpcs/trait/Poses.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.DataKey;
Expand All @@ -25,6 +26,8 @@
*/
@TraitName("poses")
public class Poses extends Trait {
@Persist
private String defaultPose;
private final Map<String, Pose> poses = Maps.newHashMap();

public Poses() {
Expand All @@ -37,11 +40,23 @@ public Poses() {
* @return whether the pose has already been added
*/
public boolean addPose(String name, Location location) {
return addPose(name, location, false);
}

/**
* Add a {@link Pose}
*
* @return whether the pose has already been added
*/
public boolean addPose(String name, Location location, boolean isDefault) {
name = name.toLowerCase();
Pose newPose = new Pose(name, location.getPitch(), location.getYaw());
if (poses.containsValue(newPose) || poses.containsKey(name))
return false;
poses.put(name, newPose);
if (isDefault) {
defaultPose = name;
}
return true;
}

Expand Down Expand Up @@ -82,12 +97,7 @@ public void describe(CommandSender sender, int page) throws CommandException {
}

public Pose getPose(String name) {
for (Pose pose : poses.values()) {
if (pose.getName().equalsIgnoreCase(name)) {
return pose;
}
}
return null;
return poses.get(name.toLowerCase());
}

public boolean hasPose(String pose) {
Expand All @@ -100,7 +110,7 @@ public void load(DataKey key) throws NPCLoadException {
for (DataKey sub : key.getRelative("list").getIntegerSubKeys())
try {
String[] parts = sub.getString("").split(";");
poses.put(parts[0], new Pose(parts[0], Float.valueOf(parts[1]), Float.valueOf(parts[2])));
poses.put(parts[0].toLowerCase(), new Pose(parts[0], Float.valueOf(parts[1]), Float.valueOf(parts[2])));
} catch (NumberFormatException e) {
Messaging.logTr(Messages.SKIPPING_INVALID_POSE, sub.name(), e.getMessage());
}
Expand All @@ -110,6 +120,16 @@ public boolean removePose(String pose) {
return poses.remove(pose.toLowerCase()) != null;
}

@Override
public void run() {
if (!hasPose(defaultPose))
return;
if (!npc.getNavigator().isNavigating()
&& (!npc.hasTrait(LookClose.class) || npc.getTrait(LookClose.class).canSeeTarget())) {
assumePose(defaultPose);
}
}

@Override
public void save(DataKey key) {
key.removeKey("list");
Expand All @@ -119,4 +139,8 @@ public void save(DataKey key) {
i++;
}
}

public void setDefaultPose(String pose) {
this.defaultPose = pose;
}
}
1 change: 1 addition & 0 deletions main/src/main/java/net/citizensnpcs/util/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class Messages {
public static final String CURRENT_SCRIPTS = "citizens.commands.npc.script.current-scripts";
public static final String CURRENT_WAYPOINT_PROVIDER = "citizens.waypoints.current-provider";
public static final String DATABASE_CONNECTION_FAILED = "citizens.notifications.database-connection-failed";
public static final String DEFAULT_POSE_SET = "citizens.commands.npc.pose.default-pose-set";
public static final String DELAY_TRIGGER_PROMPT = "citizens.editors.waypoints.triggers.delay.prompt";
public static final String ELDER_SET = "citizens.commands.npc.guardian.elder-set";
public static final String ELDER_UNSET = "citizens.commands.npc.guardian.elder-unset";
Expand Down
1 change: 1 addition & 0 deletions main/src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ citizens.commands.npc.pose.added=Pose added.
citizens.commands.npc.pose.already-exists=The pose [[{0}]] already exists.
citizens.commands.npc.pose.invalid-name=Invalid pose name.
citizens.commands.npc.pose.missing=The pose [[{0}]] does not exist.
citizens.commands.npc.pose.default-pose-set=Default pose set to [[{0}]].
citizens.commands.npc.pose.removed=Pose removed.
citizens.commands.npc.powered.set=[[{0}]] will now be powered.
citizens.commands.npc.powered.stopped=[[{0}]] will no longer be powered.
Expand Down

0 comments on commit 0293ac1

Please sign in to comment.