Java Code Examples for org.bukkit.World#getChunkAt()

The following examples show how to use org.bukkit.World#getChunkAt() . 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: BukkitQueue_All.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ChunkSnapshot getCachedChunk(World world, int cx, int cz) {
    long pair = MathMan.pairInt(cx, cz);
    ChunkSnapshot cached = chunkCache.get(pair);
    if (cached != null) return cached;
    if (world.isChunkLoaded(cx, cz)) {
        Long originalKeep = keepLoaded.get(pair);
        keepLoaded.put(pair, Long.MAX_VALUE);
        if (world.isChunkLoaded(cx, cz)) {
            Chunk chunk = world.getChunkAt(cx, cz);
            ChunkSnapshot snapshot = getAndCacheChunk(chunk);
            if (originalKeep != null) {
                keepLoaded.put(pair, originalKeep);
            } else {
                keepLoaded.remove(pair);
            }
            return snapshot;
        } else {
            keepLoaded.remove(pair);
            return null;
        }
    } else {
        return null;
    }
}
 
Example 2
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 3
Source File: Util.java    From SkyWarsReloaded with GNU General Public License v3.0 6 votes vote down vote up
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;
  }
 
Example 4
Source File: ChunkPosition.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public Chunk getChunk(World world) {
    return world.getChunkAt(x, z);
}
 
Example 5
Source File: MapScanner.java    From VoxelGamesLibv2 with MIT License 4 votes vote down vote up
/**
 * Searches the map for "markers". Most of the time these are implemented as tile entities (skulls)
 *
 * @param map    the map to scan
 * @param center the center location
 * @param range  the range in where to scan
 */
public void searchForMarkers(@Nonnull Map map, @Nonnull Vector3D center, int range, @Nonnull UUID gameid) {
    World world = Bukkit.getWorld(map.getLoadedName(gameid));
    if (world == null) {
        throw new MapException("Could not find world " + map.getLoadedName(gameid) + "(" + map.getInfo().getDisplayName() + ")" + ". Is it loaded?");
    }

    List<Marker> markers = new ArrayList<>();
    List<ChestMarker> chestMarkers = new ArrayList<>();

    int startX = (int) center.getX();
    int startY = (int) center.getZ();

    int minX = Math.min(startX - range, startX + range);
    int minZ = Math.min(startY - range, startY + range);

    int maxX = Math.max(startX - range, startX + range);
    int maxZ = Math.max(startY - range, startY + range);

    for (int x = minX; x <= maxX; x += 16) {
        for (int z = minZ; z <= maxZ; z += 16) {
            Chunk chunk = world.getChunkAt(x >> 4, z >> 4);
            for (BlockState te : chunk.getTileEntities()) {
                if (te.getType() == Material.PLAYER_HEAD) {
                    Skull skull = (Skull) te;
                    String markerData = getMarkerData(skull);
                    if (markerData == null) continue;
                    MarkerDefinition markerDefinition = mapHandler.createMarkerDefinition(markerData);
                    markers.add(new Marker(new Vector3D(skull.getX(), skull.getY(), skull.getZ()),
                            DirectionUtil.directionToYaw(skull.getRotation()),
                            markerData, markerDefinition));
                } else if (te.getType() == Material.CHEST) {
                    Chest chest = (Chest) te;
                    String name = chest.getBlockInventory().getName();
                    ItemStack[] items = new ItemStack[chest.getBlockInventory()
                            .getStorageContents().length];
                    for (int i = 0; i < items.length; i++) {
                        ItemStack is = chest.getBlockInventory().getItem(i);
                        if (is == null) {
                            items[i] = new ItemStack(Material.AIR);
                        } else {
                            items[i] = is;
                        }
                    }
                    chestMarkers
                            .add(new ChestMarker(new Vector3D(chest.getX(), chest.getY(), chest.getZ()), name,
                                    items));
                }
            }
        }
    }

    map.setMarkers(markers);
    map.setChestMarkers(chestMarkers);
}
 
Example 6
Source File: BukkitBlockConnectionProvider.java    From ViaVersion with MIT License 4 votes vote down vote up
public Chunk getChunk(World world, int x, int z) {
    if (lastChunk != null && lastChunk.getX() == x && lastChunk.getZ() == z) {
        return lastChunk;
    }
    return lastChunk = world.getChunkAt(x, z);
}
 
Example 7
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 8
Source File: TradeGoodPostGenTask.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void run() {
	World world = Bukkit.getWorld("world");
	BlockCoord bcoord2 = new BlockCoord();

	for(int i = 0; i < amount; i++) {
		TradeGoodPick pick = picksQueue.poll();
		if (pick == null) {
			return;
		}
		
		ChunkCoord coord = pick.chunkCoord;
		Chunk chunk = world.getChunkAt(coord.getX(), coord.getZ());
		
		int centerX = (chunk.getX() << 4) + 8;
		int centerZ = (chunk.getZ() << 4) + 8;
		int centerY = world.getHighestBlockYAt(centerX, centerZ);
		
		
		
		bcoord2.setWorldname("world");
		bcoord2.setX(centerX);
		bcoord2.setY(centerY - 1);
		bcoord2.setZ(centerZ);
		
		/* try to detect already existing trade goods. */
		while(true) {
			Block top = world.getBlockAt(bcoord2.getX(), bcoord2.getY(), bcoord2.getZ());
			
			if (!top.getChunk().isLoaded()) {
				top.getChunk().load();
			}
			
			if (ItemManager.getId(top) == CivData.BEDROCK) {
				ItemManager.setTypeId(top, CivData.AIR);
    			ItemManager.setData(top, 0, true);
    			bcoord2.setY(bcoord2.getY() - 1);
    			
    			top = top.getRelative(BlockFace.NORTH);
    			if (ItemManager.getId(top) == CivData.WALL_SIGN) {
    				ItemManager.setTypeId(top, CivData.AIR);
	    			ItemManager.setData(top, 0, true);	    			
	    		}
    			
    			top = top.getRelative(BlockFace.SOUTH);
    			if (ItemManager.getId(top) == CivData.WALL_SIGN) {
    				ItemManager.setTypeId(top, CivData.AIR);
	    			ItemManager.setData(top, 0, true);	    			
	    		}
    			
    			top = top.getRelative(BlockFace.EAST);
    			if (ItemManager.getId(top) == CivData.WALL_SIGN) {
    				ItemManager.setTypeId(top, CivData.AIR);
	    			ItemManager.setData(top, 0, true);	    			
	    		}
    			
    			top = top.getRelative(BlockFace.WEST);
    			if (ItemManager.getId(top) == CivData.WALL_SIGN) {
    				ItemManager.setTypeId(top, CivData.AIR);
	    			ItemManager.setData(top, 0, true);
	    		}
			} else {
				break;
			}
			
		}
		
		centerY = world.getHighestBlockYAt(centerX, centerZ);
		
		// Determine if we should be a water good.
		ConfigTradeGood good;
		if (ItemManager.getBlockTypeIdAt(world, centerX, centerY-1, centerZ) == CivData.WATER || 
			ItemManager.getBlockTypeIdAt(world, centerX, centerY-1, centerZ) == CivData.WATER_RUNNING) {
			good = pick.waterPick;
		}  else {
			good = pick.landPick;
		}
		
		// Randomly choose a land or water good.
		if (good == null) {
			System.out.println("Could not find suitable good type during populate! aborting.");
			continue;
		}
		
		// Create a copy and save it in the global hash table.
		BlockCoord bcoord = new BlockCoord(world.getName(), centerX, centerY, centerZ);
		TradeGoodPopulator.buildTradeGoodie(good, bcoord, world, true);
		
	}
}
 
Example 9
Source File: Utils.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
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;
}