下面列出了org.bukkit.Chunk#getZ ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public List<Chunk> getChunks(World mapWorld) {
int size = 400;
int maxX = size/2;
int minX = -size/2;
int maxZ = size/2;
int minZ = -size/2;
int minY = 0;
int maxY = 0;
Block min = mapWorld.getBlockAt(minX, minY, minZ);
Block max = mapWorld.getBlockAt(maxX, maxY, maxZ);
Chunk cMin = min.getChunk();
Chunk cMax = max.getChunk();
List<Chunk> chunks = new ArrayList<>();
for(int cx = cMin.getX(); cx < cMax.getX(); cx++) {
for(int cz = cMin.getZ(); cz < cMax.getZ(); cz++) {
Chunk currentChunk = mapWorld.getChunkAt(cx, cz);
chunks.add(currentChunk);
}
}
return chunks;
}
@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();
}
}
}
public static boolean isChunkInArea(Chunk l, Location p1, Location p2) {
if (!p1.getWorld().equals(l.getWorld())) {
return false;
}
Chunk min = new Location(p1.getWorld(), Math.min(p1.getX(), p2.getX()), Math.min(p1.getY(), p2.getY()),
Math.min(p1.getZ(), p2.getZ())).getChunk();
Chunk max = new Location(p1.getWorld(), Math.max(p1.getX(), p2.getX()), Math.max(p1.getY(), p2.getY()),
Math.max(p1.getZ(), p2.getZ())).getChunk();
return (min.getX() <= l.getX() && min.getZ() <= l.getZ() && max.getX() >= l.getX() && max.getZ() >= l.getZ());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public synchronized void onChunkFinishedGeneration(ChunkPopulateEvent evt) {
Chunk chunk = evt.getChunk();
UUID world = chunk.getWorld().getUID();
Vector2i chunkPos = new Vector2i(chunk.getX(), chunk.getZ());
listeners.forEach(l -> l.onChunkFinishedGeneration(world, chunkPos));
}
@Override
public void populate(World world, Random random, Chunk chunk) {
int x, y, z, i;
int worldChunkX = chunk.getX() * 16;
int worldChunkZ = chunk.getZ() * 16;
for (i = 0; i < pockets; i++) {
x = worldChunkX + random.nextInt(16);
z = worldChunkZ + random.nextInt(16);
y = random.nextInt(257);
this.createVein(world, max, x, y, z);
}
}
public static boolean isChunkInArea(Chunk l, Location p1, Location p2) {
if (!p1.getWorld().equals(l.getWorld())) {
return false;
}
Chunk min = new Location(p1.getWorld(), Math.min(p1.getX(), p2.getX()), Math.min(p1.getY(), p2.getY()),
Math.min(p1.getZ(), p2.getZ())).getChunk();
Chunk max = new Location(p1.getWorld(), Math.max(p1.getX(), p2.getX()), Math.max(p1.getY(), p2.getY()),
Math.max(p1.getZ(), p2.getZ())).getChunk();
return (min.getX() <= l.getX() && min.getZ() <= l.getZ() && max.getX() >= l.getX() && max.getZ() >= l.getZ());
}
public void syncAllInventoriesInChunk(Chunk chunk) {
String chunkString = "c:" + chunk.getX() + ":" + chunk.getZ();
if (!unloadedChestInventories.containsKey(chunkString)) {
return;
}
for (String locationString : unloadedChestInventories.get(chunkString).keySet()) {
syncInventory(locationString);
}
}
private static boolean getChunksEqual(Chunk a, Chunk b) {
if (Objects.equals(a, b)) return true;
if (a == null || b == null) return false;
return (Objects.equals(a.getWorld(), b.getWorld())
&& a.getX() == b.getX()
&& a.getZ() == b.getZ());
}
/**
* Wraps a chunk with a ChunkWrapper
*
* @param chunk the chunk to wrap
* @param durabilitiesDir the directory to store this wrapper in
*/
ChunkWrapper(Chunk chunk, File durabilitiesDir) {
this.chunkX = chunk.getX();
this.chunkZ = chunk.getZ();
this.world = chunk.getWorld().getName();
this.durabilitiesDir = durabilitiesDir;
}
@Override
public boolean check(Chunk chunk) {
Random random = new Random(chunk.getWorld().getSeed() +
(long) (chunk.getX() * chunk.getX() * 0x4c1906) +
(long) (chunk.getX() * 0x5ac0db) +
(long) (chunk.getZ() * chunk.getZ()) * 0x4307a7L +
(long) (chunk.getZ() * 0x5f24f) ^ 0x3ad8025f);
return random.nextInt(10) == 0;
}
public static ChunkVector of(Chunk chunk) {
return new ChunkVector(chunk.getX(), chunk.getZ());
}
public static Region forChunk(Chunk chunk) {
Vector min = new Vector(chunk.getX() * 16, 0, chunk.getZ() * 16);
return new CuboidRegion(min, new Vector(min.getX() + 16, 256, min.getZ() + 16));
}
public GChunk(Chunk chunk) {
this.world = chunk.getWorld().getName();
this.x = chunk.getX();
this.z = chunk.getZ();
}
public static Region forChunk(Chunk chunk) {
Vector min = new Vector(chunk.getX() * 16, 0, chunk.getZ() * 16);
return new CuboidRegion(min, new Vector(min.getX() + 16, 256, min.getZ() + 16));
}
public static List<Entity> getNearbyEntities(Location location, double radius, EntityType... types) {
List<Entity> entities = new ArrayList<Entity>();
if (location == null) return entities;
if (radius <= 0.0D) return entities;
double radius2 = radius * radius;
int chunkRadius = ((int) (radius / 16)) + 1;
Chunk center = location.getChunk();
int startX = center.getX() - chunkRadius;
int endX = center.getX() + chunkRadius;
int startZ = center.getZ() - chunkRadius;
int endZ = center.getZ() + chunkRadius;
World world = location.getWorld();
for (int chunkX = startX; chunkX <= endX; chunkX++) {
for (int chunkZ = startZ; chunkZ <= endZ; chunkZ++) {
if (!world.isChunkLoaded(chunkX, chunkZ)) continue;
Chunk chunk = world.getChunkAt(chunkX, chunkZ);
for (Entity entity : chunk.getEntities()) {
Location entityLoc = entity.getLocation();
// TODO: this is a workaround: for some yet unknown reason entities sometimes report to be in a
// different world..
if (!entityLoc.getWorld().equals(world)) {
Log.debug("Found an entity which reports to be in a different world than the chunk we got it from:");
Log.debug("Location=" + location + ", Chunk=" + chunk + ", ChunkWorld=" + chunk.getWorld()
+ ", entityType=" + entity.getType() + ", entityLocation=" + entityLoc);
continue; // skip this entity
}
if (entityLoc.distanceSquared(location) <= radius2) {
if (types == null) {
entities.add(entity);
} else {
EntityType type = entity.getType();
for (EntityType t : types) {
if (type.equals(t)) {
entities.add(entity);
break;
}
}
}
}
}
}
}
return entities;
}
public ChunkData(Chunk chunk) {
Validate.notNull(chunk);
this.worldName = chunk.getWorld().getName();
this.chunkX = chunk.getX();
this.chunkZ = chunk.getZ();
}
public long getTimings(Chunk c) {
String id = c.getWorld().getName() + ';' + c.getX() + ';' + c.getZ();
return chunkTimings.getOrDefault(id, 0L);
}
@SuppressWarnings("unused")
@EventHandler
public void onPlayerMove(PlayerMoveEvent e) {
if (e != null && !e.isCancelled() && e.getTo() != null) {
// Get the previous and current chunks
Chunk prev = e.getFrom().getChunk();
Chunk to = e.getTo().getChunk();
// Make sure the player moved into a new chunk
if (prev.getX() != to.getX() || prev.getZ() != to.getZ()) {
// If the claim is currently autoclaiming, try to claim this chunk
if (AutoClaimHandler.inList(e.getPlayer())) {
claimChunk.getCommandHandler().mainHandler.claimChunk(e.getPlayer(), to);
return;
}
ChunkHandler ch = claimChunk.getChunkHandler();
// Check if the previous chunk was already claimed
boolean lastClaimed = ch.isClaimed(prev.getWorld(), prev.getX(), prev.getZ());
// Check if the new chunk is already claimed
if (ch.isClaimed(to.getWorld(), to.getX(), to.getZ())) {
// If the new chunk and the previous chunk were claimed, check if the owners differ
if (lastClaimed) {
UUID prevOwner = ch.getOwner(prev.getWorld(), prev.getX(), prev.getZ());
UUID newOwner = ch.getOwner(to.getWorld(), to.getX(), to.getZ());
// Only display the new chunk's owner if they differ from the previous chunk's owner
if ((prevOwner == null && newOwner == null) || (prevOwner != null && !prevOwner.equals(newOwner))) {
showTitle(e.getPlayer(), to);
}
} else {
// Show the player the chunk's owner
showTitle(e.getPlayer(), to);
}
} else {
// The player entered an unclaimed chunk from a claimed chunk
if (lastClaimed) {
UUID lastOwner = ch.getOwner(prev.getWorld(), prev.getX(), prev.getZ());
String name = claimChunk.getPlayerHandler().getChunkName(lastOwner);
String msg = (e.getPlayer().getUniqueId().equals(lastOwner) ? claimChunk.getMessages().chunkLeaveSelf : claimChunk.getMessages().chunkLeave)
.replace("%%PLAYER%%",
((name == null) ? claimChunk.getMessages().chunkLeaveUnknown : name));
Utils.toPlayer(e.getPlayer(), msg);
}
}
}
}
}
/**
* Convert a chunk to a string to use as map key.
* Use a Location argument to prevent chunk loading!
* @param chunk The location to get the key for
* @return A string to use in a map for a chunk
*/
public static String chunkToString(Chunk chunk) {
return chunk.getWorld().getName() + ";" + chunk.getX() + ";" + chunk.getZ();
}
/**
* Create an instance of a chunk position from Spigot's chunk position
* representation.
*
* @param chunk The Spigot chunk representation.
*/
public ChunkPos(Chunk chunk) {
this(chunk.getWorld().getName(), chunk.getX(), chunk.getZ());
}