下面列出了怎么用org.bukkit.event.world.ChunkUnloadEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@EventHandler
public void onUnload(ChunkUnloadEvent event) {
Chunk unloadedChunk = event.getChunk();
for (Entity entity : unloadedChunk.getEntities()) {
if (entity instanceof LivingEntity) {
Object handle = BukkitUnwrapper.getInstance().unwrap(entity);
if (handle instanceof ControllableEntityHandle) {
ControllableEntity controllableEntity = ((ControllableEntityHandle) handle).getControllableEntity();
if (controllableEntity != null && controllableEntity.isSpawned()) {
this.SPAWN_QUEUE.add(new EntityChunkData(controllableEntity, entity.getLocation()));
controllableEntity.despawn(DespawnReason.CHUNK_UNLOAD);
}
}
}
}
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
Chunk chunk = event.getChunk();
for (Entity entity : chunk.getEntities()) {
HologramEntity hologramEntity = plugin.getEntityController().getHologramEntity(entity);
if (hologramEntity != null) {
hologramEntity.remove();
}
}
Collection<Hologram> holograms = plugin.getHologramManager().getActiveHolograms().values();
for (Hologram holo : holograms) {
Location loc = holo.getLocation();
int chunkX = (int) Math.floor(loc.getBlockX() / 16.0D);
int chunkZ = (int) Math.floor(loc.getBlockZ() / 16.0D);
if (chunkX == chunk.getX() && chunkZ == chunk.getZ()) {
holo.despawn();
}
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onChunkUnload(ChunkUnloadEvent event) {
final Chunk chunk = event.getChunk();
final List<Integer> entityIds = new ArrayList<Integer>();
for(Entity entity : chunk.getEntities()) {
if(entity instanceof LivingEntity) {
entityIds.add(entity.getEntityId());
}
}
Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
public void run() {
if(!chunk.isLoaded()) {
for(int entityId : entityIds) {
EntityIdList.removeEntity(entityId);
}
}
}
}, 40L); // TODO: increase delay?
}
@EventHandler(priority = EventPriority.MONITOR)
public void OnChunkUnload(ChunkUnloadEvent event) {
BonusGoodie goodie;
for (Entity entity : event.getChunk().getEntities()) {
if (!(entity instanceof Item)) {
continue;
}
goodie = CivGlobal.getBonusGoodie(((Item)entity).getItemStack());
if (goodie == null) {
continue;
}
goodie.replenish(((Item)entity).getItemStack(), (Item)entity, null, null);
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent event) {
if (!Settings.saveEntities || !IslandGuard.inWorld(event.getWorld())) {
return;
}
// Delete the chunk data
entities.set(event.getWorld().getName() + "." + event.getChunk().getX() + "." + event.getChunk().getZ() , null);
// Create new entry
Arrays.stream(event.getChunk().getEntities()).filter(x -> x.hasMetadata("spawnLoc")).forEach(entity -> {
// Get the meta data
entity.getMetadata("spawnLoc").stream().filter(y -> y.getOwningPlugin().equals(plugin)).forEach(v -> {
entities.set(event.getWorld().getName() + "."
+ event.getChunk().getX() + "." + event.getChunk().getZ() + "."
+ entity.getUniqueId().toString(), v.asString());
});
});
Util.saveYamlFile(entities, "entitylimits.yml", true);
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent unload) {
if (unload instanceof Cancellable) {
Chunk chunk = unload.getChunk();
for (String name : Main.getGameNames()) {
Game game = Main.getGame(name);
if (game.getStatus() != GameStatus.DISABLED && game.getStatus() != GameStatus.WAITING
&& GameCreator.isChunkInArea(chunk, game.getPos1(), game.getPos2())) {
((Cancellable) unload).setCancelled(false);
return;
}
}
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onChunkUnload(ChunkUnloadEvent event) {
if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(event.getWorld().getUID())) {
return;
}
final GDClaimManager claimWorldManager = GriefDefenderPlugin.getInstance().dataStore.getClaimWorldManager(event.getWorld().getUID());
final GDChunk gdChunk = claimWorldManager.getChunk(event.getChunk());
if (gdChunk != null) {
if (gdChunk.getTrackedShortPlayerPositions().size() > 0) {
gdChunk.saveChunkTrackingData();
}
claimWorldManager.removeChunk(gdChunk.getChunkKey());
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent e) {
final Map<Location, Shop> inChunk = plugin.getShopManager().getShops(e.getChunk());
if (inChunk == null) {
return;
}
for (Shop shop : inChunk.values()) {
if (shop.isLoaded()) {
shop.onUnload();
}
}
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent unload) {
if (unload instanceof Cancellable) {
Chunk chunk = unload.getChunk();
for (String name : Main.getGameNames()) {
Game game = Main.getGame(name);
if (game.getStatus() != GameStatus.DISABLED && game.getStatus() != GameStatus.WAITING
&& GameCreator.isChunkInArea(chunk, game.getPos1(), game.getPos2())) {
((Cancellable) unload).setCancelled(false);
return;
}
}
}
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent e) {
if (sm.getCustomConfig().getStringList("no-stack-worlds")
.contains(e.getWorld().getName())) {
return;
}
for (Entity entity : e.getChunk().getEntities()) {
cleanupEntity(entity);
}
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
Chunk chunk = event.getChunk();
for (Region r : new HashMap<>(carts).keySet()) {
StorageMinecart sm = carts.get(r);
if (Util.isWithinChunk(chunk, sm.getLocation())) {
carts.remove(r);
orphanCarts.put(r, sm);
}
}
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent e){
LobbyManager.getInstance();
if(LobbyManager.lobbychunks.contains(e.getChunk())){
// e.setCancelled(true)
// TODO find a alternative way to keep the lobby chunks loaded
SurvivalGames.debug("[KeepLobbyLoadedEvent] Lobby Chunk unloading");
}
//System.out.println("Chunk unloading");
}
@EventHandler(ignoreCancelled = true)
public void onChunkUnloadEvent(ChunkUnloadEvent event)
{
World world = event.getWorld();
Chunk chunk = event.getChunk();
if (!this.plugin.getWorldGuardCommunicator().doUnloadChunkFlagCheck(world, chunk))
{
event.setCancelled(true);
}
}
@EventHandler
public void onUnload(ChunkUnloadEvent unload) {
Game game = BedwarsRel.getInstance().getGameManager()
.getGameByChunkLocation(unload.getChunk().getX(),
unload.getChunk().getZ());
if (game == null) {
return;
}
if (game.getState() != GameState.RUNNING) {
return;
}
unload.setCancelled(true);
}
@EventHandler
public static void onChunkUnload(ChunkUnloadEvent event) {
Chunk chunk = event.getChunk();
long pair = MathMan.pairInt(chunk.getX(), chunk.getZ());
Long lastLoad = keepLoaded.get(pair);
if (lastLoad != null) {
if (Fawe.get().getTimer().getTickStart() - lastLoad < 10000) {
event.setCancelled(true);
} else {
keepLoaded.remove(pair);
}
}
}
@EventHandler(priority = EventPriority.HIGH)
public void onChunkUnloadEvent(ChunkUnloadEvent event) {
Boolean persist = CivGlobal.isPersistChunk(event.getChunk());
if (persist != null && persist == true) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent event) {
for (Entity entity : event.getChunk().getEntities()) {
if (!entity.isDead()) {
NMSEntityBase entityBase = nmsManager.getNMSEntityBase(entity);
if (entityBase != null) {
((CraftHologram) entityBase.getHologramLine().getParent()).despawnEntities();
}
}
}
}
@EventHandler
public void chunkUnload(@Nonnull ChunkUnloadEvent event) {
Arrays.stream(event.getChunk().getTileEntities())
.filter(blockState -> blockState instanceof Sign)
.forEach(sign -> lastSeenSigns.remove(sign.getLocation()));
}
@EventHandler
public void chunkUnload(@Nonnull ChunkUnloadEvent event) {
Arrays.stream(event.getChunk().getTileEntities())
.filter(blockState -> blockState instanceof Skull)
.forEach(sign -> lastSeenSkulls.remove(sign.getLocation()));
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
// System.out.println("chunk unloaded: " + event.getChunk().getX() + ", " + event.getChunk().getZ());
UnloadedInventoryHandler.getInstance().updateInventoriesInChunk(event.getChunk());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void recalculate(ChunkUnloadEvent event) {
plugin.getWorthManager().recalculate(event.getChunk(), RecalculateReason.UNLOAD);
}
@EventHandler
public void onUnload(ChunkUnloadEvent event) {
EntityTracker.chunkWiper(event);
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent chunkUnloadEvent) {
checkSafety(chunkUnloadEvent);
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent chunkUnloadEvent) {
checkSafety(chunkUnloadEvent);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent event) {
// Save the chunk in its current state and use it instead of loading
// the chunk
plugin.getChunkManager(event.getWorld()).deactivateChunk(event.getChunk());
}
@EventHandler(priority = EventPriority.MONITOR)
public void onChunkUnload(ChunkUnloadEvent event) {
ChunkManager.getInstance().unloadChunk(event.getChunk());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
void onChunkUnload(ChunkUnloadEvent event) {
plugin.unloadShopkeepersInChunk(event.getChunk());
}
/**
* Wipes a chunk clean of all relevant plugin entities and data.
*
* @param event ChunkUnloadEvent to be cleared
*/
public static void chunkWiper(ChunkUnloadEvent event) {
for (Entity entity : event.getChunk().getEntities())
wipeEntity(entity);
}