Implement new events

This commit is contained in:
fullwall 2013-02-06 16:04:26 +08:00
parent 8787d4297d
commit b084b2995b
2 changed files with 14 additions and 9 deletions

View File

@ -15,6 +15,7 @@ import net.citizensnpcs.api.command.Requirements;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.command.exception.NoPermissionsException;
import net.citizensnpcs.api.command.exception.ServerCommandException;
import net.citizensnpcs.api.event.CommandSenderCreateNPCEvent;
import net.citizensnpcs.api.event.PlayerCreateNPCEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
@ -326,18 +327,19 @@ public class NPCCommands {
Location spawnLoc = null;
if (sender instanceof Player) {
spawnLoc = args.getSenderLocation();
PlayerCreateNPCEvent event = new PlayerCreateNPCEvent((Player) sender, npc);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
npc.destroy();
String reason = "Couldn't create NPC.";
if (!event.getCancelReason().isEmpty())
reason += " Reason: " + event.getCancelReason();
throw new CommandException(reason);
}
} else if (sender instanceof BlockCommandSender) {
spawnLoc = args.getSenderLocation();
}
CommandSenderCreateNPCEvent event = sender instanceof Player ? new PlayerCreateNPCEvent((Player) sender, npc)
: new CommandSenderCreateNPCEvent(sender, npc);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
npc.destroy();
String reason = "Couldn't create NPC.";
if (!event.getCancelReason().isEmpty())
reason += " Reason: " + event.getCancelReason();
throw new CommandException(reason);
}
if (args.hasValueFlag("at")) {
String[] parts = Iterables.toArray(Splitter.on(':').split(args.getFlag("at")), String.class);

View File

@ -3,6 +3,7 @@ package net.citizensnpcs.npc;
import java.util.Iterator;
import net.citizensnpcs.NPCDataStore;
import net.citizensnpcs.api.event.NPCCreateEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.trait.Trait;
@ -10,6 +11,7 @@ import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.ByIdArray;
import net.citizensnpcs.util.NMS;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
@ -31,6 +33,7 @@ public class CitizensNPCRegistry implements NPCRegistry {
if (npc == null)
throw new IllegalStateException("Could not create NPC.");
npcs.put(npc.getId(), npc);
Bukkit.getPluginManager().callEvent(new NPCCreateEvent(npc));
return npc;
}