This commit is contained in:
fullwall 2013-10-17 15:28:06 +08:00
commit 7229958dcf
5 changed files with 103 additions and 36 deletions

View File

@ -181,12 +181,19 @@ public class NPCCommands {
Paginator paginator = new Paginator().header("Anchors"); Paginator paginator = new Paginator().header("Anchors");
paginator.addLine("<e>Key: <a>ID <b>Name <c>World <d>Location (X,Y,Z)"); paginator.addLine("<e>Key: <a>ID <b>Name <c>World <d>Location (X,Y,Z)");
for (int i = 0; i < trait.getAnchors().size(); i++) { for (int i = 0; i < trait.getAnchors().size(); i++) {
if (trait.getAnchors().get(i).isLoaded()) {
String line = "<a>" + i + "<b> " + trait.getAnchors().get(i).getName() + "<c> " String line = "<a>" + i + "<b> " + trait.getAnchors().get(i).getName() + "<c> "
+ trait.getAnchors().get(i).getLocation().getWorld().getName() + "<d> " + trait.getAnchors().get(i).getLocation().getWorld().getName() + "<d> "
+ trait.getAnchors().get(i).getLocation().getBlockX() + ", " + trait.getAnchors().get(i).getLocation().getBlockX() + ", "
+ trait.getAnchors().get(i).getLocation().getBlockY() + ", " + trait.getAnchors().get(i).getLocation().getBlockY() + ", "
+ trait.getAnchors().get(i).getLocation().getBlockZ(); + trait.getAnchors().get(i).getLocation().getBlockZ();
paginator.addLine(line); paginator.addLine(line);
} else {
String[] parts = trait.getAnchors().get(i).getUnloadedValue();
String line = "<a>" + i + "<b> " + trait.getAnchors().get(i).getName() + "<c> "
+ parts[0] + "<d> " + parts[1] + ", " + parts[2] + ", " + parts[3] + " <f>(unloaded)";
paginator.addLine(line);
}
} }
int page = args.getInteger(1, 1); int page = args.getInteger(1, 1);

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.ai.speech; package net.citizensnpcs.npc.ai.speech;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -61,7 +62,7 @@ public class Chat implements VocalChord {
else { // Multiple recipients else { // Multiple recipients
String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName()) String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName())
.replace("<text>", context.getMessage()); .replace("<text>", context.getMessage());
List<String> targetNames = Collections.emptyList(); List<String> targetNames = new ArrayList<String>();
// Talk to each recipient // Talk to each recipient
for (Talkable entity : context) { for (Talkable entity : context) {
entity.talkTo(context, text, this); entity.talkTo(context, text, this);
@ -72,20 +73,20 @@ public class Chat implements VocalChord {
return; return;
String targets = ""; String targets = "";
int max = Setting.CHAT_MAX_NUMBER_OF_TARGETS.asInt(); int max = Setting.CHAT_MAX_NUMBER_OF_TARGETS.asInt();
String[] format = Setting.CHAT_FORMAT_WITH_TARGETS_TO_BYSTANDERS.asString().split("\\|"); String[] format = Setting.CHAT_MULTIPLE_TARGETS_FORMAT.asString().split("\\|");
if (format.length != 4) if (format.length != 4)
Messaging.severe("npc.chat.format.with-target-to-bystanders invalid!"); Messaging.severe("npc.chat.options.multiple-targets-format invalid!");
if (max == 1) { if (max == 1) {
targets = format[0].replace("<npc>", targetNames.get(0)) + format[3]; targets = format[0].replace("<target>", targetNames.get(0)) + format[3];
} else if (max == 2 || targetNames.size() == 2) { } else if (max == 2 || targetNames.size() == 2) {
if (targetNames.size() == 2) { if (targetNames.size() == 2) {
targets = format[0].replace("<npc>", targetNames.get(0)) targets = format[0].replace("<target>", targetNames.get(0))
+ format[2].replace("<npc>", targetNames.get(1)); + format[2].replace("<target>", targetNames.get(1));
} else } else
targets = format[0].replace("<npc>", targetNames.get(0)) targets = format[0].replace("<target>", targetNames.get(0))
+ format[1].replace("<npc>", targetNames.get(1)) + format[3]; + format[1].replace("<target>", targetNames.get(1)) + format[3];
} else if (max >= 3) { } else if (max >= 3) {
targets = format[0].replace("<npc>", targetNames.get(0)); targets = format[0].replace("<target>", targetNames.get(0));
int x = 1; int x = 1;
for (x = 1; x < max - 1; x++) { for (x = 1; x < max - 1; x++) {
@ -114,17 +115,20 @@ public class Chat implements VocalChord {
// Continue if a LivingEntity, which is compatible with // Continue if a LivingEntity, which is compatible with
// TalkableEntity // TalkableEntity
if (bystander instanceof LivingEntity) { if (bystander instanceof LivingEntity) {
boolean should_talk = true;
// Exclude targeted recipients // Exclude targeted recipients
if (context.hasRecipients()) { if (context.hasRecipients()) {
for (Talkable target : context) for (Talkable target : context)
if (target.getEntity() == bystander) if (target.getEntity().equals(bystander))
continue; should_talk = false;
else }
new TalkableEntity((LivingEntity) bystander).talkNear(context, text, this);
} else
// Found a nearby LivingEntity, make it Talkable and // Found a nearby LivingEntity, make it Talkable and
// talkNear it // talkNear it if 'should_talk'
if (should_talk)
new TalkableEntity((LivingEntity) bystander).talkNear(context, text, this); new TalkableEntity((LivingEntity) bystander).talkNear(context, text, this);
} }
} }

View File

@ -12,9 +12,13 @@ import net.citizensnpcs.util.Messages;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
public class Anchors extends Trait { public class Anchors extends Trait {
private final List<Anchor> anchors = new ArrayList<Anchor>(); private List<Anchor> anchors = new ArrayList<Anchor>();
public Anchors() { public Anchors() {
super("anchors"); super("anchors");
@ -41,13 +45,19 @@ public class Anchors extends Trait {
@Override @Override
public void load(DataKey key) throws NPCLoadException { public void load(DataKey key) throws NPCLoadException {
for (DataKey sub : key.getRelative("list").getIntegerSubKeys()) for (DataKey sub : key.getRelative("list").getIntegerSubKeys()) {
try {
String[] parts = sub.getString("").split(";"); String[] parts = sub.getString("").split(";");
anchors.add(new Anchor(parts[0], new Location(Bukkit.getServer().getWorld(parts[1]), Double Location location;
.valueOf(parts[2]), Double.valueOf(parts[3]), Double.valueOf(parts[4])))); try {
location = new Location(Bukkit.getServer().getWorld(parts[1]), Double
.valueOf(parts[2]), Double.valueOf(parts[3]), Double.valueOf(parts[4]));
anchors.add(new Anchor(parts[0], location));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Messaging.logTr(Messages.SKIPPING_INVALID_ANCHOR, sub.name(), e.getMessage()); Messaging.logTr(Messages.SKIPPING_INVALID_ANCHOR, sub.name(), e.getMessage());
} catch (NullPointerException e) {
// Invalid world/location/etc. Still enough data to build an unloaded anchor
anchors.add(new Anchor(parts[0], sub.getString("").split(";", 2)[1]));
}
} }
} }
@ -65,4 +75,12 @@ public class Anchors extends Trait {
for (int i = 0; i < anchors.size(); i++) for (int i = 0; i < anchors.size(); i++)
key.setString("list." + String.valueOf(i), anchors.get(i).stringValue()); key.setString("list." + String.valueOf(i), anchors.get(i).stringValue());
} }
@EventHandler
public void checkWorld(WorldLoadEvent event) {
for (Anchor anchor : anchors)
if (!anchor.isLoaded())
anchor.load();
}
} }

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.util;
import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.HashCodeBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
/* /*
@ -9,12 +10,39 @@ import org.bukkit.Location;
*/ */
public class Anchor { public class Anchor {
private final Location location; private Location location;
private final String name; private final String name;
// Needed for Anchors defined that can't currently have a valid 'Location'
private final String unloaded_value;
// Allow construction of anchor for unloaded worlds
public Anchor(String name, String unloaded_value) {
this.location = null;
this.unloaded_value = unloaded_value;
this.name = name;
}
public Anchor(String name, Location location) { public Anchor(String name, Location location) {
this.location = location; this.location = location;
this.name = name; this.name = name;
this.unloaded_value = location.getWorld().getName() + ';'
+ location.getX() + ';' + location.getY() + ';' + location.getZ();
}
public boolean isLoaded() {
return location != null;
}
public boolean load() {
try {
String[] parts = getUnloadedValue();
this.location = new Location(Bukkit.getWorld(parts[0]),
Double.valueOf(parts[1]), Double.valueOf(parts[2]), Double.valueOf(parts[3]));
} catch (Exception e) {
// Still not able to be loaded
}
return location != null;
} }
@Override @Override
@ -27,7 +55,7 @@ public class Anchor {
return false; return false;
Anchor op = (Anchor) object; Anchor op = (Anchor) object;
return new EqualsBuilder().append(name, op.getName()).isEquals(); return new EqualsBuilder().append(name, op.name).isEquals();
} }
public Location getLocation() { public Location getLocation() {
@ -38,20 +66,30 @@ public class Anchor {
return name; return name;
} }
/**
* Returns a String[] of the 'world_name, x, y, z' information needed to create the Location
* that is associated with the Anchor, in that order.
*
* @return a String array of the anchor's location data
*/
public String[] getUnloadedValue() {
return unloaded_value.split(";");
}
@Override @Override
public int hashCode() { public int hashCode() {
return new HashCodeBuilder(13, 21).append(name).toHashCode(); return new HashCodeBuilder(13, 21).append(name).toHashCode();
} }
// A friendly representation for use in saves.yml
public String stringValue() { public String stringValue() {
return name + ";" + location.getWorld().getName() + ";" + location.getX() + ";" + location.getY() + ";" return name + ';' + unloaded_value;
+ location.getZ();
} }
@Override @Override
public String toString() { public String toString() {
return "Name: " + name + " World: " + location.getWorld().getName() + " Location: " + location.getBlockX() String[] parts = getUnloadedValue();
+ "," + location.getBlockY() + "," + location.getBlockZ(); return "Anchor{Name='" + name + "';World='" + parts[0] + "';Location='" + parts[1] + ',' + parts[2] + ',' + parts[3] + "';}";
} }
} }

View File

@ -49,12 +49,12 @@ public class Pose {
} }
public String stringValue() { public String stringValue() {
return name + ";" + pitch + ";" + yaw; return name + ';' + pitch + ';' + yaw;
} }
@Override @Override
public String toString() { public String toString() {
return "Name: " + name + " Pitch: " + pitch + " Yaw: " + yaw; return "Pose{Name='" + name + "';Pitch='" + pitch + "';Yaw='" + yaw + "';}";
} }
} }