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);
}
}
}
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();
}
Aggregations