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