Search in sources :

Example 1 with PacketDescription

use of pneumaticCraft.common.network.PacketDescription in project PneumaticCraft by MineMaarten.

the class SemiBlockManager method syncWithPlayers.

private void syncWithPlayers(World world) {
    List<EntityPlayerMP> players = world.playerEntities;
    for (Map.Entry<Chunk, Set<EntityPlayerMP>> entry : syncList.entrySet()) {
        Chunk chunk = entry.getKey();
        Set<EntityPlayerMP> syncedPlayers = entry.getValue();
        int chunkX = chunk.xPosition * 16 - 8;
        int chunkZ = chunk.zPosition * 16 - 8;
        for (EntityPlayerMP player : players) {
            if (chunk.worldObj == world) {
                double dist = PneumaticCraftUtils.distBetween(player.posX, 0, player.posZ, chunkX, 0, chunkZ);
                if (dist < SYNC_DISTANCE) {
                    if (syncedPlayers.add(player)) {
                        for (ISemiBlock semiBlock : semiBlocks.get(chunk).values()) {
                            if (!semiBlock.isInvalid()) {
                                NetworkHandler.sendTo(new PacketSetSemiBlock(semiBlock), player);
                                PacketDescription descPacket = semiBlock.getDescriptionPacket();
                                if (descPacket != null)
                                    NetworkHandler.sendTo(descPacket, player);
                            }
                        }
                    }
                } else if (dist > SYNC_DISTANCE + 5) {
                    syncedPlayers.remove(player);
                }
            } else {
                syncedPlayers.remove(player);
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) PacketDescription(pneumaticCraft.common.network.PacketDescription) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Chunk(net.minecraft.world.chunk.Chunk) HashMap(java.util.HashMap) Map(java.util.Map) HashBiMap(com.google.common.collect.HashBiMap) PacketSetSemiBlock(pneumaticCraft.common.network.PacketSetSemiBlock)

Example 2 with PacketDescription

use of pneumaticCraft.common.network.PacketDescription in project PneumaticCraft by MineMaarten.

the class SemiBlockManager method onServerTick.

@SubscribeEvent
public void onServerTick(TickEvent.ServerTickEvent event) {
    for (ISemiBlock semiBlock : addingBlocks) {
        Chunk chunk = semiBlock.getWorld().getChunkFromBlockCoords(semiBlock.getPos().chunkPosX, semiBlock.getPos().chunkPosZ);
        getOrCreateMap(chunk).put(semiBlock.getPos(), semiBlock);
        chunk.setChunkModified();
        for (EntityPlayerMP player : syncList.get(chunk)) {
            NetworkHandler.sendTo(new PacketSetSemiBlock(semiBlock), player);
            PacketDescription descPacket = semiBlock.getDescriptionPacket();
            if (descPacket != null)
                NetworkHandler.sendTo(descPacket, player);
        }
    }
    addingBlocks.clear();
    for (Chunk removingChunk : chunksMarkedForRemoval) {
        if (!removingChunk.isChunkLoaded) {
            semiBlocks.remove(removingChunk);
            syncList.remove(removingChunk);
        }
    }
    chunksMarkedForRemoval.clear();
    for (Map<ChunkPosition, ISemiBlock> map : semiBlocks.values()) {
        for (ISemiBlock semiBlock : map.values()) {
            if (!semiBlock.isInvalid())
                semiBlock.update();
        }
        Iterator<ISemiBlock> iterator = map.values().iterator();
        while (iterator.hasNext()) {
            if (iterator.next().isInvalid()) {
                iterator.remove();
            }
        }
    }
}
Also used : PacketDescription(pneumaticCraft.common.network.PacketDescription) ChunkPosition(net.minecraft.world.ChunkPosition) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Chunk(net.minecraft.world.chunk.Chunk) PacketSetSemiBlock(pneumaticCraft.common.network.PacketSetSemiBlock) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Aggregations

EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)2 Chunk (net.minecraft.world.chunk.Chunk)2 PacketDescription (pneumaticCraft.common.network.PacketDescription)2 PacketSetSemiBlock (pneumaticCraft.common.network.PacketSetSemiBlock)2 HashBiMap (com.google.common.collect.HashBiMap)1 SubscribeEvent (cpw.mods.fml.common.eventhandler.SubscribeEvent)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ChunkPosition (net.minecraft.world.ChunkPosition)1