added is_chunk_generated and docs on reset_chunk
This commit is contained in:
1 parent
9806af6ff9
commit
5d94e4a1ad
6 files changed
+431
-348
No files matched your search
@@ -134,6 +134,7 @@ import java.util.function.BiFunction;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static carpet.script.utils.WorldTools.canHasChunk;
|
||||
import static java.lang.Math.abs;
|
||||
import static java.lang.Math.max;
|
||||
import static java.lang.Math.min;
|
||||
@@ -678,6 +679,17 @@ public class CarpetExpression
|
||||
return (c_, t_) -> retval;
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("is_chunk_generated", -1, (c, t, lv) ->
|
||||
{
|
||||
BlockValue.LocatorResult locator = BlockValue.fromParams((CarpetContext)c, lv, 0);
|
||||
BlockPos pos = locator.block.getPos();
|
||||
boolean force = false;
|
||||
if (lv.size() > locator.offset)
|
||||
force = lv.get(locator.offset).evalValue(c, Context.BOOLEAN).getBoolean();
|
||||
Value retval = new NumericValue(canHasChunk(((CarpetContext)c).s.getWorld(), new ChunkPos(pos), null, force));
|
||||
return (c_, t_) -> retval;
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("generation_status", -1, (c, t, lv) ->
|
||||
{
|
||||
BlockValue.LocatorResult locatorResult = BlockValue.fromParams((CarpetContext)c, lv, 0);
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package carpet.script.utils;
|
||||
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.util.math.ChunkPos;
|
||||
import net.minecraft.world.chunk.ChunkStatus;
|
||||
import net.minecraft.world.storage.RegionFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
public class WorldTools
|
||||
{
|
||||
|
||||
public static boolean canHasChunk(ServerWorld 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 = world.getDimension().getType().getSaveDirectory(
|
||||
world.getSaveHandler().getWorldDir()
|
||||
).toPath().resolve("region");
|
||||
Path regionFilePath = regionPath.resolve(currentRegionName);
|
||||
File regionFile = regionFilePath.toFile();
|
||||
if (!regionFile.exists())
|
||||
{
|
||||
if (regionCache != null) regionCache.put(currentRegionName, null);
|
||||
return false;
|
||||
}
|
||||
if (!deepcheck) return true; // not using cache in this case.
|
||||
try
|
||||
{
|
||||
RegionFile region = new RegionFile(regionFile, regionPath.toFile());
|
||||
if (regionCache != null) regionCache.put(currentRegionName, region);
|
||||
return region.hasChunk(chpos);
|
||||
}
|
||||
catch (IOException ignored) { }
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user