Search in sources :

Example 1 with LoadingTicket

use of org.spongepowered.api.world.ChunkTicketManager.LoadingTicket in project LanternServer by LanternPowered.

the class LanternChunkManager method loadTickets.

public void loadTickets() throws IOException {
    final Multimap<String, LanternLoadingTicket> tickets = LanternLoadingTicketIO.load(this.worldFolder, this, this.chunkLoadService);
    final Iterator<Entry<String, LanternLoadingTicket>> it = tickets.entries().iterator();
    final CauseStack causeStack = CauseStack.current();
    while (it.hasNext()) {
        final LanternLoadingTicket ticket = it.next().getValue();
        if (ticket instanceof LanternEntityLoadingTicket) {
            final LanternEntityLoadingTicket ticket0 = (LanternEntityLoadingTicket) ticket;
            final EntityReference ref = ticket0.getEntityReference().orElse(null);
            if (ref != null) {
                causeStack.pushCause(ticket0);
                final LanternChunk chunk = getOrCreateChunk(ref.getChunkCoords(), causeStack, true, true);
                causeStack.popCause();
                final Entity entity = chunk.getEntity(ref.getUniqueId()).orElse(null);
                if (entity != null) {
                    ticket0.bindToEntity(entity);
                } else {
                    // The entity is gone?
                    it.remove();
                }
            } else {
                // The entity is gone?
                it.remove();
            }
        }
    }
    for (Entry<String, Collection<LanternLoadingTicket>> entry : tickets.asMap().entrySet()) {
        final Collection<ChunkTicketManager.Callback> callbacks = this.chunkLoadService.getCallbacks().get(entry.getKey());
        // These maps will be loaded lazily
        ImmutableListMultimap<UUID, LoadingTicket> playerLoadedTickets = null;
        ImmutableList<LoadingTicket> nonPlayerLoadedTickets = null;
        final Set<LoadingTicket> resultPlayerLoadedTickets = entry.getValue().stream().filter(ticket -> ticket instanceof PlayerLoadingTicket).collect(Collectors.toSet());
        final Set<LoadingTicket> resultNonPlayerLoadedTickets = entry.getValue().stream().filter(ticket -> !(ticket instanceof PlayerLoadingTicket)).collect(Collectors.toSet());
        final int maxTickets = this.chunkLoadService.getMaxTicketsById(entry.getKey());
        for (ChunkTicketManager.Callback callback : callbacks) {
            if (callback instanceof ChunkTicketManager.OrderedCallback) {
                if (nonPlayerLoadedTickets == null) {
                    nonPlayerLoadedTickets = ImmutableList.copyOf(resultNonPlayerLoadedTickets);
                    resultNonPlayerLoadedTickets.clear();
                }
                final List<LoadingTicket> result = ((ChunkTicketManager.OrderedCallback) callback).onLoaded(nonPlayerLoadedTickets, this.world, maxTickets);
                checkNotNull(result, "The OrderedCallback#onLoaded method may not return null, " + "error caused by (plugin=%s, clazz=%s)", entry.getKey(), callback.getClass().getName());
                resultNonPlayerLoadedTickets.addAll(result);
            }
            if (callback instanceof ChunkTicketManager.PlayerOrderedCallback) {
                if (playerLoadedTickets == null) {
                    final ImmutableListMultimap.Builder<UUID, LoadingTicket> mapBuilder = ImmutableListMultimap.builder();
                    resultPlayerLoadedTickets.forEach(ticket -> mapBuilder.put(((PlayerLoadingTicket) ticket).getPlayerUniqueId(), ticket));
                    resultPlayerLoadedTickets.clear();
                    playerLoadedTickets = mapBuilder.build();
                }
                final ListMultimap<UUID, LoadingTicket> result = ((ChunkTicketManager.PlayerOrderedCallback) callback).onPlayerLoaded(playerLoadedTickets, this.world);
                checkNotNull(result, "The PlayerOrderedCallback#onPlayerLoaded method may not return null, " + "error caused by (plugin=%s, clazz=%s)", entry.getKey(), callback.getClass().getName());
                resultPlayerLoadedTickets.addAll(result.values());
            }
        }
        final List<LoadingTicket> resultLoadedTickets = new ArrayList<>();
        resultLoadedTickets.addAll(resultPlayerLoadedTickets);
        resultLoadedTickets.addAll(resultNonPlayerLoadedTickets);
        // Lets see how many plugins attempted to add loading tickets
        final int sizeA = resultLoadedTickets.size();
        resultLoadedTickets.retainAll(entry.getValue());
        final int sizeB = resultLoadedTickets.size();
        if (sizeA != sizeB) {
            Lantern.getLogger().warn("The plugin {} attempted to add LoadingTicket's that were previously not present.", entry.getKey());
        }
        // Remove all the tickets that are already released
        resultLoadedTickets.removeIf(ticket -> ((ChunkLoadingTicket) ticket).isReleased());
        if (resultLoadedTickets.size() > maxTickets) {
            Lantern.getLogger().warn("The plugin {} has too many open chunk loading tickets {}. " + "Excess will be dropped", entry.getKey(), resultLoadedTickets.size());
            resultLoadedTickets.subList(maxTickets, resultLoadedTickets.size()).clear();
        }
        // Release all the tickets that were no longer usable
        final List<LoadingTicket> removedTickets = new ArrayList<>(entry.getValue());
        removedTickets.removeAll(resultLoadedTickets);
        removedTickets.forEach(LoadingTicket::release);
        final ImmutableList<LoadingTicket> loadedTickets = ImmutableList.copyOf(resultLoadedTickets);
        for (ChunkTicketManager.Callback callback : callbacks) {
            callback.onLoaded(loadedTickets, this.world);
        }
    }
}
Also used : ImmutableBlockVolume(org.spongepowered.api.world.extent.ImmutableBlockVolume) GenerationPopulator(org.spongepowered.api.world.gen.GenerationPopulator) Arrays(java.util.Arrays) ListMultimap(com.google.common.collect.ListMultimap) ChunkSection(org.lanternpowered.server.world.chunk.LanternChunk.ChunkSection) Inject(com.google.inject.Inject) ChunkTicketManager(org.spongepowered.api.world.ChunkTicketManager) ShortArrayMutableBiomeBuffer(org.lanternpowered.server.util.gen.biome.ShortArrayMutableBiomeBuffer) BiomeGenerationSettings(org.spongepowered.api.world.biome.BiomeGenerationSettings) Random(java.util.Random) BiomeTypes(org.spongepowered.api.world.biome.BiomeTypes) WorldConfig(org.lanternpowered.server.config.world.WorldConfig) BiomeGenerator(org.spongepowered.api.world.gen.BiomeGenerator) EventManager(org.spongepowered.api.event.EventManager) Future(java.util.concurrent.Future) Populator(org.spongepowered.api.world.gen.Populator) DirectoryKeys(org.lanternpowered.server.game.DirectoryKeys) ChunkIOService(org.lanternpowered.server.data.io.ChunkIOService) Map(java.util.Map) BlockRegistryModule(org.lanternpowered.server.game.registry.type.block.BlockRegistryModule) ShortArrayMutableBlockBuffer(org.lanternpowered.server.util.gen.block.ShortArrayMutableBlockBuffer) EntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.EntityLoadingTicket) VirtualBiomeType(org.spongepowered.api.world.biome.VirtualBiomeType) Path(java.nio.file.Path) ObjectArrayImmutableBiomeBuffer(org.lanternpowered.server.util.gen.biome.ObjectArrayImmutableBiomeBuffer) PlayerLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) CHUNK_SECTIONS(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_SECTIONS) ImmutableSet(com.google.common.collect.ImmutableSet) CHUNK_AREA(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_AREA) Extent(org.spongepowered.api.world.extent.Extent) BlockTypes(org.spongepowered.api.block.BlockTypes) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) UUID(java.util.UUID) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) LanternGame(org.lanternpowered.server.game.LanternGame) BlockState(org.spongepowered.api.block.BlockState) FastSoftThreadLocal(org.lanternpowered.server.util.concurrent.FastSoftThreadLocal) WorldGenerator(org.spongepowered.api.world.gen.WorldGenerator) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) SoftBufferExtentViewDownsize(org.lanternpowered.server.world.extent.SoftBufferExtentViewDownsize) Lantern(org.lanternpowered.server.game.Lantern) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Entry(java.util.Map.Entry) Optional(java.util.Optional) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) InternalPluginsInfo(org.lanternpowered.server.plugin.InternalPluginsInfo) AtomicShortArrayMutableBlockBuffer(org.lanternpowered.server.util.gen.block.AtomicShortArrayMutableBlockBuffer) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) AbstractMutableBlockBuffer(org.lanternpowered.server.util.gen.block.AbstractMutableBlockBuffer) MutableBlockVolume(org.spongepowered.api.world.extent.MutableBlockVolume) BiomeType(org.spongepowered.api.world.biome.BiomeType) Callable(java.util.concurrent.Callable) Multimap(com.google.common.collect.Multimap) CHUNK_SECTION_VOLUME(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_SECTION_VOLUME) ImmutableBiomeVolume(org.spongepowered.api.world.extent.ImmutableBiomeVolume) ArrayList(java.util.ArrayList) Vector2i(com.flowpowered.math.vector.Vector2i) HashSet(java.util.HashSet) CHUNK_SECTION_SIZE(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_SECTION_SIZE) ThreadHelper(org.lanternpowered.server.util.ThreadHelper) CauseStack(org.lanternpowered.server.event.CauseStack) ImmutableList(com.google.common.collect.ImmutableList) ShortArrayImmutableBlockBuffer(org.lanternpowered.server.util.gen.block.ShortArrayImmutableBlockBuffer) LanternWorld(org.lanternpowered.server.world.LanternWorld) CHUNK_BIOME_VOLUME(org.lanternpowered.server.world.chunk.LanternChunkLayout.CHUNK_BIOME_VOLUME) LinkedList(java.util.LinkedList) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Nullable(javax.annotation.Nullable) ExtentBufferHelper(org.lanternpowered.server.world.extent.ExtentBufferHelper) Iterator(java.util.Iterator) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CHUNK_HEIGHT(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_HEIGHT) Chunk(org.spongepowered.api.world.Chunk) IOException(java.io.IOException) Entity(org.spongepowered.api.entity.Entity) LoadingTicket(org.spongepowered.api.world.ChunkTicketManager.LoadingTicket) TimeUnit(java.util.concurrent.TimeUnit) StorageType(org.spongepowered.api.world.extent.StorageType) Vector3i(com.flowpowered.math.vector.Vector3i) Named(com.google.inject.name.Named) PlayerEntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerEntityLoadingTicket) MapMaker(com.google.common.collect.MapMaker) Conditions.checkPlugin(org.lanternpowered.server.util.Conditions.checkPlugin) Entity(org.spongepowered.api.entity.Entity) ArrayList(java.util.ArrayList) EntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.EntityLoadingTicket) PlayerLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket) LoadingTicket(org.spongepowered.api.world.ChunkTicketManager.LoadingTicket) PlayerEntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerEntityLoadingTicket) Entry(java.util.Map.Entry) PlayerLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) UUID(java.util.UUID) CauseStack(org.lanternpowered.server.event.CauseStack) ChunkTicketManager(org.spongepowered.api.world.ChunkTicketManager) Collection(java.util.Collection)

Example 2 with LoadingTicket

use of org.spongepowered.api.world.ChunkTicketManager.LoadingTicket in project LanternServer by LanternPowered.

the class LanternChunkManager method getForced.

/**
 * Gets a map with all the forced chunk and the assigned tickets.
 *
 * @return the tickets
 */
public ImmutableSetMultimap<Vector3i, LoadingTicket> getForced() {
    final ImmutableSetMultimap.Builder<Vector3i, LoadingTicket> builder = ImmutableSetMultimap.builder();
    for (Entry<Vector2i, Set<ChunkLoadingTicket>> en : this.ticketsByPos.entrySet()) {
        final Vector2i pos0 = en.getKey();
        final Vector3i pos = new Vector3i(pos0.getX(), 0, pos0.getY());
        for (ChunkLoadingTicket ticket : en.getValue()) {
            builder.put(pos, ticket);
        }
    }
    return builder.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Vector3i(com.flowpowered.math.vector.Vector3i) Vector2i(com.flowpowered.math.vector.Vector2i) EntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.EntityLoadingTicket) PlayerLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket) LoadingTicket(org.spongepowered.api.world.ChunkTicketManager.LoadingTicket) PlayerEntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerEntityLoadingTicket)

Aggregations

Vector2i (com.flowpowered.math.vector.Vector2i)2 Vector3i (com.flowpowered.math.vector.Vector3i)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 EntityLoadingTicket (org.spongepowered.api.world.ChunkTicketManager.EntityLoadingTicket)2 LoadingTicket (org.spongepowered.api.world.ChunkTicketManager.LoadingTicket)2 PlayerEntityLoadingTicket (org.spongepowered.api.world.ChunkTicketManager.PlayerEntityLoadingTicket)2 PlayerLoadingTicket (org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1 MapMaker (com.google.common.collect.MapMaker)1 Multimap (com.google.common.collect.Multimap)1 Sets (com.google.common.collect.Sets)1 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 IOException (java.io.IOException)1