This commit is contained in:
fullwall 2013-10-17 15:28:06 +08:00
commit 262f4353a6
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.addLine("<e>Key: <a>ID <b>Name <c>World <d>Location (X,Y,Z)");
for (int i = 0; i < trait.getAnchors().size(); i++) {
String line = "<a>" + i + "<b> " + trait.getAnchors().get(i).getName() + "<c> "
+ trait.getAnchors().get(i).getLocation().getWorld().getName() + "<d> "
+ trait.getAnchors().get(i).getLocation().getBlockX() + ", "
+ trait.getAnchors().get(i).getLocation().getBlockY() + ", "
+ trait.getAnchors().get(i).getLocation().getBlockZ();
paginator.addLine(line);
if (trait.getAnchors().get(i).isLoaded()) {
String line = "<a>" + i + "<b> " + trait.getAnchors().get(i).getName() + "<c> "
+ trait.getAnchors().get(i).getLocation().getWorld().getName() + "<d> "
+ trait.getAnchors().get(i).getLocation().getBlockX() + ", "
+ trait.getAnchors().get(i).getLocation().getBlockY() + ", "
+ trait.getAnchors().get(i).getLocation().getBlockZ();
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);
@ -1004,7 +1011,7 @@ public class NPCCommands {
if (range > 0
&& test.isSpawned()
&& !Util.locationWithinRange(args.getSenderLocation(), test.getBukkitEntity()
.getLocation(), range))
.getLocation(), range))
continue;
possible.add(test);
}

View File

@ -1,5 +1,6 @@
package net.citizensnpcs.npc.ai.speech;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -61,7 +62,7 @@ public class Chat implements VocalChord {
else { // Multiple recipients
String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName())
.replace("<text>", context.getMessage());
List<String> targetNames = Collections.emptyList();
List<String> targetNames = new ArrayList<String>();
// Talk to each recipient
for (Talkable entity : context) {
entity.talkTo(context, text, this);
@ -72,20 +73,20 @@ public class Chat implements VocalChord {
return;
String targets = "";
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)
Messaging.severe("npc.chat.format.with-target-to-bystanders invalid!");
Messaging.severe("npc.chat.options.multiple-targets-format invalid!");
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) {
if (targetNames.size() == 2) {
targets = format[0].replace("<npc>", targetNames.get(0))
+ format[2].replace("<npc>", targetNames.get(1));
targets = format[0].replace("<target>", targetNames.get(0))
+ format[2].replace("<target>", targetNames.get(1));
} else
targets = format[0].replace("<npc>", targetNames.get(0))
+ format[1].replace("<npc>", targetNames.get(1)) + format[3];
targets = format[0].replace("<target>", targetNames.get(0))
+ format[1].replace("<target>", targetNames.get(1)) + format[3];
} else if (max >= 3) {
targets = format[0].replace("<npc>", targetNames.get(0));
targets = format[0].replace("<target>", targetNames.get(0));
int x = 1;
for (x = 1; x < max - 1; x++) {
@ -114,17 +115,20 @@ public class Chat implements VocalChord {
// Continue if a LivingEntity, which is compatible with
// TalkableEntity
if (bystander instanceof LivingEntity) {
boolean should_talk = true;
// Exclude targeted recipients
if (context.hasRecipients()) {
for (Talkable target : context)
if (target.getEntity() == bystander)
continue;
else
new TalkableEntity((LivingEntity) bystander).talkNear(context, text, this);
} else
// Found a nearby LivingEntity, make it Talkable and
// talkNear it
if (target.getEntity().equals(bystander))
should_talk = false;
}
// Found a nearby LivingEntity, make it Talkable and
// talkNear it if 'should_talk'
if (should_talk)
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.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 {
private final List<Anchor> anchors = new ArrayList<Anchor>();
private List<Anchor> anchors = new ArrayList<Anchor>();
public Anchors() {
super("anchors");
@ -41,14 +45,20 @@ public class Anchors extends Trait {
@Override
public void load(DataKey key) throws NPCLoadException {
for (DataKey sub : key.getRelative("list").getIntegerSubKeys())
for (DataKey sub : key.getRelative("list").getIntegerSubKeys()) {
String[] parts = sub.getString("").split(";");
Location location;
try {
String[] parts = sub.getString("").split(";");
anchors.add(new Anchor(parts[0], new Location(Bukkit.getServer().getWorld(parts[1]), Double
.valueOf(parts[2]), Double.valueOf(parts[3]), Double.valueOf(parts[4]))));
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) {
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]));
}
}
}
public boolean removeAnchor(Anchor anchor) {
@ -65,4 +75,12 @@ public class Anchors extends Trait {
for (int i = 0; i < anchors.size(); i++)
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.HashCodeBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
/*
@ -9,12 +10,39 @@ import org.bukkit.Location;
*/
public class Anchor {
private final Location location;
private Location location;
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) {
this.location = location;
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
@ -27,7 +55,7 @@ public class Anchor {
return false;
Anchor op = (Anchor) object;
return new EqualsBuilder().append(name, op.getName()).isEquals();
return new EqualsBuilder().append(name, op.name).isEquals();
}
public Location getLocation() {
@ -38,20 +66,30 @@ public class Anchor {
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
public int hashCode() {
return new HashCodeBuilder(13, 21).append(name).toHashCode();
}
// A friendly representation for use in saves.yml
public String stringValue() {
return name + ";" + location.getWorld().getName() + ";" + location.getX() + ";" + location.getY() + ";"
+ location.getZ();
return name + ';' + unloaded_value;
}
@Override
public String toString() {
return "Name: " + name + " World: " + location.getWorld().getName() + " Location: " + location.getBlockX()
+ "," + location.getBlockY() + "," + location.getBlockZ();
String[] parts = getUnloadedValue();
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() {
return name + ";" + pitch + ";" + yaw;
return name + ';' + pitch + ';' + yaw;
}
@Override
public String toString() {
return "Name: " + name + " Pitch: " + pitch + " Yaw: " + yaw;
return "Pose{Name='" + name + "';Pitch='" + pitch + "';Yaw='" + yaw + "';}";
}
}