Search in sources :

Example 1 with ForcedChunksSavedData

use of net.minecraft.world.level.ForcedChunksSavedData in project MinecraftForge by MinecraftForge.

the class ForgeChunkManager method forceChunk.

/**
 * Forces a chunk to be loaded for the given mod with the given "owner".
 *
 * @param add {@code true} to force the chunk, {@code false} to unforce the chunk.
 *
 * @implNote Based on {@link ServerLevel#setChunkForced(int, int, boolean)}
 */
private static <T extends Comparable<? super T>> boolean forceChunk(ServerLevel world, String modId, T owner, int chunkX, int chunkZ, boolean add, boolean ticking, TicketType<TicketOwner<T>> type, Function<ForcedChunksSavedData, TicketTracker<T>> ticketGetter) {
    if (!ModList.get().isLoaded(modId)) {
        LOGGER.warn("A mod attempted to force a chunk for an unloaded mod of id: {}", modId);
        return false;
    }
    ForcedChunksSavedData saveData = world.getDataStorage().computeIfAbsent(ForcedChunksSavedData::load, ForcedChunksSavedData::new, "chunks");
    ChunkPos pos = new ChunkPos(chunkX, chunkZ);
    long chunk = pos.toLong();
    TicketTracker<T> tickets = ticketGetter.apply(saveData);
    TicketOwner<T> ticketOwner = new TicketOwner<>(modId, owner);
    boolean success;
    if (add) {
        success = tickets.add(ticketOwner, chunk, ticking);
        if (success)
            world.getChunk(chunkX, chunkZ);
    } else {
        success = tickets.remove(ticketOwner, chunk, ticking);
    }
    if (success) {
        saveData.setDirty(true);
        forceChunk(world, pos, type, ticketOwner, add, ticking);
    }
    return success;
}
Also used : ForcedChunksSavedData(net.minecraft.world.level.ForcedChunksSavedData) ChunkPos(net.minecraft.world.level.ChunkPos)

Example 2 with ForcedChunksSavedData

use of net.minecraft.world.level.ForcedChunksSavedData in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method updateForcedChunks.

private void updateForcedChunks(final ServerLevel world, final ServerChunkCache serverChunkProvider) {
    final ForcedChunksSavedData forcedChunksSaveData = world.getDataStorage().get(ForcedChunksSavedData::new, "chunks");
    if (forcedChunksSaveData != null) {
        final LongIterator longIterator = forcedChunksSaveData.getChunks().iterator();
        while (longIterator.hasNext()) {
            final long i = longIterator.nextLong();
            final ChunkPos forceChunkPos = new ChunkPos(i);
            serverChunkProvider.updateChunkForced(forceChunkPos, true);
        }
    }
}
Also used : ForcedChunksSavedData(net.minecraft.world.level.ForcedChunksSavedData) ChunkPos(net.minecraft.world.level.ChunkPos) LongIterator(it.unimi.dsi.fastutil.longs.LongIterator)

Aggregations

ChunkPos (net.minecraft.world.level.ChunkPos)2 ForcedChunksSavedData (net.minecraft.world.level.ForcedChunksSavedData)2 LongIterator (it.unimi.dsi.fastutil.longs.LongIterator)1