下面列出了org.bukkit.Chunk#getTileEntities ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void handleChunkLoadSync(Chunk loadedChunk, boolean onServerStart) {
PipeManager pipeManager = (PipeManager) (DuctManager<? extends Duct>) ductRegister.baseDuctTypeOf("pipe").getDuctManager();
if (loadedChunk.getTileEntities() != null) {
for (BlockState bs : loadedChunk.getTileEntities()) {
if (WorldUtils.isContainerBlock(bs.getType())) {
//automatically ignores this block if it is already registered as container block
updateContainerBlock(bs.getBlock(), true, !onServerStart);
//if this block is already registered, update the block, because the blockState object changes after a chunk unload and load
TransportPipesContainer container = pipeManager.getContainerAtLoc(bs.getLocation());
if (container instanceof BlockContainer) {
((BlockContainer) container).updateBlock();
}
}
}
}
}
/**
* Calculates the spawner worth of a chunk.
*
* @param chunk the chunk.
* @param spawners the spawner totals to add to.
* @return the chunk worth in spawners.
*/
private double getSpawnerWorth(Chunk chunk, Map<EntityType, Integer> spawners) {
int count;
double worth = 0;
for (BlockState blockState : chunk.getTileEntities()) {
if (!(blockState instanceof CreatureSpawner)) {
continue;
}
CreatureSpawner spawner = (CreatureSpawner) blockState;
EntityType spawnType = spawner.getSpawnedType();
int stackSize = plugin.getSpawnerStackerHook().getStackSize(spawner);
double blockPrice = plugin.getSettings().getSpawnerPrice(spawnType) * stackSize;
worth += blockPrice;
if (blockPrice != 0) {
count = spawners.getOrDefault(spawnType, 0);
spawners.put(spawnType, count + stackSize);
}
}
return worth;
}
private void checkChunk(ChunkPosition pos, @Nullable Chunk chunk) {
if(repairedChunks.add(pos)) {
if(chunk == null) {
chunk = pos.getChunk(match.getWorld());
}
for(BlockState state : chunk.getTileEntities()) {
if(state instanceof Skull) {
if(!NMSHacks.isSkullCached((Skull) state)) {
Location loc = state.getLocation();
broadcastDeveloperWarning("Uncached skull \"" + ((Skull) state).getOwner() + "\" at " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
}
}
}
// Replace formerly invisible half-iron-door blocks with barriers
for(Block ironDoor : chunk.getBlocks(Material.IRON_DOOR_BLOCK)) {
BlockFace half = (ironDoor.getData() & 8) == 0 ? BlockFace.DOWN : BlockFace.UP;
if(ironDoor.getRelative(half.getOppositeFace()).getType() != Material.IRON_DOOR_BLOCK) {
ironDoor.setType(Material.BARRIER, false);
}
}
// Remove all block 36 and remember the ones at y=0 so VoidFilter can check them
for(Block block36 : chunk.getBlocks(Material.PISTON_MOVING_PIECE)) {
if(block36.getY() == 0) {
block36Locations.add(block36.getX(), block36.getY(), block36.getZ());
}
block36.setType(Material.AIR, false);
}
}
}
void load(Chunk chunk) {
if(chunks.add(ChunkLocation.of(chunk))) {
for(BlockState blockState : chunk.getTileEntities()) {
if(blockState instanceof Sign) {
final SignHandle sign = createSign((Sign) blockState);
if(sign != null) {
signs.put(blockState.getLocation(), sign);
}
}
}
logger.fine(() -> "Loaded chunk " + chunk.getX() + "," + chunk.getZ() +" with " + signs.size());
}
}
public static WorldData fromWorld(World world) {
String worldName = world.getName();
int tileEntities = 0;
for (Chunk loadedChunk : world.getLoadedChunks()) {
tileEntities += loadedChunk.getTileEntities().length;
}
int entities = world.getEntities().size();
int chunks = world.getLoadedChunks().length;
return new WorldData(worldName, chunks, tileEntities, entities);
}
private void displayWorldInfo(CommandSender sender) {
int entities = 0;
int chunks = 0;
int livingEntities = 0;
int tileEntities = 0;
long usedWorldSize = 0;
List<World> worlds = Bukkit.getWorlds();
for (World world : worlds) {
for (Chunk loadedChunk : world.getLoadedChunks()) {
tileEntities += loadedChunk.getTileEntities().length;
}
livingEntities += world.getLivingEntities().size();
entities += world.getEntities().size();
chunks += world.getLoadedChunks().length;
File worldFolder = Bukkit.getWorld(world.getUID()).getWorldFolder();
usedWorldSize += LagUtils.getFolderSize(plugin.getLogger(), worldFolder.toPath());
}
sendMessage(sender, "Entities", String.format("%d/%d", livingEntities, entities));
sendMessage(sender, "Tile Entities", String.valueOf(tileEntities));
sendMessage(sender, "Loaded Chunks", String.valueOf(chunks));
sendMessage(sender, "Worlds", String.valueOf(worlds.size()));
sendMessage(sender, "World Size", readableBytes(usedWorldSize));
}
public static WorldData fromWorld(World world) {
String worldName = world.getName();
int tileEntities = 0;
for (Chunk loadedChunk : world.getLoadedChunks()) {
tileEntities += loadedChunk.getTileEntities().length;
}
int entities = world.getEntities().size();
int chunks = world.getLoadedChunks().length;
return new WorldData(worldName, chunks, tileEntities, entities);
}
private void displayWorldInfo(CommandSender sender) {
int entities = 0;
int chunks = 0;
int livingEntities = 0;
int tileEntities = 0;
long usedWorldSize = 0;
List<World> worlds = Bukkit.getWorlds();
for (World world : worlds) {
for (Chunk loadedChunk : world.getLoadedChunks()) {
tileEntities += loadedChunk.getTileEntities().length;
}
livingEntities += world.getLivingEntities().size();
entities += world.getEntities().size();
chunks += world.getLoadedChunks().length;
File worldFolder = Bukkit.getWorld(world.getUID()).getWorldFolder();
usedWorldSize += LagUtils.getFolderSize(plugin.getLogger(), worldFolder.toPath());
}
sendMessage(sender, "Entities", String.format("%d/%d", livingEntities, entities));
sendMessage(sender, "Tile Entities", String.valueOf(tileEntities));
sendMessage(sender, "Loaded Chunks", String.valueOf(chunks));
sendMessage(sender, "Worlds", String.valueOf(worlds.size()));
sendMessage(sender, "World Size", readableBytes(usedWorldSize));
}
/**
* 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);
}
/**
* Calculates the chest worth of a chunk.
*
* @param chunk the chunk.
* @param materials the material totals to add to.
* @param spawners the spawner totals to add to.
* @return the chunk worth in materials.
*/
private double getChestWorth(Chunk chunk, Map<Material, Integer> materials, Map<EntityType, Integer> spawners) {
int count;
double worth = 0;
double materialPrice;
EntityType spawnerType;
for (BlockState blockState : chunk.getTileEntities()) {
if (!(blockState instanceof Chest)) {
continue;
}
Chest chest = (Chest) blockState;
for (ItemStack item : chest.getBlockInventory()) {
if (item == null) continue;
int stackSize = item.getAmount();
switch (item.getType()) {
case MOB_SPAWNER:
stackSize *= plugin.getSpawnerStackerHook().getStackSize(item);
spawnerType = plugin.getSpawnerStackerHook().getSpawnedType(item);
double price = plugin.getSettings().getSpawnerPrice(spawnerType);
materialPrice = price * stackSize;
break;
default:
materialPrice = plugin.getSettings().getBlockPrice(item.getType()) * stackSize;
spawnerType = null;
break;
}
worth += materialPrice;
if (materialPrice != 0) {
if (spawnerType == null) {
count = materials.getOrDefault(item.getType(), 0);
materials.put(item.getType(), count + stackSize);
} else {
count = spawners.getOrDefault(spawnerType, 0);
spawners.put(spawnerType, count + stackSize);
}
}
}
}
return worth;
}