Fix small bugs/update to /npc position. Add /npc position --remove [name].

This commit is contained in:
Jeremy Schroeder 2012-09-28 12:02:05 -04:00
parent 3748ee098f
commit 669da4fc95
4 changed files with 52 additions and 25 deletions

View File

@ -35,14 +35,12 @@ import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.Position;
import net.citizensnpcs.util.StringHelper;
import net.citizensnpcs.util.Util;
import net.minecraft.server.EntityLiving;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.entity.CraftLivingEntity;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
@ -457,8 +455,8 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "position (-a)",
desc = "Changes NPC's head position",
usage = "position (--save [name]|--load [name]|--remove [name]|--list) (-a)",
desc = "Changes/Saves/Lists NPC's head position(s)",
flags = "a",
modifiers = { "position" },
min = 1,
@ -470,34 +468,46 @@ public class NPCCommands {
Positions trait = npc.getTrait(Positions.class);
if (args.hasValueFlag("save")) {
if (args.getFlag("save").matches("[a-zA-Z0-9_\\-]+")) {
if (!args.getFlag("save").isEmpty()) {
if (sender instanceof Player) {
if (trait.addPosition(args.getFlag("save"), ((Player) sender).getLocation()))
Messaging.sendF(sender, ChatColor.RED + "Position added.");
Messaging.sendF(sender, ChatColor.GREEN + "Position added.");
else throw new CommandException("The position '" + args.getFlag("load") + "' already exists.");
}
else
Messaging.sendF(sender, ChatColor.YELLOW + "This command can only be used by a Player in-game");
throw new CommandException("This command may be used in-game only.");
}
else
Messaging.sendF(sender, ChatColor.YELLOW + "Save name can only be one word. Valid characters: A-Z a-z 0-9 - _");
throw new CommandException("Invalid name.");
}
else if (args.hasValueFlag("load")) {
if (args.getFlag("load").matches("[a-zA-Z0-9_\\-]+")) {
if (!args.getFlag("load").isEmpty()) {
if (trait.getPosition(args.getFlag("load")) != null)
trait.assumePosition(trait.getPosition(args.getFlag("load")));
else
throw new CommandException("The position '" + args.getFlag("load") + "' does not exist.");
}
else
Messaging.sendF(sender, ChatColor.YELLOW + "Invalid load name.");
throw new CommandException("Invalid name.");
}
else {
else if (args.hasValueFlag("remove")) {
if (!args.getFlag("remove").isEmpty()) {
if (trait.removePosition(trait.getPosition(args.getFlag("remove"))))
Messaging.sendF(sender, ChatColor.GREEN + "Position removed.");
else
throw new CommandException("The position '" + args.getFlag("remove") + "' does not exist.");
}
else
throw new CommandException("Invalid name.");
}
else if (!args.hasFlag('a')) {
Paginator paginator = new Paginator().header("Positions");
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).name + "<c> " + Double.valueOf(trait.getPositions().get(i).getPitch()) + "/" + Double.valueOf(trait.getPositions().get(i).getYaw());
String line = "<a>" + i + "<b> " + trait.getPositions().get(i).getName() + "<c> " + trait.getPositions().get(i).getPitch() + "/" + trait.getPositions().get(i).getYaw();
paginator.addLine(line);
}
@ -512,7 +522,6 @@ public class NPCCommands {
trait.assumePosition(new Position(sender.getName(), ((Player) sender).getLocation().getPitch(), ((Player) sender).getLocation().getYaw()));
return;
}
else
Messaging.sendF(sender, ChatColor.YELLOW + "This command can only be used by a Player in-game");
}

View File

@ -20,6 +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.Powered;
import net.citizensnpcs.trait.Saddle;
import net.citizensnpcs.trait.Sheared;
@ -53,6 +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"));
for (String trait : registered.keySet())
INTERNAL_TRAITS.add(trait);

View File

@ -31,10 +31,7 @@ public class Positions extends Trait {
@Override
public void save(DataKey key) {
for (int i = 0; i < 100; i++)
key.removeKey(String.valueOf(i));
key.removeKey("list");
for (int i = 0; i < positions.size(); i++)
key.setString("list." + String.valueOf(i), positions.get(i).stringValue());
}
@ -61,7 +58,7 @@ public class Positions extends Trait {
public Position getPosition(String name) {
for (Position position : positions)
if (position.name.equalsIgnoreCase(name)) return position;
if (position.getName().equalsIgnoreCase(name)) return position;
return null;
}

View File

@ -1,14 +1,16 @@
package net.citizensnpcs.util;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/*
* Position object which holds yaw/pitch of the head with a name to identify.
*/
public class Position {
public final String name;
private final Float yaw;
private final Float pitch;
private final String name;
private final float yaw;
private final float pitch;
public Position(String name, float pitch, float yaw) {
this.yaw = yaw;
@ -16,9 +18,16 @@ public class Position {
this.name = name;
}
@Override
public int hashCode() {
return new HashCodeBuilder(13, 21).
append(name).
toHashCode();
}
@Override
public String toString() {
return "Name: " + name + " Pitch: " + pitch.doubleValue() + " Yaw: " + yaw.doubleValue();
return "Name: " + name + " Pitch: " + pitch + " Yaw: " + yaw;
}
public String stringValue() {
@ -33,11 +42,21 @@ public class Position {
return pitch;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object otherPosition) {
if (otherPosition == null) return false;
if (otherPosition.toString() == this.name) return true;
else return false;
public boolean equals(Object object) {
if (object == null) return false;
if (object == this) return true;
if (object.getClass() != getClass())
return false;
Position op = (Position) object;
return new EqualsBuilder().
append(name, op.getName()).
isEquals();
}
}