mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-01-01 14:08:08 +01:00
Change Positions to Poses. Use /npc pose
This commit is contained in:
parent
669da4fc95
commit
b9ce5d2f9b
@ -27,12 +27,12 @@ import net.citizensnpcs.trait.Behaviour;
|
||||
import net.citizensnpcs.trait.Controllable;
|
||||
import net.citizensnpcs.trait.CurrentLocation;
|
||||
import net.citizensnpcs.trait.LookClose;
|
||||
import net.citizensnpcs.trait.Positions;
|
||||
import net.citizensnpcs.trait.Poses;
|
||||
import net.citizensnpcs.trait.Powered;
|
||||
import net.citizensnpcs.trait.VillagerProfession;
|
||||
import net.citizensnpcs.util.Messaging;
|
||||
import net.citizensnpcs.util.Paginator;
|
||||
import net.citizensnpcs.util.Position;
|
||||
import net.citizensnpcs.util.Pose;
|
||||
import net.citizensnpcs.util.StringHelper;
|
||||
import net.citizensnpcs.util.Util;
|
||||
|
||||
@ -455,24 +455,24 @@ public class NPCCommands {
|
||||
|
||||
@Command(
|
||||
aliases = { "npc" },
|
||||
usage = "position (--save [name]|--load [name]|--remove [name]|--list) (-a)",
|
||||
desc = "Changes/Saves/Lists NPC's head position(s)",
|
||||
usage = "pose (--save [name]|--load [name]|--remove [name]|--list) (-a)",
|
||||
desc = "Changes/Saves/Lists NPC's head pose(s)",
|
||||
flags = "a",
|
||||
modifiers = { "position" },
|
||||
modifiers = { "pose" },
|
||||
min = 1,
|
||||
max = 2,
|
||||
permission = "npc.position")
|
||||
permission = "npc.pose")
|
||||
@Requirements(selected = true, ownership = true, types = { EntityType.PLAYER })
|
||||
public void position(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
|
||||
|
||||
Positions trait = npc.getTrait(Positions.class);
|
||||
Poses trait = npc.getTrait(Poses.class);
|
||||
|
||||
if (args.hasValueFlag("save")) {
|
||||
if (!args.getFlag("save").isEmpty()) {
|
||||
if (sender instanceof Player) {
|
||||
if (trait.addPosition(args.getFlag("save"), ((Player) sender).getLocation()))
|
||||
Messaging.sendF(sender, ChatColor.GREEN + "Position added.");
|
||||
else throw new CommandException("The position '" + args.getFlag("load") + "' already exists.");
|
||||
if (trait.addPose(args.getFlag("save"), ((Player) sender).getLocation()))
|
||||
Messaging.sendF(sender, ChatColor.GREEN + "Pose added.");
|
||||
else throw new CommandException("The pose '" + args.getFlag("load") + "' already exists.");
|
||||
}
|
||||
else
|
||||
throw new CommandException("This command may be used in-game only.");
|
||||
@ -483,10 +483,10 @@ public class NPCCommands {
|
||||
|
||||
else if (args.hasValueFlag("load")) {
|
||||
if (!args.getFlag("load").isEmpty()) {
|
||||
if (trait.getPosition(args.getFlag("load")) != null)
|
||||
trait.assumePosition(trait.getPosition(args.getFlag("load")));
|
||||
if (trait.getPose(args.getFlag("load")) != null)
|
||||
trait.assumePose(trait.getPose(args.getFlag("load")));
|
||||
else
|
||||
throw new CommandException("The position '" + args.getFlag("load") + "' does not exist.");
|
||||
throw new CommandException("The pose '" + args.getFlag("load") + "' does not exist.");
|
||||
}
|
||||
else
|
||||
throw new CommandException("Invalid name.");
|
||||
@ -494,20 +494,20 @@ public class NPCCommands {
|
||||
|
||||
else if (args.hasValueFlag("remove")) {
|
||||
if (!args.getFlag("remove").isEmpty()) {
|
||||
if (trait.removePosition(trait.getPosition(args.getFlag("remove"))))
|
||||
if (trait.removePose(trait.getPose(args.getFlag("remove"))))
|
||||
Messaging.sendF(sender, ChatColor.GREEN + "Position removed.");
|
||||
else
|
||||
throw new CommandException("The position '" + args.getFlag("remove") + "' does not exist.");
|
||||
throw new CommandException("The pose '" + args.getFlag("remove") + "' does not exist.");
|
||||
}
|
||||
else
|
||||
throw new CommandException("Invalid name.");
|
||||
}
|
||||
|
||||
else if (!args.hasFlag('a')) {
|
||||
Paginator paginator = new Paginator().header("Positions");
|
||||
Paginator paginator = new Paginator().header("Pose");
|
||||
paginator.addLine("<e>Key: <a>ID <b>Name <c>Pitch/Yaw");
|
||||
for (int i = 0; i < trait.getPositions().size(); i ++) {
|
||||
String line = "<a>" + i + "<b> " + trait.getPositions().get(i).getName() + "<c> " + trait.getPositions().get(i).getPitch() + "/" + trait.getPositions().get(i).getYaw();
|
||||
for (int i = 0; i < trait.getPoses().size(); i ++) {
|
||||
String line = "<a>" + i + "<b> " + trait.getPoses().get(i).getName() + "<c> " + trait.getPoses().get(i).getPitch() + "/" + trait.getPoses().get(i).getYaw();
|
||||
paginator.addLine(line);
|
||||
}
|
||||
|
||||
@ -516,10 +516,10 @@ public class NPCCommands {
|
||||
throw new CommandException("The page '" + page + "' does not exist.");
|
||||
}
|
||||
|
||||
// Assume Player's position
|
||||
// Assume Player's pose
|
||||
if (args.hasFlag('a')) {
|
||||
if (sender instanceof Player) {
|
||||
trait.assumePosition(new Position(sender.getName(), ((Player) sender).getLocation().getPitch(), ((Player) sender).getLocation().getYaw()));
|
||||
trait.assumePose(new Pose(sender.getName(), ((Player) sender).getLocation().getPitch(), ((Player) sender).getLocation().getYaw()));
|
||||
return;
|
||||
}
|
||||
else
|
||||
|
@ -20,7 +20,7 @@ import net.citizensnpcs.trait.Behaviour;
|
||||
import net.citizensnpcs.trait.Controllable;
|
||||
import net.citizensnpcs.trait.CurrentLocation;
|
||||
import net.citizensnpcs.trait.LookClose;
|
||||
import net.citizensnpcs.trait.Positions;
|
||||
import net.citizensnpcs.trait.Poses;
|
||||
import net.citizensnpcs.trait.Powered;
|
||||
import net.citizensnpcs.trait.Saddle;
|
||||
import net.citizensnpcs.trait.Sheared;
|
||||
@ -54,7 +54,7 @@ public class CitizensTraitFactory implements TraitFactory {
|
||||
registerTrait(TraitInfo.create(WoolColor.class).withName("woolcolor"));
|
||||
registerTrait(TraitInfo.create(Controllable.class).withName("controllable"));
|
||||
registerTrait(TraitInfo.create(Behaviour.class).withName("behaviour"));
|
||||
registerTrait(TraitInfo.create(Positions.class).withName("positions"));
|
||||
registerTrait(TraitInfo.create(Poses.class).withName("poses"));
|
||||
|
||||
for (String trait : registered.keySet())
|
||||
INTERNAL_TRAITS.add(trait);
|
||||
|
75
src/main/java/net/citizensnpcs/trait/Poses.java
Normal file
75
src/main/java/net/citizensnpcs/trait/Poses.java
Normal file
@ -0,0 +1,75 @@
|
||||
package net.citizensnpcs.trait;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
import net.citizensnpcs.api.exception.NPCLoadException;
|
||||
import net.citizensnpcs.api.trait.Trait;
|
||||
import net.citizensnpcs.api.util.DataKey;
|
||||
import net.citizensnpcs.util.Pose;
|
||||
import net.citizensnpcs.util.Util;
|
||||
|
||||
public class Poses extends Trait {
|
||||
private final List<Pose> poses = new ArrayList<Pose>();
|
||||
|
||||
Pose currentPosition = null;
|
||||
|
||||
public Poses() {
|
||||
super("poses");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(DataKey key) throws NPCLoadException {
|
||||
for (DataKey sub : key.getRelative("list").getIntegerSubKeys())
|
||||
try {
|
||||
poses.add(new Pose(sub.getString("").split(";")[0], Float.valueOf(sub.getString("").split(";")[1]), Float.valueOf(sub.getString("").split(";")[2]))) ;
|
||||
} catch(Exception e) { /* Perhaps remove the entry if bad? Warn console? */ }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(DataKey key) {
|
||||
key.removeKey("list");
|
||||
for (int i = 0; i < poses.size(); i++)
|
||||
key.setString("list." + String.valueOf(i), poses.get(i).stringValue());
|
||||
}
|
||||
|
||||
public List<Pose> getPoses() {
|
||||
return poses;
|
||||
}
|
||||
|
||||
public boolean addPose(String name, Location location) {
|
||||
Pose newPose = new Pose(name, location.getPitch(), location.getYaw());
|
||||
|
||||
if (poses.contains(newPose)) return false;
|
||||
poses.add(newPose);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean removePose(Pose pose) {
|
||||
if (poses.contains(pose)) {
|
||||
poses.remove(pose);
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
public Pose getPose(String name) {
|
||||
for (Pose pose : poses)
|
||||
if (pose.getName().equalsIgnoreCase(name)) return pose;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void assumePose(Pose pose) {
|
||||
|
||||
if (!npc.isSpawned())
|
||||
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
|
||||
|
||||
Util.assumePose(npc.getBukkitEntity(), pose);
|
||||
}
|
||||
|
||||
|
||||
}
|
62
src/main/java/net/citizensnpcs/util/Pose.java
Normal file
62
src/main/java/net/citizensnpcs/util/Pose.java
Normal file
@ -0,0 +1,62 @@
|
||||
package net.citizensnpcs.util;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang.builder.HashCodeBuilder;
|
||||
|
||||
/*
|
||||
* Pose object which holds yaw/pitch of the head with a name to identify.
|
||||
*/
|
||||
|
||||
public class Pose {
|
||||
private final String name;
|
||||
private final float yaw;
|
||||
private final float pitch;
|
||||
|
||||
public Pose(String name, float pitch, float yaw) {
|
||||
this.yaw = yaw;
|
||||
this.pitch = pitch;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return new HashCodeBuilder(13, 21).
|
||||
append(name).
|
||||
toHashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Name: " + name + " Pitch: " + pitch + " Yaw: " + yaw;
|
||||
}
|
||||
|
||||
public String stringValue() {
|
||||
return name + ";" + pitch + ";" + yaw;
|
||||
}
|
||||
|
||||
public float getYaw() {
|
||||
return yaw;
|
||||
}
|
||||
|
||||
public float getPitch() {
|
||||
return pitch;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (object == null) return false;
|
||||
if (object == this) return true;
|
||||
if (object.getClass() != getClass())
|
||||
return false;
|
||||
|
||||
Pose op = (Pose) object;
|
||||
return new EqualsBuilder().
|
||||
append(name, op.getName()).
|
||||
isEquals();
|
||||
}
|
||||
|
||||
}
|
@ -64,10 +64,10 @@ public class Util {
|
||||
handle.as = handle.yaw;
|
||||
}
|
||||
|
||||
public static void assumePosition(Entity entity, Position position) {
|
||||
public static void assumePose(Entity entity, Pose pose) {
|
||||
EntityLiving handle = ((CraftLivingEntity) entity).getHandle();
|
||||
handle.yaw = (float) position.getYaw();
|
||||
handle.pitch = (float) position.getPitch();
|
||||
handle.yaw = (float) pose.getYaw();
|
||||
handle.pitch = (float) pose.getPitch();
|
||||
handle.as = handle.yaw;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user