Restrict Pose object to inside Poses, metrics version should only report the major version

This commit is contained in:
fullwall 2012-11-06 13:42:32 +08:00
parent f0a29c3f7f
commit fc48b53d74
7 changed files with 69 additions and 56 deletions

View File

@ -72,14 +72,19 @@ public class EventListen implements Listener {
List<Integer> ids = toRespawn.get(coord); List<Integer> ids = toRespawn.get(coord);
for (int i = 0; i < ids.size(); i++) { for (int i = 0; i < ids.size(); i++) {
int id = ids.get(i); int id = ids.get(i);
NPC npc = npcRegistry.getById(id); spawn(id);
if (npc == null) Messaging.debug("Spawned", id, "due to chunk load at [" + coord.x + "," + coord.z + "]");
continue;
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
} }
toRespawn.removeAll(coord); toRespawn.removeAll(coord);
} }
private void spawn(int id) {
NPC npc = npcRegistry.getById(id);
if (npc == null)
return;
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
}
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent event) { public void onChunkUnload(ChunkUnloadEvent event) {
ChunkCoord coord = toCoord(event.getChunk()); ChunkCoord coord = toCoord(event.getChunk());
@ -91,6 +96,8 @@ public class EventListen implements Listener {
if (event.getWorld().equals(loc.getWorld()) && sameChunkCoordinates) { if (event.getWorld().equals(loc.getWorld()) && sameChunkCoordinates) {
npc.despawn(); npc.despawn();
toRespawn.put(coord, npc.getId()); toRespawn.put(coord, npc.getId());
Messaging.debug("Despawned", npc.getId(), "due to chunk unload at [" + coord.x + ","
+ coord.z + "]");
} }
} }
} }
@ -244,9 +251,9 @@ public class EventListen implements Listener {
continue; continue;
List<Integer> ids = toRespawn.get(chunk); List<Integer> ids = toRespawn.get(chunk);
for (int i = 0; i < ids.size(); i++) { for (int i = 0; i < ids.size(); i++) {
int id = ids.get(i); spawn(ids.get(i));
NPC npc = npcRegistry.getById(id); Messaging
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation()); .debug("Spawned", ids.get(0), "due to world " + event.getWorld().getName() + " load");
} }
toRespawn.removeAll(chunk); toRespawn.removeAll(chunk);
} }
@ -259,6 +266,7 @@ public class EventListen implements Listener {
continue; continue;
storeForRespawn(npc); storeForRespawn(npc);
npc.despawn(); npc.despawn();
Messaging.debug("Despawned", npc.getId() + "due to world unload at", event.getWorld().getName());
} }
} }

View File

@ -307,7 +307,7 @@ public class Metrics {
// Construct the post data // Construct the post data
final StringBuilder data = new StringBuilder(); final StringBuilder data = new StringBuilder();
data.append(encode("guid")).append('=').append(encode(guid)); data.append(encode("guid")).append('=').append(encode(guid));
encodeDataPair(data, "version", description.getVersion()); encodeDataPair(data, "version", description.getVersion().replaceAll("\\(.*$", ""));
encodeDataPair(data, "server", Bukkit.getVersion()); encodeDataPair(data, "server", Bukkit.getVersion());
encodeDataPair(data, "players", Integer.toString(Bukkit.getServer().getOnlinePlayers().length)); encodeDataPair(data, "players", Integer.toString(Bukkit.getServer().getOnlinePlayers().length));
encodeDataPair(data, "revision", String.valueOf(REVISION)); encodeDataPair(data, "revision", String.valueOf(REVISION));

View File

@ -45,7 +45,7 @@ public class NPCDataStore {
created++; created++;
} }
Messaging.logTr(Messages.NUM_LOADED_NOTIFICATION, created); Messaging.logTr(Messages.NUM_LOADED_NOTIFICATION, created, "?");
} }
public void remove(NPC npc) { public void remove(NPC npc) {

View File

@ -43,7 +43,6 @@ import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Paginator; import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.Pose;
import net.citizensnpcs.util.StringHelper; import net.citizensnpcs.util.StringHelper;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;
@ -705,32 +704,22 @@ public class NPCCommands {
} else } else
throw new CommandException(Messages.POSE_ALREADY_EXISTS, args.getFlag("assume")); throw new CommandException(Messages.POSE_ALREADY_EXISTS, args.getFlag("assume"));
} else if (args.hasValueFlag("assume")) { } else if (args.hasValueFlag("assume")) {
if (args.getFlag("assume").isEmpty()) String pose = args.getFlag("assume");
if (pose.isEmpty())
throw new CommandException(Messages.INVALID_POSE_NAME); throw new CommandException(Messages.INVALID_POSE_NAME);
Pose pose = trait.getPose(args.getFlag("assume")); if (!trait.hasPose(pose))
if (pose == null) throw new CommandException(Messages.POSE_MISSING, pose);
throw new CommandException(Messages.POSE_MISSING, args.getFlag("assume"));
trait.assumePose(pose); trait.assumePose(pose);
} else if (args.hasValueFlag("remove")) { } else if (args.hasValueFlag("remove")) {
if (args.getFlag("remove").isEmpty()) if (args.getFlag("remove").isEmpty())
throw new CommandException(Messages.INVALID_POSE_NAME); throw new CommandException(Messages.INVALID_POSE_NAME);
if (trait.removePose(trait.getPose(args.getFlag("remove")))) if (trait.removePose(args.getFlag("remove"))) {
Messaging.sendTr(sender, Messages.POSE_REMOVED); Messaging.sendTr(sender, Messages.POSE_REMOVED);
else } else
throw new CommandException(Messages.POSE_MISSING, args.getFlag("remove")); throw new CommandException(Messages.POSE_MISSING, args.getFlag("remove"));
} else if (!args.hasFlag('a')) { } else if (!args.hasFlag('a')) {
Paginator paginator = new Paginator().header("Pose"); trait.describe(sender, args.getInteger(1, 1));
paginator.addLine("<e>Key: <a>ID <b>Name <c>Pitch/Yaw");
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);
}
int page = args.getInteger(1, 1);
if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
} }
// Assume Player's pose // Assume Player's pose
@ -738,7 +727,7 @@ public class NPCCommands {
return; return;
if (sender instanceof Player) { if (sender instanceof Player) {
Location location = ((Player) sender).getLocation(); Location location = ((Player) sender).getLocation();
trait.assumePose(new Pose(sender.getName(), location.getPitch(), location.getYaw())); trait.assumePose(location);
} else } else
throw new ServerCommandException(); throw new ServerCommandException();
} }

View File

@ -32,7 +32,6 @@ public class CitizensHumanNPC extends CitizensNPC implements Equipable {
final EntityHumanNPC handle = new EntityHumanNPC(ws.getServer().getServer(), ws, final EntityHumanNPC handle = new EntityHumanNPC(ws.getServer().getServer(), ws,
StringHelper.parseColors(getFullName()), new ItemInWorldManager(ws), this); StringHelper.parseColors(getFullName()), new ItemInWorldManager(ws), this);
handle.getBukkitEntity().teleport(loc); handle.getBukkitEntity().teleport(loc);
NMS.setHeadYaw(handle, loc.getYaw() % 360);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() { Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override @Override
public void run() { public void run() {

View File

@ -1,20 +1,24 @@
package net.citizensnpcs.trait; package net.citizensnpcs.trait;
import java.util.ArrayList; import java.util.Map;
import java.util.List;
import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.Paginator;
import net.citizensnpcs.util.Pose; import net.citizensnpcs.util.Pose;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import com.google.common.collect.Maps;
public class Poses extends Trait { public class Poses extends Trait {
private final List<Pose> poses = new ArrayList<Pose>(); private final Map<String, Pose> poses = Maps.newHashMap();
public Poses() { public Poses() {
super("poses"); super("poses");
@ -22,28 +26,21 @@ public class Poses extends Trait {
public boolean addPose(String name, Location location) { public boolean addPose(String name, Location location) {
Pose newPose = new Pose(name, location.getPitch(), location.getYaw()); Pose newPose = new Pose(name, location.getPitch(), location.getYaw());
if (poses.contains(newPose)) if (poses.containsValue(newPose) || poses.containsKey(name))
return false; return false;
poses.add(newPose); poses.put(name.toLowerCase(), newPose);
return true; return true;
} }
public void assumePose(Pose pose) { public void assumePose(Location location) {
assumePose(location.getYaw(), location.getPitch());
}
private void assumePose(float yaw, float pitch) {
if (!npc.isSpawned()) if (!npc.isSpawned())
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation()); npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
Util.assumePose(npc.getBukkitEntity(), pose); Util.assumePose(npc.getBukkitEntity(), yaw, pitch);
}
public Pose getPose(String name) {
for (Pose pose : poses)
if (pose.getName().equalsIgnoreCase(name))
return pose;
return null;
}
public List<Pose> getPoses() {
return poses;
} }
@Override @Override
@ -51,18 +48,14 @@ public class Poses extends Trait {
for (DataKey sub : key.getRelative("list").getIntegerSubKeys()) for (DataKey sub : key.getRelative("list").getIntegerSubKeys())
try { try {
String[] parts = sub.getString("").split(";"); String[] parts = sub.getString("").split(";");
poses.add(new Pose(parts[0], Float.valueOf(parts[1]), Float.valueOf(parts[2]))); poses.put(parts[0], new Pose(parts[0], Float.valueOf(parts[1]), Float.valueOf(parts[2])));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Messaging.logTr(Messages.SKIPPING_INVALID_POSE, sub.name(), e.getMessage()); Messaging.logTr(Messages.SKIPPING_INVALID_POSE, sub.name(), e.getMessage());
} }
} }
public boolean removePose(Pose pose) { public boolean removePose(String pose) {
if (poses.contains(pose)) { return poses.remove(pose.toLowerCase()) != null;
poses.remove(pose);
return true;
}
return false;
} }
@Override @Override
@ -71,4 +64,28 @@ public class Poses extends Trait {
for (int i = 0; i < poses.size(); i++) for (int i = 0; i < poses.size(); i++)
key.setString("list." + String.valueOf(i), poses.get(i).stringValue()); key.setString("list." + String.valueOf(i), poses.get(i).stringValue());
} }
public void assumePose(String flag) {
Pose pose = poses.get(flag.toLowerCase());
assumePose(pose.getYaw(), pose.getPitch());
}
public boolean hasPose(String pose) {
return poses.containsKey(pose.toLowerCase());
}
public void describe(CommandSender sender, int page) throws CommandException {
Paginator paginator = new Paginator().header("Pose");
paginator.addLine("<e>Key: <a>ID <b>Name <c>Pitch/Yaw");
int i = 0;
for (Pose pose : poses.values()) {
String line = "<a>" + i + "<b> " + pose.getName() + "<c> " + pose.getPitch() + "/"
+ pose.getYaw();
paginator.addLine(line);
i++;
}
if (!paginator.sendPage(sender, page))
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
}
} }

View File

@ -27,9 +27,9 @@ public class Util {
private Util() { private Util() {
} }
public static void assumePose(org.bukkit.entity.Entity entity, Pose pose) { public static void assumePose(org.bukkit.entity.Entity entity, float yaw, float pitch) {
EntityLiving handle = ((CraftLivingEntity) entity).getHandle(); EntityLiving handle = ((CraftLivingEntity) entity).getHandle();
NMS.look(handle, pose.getYaw(), pose.getPitch()); NMS.look(handle, yaw,pitch);
} }
public static void callCollisionEvent(NPC npc, net.minecraft.server.Entity entity) { public static void callCollisionEvent(NPC npc, net.minecraft.server.Entity entity) {