EventListen calls with DespawnReason

This commit is contained in:
fullwall 2012-11-21 20:10:21 +08:00
parent 5aebf7c2bb
commit 62d3eb8234
5 changed files with 41 additions and 23 deletions

View File

@ -4,6 +4,7 @@ import java.util.List;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.EntityTargetNPCEvent;
import net.citizensnpcs.api.event.NPCCombustByBlockEvent;
import net.citizensnpcs.api.event.NPCCombustByEntityEvent;
@ -82,16 +83,13 @@ public class EventListen implements Listener {
@EventHandler(ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent event) {
ChunkCoord coord = toCoord(event.getChunk());
boolean forceLoad = Setting.KEEP_CHUNKS_LOADED.asBoolean();
for (NPC npc : npcRegistry) {
if (!npc.isSpawned())
continue;
Location loc = npc.getBukkitEntity().getLocation();
if (forceLoad && loc.getChunk().isLoaded())
continue; // location#getChunk() forces chunk to load
boolean sameChunkCoordinates = coord.z == loc.getBlockZ() >> 4 && coord.x == loc.getBlockX() >> 4;
if (event.getWorld().equals(loc.getWorld()) && sameChunkCoordinates) {
npc.despawn();
if (sameChunkCoordinates && event.getWorld().equals(loc.getWorld())) {
npc.despawn(DespawnReason.CHUNK_UNLOAD);
toRespawn.put(coord, npc.getId());
Messaging.debug("Despawned", npc.getId(), "due to chunk unload at [" + coord.x + ","
+ coord.z + "]");
@ -161,7 +159,7 @@ public class EventListen implements Listener {
return;
NPC npc = npcRegistry.getNPC(event.getEntity());
Bukkit.getPluginManager().callEvent(new NPCDeathEvent(npc, event));
npc.despawn();
npc.despawn(DespawnReason.DEATH);
}
@EventHandler(priority = EventPriority.HIGHEST)
@ -272,7 +270,12 @@ public class EventListen implements Listener {
NPC npc = npcRegistry.getById(id);
if (npc == null)
return;
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
Location spawn = npc.getTrait(CurrentLocation.class).getLocation();
if (spawn == null) {
Messaging.debug("Couldn't find a spawn location for despawned NPC ID: " + id);
return;
}
npc.spawn(spawn);
}
private void storeForRespawn(NPC npc) {

View File

@ -68,6 +68,7 @@ public class Settings {
}
},
HIGHLIGHT_COLOUR("general.color-scheme.message-highlight", "<e>"),
KEEP_CHUNKS_LOADED("npc.chunks.always-keep-loaded", false),
LOCALE("general.translation.locale", ""),
MAX_NPC_LIMIT_CHECKS("npc.limits.max-permission-checks", 100),
MAX_SPEED("npc.limits.max-speed", 100),
@ -84,8 +85,7 @@ public class Settings {
SUBPLUGIN_FOLDER("subplugins.folder", "plugins"),
TALK_CLOSE_MAXIMUM_COOLDOWN("npc.text.max-talk-cooldown", 60),
TALK_CLOSE_MINIMUM_COOLDOWN("npc.text.min-talk-cooldown", 30),
TALK_ITEM("npc.text.talk-item", "340"),
KEEP_CHUNKS_LOADED("npc.chunks.always-keep-loaded", false);
TALK_ITEM("npc.text.talk-item", "340");
protected String path;
protected Object value;

View File

@ -5,8 +5,10 @@ import java.util.List;
import javax.annotation.Nullable;
import net.citizensnpcs.EventListen;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.Navigator;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.exception.NPCLoadException;
@ -48,10 +50,23 @@ public abstract class CitizensNPC extends AbstractNPC {
@Override
public boolean despawn() {
return despawn(DespawnReason.PLUGIN);
}
@Override
public boolean despawn(DespawnReason reason) {
if (!isSpawned())
return false;
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
NPCDespawnEvent event = new NPCDespawnEvent(this, reason);
if (reason == DespawnReason.CHUNK_UNLOAD)
event.setCancelled(Setting.KEEP_CHUNKS_LOADED.asBoolean());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
getBukkitEntity().getLocation().getChunk();
// ensure that we are in a loaded chunk.
return false;
}
boolean keepSelected = getTrait(Spawned.class).shouldSpawn();
if (!keepSelected)
data().remove("selectors");

View File

@ -82,6 +82,7 @@ public class Messages {
public static final String INVALID_ANIMATION = "citizens.editors.waypoints.triggers.animation.invalid-animation";
public static final String INVALID_POSE_NAME = "citizens.commands.npc.pose.invalid-name";
public static final String INVALID_PROFESSION = "citizens.commands.npc.profession.invalid-profession";
public static final String INVALID_SKELETON_TYPE = "citizens.commands.npc.skeletontype.invalid-type";
public static final String INVALID_SPAWN_LOCATION = "citizens.commands.npc.create.invalid-location";
public static final String INVALID_TRIGGER_TELEPORT_FORMAT = "citizens.editors.waypoints.triggers.teleport.invalid-format";
public static final String LINEAR_WAYPOINT_EDITOR_ADDED_WAYPOINT = "citizens.editors.waypoints.linear.added-waypoint";
@ -211,5 +212,4 @@ public class Messages {
public static final String WAYPOINT_TRIGGER_TELEPORT_PROMPT = "citizens.editors.waypoints.triggers.teleport.prompt";
public static final String WORLD_NOT_FOUND = "citizens.commands.errors.missing-world";
public static final String WRITING_DEFAULT_SETTING = "citizens.settings.writing-default";
public static final String INVALID_SKELETON_TYPE = "citizens.commands.npc.skeletontype.invalid-type";
}

View File

@ -24,6 +24,12 @@ public enum PlayerAnimation {
sendPacketNearby(packet, player, radius);
}
},
SIT {
@Override
protected void playAnimation(EntityPlayer player, int radius) {
player.mount(player);
}
},
SLEEP {
@Override
protected void playAnimation(EntityPlayer player, int radius) {
@ -32,18 +38,6 @@ public enum PlayerAnimation {
sendPacketNearby(packet, player, radius);
}
},
SIT {
@Override
protected void playAnimation(EntityPlayer player, int radius) {
player.mount(player);
}
},
STOP_SITTING {
@Override
protected void playAnimation(EntityPlayer player, int radius) {
player.mount(null);
}
},
SNEAK {
@Override
protected void playAnimation(EntityPlayer player, int radius) {
@ -52,6 +46,12 @@ public enum PlayerAnimation {
radius);
}
},
STOP_SITTING {
@Override
protected void playAnimation(EntityPlayer player, int radius) {
player.mount(null);
}
},
STOP_SLEEPING {
@Override
protected void playAnimation(EntityPlayer player, int radius) {