Search in sources :

Example 1 with ScheduledBlockChange

use of micdoodle8.mods.galacticraft.core.wrappers.ScheduledBlockChange in project Galacticraft by micdoodle8.

the class TickHandlerServer method onWorldTick.

@SubscribeEvent
public void onWorldTick(WorldTickEvent event) {
    if (event.phase == Phase.START) {
        final WorldServer world = (WorldServer) event.world;
        CopyOnWriteArrayList<ScheduledBlockChange> changeList = TickHandlerServer.scheduledBlockChanges.get(GCCoreUtil.getDimensionID(world));
        if (changeList != null && !changeList.isEmpty()) {
            int blockCount = 0;
            int blockCountMax = Math.max(this.MAX_BLOCKS_PER_TICK, changeList.size() / 4);
            List<ScheduledBlockChange> newList = new ArrayList<ScheduledBlockChange>(Math.max(0, changeList.size() - blockCountMax));
            for (ScheduledBlockChange change : changeList) {
                if (++blockCount > blockCountMax) {
                    newList.add(change);
                } else {
                    if (change != null) {
                        BlockPos changePosition = change.getChangePosition();
                        Block block = world.getBlockState(changePosition).getBlock();
                        // Only replace blocks of type BlockAir or fire - this is to prevent accidents where other mods have moved blocks
                        if (changePosition != null && (block instanceof BlockAir || block == Blocks.fire)) {
                            world.setBlockState(changePosition, change.getChangeID().getStateFromMeta(change.getChangeMeta()), change.getChangeUpdateFlag());
                        }
                    }
                }
            }
            changeList.clear();
            TickHandlerServer.scheduledBlockChanges.remove(GCCoreUtil.getDimensionID(world));
            if (newList.size() > 0) {
                TickHandlerServer.scheduledBlockChanges.put(GCCoreUtil.getDimensionID(world), new CopyOnWriteArrayList<ScheduledBlockChange>(newList));
            }
        }
        CopyOnWriteArrayList<BlockVec3> torchList = TickHandlerServer.scheduledTorchUpdates.get(GCCoreUtil.getDimensionID(world));
        if (torchList != null && !torchList.isEmpty()) {
            for (BlockVec3 torch : torchList) {
                if (torch != null) {
                    BlockPos pos = new BlockPos(torch.x, torch.y, torch.z);
                    Block b = world.getBlockState(pos).getBlock();
                    if (b instanceof BlockUnlitTorch) {
                        world.scheduleUpdate(pos, b, 2 + world.rand.nextInt(30));
                    }
                }
            }
            torchList.clear();
            TickHandlerServer.scheduledTorchUpdates.remove(GCCoreUtil.getDimensionID(world));
        }
        if (world.provider instanceof IOrbitDimension) {
            try {
                int dim = GCCoreUtil.getDimensionID(WorldUtil.getProviderForNameServer(((IOrbitDimension) world.provider).getPlanetToOrbit()));
                int minY = ((IOrbitDimension) world.provider).getYCoordToTeleportToPlanet();
                final Entity[] entityList = world.loadedEntityList.toArray(new Entity[world.loadedEntityList.size()]);
                for (final Entity e : entityList) {
                    if (e.posY <= minY && e.worldObj == world) {
                        WorldUtil.transferEntityToDimension(e, dim, world, false, null);
                    }
                }
            } catch (Exception ex) {
            }
        }
        int dimensionID = GCCoreUtil.getDimensionID(world);
        if (worldsNeedingUpdate.contains(dimensionID)) {
            worldsNeedingUpdate.remove(dimensionID);
            for (Object obj : event.world.loadedTileEntityList) {
                TileEntity tile = (TileEntity) obj;
                if (tile instanceof TileEntityFluidTank) {
                    ((TileEntityFluidTank) tile).updateClient = true;
                }
            }
        }
    } else if (event.phase == Phase.END) {
        final WorldServer world = (WorldServer) event.world;
        for (GalacticraftPacketHandler handler : packetHandlers) {
            handler.tick(world);
        }
        int dimID = GCCoreUtil.getDimensionID(world);
        Set<BlockPos> edgesList = TickHandlerServer.edgeChecks.get(dimID);
        final HashSet<BlockPos> checkedThisTick = new HashSet<>();
        if (edgesList != null && !edgesList.isEmpty()) {
            List<BlockPos> edgesListCopy = new ArrayList<>();
            edgesListCopy.addAll(edgesList);
            for (BlockPos edgeBlock : edgesListCopy) {
                if (edgeBlock != null && !checkedThisTick.contains(edgeBlock)) {
                    if (TickHandlerServer.scheduledForChange(dimID, edgeBlock)) {
                        continue;
                    }
                    ThreadFindSeal done = new ThreadFindSeal(world, edgeBlock, 0, new ArrayList<TileEntityOxygenSealer>());
                    checkedThisTick.addAll(done.checkedAll());
                }
            }
            TickHandlerServer.edgeChecks.remove(GCCoreUtil.getDimensionID(world));
        }
    }
}
Also used : Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) WorldServer(net.minecraft.world.WorldServer) GalacticraftPacketHandler(micdoodle8.mods.galacticraft.core.network.GalacticraftPacketHandler) TileEntity(net.minecraft.tileentity.TileEntity) BlockPos(net.minecraft.util.BlockPos) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BlockVec3(micdoodle8.mods.galacticraft.api.vector.BlockVec3) BlockAir(net.minecraft.block.BlockAir) ThreadFindSeal(micdoodle8.mods.galacticraft.core.fluid.ThreadFindSeal) ScheduledBlockChange(micdoodle8.mods.galacticraft.core.wrappers.ScheduledBlockChange) Footprint(micdoodle8.mods.galacticraft.core.wrappers.Footprint) BlockUnlitTorch(micdoodle8.mods.galacticraft.core.blocks.BlockUnlitTorch) TileEntityFluidTank(micdoodle8.mods.galacticraft.core.tile.TileEntityFluidTank) Block(net.minecraft.block.Block) IOrbitDimension(micdoodle8.mods.galacticraft.api.world.IOrbitDimension) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with ScheduledBlockChange

use of micdoodle8.mods.galacticraft.core.wrappers.ScheduledBlockChange in project Galacticraft by micdoodle8.

the class ThreadFindSeal method makeSealGood.

private void makeSealGood(boolean ambientThermal) {
    if (!this.airToReplace.isEmpty() || !this.airToReplaceBright.isEmpty() || !ambientThermalTracked.isEmpty() || !ambientThermalTracked.isEmpty()) {
        List<ScheduledBlockChange> changeList = new LinkedList<ScheduledBlockChange>();
        Block breatheableAirID = GCBlocks.breatheableAir;
        Block breatheableAirIDBright = GCBlocks.brightBreatheableAir;
        int metadata = ambientThermal ? 1 : 0;
        // TODO: Can we somehow detect only changes in state of ambientThermal since last check?  tricky...
        for (BlockVec3 checkedVec : this.airToReplace) {
            // No block update for performance reasons; deal with unlit torches separately
            changeList.add(new ScheduledBlockChange(checkedVec.toBlockPos(), breatheableAirID, metadata, 0));
        }
        for (BlockVec3 checkedVec : this.airToReplaceBright) {
            changeList.add(new ScheduledBlockChange(checkedVec.toBlockPos(), breatheableAirIDBright, metadata, 0));
        }
        for (BlockVec3 checkedVec : this.ambientThermalTracked) {
            if (checkedVec.getBlockMetadata(this.world) != metadata) {
                changeList.add(new ScheduledBlockChange(checkedVec.toBlockPos(), breatheableAirID, metadata, 0));
            }
        }
        for (BlockVec3 checkedVec : this.ambientThermalTrackedBright) {
            if (checkedVec.getBlockMetadata(this.world) != metadata) {
                changeList.add(new ScheduledBlockChange(checkedVec.toBlockPos(), breatheableAirIDBright, metadata, 0));
            }
        }
        TickHandlerServer.scheduleNewBlockChange(GCCoreUtil.getDimensionID(this.world), changeList);
    }
    if (!this.torchesToUpdate.isEmpty()) {
        TickHandlerServer.scheduleNewTorchUpdate(GCCoreUtil.getDimensionID(this.world), this.torchesToUpdate);
    }
}
Also used : ScheduledBlockChange(micdoodle8.mods.galacticraft.core.wrappers.ScheduledBlockChange) IPartialSealableBlock(micdoodle8.mods.galacticraft.api.block.IPartialSealableBlock) BlockVec3(micdoodle8.mods.galacticraft.api.vector.BlockVec3)

Example 3 with ScheduledBlockChange

use of micdoodle8.mods.galacticraft.core.wrappers.ScheduledBlockChange in project Galacticraft by micdoodle8.

the class ThreadFindSeal method makeSealBad.

private void makeSealBad() {
    if (!this.breatheableToReplace.isEmpty() || !this.breatheableToReplaceBright.isEmpty()) {
        List<ScheduledBlockChange> changeList = new LinkedList<ScheduledBlockChange>();
        for (BlockVec3 checkedVec : this.breatheableToReplace) {
            changeList.add(new ScheduledBlockChange(checkedVec.toBlockPos(), Blocks.air, 0, 0));
        }
        for (BlockVec3 checkedVec : this.fireToReplace) {
            changeList.add(new ScheduledBlockChange(checkedVec.toBlockPos(), Blocks.air, 0, 2));
        }
        for (BlockVec3 checkedVec : this.breatheableToReplaceBright) {
            changeList.add(new ScheduledBlockChange(checkedVec.toBlockPos(), GCBlocks.brightAir, 0, 0));
        }
        TickHandlerServer.scheduleNewBlockChange(GCCoreUtil.getDimensionID(this.world), changeList);
    }
    if (!this.torchesToUpdate.isEmpty()) {
        TickHandlerServer.scheduleNewTorchUpdate(GCCoreUtil.getDimensionID(this.world), this.torchesToUpdate);
    }
}
Also used : ScheduledBlockChange(micdoodle8.mods.galacticraft.core.wrappers.ScheduledBlockChange) BlockVec3(micdoodle8.mods.galacticraft.api.vector.BlockVec3)

Aggregations

BlockVec3 (micdoodle8.mods.galacticraft.api.vector.BlockVec3)3 ScheduledBlockChange (micdoodle8.mods.galacticraft.core.wrappers.ScheduledBlockChange)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 IPartialSealableBlock (micdoodle8.mods.galacticraft.api.block.IPartialSealableBlock)1 IOrbitDimension (micdoodle8.mods.galacticraft.api.world.IOrbitDimension)1 BlockUnlitTorch (micdoodle8.mods.galacticraft.core.blocks.BlockUnlitTorch)1 ThreadFindSeal (micdoodle8.mods.galacticraft.core.fluid.ThreadFindSeal)1 GalacticraftPacketHandler (micdoodle8.mods.galacticraft.core.network.GalacticraftPacketHandler)1 TileEntityFluidTank (micdoodle8.mods.galacticraft.core.tile.TileEntityFluidTank)1 Footprint (micdoodle8.mods.galacticraft.core.wrappers.Footprint)1 Block (net.minecraft.block.Block)1 BlockAir (net.minecraft.block.BlockAir)1 Entity (net.minecraft.entity.Entity)1 TileEntity (net.minecraft.tileentity.TileEntity)1 BlockPos (net.minecraft.util.BlockPos)1 WorldServer (net.minecraft.world.WorldServer)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1