Skip to content

Commit

Permalink
Using /npc despawn now removes from the respawn list
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 14, 2013
1 parent 19a6897 commit fb32f68
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/net/citizensnpcs/Citizens.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.citizensnpcs.api.event.CitizensDisableEvent;
import net.citizensnpcs.api.event.CitizensEnableEvent;
import net.citizensnpcs.api.event.CitizensReloadEvent;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCDataStore;
Expand Down Expand Up @@ -114,7 +115,7 @@ private void despawnNPCs() {
while (itr.hasNext()) {
NPC npc = itr.next();
try {
npc.despawn();
npc.despawn(DespawnReason.REMOVAL);
for (Trait trait : npc.getTraits())
trait.onRemove();
} catch (Throwable e) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.citizensnpcs.api.event.NPCDamageByEntityEvent;
import net.citizensnpcs.api.event.NPCDamageEvent;
import net.citizensnpcs.api.event.NPCDeathEvent;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.event.PlayerCreateNPCEvent;
Expand Down Expand Up @@ -179,6 +180,12 @@ public void onNeedsRespawn(NPCNeedsRespawnEvent event) {
toRespawn.put(toCoord(event.getSpawnLocation()), event.getNPC());
}

@EventHandler
public void onNPCDespawn(NPCDespawnEvent event) {
if (event.getReason() == DespawnReason.PLUGIN || event.getReason() == DespawnReason.REMOVAL)
toRespawn.remove(toCoord(event.getNPC().getBukkitEntity().getLocation()), event.getNPC());
}

@EventHandler(ignoreCancelled = true)
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
if (!(event.getPlayer() instanceof NPCHolder))
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/net/citizensnpcs/commands/NPCCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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.DespawnReason;
import net.citizensnpcs.api.event.PlayerCreateNPCEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
Expand Down Expand Up @@ -444,7 +445,7 @@ public void despawn(CommandContext args, CommandSender sender, NPC npc) throws C
throw new CommandException(Messages.NO_NPC_WITH_ID_FOUND, id);
}
npc.getTrait(Spawned.class).setSpawned(false);
npc.despawn();
npc.despawn(DespawnReason.REMOVAL);
Messaging.sendTr(sender, Messages.NPC_DESPAWNED, npc.getName());
}

Expand Down Expand Up @@ -851,7 +852,7 @@ public void rename(CommandContext args, CommandSender sender, NPC npc) {
newName = newName.substring(0, 15);
}
Location prev = npc.isSpawned() ? npc.getBukkitEntity().getLocation() : null;
npc.despawn();
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.setName(newName);
if (prev != null)
npc.spawn(prev);
Expand Down Expand Up @@ -1059,13 +1060,13 @@ public void tphere(CommandContext args, CommandSender sender, NPC npc) throws Co
npc.spawn(args.getSenderLocation());
if (!sender.hasPermission("citizens.npc.tphere.multiworld")
&& npc.getBukkitEntity().getLocation().getWorld() != args.getSenderLocation().getWorld()) {
npc.despawn();
npc.despawn(DespawnReason.REMOVAL);
throw new CommandException(Messages.CANNOT_TELEPORT_ACROSS_WORLDS);
}
} else {
if (!sender.hasPermission("citizens.npc.tphere.multiworld")
&& npc.getBukkitEntity().getLocation().getWorld() != args.getSenderLocation().getWorld()) {
npc.despawn();
npc.despawn(DespawnReason.REMOVAL);
throw new CommandException(Messages.CANNOT_TELEPORT_ACROSS_WORLDS);
}
npc.getBukkitEntity().teleport(args.getSenderLocation(), TeleportCause.COMMAND);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void setEntityController(EntityController newController) {
Location prev = null;
if (wasSpawned) {
prev = getBukkitEntity().getLocation();
despawn();
despawn(DespawnReason.PENDING_RESPAWN);
}
entityController = newController;
if (wasSpawned)
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/citizensnpcs/npc/CitizensNPCRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Iterator;

import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCCreateEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCDataStore;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void deregister(NPC npc) {
npcs.remove(npc.getId());
if (saves != null)
saves.clearData(npc);
npc.despawn();
npc.despawn(DespawnReason.REMOVAL);
}

@Override
Expand All @@ -57,7 +58,7 @@ public void deregisterAll() {
while (itr.hasNext()) {
NPC npc = itr.next();
itr.remove();
npc.despawn();
npc.despawn(DespawnReason.REMOVAL);
for (Trait t : npc.getTraits())
t.onRemove();
if (saves != null)
Expand Down

0 comments on commit fb32f68

Please sign in to comment.