Java Code Examples for org.bukkit.Chunk#isLoaded()

The following examples show how to use org.bukkit.Chunk#isLoaded() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HologramListener.java    From Holograms with MIT License 6 votes vote down vote up
@EventHandler
public void onChunkLoad(ChunkLoadEvent event) {
    Chunk chunk = event.getChunk();
    if (chunk == null || !chunk.isLoaded()) {
        return;
    }

    Collection<Hologram> holograms = plugin.getHologramManager().getActiveHolograms().values();
    for (Hologram holo : holograms) {
        int chunkX = (int) Math.floor(holo.getLocation().getBlockX() / 16.0D);
        int chunkZ = (int) Math.floor(holo.getLocation().getBlockZ() / 16.0D);
        if (chunkX == chunk.getX() && chunkZ == chunk.getZ()) {
            plugin.getServer().getScheduler().runTaskLater(plugin, holo::spawn, 10L);
        }
    }
}
 
Example 2
Source File: HologramEntityControllerImpl.java    From Holograms with MIT License 6 votes vote down vote up
private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) {
    net.minecraft.server.v1_8_R2.Chunk nmsChunk = nmsWorld.getChunkAtWorldCoords(nmsEntity.getChunkCoordinates());

    if (nmsChunk != null) {
        Chunk chunk = nmsChunk.bukkitChunk;

        if (chunk == null) {
            try {
                chunk = new CraftChunk(nmsChunk);
            } catch (NullPointerException e) {
                throw new HologramEntitySpawnException("Attempted to spawn hologram entity in invalid chunk", e);
            }
        }

        if (!chunk.isLoaded()) {
            chunk.load();
            plugin.getLogger().info("Loaded chunk (x:" + chunk.getX() + " z:" + chunk.getZ() + ") to spawn a Hologram");
        }
    }

    return nmsWorld.addEntity(nmsEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
 
Example 3
Source File: HologramEntityControllerImpl.java    From Holograms with MIT License 6 votes vote down vote up
private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) {
    net.minecraft.server.v1_8_R1.Chunk nmsChunk = nmsWorld.getChunkAtWorldCoords(nmsEntity.getChunkCoordinates());

    if (nmsChunk != null) {
        Chunk chunk = nmsChunk.bukkitChunk;

        if (chunk == null) {
            try {
                chunk = new CraftChunk(nmsChunk);
            } catch (NullPointerException e) {
                throw new HologramEntitySpawnException("Attempted to spawn hologram entity in invalid chunk", e);
            }
        }

        if (!chunk.isLoaded()) {
            chunk.load();
            plugin.getLogger().info("Loaded chunk (x:" + chunk.getX() + " z:" + chunk.getZ() + ") to spawn a Hologram");
        }
    }

    return nmsWorld.addEntity(nmsEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
 
Example 4
Source File: VersionHelper18.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
private static Set<Player> getNearbyPlayersInChunks(Location location) {
    World world = location.getWorld();
    int chunkX = location.getBlockX() >> 4;
    int chunkZ = location.getBlockZ() >> 4;
    int range = 3;

    Set<Player> players = new HashSet<>();
    for (int x = chunkX - range; x <= chunkX + range; x++) {
        for (int z = chunkZ - range; z <= chunkZ + range; z++) {
            Chunk chunk = world.getChunkAt(x, z);
            if (chunk.isLoaded())
                players.addAll(Arrays.stream(chunk.getEntities())
                        .filter((it) -> it.getType() == EntityType.PLAYER)
                        .map((it) -> (Player) it).collect(Collectors.toSet()));
        }
    }

    return players;
}
 
Example 5
Source File: GameStore.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @return killed entity
 */
public LivingEntity kill() {
    final LivingEntity livingEntity = entity;
    if (entity != null) {
        final Chunk chunk = entity.getLocation().getChunk();

        if (!chunk.isLoaded()) {
            chunk.load();
        }
        entity.remove();
        entity = null;
    }
    return livingEntity;
}
 
Example 6
Source File: HologramEntityControllerImpl.java    From Holograms with MIT License 5 votes vote down vote up
private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) {
    net.minecraft.server.v1_9_R1.Chunk nmsChunk = nmsWorld.getChunkAtWorldCoords(nmsEntity.getChunkCoordinates());

    if (nmsChunk != null) {
        Chunk chunk = nmsChunk.bukkitChunk;

        if (!chunk.isLoaded()) {
            chunk.load();
            plugin.getLogger().info("Loaded chunk (x:" + chunk.getX() + " z:" + chunk.getZ() + ") to spawn a Hologram");
        }
    }

    return nmsWorld.addEntity(nmsEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
 
Example 7
Source File: HologramEntityControllerImpl.java    From Holograms with MIT License 5 votes vote down vote up
private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) {
    net.minecraft.server.v1_13_R2.Chunk nmsChunk = nmsWorld.getChunkAtWorldCoords(nmsEntity.getChunkCoordinates());

    if (nmsChunk != null) {
        Chunk chunk = nmsChunk.bukkitChunk;

        if (!chunk.isLoaded()) {
            chunk.load();
            plugin.getLogger().info("Loaded chunk (x:" + chunk.getX() + " z:" + chunk.getZ() + ") to spawn a Hologram");
        }
    }

    return nmsWorld.addEntity(nmsEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
 
Example 8
Source File: HologramEntityControllerImpl.java    From Holograms with MIT License 5 votes vote down vote up
private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) {
    net.minecraft.server.v1_9_R2.Chunk nmsChunk = nmsWorld.getChunkAtWorldCoords(nmsEntity.getChunkCoordinates());

    if (nmsChunk != null) {
        Chunk chunk = nmsChunk.bukkitChunk;

        if (!chunk.isLoaded()) {
            chunk.load();
            plugin.getLogger().info("Loaded chunk (x:" + chunk.getX() + " z:" + chunk.getZ() + ") to spawn a Hologram");
        }
    }

    return nmsWorld.addEntity(nmsEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
 
Example 9
Source File: HologramEntityControllerImpl.java    From Holograms with MIT License 5 votes vote down vote up
private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) {
    net.minecraft.server.v1_11_R1.Chunk nmsChunk = nmsWorld.getChunkAtWorldCoords(nmsEntity.getChunkCoordinates());

    if (nmsChunk != null) {
        Chunk chunk = nmsChunk.bukkitChunk;

        if (!chunk.isLoaded()) {
            chunk.load();
            plugin.getLogger().info("Loaded chunk (x:" + chunk.getX() + " z:" + chunk.getZ() + ") to spawn a Hologram");
        }
    }

    return nmsWorld.addEntity(nmsEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
 
Example 10
Source File: SyncLoadChunk.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void run() {
	
	if (lock.tryLock()) {
		try {
			for (int i = 0; i < UPDATE_LIMIT; i++) {
				LoadChunkRequest request = requestQueue.poll();
				if (request == null) {
					return;
				}
				
				Chunk chunk = Bukkit.getWorld(request.worldName).getChunkAt(request.x, request.z);
				if (!chunk.isLoaded()) {
					if (!chunk.load()) {
						CivLog.error("Couldn't load chunk at "+request.x+","+request.z);
						continue;
					}
				}
				
				request.finished = true;
				request.condition.signalAll();					
			}
		} finally {
			lock.unlock();
		}
		
	} else {
		//CivLog.warning("SyncLoadChunk: lock was busy, try again next tick.");
	}
}
 
Example 11
Source File: AsyncWorld.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean unloadChunk(final Chunk chunk) {
    if (chunk.isLoaded()) {
        return TaskManager.IMP.sync(new RunnableVal<Boolean>() {
            @Override
            public void run(Boolean value) {
                this.value = parent.unloadChunk(chunk);
            }
        });
    }
    return true;
}
 
Example 12
Source File: MainListener.java    From HolographicDisplays with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler (priority = EventPriority.MONITOR)
public void onChunkLoad(ChunkLoadEvent event) {
	Chunk chunk = event.getChunk();
	
	// Other plugins could call this event wrongly, check if the chunk is actually loaded.
	if (chunk.isLoaded()) {
		
		// In case another plugin loads the chunk asynchronously always make sure to load the holograms on the main thread.
		if (Bukkit.isPrimaryThread()) {
			processChunkLoad(chunk);
		} else {
			Bukkit.getScheduler().runTask(HolographicDisplays.getInstance(), () -> processChunkLoad(chunk));
		}
	}
}
 
Example 13
Source File: BukkitQueue_1_12.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Deprecated
public boolean unloadChunk(final String world, final Chunk chunk) {
    net.minecraft.server.v1_12_R1.Chunk c = ((CraftChunk) chunk).getHandle();
    c.mustSave = false;
    if (chunk.isLoaded()) {
        chunk.unload(false, false);
    }
    return true;
}
 
Example 14
Source File: BukkitQueue_1_10.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Deprecated
public boolean unloadChunk(final String world, final Chunk chunk) {
    net.minecraft.server.v1_10_R1.Chunk c = ((CraftChunk) chunk).getHandle();
    c.mustSave = false;
    if (chunk.isLoaded()) {
        chunk.unload(false, false);
    }
    return true;
}
 
Example 15
Source File: Game.java    From BedwarsRel with GNU General Public License v3.0 5 votes vote down vote up
public void updateSigns() {
  boolean removedItem = false;

  Iterator<GameJoinSign> iterator = Game.this.joinSigns.values().iterator();
  while (iterator.hasNext()) {
    GameJoinSign sign = iterator.next();

    Chunk signChunk = sign.getSign().getLocation().getChunk();
    if (!signChunk.isLoaded()) {
      signChunk.load(true);
    }

    if (sign.getSign() == null) {
      iterator.remove();
      removedItem = true;
      continue;
    }

    Block signBlock = sign.getSign().getLocation().getBlock();
    if (!(signBlock.getState() instanceof Sign)) {
      iterator.remove();
      removedItem = true;
      continue;
    }
    sign.updateSign();
  }

  if (removedItem) {
    Game.this.updateSignConfig();
  }
}
 
Example 16
Source File: UnloadedInventoryHandler.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
public void loadChunks() {
    for (Map.Entry<String, HashMap<String, CVInventory>> outerEntry : unloadedChestInventories.entrySet()) {
        for (Map.Entry<String, CVInventory> entry : outerEntry.getValue().entrySet()) {
            CVInventory cvInventory = entry.getValue();
            if (cvInventory.getLastUnloadedModification() != -1 &&
                    System.currentTimeMillis() > ConfigManager.getInstance().getUnloadedChestRefreshRate() +
                            cvInventory.getLastUnloadedModification()) {
                Chunk chunk = cvInventory.getLocation().getChunk();
                if (!chunk.isLoaded()) {
                    chunk.load();
                }
            }
        }
    }
}
 
Example 17
Source File: GameStore.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @return killed entity
 */
public LivingEntity kill() {
    final LivingEntity livingEntity = entity;
    if (entity != null) {
        final Chunk chunk = entity.getLocation().getChunk();

        if (!chunk.isLoaded()) {
            chunk.load();
        }
        entity.remove();
        entity = null;
    }
    return livingEntity;
}
 
Example 18
Source File: SimpleChunkManager.java    From EntityAPI with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean canSpawn(Chunk chunk) {
    return chunk.isLoaded();
}
 
Example 19
Source File: BukkitQueue_All.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ChunkSnapshot loadChunk(World world, int x, int z, boolean generate) {
    Chunk chunk = world.getChunkAt(x, z);
    chunk.load(generate);
    return chunk.isLoaded() ? getAndCacheChunk(chunk) : null;
}
 
Example 20
Source File: AsyncWorld.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isChunkLoaded(Chunk chunk) {
    return chunk.isLoaded();
}