use of dev.frankheijden.insights.api.exceptions.ChunkReflectionException in project Insights by InsightsPlugin.
the class ChunkContainer method get.
@Override
public DistributionStorage get() {
ChunkVector min = cuboid.getMin();
ChunkVector max = cuboid.getMax();
int minX = min.getX();
int maxX = max.getX();
int minZ = min.getZ();
int maxZ = max.getZ();
int blockMinY = Math.max(min.getY(), 0);
int blockMaxY = Math.abs(Math.min(min.getY(), 0)) + max.getY();
ServerLevel serverLevel = ((CraftWorld) world).getHandle();
if (options.materials()) {
LevelChunkSection[] chunkSections;
try {
chunkSections = getChunkSections();
} catch (IOException ex) {
throw new ChunkIOException(ex);
}
int minSectionY = blockMinY >> 4;
int maxSectionY = blockMaxY >> 4;
for (int sectionY = minSectionY; sectionY <= maxSectionY; sectionY++) {
int minY = sectionY == minSectionY ? blockMinY & 15 : 0;
int maxY = sectionY == maxSectionY ? blockMaxY & 15 : 15;
LevelChunkSection section = chunkSections[sectionY];
if (section == null) {
// Section is empty, count everything as air
long count = (maxX - minX + 1L) * (maxY - minY + 1L) * (maxZ - minZ + 1L);
materialMap.merge(Material.AIR, count, Long::sum);
} else if (minX == 0 && maxX == 15 && minY == 0 && maxY == 15 && minZ == 0 && maxZ == 15) {
// Section can be counted as a whole
section.getStates().count((state, count) -> {
try {
materialMap.merge(CraftMagicNumbers.getMaterial(state.getBlock()), (long) count, Long::sum);
} catch (Throwable th) {
th.printStackTrace();
}
});
} else {
// Section must be scanned block by block
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
materialMap.merge(CraftMagicNumbers.getMaterial(section.getBlockState(x, y, z).getBlock()), 1L, Long::sum);
}
}
}
}
}
}
if (options.entities()) {
PersistentEntitySectionManager<Entity> entityManager = serverLevel.entityManager;
long chunkKey = getChunkKey();
final Stream<Entity> entityStream;
if (entityManager.areEntitiesLoaded(chunkKey)) {
EntitySectionStorage<Entity> sectionStorage;
try {
sectionStorage = RPersistentEntitySectionManager.getSectionStorage(entityManager);
} catch (Throwable th) {
throw new ChunkReflectionException(th);
}
entityStream = sectionStorage.getExistingSectionsInChunk(chunkKey).flatMap(EntitySection::getEntities);
} else {
EntityPersistentStorage<Entity> permanentStorage;
try {
permanentStorage = RPersistentEntitySectionManager.getPermanentStorage(entityManager);
} catch (Throwable th) {
throw new ChunkReflectionException(th);
}
entityStream = permanentStorage.loadEntities(new ChunkPos(chunkX, chunkZ)).join().getEntities();
}
entityStream.filter(entity -> {
int x = entity.getBlockX() & 15;
int y = entity.getBlockY();
int z = entity.getBlockZ() & 15;
return minX <= x && x <= maxX && blockMinY <= y && y <= blockMaxY && minZ <= z && z <= maxZ;
}).forEach(entity -> entityMap.merge(entity.getBukkitEntity().getType(), 1L, Long::sum));
}
return DistributionStorage.of(materialMap, entityMap);
}
Aggregations