Skip to content

Commit

Permalink
Implement world chunkPositions and offlineChunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed Oct 13, 2023
1 parent 928ecde commit 7ab397a
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.world.level.chunk;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.storage.RegionFile;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.world.chunk.OfflineChunk;
import org.spongepowered.common.data.persistence.NBTTranslator;
import org.spongepowered.math.vector.Vector3i;

import java.io.DataInputStream;
import java.io.IOException;

public final class SpongeOfflineChunk implements OfflineChunk {

private final CompoundTag nbt;
private final Vector3i chunkPos;

public SpongeOfflineChunk(final CompoundTag nbt, final int cx, final int cz) {
this.nbt = nbt;
this.chunkPos = new Vector3i(cx, 0, cz);
}

@Nullable
public static OfflineChunk of(final RegionFile regionFile, final ChunkPos pos) {
CompoundTag chunkNbt;
try (DataInputStream $$2 = regionFile.getChunkDataInputStream(pos)) {
if ($$2 == null) {
return null;
}

chunkNbt = NbtIo.read($$2);
} catch (IOException e) {
throw new RuntimeException(e);
}

return new SpongeOfflineChunk(chunkNbt, pos.x, pos.z);
}

@Override
public org.spongepowered.math.vector.Vector3i chunkPosition() {
return this.chunkPos;
}

@Override
public int contentVersion() {
return 2; // TODO get from actual data?, assuming compression type is 2
}

@Override
public DataContainer toContainer() {
return NBTTranslator.INSTANCE.translate(this.nbt);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.raid.Raid;
import net.minecraft.world.entity.raid.Raids;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.CollisionGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.storage.RegionFile;
import net.minecraft.world.level.entity.PersistentEntitySectionManager;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.storage.LevelResource;
Expand All @@ -56,6 +58,7 @@
import org.spongepowered.api.world.BlockChangeFlag;
import org.spongepowered.api.world.SerializationBehavior;
import org.spongepowered.api.world.border.WorldBorder;
import org.spongepowered.api.world.chunk.OfflineChunk;
import org.spongepowered.api.world.chunk.WorldChunk;
import org.spongepowered.api.world.generation.ChunkGenerator;
import org.spongepowered.api.world.server.ChunkManager;
Expand All @@ -66,26 +69,35 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.accessor.world.entity.raid.RaidsAccessor;
import org.spongepowered.common.bridge.server.level.ServerLevelBridge;
import org.spongepowered.common.bridge.world.level.border.WorldBorderBridge;
import org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge;
import org.spongepowered.common.data.holder.SpongeServerLocationBaseDataHolder;
import org.spongepowered.common.mixin.api.minecraft.world.level.LevelMixin_API;
import org.spongepowered.common.util.VecHelper;
import org.spongepowered.common.world.level.chunk.SpongeOfflineChunk;
import org.spongepowered.common.world.storage.SpongeChunkLayout;
import org.spongepowered.math.vector.Vector3d;
import org.spongepowered.math.vector.Vector3i;
import org.spongepowered.math.vector.Vector4i;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -316,4 +328,68 @@ public ChunkManager chunkManager() {
return (ChunkManager) this.shadow$getChunkSource().chunkMap;
}

public <T> Stream<T> api$chunkPosStream(BiFunction<RegionFile, Stream<ChunkPos>, Stream<T>> mapper) {
final Path dimensionPath = ((ServerLevelBridge) this).bridge$getLevelSave().getDimensionPath(this.shadow$dimension());
final Path regionPath = dimensionPath.resolve("region");

return Stream
.generate(() -> {
try {
// open directory stream over all region files of this world
return Stream.of(Files.newDirectoryStream(regionPath, "*.mca"));
} catch (IOException ex) {
SpongeCommon.logger().error("Could not find region files", ex);
return Stream.<DirectoryStream<Path>>empty();
}
})
.limit(1)
.flatMap(Function.identity())
.flatMap(stream -> StreamSupport.stream(stream.spliterator(), false)
.flatMap(path -> {
try { // For every region file
RegionFile regionFile = new RegionFile(path, regionPath, true);
final Vector4i regionBound = this.api$pathToRegionPos(path);
// Find all chunks in bounds
final Stream<ChunkPos> chunkPosStream = IntStream.range(regionBound.x(), regionBound.z())
.mapToObj(x -> IntStream.range(regionBound.y(), regionBound.w()).
mapToObj(z -> new ChunkPos(x, z)))
.flatMap(Function.identity());
return mapper.apply(regionFile, chunkPosStream).onClose(() -> this.api$close(regionFile));
} catch (IOException ignored) {
return Stream.empty();
}
})
.onClose(() -> this.api$close(stream)));
}

@Override
public Stream<Vector3i> chunkPositions() {
return this.api$chunkPosStream((regionFile, stream) ->
stream.filter(regionFile::doesChunkExist) // filter out non-existent chunks
.map(cp -> new Vector3i(cp.x, 0, cp.z)) // map to API type
);
}

@Override
public Stream<OfflineChunk> offlineChunks() {
return this.api$chunkPosStream((regionFile, stream) ->
stream.map(cp -> SpongeOfflineChunk.of(regionFile, cp)) // map to API type
.filter(Objects::nonNull)); // filter out non-existent chunks
}

private void api$close(final AutoCloseable closeable) {
try {
closeable.close();
} catch (Exception ignored) {
}
}

private Vector4i api$pathToRegionPos(Path regionPath) {
final String[] split = regionPath.getFileName().toString().split("\\.");
final int rx = Integer.parseInt(split[1]);
final int ry = Integer.parseInt(split[2]);
final ChunkPos min = ChunkPos.minFromRegion(rx, ry);
final ChunkPos max = ChunkPos.maxFromRegion(rx, ry);
return new Vector4i(min.x, min.z, max.x, max.z);
}
}

0 comments on commit 7ab397a

Please sign in to comment.