Search in sources :

Example 1 with EntityLoadingTicket

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

the class LanternLoadingTicketIO method save.

static void save(Path worldFolder, Set<LanternLoadingTicket> tickets) throws IOException {
    final Path file = worldFolder.resolve(TICKETS_FILE);
    if (!Files.exists(file)) {
        Files.createFile(file);
    }
    final Multimap<String, LanternLoadingTicket> sortedByPlugin = HashMultimap.create();
    for (LanternLoadingTicket ticket : tickets) {
        sortedByPlugin.put(ticket.getPlugin(), ticket);
    }
    final List<DataView> ticketHolders = new ArrayList<>();
    for (Entry<String, Collection<LanternLoadingTicket>> entry : sortedByPlugin.asMap().entrySet()) {
        final Collection<LanternLoadingTicket> tickets0 = entry.getValue();
        final List<DataView> ticketEntries = new ArrayList<>();
        for (LanternLoadingTicket ticket0 : tickets0) {
            final DataContainer ticketData = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
            ticketData.set(TICKET_TYPE, ticket0 instanceof EntityLoadingTicket ? TYPE_ENTITY : TYPE_NORMAL);
            final int numChunks = ticket0.getNumChunks();
            // Store the list depth for backwards compatible or something,
            // the current forge version doesn't use it either
            ticketData.set(CHUNK_LIST_DEPTH, (byte) Math.min(numChunks, 127));
            // Storing the chunks number, this number is added by us
            ticketData.set(CHUNK_NUMBER, numChunks);
            if (ticket0 instanceof PlayerLoadingTicket) {
                final PlayerLoadingTicket ticket1 = (PlayerLoadingTicket) ticket0;
                // This is a bit strange, since it already added,
                // but if forge uses it...
                ticketData.set(MOD_ID, entry.getKey());
                ticketData.set(PLAYER_UUID, ticket1.getPlayerUniqueId().toString());
            }
            if (ticket0.extraData != null) {
                ticketData.set(MOD_DATA, ticket0.extraData);
            }
            if (ticket0 instanceof EntityChunkLoadingTicket) {
                final EntityChunkLoadingTicket ticket1 = (EntityChunkLoadingTicket) ticket0;
                ticket1.getOrCreateEntityReference().ifPresent(ref -> {
                    final Vector2i position = ref.getChunkCoords();
                    final UUID uniqueId = ref.getUniqueId();
                    ticketData.set(CHUNK_X, position.getX());
                    ticketData.set(CHUNK_Z, position.getY());
                    ticketData.set(ENTITY_UUID_MOST, uniqueId.getMostSignificantBits());
                    ticketData.set(ENTITY_UUID_LEAST, uniqueId.getLeastSignificantBits());
                });
            }
            ticketEntries.add(ticketData);
        }
        ticketHolders.add(DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(HOLDER_NAME, entry.getKey()).set(TICKETS, ticketEntries));
    }
    final DataContainer dataContainer = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(HOLDER_LIST, ticketHolders);
    NbtStreamUtils.write(dataContainer, Files.newOutputStream(file), true);
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) EntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.EntityLoadingTicket) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) PlayerLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket) Collection(java.util.Collection) Vector2i(com.flowpowered.math.vector.Vector2i) UUID(java.util.UUID)

Aggregations

Vector2i (com.flowpowered.math.vector.Vector2i)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 UUID (java.util.UUID)1 DataContainer (org.spongepowered.api.data.DataContainer)1 DataView (org.spongepowered.api.data.DataView)1 EntityLoadingTicket (org.spongepowered.api.world.ChunkTicketManager.EntityLoadingTicket)1 PlayerLoadingTicket (org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket)1