use of com.sk89q.worldedit.math.BlockVector2 in project FastAsyncWorldEdit by IntellectualSites.
the class ForgeWorld method regenerate.
@Override
public boolean regenerate(Region region, EditSession editSession) {
// Don't even try to regen if it's going to fail.
AbstractChunkProvider provider = getWorld().getChunkProvider();
if (!(provider instanceof ServerChunkProvider)) {
return false;
}
File saveFolder = Files.createTempDir();
// register this just in case something goes wrong
// normally it should be deleted at the end of this method
saveFolder.deleteOnExit();
try {
ServerWorld originalWorld = (ServerWorld) getWorld();
MinecraftServer server = originalWorld.getServer();
SaveHandler saveHandler = new SaveHandler(saveFolder, originalWorld.getSaveHandler().getWorldDirectory().getName(), server, server.getDataFixer());
try (World freshWorld = new ServerWorld(server, server.getBackgroundExecutor(), saveHandler, originalWorld.getWorldInfo(), originalWorld.dimension.getType(), originalWorld.getProfiler(), new NoOpChunkStatusListener())) {
// Pre-gen all the chunks
// We need to also pull one more chunk in every direction
CuboidRegion expandedPreGen = new CuboidRegion(region.getMinimumPoint().subtract(16, 0, 16), region.getMaximumPoint().add(16, 0, 16));
for (BlockVector2 chunk : expandedPreGen.getChunks()) {
freshWorld.getChunk(chunk.getBlockX(), chunk.getBlockZ());
}
ForgeWorld from = new ForgeWorld(freshWorld);
for (BlockVector3 vec : region) {
editSession.setBlock(vec, from.getFullBlock(vec));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (MaxChangedBlocksException e) {
throw new RuntimeException(e);
} finally {
saveFolder.delete();
}
return true;
}
use of com.sk89q.worldedit.math.BlockVector2 in project FastAsyncWorldEdit by IntellectualSites.
the class PaperweightAdapter method submitChunkLoadTasks.
@SuppressWarnings("unchecked")
private List<CompletableFuture<ChunkAccess>> submitChunkLoadTasks(Region region, ServerLevel serverWorld) {
ServerChunkCache chunkManager = serverWorld.getChunkSource();
List<CompletableFuture<ChunkAccess>> chunkLoadings = new ArrayList<>();
// Pre-gen all the chunks
for (BlockVector2 chunk : region.getChunks()) {
try {
chunkLoadings.add(((CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>>) getChunkFutureMainThreadMethod.invoke(chunkManager, chunk.getX(), chunk.getZ(), ChunkStatus.FEATURES, true)).thenApply(either -> either.left().orElse(null)));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException("Couldn't load chunk for regen.", e);
}
}
return chunkLoadings;
}
use of com.sk89q.worldedit.math.BlockVector2 in project FastAsyncWorldEdit by IntellectualSites.
the class ChunkDeleter method groupChunks.
private Map<Path, Stream<BlockVector2>> groupChunks(ChunkDeletionInfo.ChunkBatch chunkBatch) {
Path worldPath = Paths.get(chunkBatch.worldPath);
if (chunkBatch.chunks != null) {
return chunkBatch.chunks.stream().collect(Collectors.groupingBy(RegionFilePos::new)).entrySet().stream().collect(Collectors.toMap(e -> worldPath.resolve("region").resolve(e.getKey().getFileName()), e -> e.getValue().stream().sorted(chunkSorter)));
} else {
final BlockVector2 minChunk = chunkBatch.minChunk;
final BlockVector2 maxChunk = chunkBatch.maxChunk;
final RegionFilePos minRegion = new RegionFilePos(minChunk);
final RegionFilePos maxRegion = new RegionFilePos(maxChunk);
Map<Path, Stream<BlockVector2>> groupedChunks = new HashMap<>();
for (int regX = minRegion.getX(); regX <= maxRegion.getX(); regX++) {
for (int regZ = minRegion.getZ(); regZ <= maxRegion.getZ(); regZ++) {
final Path regionPath = worldPath.resolve("region").resolve(new RegionFilePos(regX, regZ).getFileName());
if (!Files.exists(regionPath)) {
continue;
}
int startX = regX << 5;
int endX = (regX << 5) + 31;
int startZ = regZ << 5;
int endZ = (regZ << 5) + 31;
int minX = Math.max(Math.min(startX, endX), minChunk.getBlockX());
int minZ = Math.max(Math.min(startZ, endZ), minChunk.getBlockZ());
int maxX = Math.min(Math.max(startX, endX), maxChunk.getBlockX());
int maxZ = Math.min(Math.max(startZ, endZ), maxChunk.getBlockZ());
Stream<BlockVector2> stream = Stream.iterate(BlockVector2.at(minX, minZ), bv2 -> {
int nextX = bv2.getBlockX();
int nextZ = bv2.getBlockZ();
if (++nextX > maxX) {
nextX = minX;
if (++nextZ > maxZ) {
return null;
}
}
return BlockVector2.at(nextX, nextZ);
});
groupedChunks.put(regionPath, stream);
}
}
return groupedChunks;
}
}
use of com.sk89q.worldedit.math.BlockVector2 in project FastAsyncWorldEdit by IntellectualSites.
the class Polygons method polygonizeCylinder.
/**
* Calculates the polygon shape of a cylinder which can then be used for e.g. intersection detection.
*
* @param center the center point of the cylinder
* @param radius the radius of the cylinder
* @param maxPoints max points to be used for the calculation
* @return a list of {@link BlockVector2} which resemble the shape as a polygon
*/
public static List<BlockVector2> polygonizeCylinder(BlockVector2 center, Vector2 radius, int maxPoints) {
int nPoints = (int) Math.ceil(Math.PI * radius.length());
// These strange semantics for maxPoints are copied from the selectSecondary method.
if (maxPoints >= 0 && nPoints >= maxPoints) {
nPoints = maxPoints - 1;
}
final List<BlockVector2> points = new ArrayList<>(nPoints);
for (int i = 0; i < nPoints; ++i) {
double angle = i * (2.0 * Math.PI) / nPoints;
final Vector2 pos = Vector2.at(Math.cos(angle), Math.sin(angle));
final BlockVector2 blockVector2D = pos.multiply(radius).toBlockPoint().add(center);
points.add(blockVector2D);
}
return points;
}
use of com.sk89q.worldedit.math.BlockVector2 in project FastAsyncWorldEdit by IntellectualSites.
the class FolderSnapshot method getChunkTag.
@Override
public CompoundTag getChunkTag(BlockVector3 position) throws DataException, IOException {
BlockVector2 pos = position.toBlockVector2();
Optional<Path> regFolder = getRegionFolder();
if (!regFolder.isPresent()) {
Path chunkFile = getFolder().resolve(LegacyChunkStore.getFilename(pos, "/"));
if (!Files.exists(chunkFile)) {
throw new MissingChunkException();
}
return ChunkStoreHelper.readCompoundTag(() -> new GZIPInputStream(Files.newInputStream(chunkFile)));
}
Path regionFile = regFolder.get().resolve(McRegionChunkStore.getFilename(pos));
if (!Files.exists(regionFile)) {
// Try mcr as well
regionFile = regionFile.resolveSibling(regionFile.getFileName().toString().replace(".mca", ".mcr"));
if (!Files.exists(regionFile)) {
throw new MissingChunkException();
}
}
try (InputStream stream = Files.newInputStream(regionFile)) {
McRegionReader regionReader = new McRegionReader(stream);
return ChunkStoreHelper.readCompoundTag(() -> regionReader.getChunkInputStream(pos));
}
}
Aggregations