use of net.minecraft.world.level.chunk.storage.RegionFile in project fabric-carpet by gnembon.
the class WorldTools method canHasChunk.
public static boolean canHasChunk(ServerLevel world, ChunkPos chpos, Map<String, RegionFile> regionCache, boolean deepcheck) {
if (world.getChunk(chpos.x, chpos.z, ChunkStatus.STRUCTURE_STARTS, false) != null)
return true;
String currentRegionName = "r." + chpos.getRegionX() + "." + chpos.getRegionZ() + ".mca";
if (regionCache != null && regionCache.containsKey(currentRegionName)) {
RegionFile region = regionCache.get(currentRegionName);
if (region == null)
return false;
return region.hasChunk(chpos);
}
Path regionPath = new File(((MinecraftServerInterface) world.getServer()).getCMSession().getDimensionPath(world.dimension()).toFile(), "region").toPath();
Path regionFilePath = regionPath.resolve(currentRegionName);
File regionFile = regionFilePath.toFile();
if (!regionFile.exists()) {
if (regionCache != null)
regionCache.put(currentRegionName, null);
return false;
}
// not using cache in this case.
if (!deepcheck)
return true;
try {
RegionFile region = new RegionFile(regionFile.toPath(), regionPath, true);
if (regionCache != null)
regionCache.put(currentRegionName, region);
return region.hasChunk(chpos);
} catch (IOException ignored) {
}
return true;
}
use of net.minecraft.world.level.chunk.storage.RegionFile in project fabric-carpet by gnembon.
the class ChunkMap_scarpetChunkCreationMixin method getExistingChunks.
@Unique
private Set<ChunkPos> getExistingChunks(final Set<ChunkPos> requestedChunks) {
final Map<String, RegionFile> regionCache = new HashMap<>();
final Set<ChunkPos> ret = new HashSet<>();
for (final ChunkPos pos : requestedChunks) if (WorldTools.canHasChunk(this.level, pos, regionCache, true))
ret.add(pos);
return ret;
}
use of net.minecraft.world.level.chunk.storage.RegionFile in project BCLib by paulevsGitch.
the class DataFixerAPI method fixRegion.
private static void fixRegion(MigrationProfile data, State state, File file) {
try {
Path path = file.toPath();
LOGGER.info("Inspecting " + path);
boolean[] changed = new boolean[1];
RegionFile region = new RegionFile(path, path.getParent(), true);
for (int x = 0; x < 32; x++) {
for (int z = 0; z < 32; z++) {
ChunkPos pos = new ChunkPos(x, z);
changed[0] = false;
if (region.hasChunk(pos) && !state.didFail) {
DataInputStream input = region.getChunkDataInputStream(pos);
CompoundTag root = NbtIo.read(input);
// if ((root.toString().contains("betternether:chest") || root.toString().contains("bclib:chest"))) {
// NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + ".nbt"));
// }
input.close();
// Checking TileEntities
ListTag tileEntities = root.getCompound("Level").getList("TileEntities", Tag.TAG_COMPOUND);
fixItemArrayWithID(tileEntities, changed, data, true);
// Checking Entities
ListTag entities = root.getList("Entities", Tag.TAG_COMPOUND);
fixItemArrayWithID(entities, changed, data, true);
// Checking Block Palette
ListTag sections = root.getCompound("Level").getList("Sections", Tag.TAG_COMPOUND);
sections.forEach((tag) -> {
ListTag palette = ((CompoundTag) tag).getList("Palette", Tag.TAG_COMPOUND);
palette.forEach((blockTag) -> {
CompoundTag blockTagCompound = ((CompoundTag) blockTag);
changed[0] |= data.replaceStringFromIDs(blockTagCompound, "Name");
});
try {
changed[0] |= data.patchBlockState(palette, ((CompoundTag) tag).getList("BlockStates", Tag.TAG_LONG));
} catch (PatchDidiFailException e) {
BCLib.LOGGER.error("Failed fixing BlockState in " + pos);
state.addError("Failed fixing BlockState in " + pos + " (" + e.getMessage() + ")");
state.didFail = true;
changed[0] = false;
e.printStackTrace();
}
});
if (changed[0]) {
LOGGER.warning("Writing '{}': {}/{}", file, x, z);
// NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + "-changed.nbt"));
DataOutputStream output = region.getChunkDataOutputStream(pos);
NbtIo.write(root, output);
output.close();
}
}
}
}
region.close();
} catch (Exception e) {
BCLib.LOGGER.error("Failed fixing Region.");
state.addError("Failed fixing Region in " + file.getName() + " (" + e.getMessage() + ")");
state.didFail = true;
e.printStackTrace();
}
}
Aggregations