Created
October 14, 2023 10:19
-
-
Save ustc-zzzz/4d56ccafca2ac94ece682304725c5210 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SubscribeEvent | |
public static void register(RegisterCommandsEvent event) { | |
event.getDispatcher().register(literal("world-blocks") | |
.then(argument("dimension", DimensionArgument.dimension()) | |
.then(argument("limit", LongArgumentType.longArg(0L)).executes(stack -> { | |
var level = DimensionArgument.getDimension(stack, "dimension"); | |
var limit = LongArgumentType.getLong(stack, "limit"); | |
var border = level.getWorldBorder(); | |
var minChunkX = Mth.floor(border.getMinX() / 16.0); | |
var minChunkZ = Mth.floor(border.getMinZ() / 16.0); | |
var maxChunkX = Mth.ceil(border.getMaxX() / 16.0) - 1; | |
var maxChunkZ = Mth.ceil(border.getMaxX() / 16.0) - 1; | |
var size = (long) (maxChunkZ - minChunkX + 1) * (long) (maxChunkZ - minChunkZ + 1); | |
if (size > limit) { | |
stack.getSource().sendFailure(Component.literal("Limit exceeded: " + size + " > " + limit)); | |
return Command.SINGLE_SUCCESS; | |
} | |
var grouped = ChunkPos | |
.rangeClosed(new ChunkPos(minChunkX, minChunkZ), new ChunkPos(maxChunkX, maxChunkZ)) | |
.collect(Collectors.groupingBy(p -> IntIntPair.of(p.getRegionX(), p.getRegionZ()))); | |
var index = new long[1]; | |
var count = new long[1]; | |
var oldNoSave = level.noSave; | |
var future = CompletableFuture.runAsync(() -> level.noSave = true, level.getServer()); | |
for (var posListList : Iterables.partition(grouped.values(), 50)) { | |
future = future.thenCompose(v -> level.getServer().submitAsync(() -> { | |
for (var posList : posListList) { | |
for (var chunkPos : posList) { | |
var chunkCount = 0L; | |
var sections = level.getChunk(chunkPos.x, chunkPos.z).getSections(); | |
for (var section : sections) { | |
chunkCount += section.hasOnlyAir() ? 0 : IntStream | |
.range(0, 4096).filter(i -> !section.getBlockState( | |
i >> 8, i >> 4 & 15, i & 15).isAir()).count(); | |
} | |
var msg = Component.literal("[" + ++index[0] + " / " + size + "] " + | |
"Chunk " + chunkPos + " has " + chunkCount + " non-air block(s)"); | |
stack.getSource().sendSystemMessage(msg); | |
count[0] += chunkCount; | |
} | |
} | |
})); | |
} | |
future.thenRun(() -> { | |
level.noSave = oldNoSave; | |
var msg = Component.literal("Totally " + count[0] + " non-air block(s)"); | |
stack.getSource().sendSystemMessage(msg.withStyle(ChatFormatting.DARK_GREEN)); | |
}); | |
return Command.SINGLE_SUCCESS; | |
})))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment