Search in sources :

Example 1 with ShipIndexDataMessage

use of org.valkyrienskies.mod.common.network.ShipIndexDataMessage in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class WorldShipLoadingController method sendUpdatesPackets.

/**
 * Send load/unload/update packets accordingly.
 */
private void sendUpdatesPackets(Map<ShipData, Set<EntityPlayerMP>> oldWatching, Map<ShipData, Set<EntityPlayerMP>> newWatching) {
    // First send the update packets
    // Create a map for every player to the ship data updates it will receive
    Map<EntityPlayerMP, List<ShipData>> updatesMap = new HashMap<>();
    shipManager.getWorld().playerEntities.forEach((player) -> updatesMap.put((EntityPlayerMP) player, new ArrayList<>()));
    for (PhysicsObject ship : shipManager.getAllLoadedPhysObj()) {
        ShipData shipData = ship.getShipData();
        Set<EntityPlayerMP> currentWatchers = newWatching.get(shipData);
        currentWatchers.forEach((player) -> updatesMap.get(player).add(shipData));
    }
    Map<EntityPlayerMP, ShipIndexDataMessage> playerPacketMap = new HashMap<>();
    // Then send those updates
    updatesMap.forEach((player, updates) -> {
        ShipIndexDataMessage indexDataMessage = new ShipIndexDataMessage();
        indexDataMessage.setDimensionID(shipManager.getWorld().provider.getDimension());
        if (!updates.isEmpty()) {
            indexDataMessage.addData(updates);
        }
        playerPacketMap.put(player, indexDataMessage);
    });
    // Then send ship loads to the packets
    for (PhysicsObject ship : shipManager.getAllLoadedPhysObj()) {
        ShipData shipData = ship.getShipData();
        Set<EntityPlayerMP> newWatchers = new HashSet<>(newWatching.get(shipData));
        if (oldWatching.containsKey(shipData)) {
            newWatchers.removeAll(oldWatching.get(shipData));
        }
        if (!newWatchers.isEmpty()) {
            // First send the ship chunks to the new watchers
            for (Chunk chunk : ship.getClaimedChunkCache()) {
                SPacketChunkData data = new SPacketChunkData(chunk, 65535);
                for (EntityPlayerMP player : newWatchers) {
                    player.connection.sendPacket(data);
                    shipManager.getWorld().getEntityTracker().sendLeashedEntitiesInChunk(player, chunk);
                }
            }
            newWatchers.forEach(player -> playerPacketMap.get(player).addLoadUUID(shipData.getUuid()));
        }
    }
    // Then add ship unloads to the packets
    for (ShipData shipData : oldWatching.keySet()) {
        Set<EntityPlayerMP> removedWatchers = new HashSet<>(oldWatching.get(shipData));
        if (newWatching.containsKey(shipData)) {
            removedWatchers.removeAll(newWatching.get(shipData));
        }
        for (EntityPlayerMP player : removedWatchers) {
            // BetterPortals.
            if (!playerPacketMap.containsKey(player)) {
                playerPacketMap.put(player, new ShipIndexDataMessage());
                playerPacketMap.get(player).setDimensionID(shipManager.getWorld().provider.getDimension());
            }
            playerPacketMap.get(player).addUnloadUUID(shipData.getUuid());
        }
    }
    // Finally, send each player their update packet
    playerPacketMap.forEach((player, packet) -> {
        if (!player.hasDisconnected()) {
            ValkyrienSkiesMod.physWrapperNetwork.sendTo(packet, player);
        }
    });
}
Also used : ShipData(org.valkyrienskies.mod.common.ships.ShipData) QueryableShipData(org.valkyrienskies.mod.common.ships.QueryableShipData) Chunk(net.minecraft.world.chunk.Chunk) SPacketChunkData(net.minecraft.network.play.server.SPacketChunkData) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ShipIndexDataMessage(org.valkyrienskies.mod.common.network.ShipIndexDataMessage)

Aggregations

EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 SPacketChunkData (net.minecraft.network.play.server.SPacketChunkData)1 Chunk (net.minecraft.world.chunk.Chunk)1 ShipIndexDataMessage (org.valkyrienskies.mod.common.network.ShipIndexDataMessage)1 QueryableShipData (org.valkyrienskies.mod.common.ships.QueryableShipData)1 ShipData (org.valkyrienskies.mod.common.ships.ShipData)1