Search in sources :

Example 1 with PacketSetSemiBlock

use of pneumaticCraft.common.network.PacketSetSemiBlock 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 PacketSetSemiBlock

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

the class SemiBlockLogistics method onRightClickWithConfigurator.

@Override
public boolean onRightClickWithConfigurator(EntityPlayer player) {
    if (getGuiID() != null) {
        NetworkHandler.sendTo(new PacketSetSemiBlock(pos, this), (EntityPlayerMP) player);
        player.openGui(PneumaticCraft.instance, getGuiID().ordinal(), world, pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
    }
    return true;
}
Also used : PacketSetSemiBlock(pneumaticCraft.common.network.PacketSetSemiBlock)

Example 3 with PacketSetSemiBlock

use of pneumaticCraft.common.network.PacketSetSemiBlock 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)

Example 4 with PacketSetSemiBlock

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

the class SemiBlockManager method setSemiBlock.

private void setSemiBlock(World world, int x, int y, int z, ISemiBlock semiBlock, Chunk chunk) {
    if (semiBlock != null && !registeredTypes.containsValue(semiBlock.getClass()))
        throw new IllegalStateException("ISemiBlock \"" + semiBlock + "\" was not registered!");
    ChunkPosition pos = new ChunkPosition(x, y, z);
    if (semiBlock != null) {
        semiBlock.initialize(world, pos);
        addingBlocks.add(semiBlock);
    } else {
        ISemiBlock removedBlock = getOrCreateMap(chunk).get(pos);
        if (removedBlock != null) {
            removedBlock.invalidate();
            for (EntityPlayerMP player : syncList.get(chunk)) {
                NetworkHandler.sendTo(new PacketSetSemiBlock(pos, null), player);
            }
        }
    }
    chunk.setChunkModified();
}
Also used : ChunkPosition(net.minecraft.world.ChunkPosition) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) PacketSetSemiBlock(pneumaticCraft.common.network.PacketSetSemiBlock)

Aggregations

PacketSetSemiBlock (pneumaticCraft.common.network.PacketSetSemiBlock)4 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)3 ChunkPosition (net.minecraft.world.ChunkPosition)2 Chunk (net.minecraft.world.chunk.Chunk)2 PacketDescription (pneumaticCraft.common.network.PacketDescription)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