Skip to content

Commit

Permalink
Prevent NPE in moveto if NPC is not spawned
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Aug 10, 2013
1 parent 8f881dc commit a5fc6eb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/citizensnpcs/commands/NPCCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,9 @@ public void moveto(CommandContext args, CommandSender sender, NPC npc) throws Co
// Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned())
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
if (npc.getBukkitEntity() == null) {
throw new CommandException("NPC could not be spawned.");
}
Location current = npc.getBukkitEntity().getLocation();
Location to;
if (args.argsLength() > 1) {
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/net/citizensnpcs/npc/ai/speech/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Collections;
import java.util.List;
import java.util.logging.Level;

import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
Expand All @@ -25,15 +24,13 @@ public String getName() {

@Override
public void talk(SpeechContext context) {
// Check valid talker
if (context.getTalker() == null)
return;
NPC npc = CitizensAPI.getNPCRegistry().getNPC(context.getTalker().getEntity());
if (npc == null)
return;

// If no recipients, chat to the world with CHAT_FORMAT and CHAT_RANGE
// settings
// chat to the world with CHAT_FORMAT and CHAT_RANGE settings
if (!context.hasRecipients()) {
String text = Setting.CHAT_FORMAT.asString().replace("<npc>", npc.getName())
.replace("<text>", context.getMessage());
Expand All @@ -42,7 +39,7 @@ public void talk(SpeechContext context) {
}

// Assumed recipients at this point
else if (context.size() <= 1) { // One recipient
else if (context.size() <= 1) {
String text = Setting.CHAT_FORMAT_TO_TARGET.asString().replace("<npc>", npc.getName())
.replace("<text>", context.getMessage());
String targetName = "";
Expand Down Expand Up @@ -77,14 +74,14 @@ else if (context.size() <= 1) { // One recipient
int max = Setting.CHAT_MAX_NUMBER_OF_TARGETS.asInt();
String[] format = Setting.CHAT_FORMAT_WITH_TARGETS_TO_BYSTANDERS.asString().split("\\|");
if (format.length != 4)
Messaging.log(Level.WARNING, "npc.chat.format.with-target-to-bystanders invalid!");
Messaging.severe("npc.chat.format.with-target-to-bystanders invalid!");
if (max == 1) {
targets = format[0].replace("<npc>", targetNames.get(0)) + format[3];
} else if (max == 2 || targetNames.size() == 2) {
if (targetNames.size() == 2)
if (targetNames.size() == 2) {
targets = format[0].replace("<npc>", targetNames.get(0))
+ format[2].replace("<npc>", targetNames.get(1));
else
} else
targets = format[0].replace("<npc>", targetNames.get(0))
+ format[1].replace("<npc>", targetNames.get(1)) + format[3];
} else if (max >= 3) {
Expand All @@ -96,9 +93,9 @@ else if (context.size() <= 1) { // One recipient
break;
targets = targets + format[1].replace("<npc>", targetNames.get(x));
}
if (targetNames.size() == max)
if (targetNames.size() == max) {
targets = targets + format[2].replace("<npc>", targetNames.get(x));
else
} else
targets = targets + format[3];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.google.common.base.Preconditions;

public class CitizensSpeechFactory implements SpeechFactory {

Map<String, Class<? extends VocalChord>> registered = new HashMap<String, Class<? extends VocalChord>>();

@Override
Expand Down

0 comments on commit a5fc6eb

Please sign in to comment.