Java Code Examples for net.minecraft.world.World#isBlockLoaded()

The following examples show how to use net.minecraft.world.World#isBlockLoaded() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockUtils.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Tries breaking a block safely and fires an event for it.
 *
 * @return Whether the block was successfully broken
 */
public static boolean breakBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nullable IBlockState oldState, @Nonnull EntityPlayerMP player) {
	if (!world.isBlockLoaded(pos)) return false;

	if (!(player instanceof FakePlayer) && !hasEditPermission(pos, player)) return false;

	if (oldState == null) oldState = world.getBlockState(pos);

	BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, oldState, player);
	MinecraftForge.EVENT_BUS.post(event);

	if (event.isCanceled()) return false;

	TileEntity tile = world.getTileEntity(pos);
	Block block = oldState.getBlock();
	if (block.removedByPlayer(oldState, world, pos, player, true)) {
		block.onPlayerDestroy(world, pos, oldState);
		block.harvestBlock(world, player, pos, oldState, tile, player.getHeldItemMainhand());
		world.notifyBlockUpdate(pos, oldState, Blocks.AIR.getDefaultState(), 3);
	} else return false;

	return true;
}
 
Example 2
Source File: WorldMana.java    From YouTubeModdingTutorial with MIT License 6 votes vote down vote up
private void growMana(World world) {
    for (Map.Entry<ChunkPos, ManaSphere> entry : spheres.entrySet()) {
        ManaSphere sphere = entry.getValue();
        if (sphere.getRadius() > 0) {
            if (world.isBlockLoaded(sphere.getCenter())) {
                float currentMana = sphere.getCurrentMana();
                currentMana += .01f;
                if (currentMana >= 5) {
                    currentMana = 5;
                }
                sphere.setCurrentMana(currentMana);
                markDirty();
            }
        }
    }
}
 
Example 3
Source File: DamageTracker.java    From YouTubeModdingTutorial with MIT License 6 votes vote down vote up
@SubscribeEvent
public void onDamage(LivingDamageEvent event) {
    float amount = event.getAmount();
    EntityLivingBase entity = event.getEntityLiving();
    World world = entity.world;
    int dimension = world.provider.getDimension();
    if (amount > 0 && tracking.containsKey(dimension)) {
        Map<BlockPos, Set<UUID>> dimensionTracking = tracking.get(dimension);
        for (Map.Entry<BlockPos, Set<UUID>> entry : dimensionTracking.entrySet()) {
            if (entry.getValue().contains(entity.getUniqueID())) {
                if (world.isBlockLoaded(entry.getKey())) {
                    TileEntity tileEntity = world.getTileEntity(entry.getKey());
                    if (tileEntity instanceof TileGenerator) {
                        ((TileGenerator) tileEntity).senseDamage(entity, amount);
                    }
                }
            }
        }
    }

}
 
Example 4
Source File: TidalGeneratorAnimationPacket.java    From EmergingTechnology with MIT License 5 votes vote down vote up
private TidalGeneratorTileEntity getTileEntity(World world, BlockPos pos) {

            if (world == null)
                return null;
            if (!world.isBlockLoaded(pos))
                return null;
            if (world.getTileEntity(pos) == null)
                return null;
            if (world.getTileEntity(pos) instanceof TidalGeneratorTileEntity == false)
                return null;

            return (TidalGeneratorTileEntity) world.getTileEntity(pos);
        }
 
Example 5
Source File: BlockUtils.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean placeBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing facing, @Nonnull ItemStack stack) {
	if (!world.isBlockLoaded(pos)) return false;

	FakePlayer player = FakePlayerFactory.get((WorldServer) world, PLACER);
	player.moveToBlockPosAndAngles(pos, 0, -90);
	player.setHeldItem(EnumHand.MAIN_HAND, stack);
	player.setSneaking(true);

	if (!hasEditPermission(pos, player)) return false;

	EnumActionResult result = player.interactionManager.processRightClickBlock(
			player, world, stack, EnumHand.MAIN_HAND,
			pos, facing, 0, 0, 0);
	return result != EnumActionResult.FAIL;
}
 
Example 6
Source File: BlockUtils.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Places the specified block into the world at the specified position if it is possible to do so without violating permission restrictions.
 *
 * @return <tt>true</tt> if the specified block was successfully placed into the world
 */
public static boolean placeBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull EntityPlayerMP player) {
	if (!world.isBlockLoaded(pos)) return false;

	if (!hasEditPermission(pos, player)) return false;

	BlockEvent.PlaceEvent event = new BlockEvent.PlaceEvent(BlockSnapshot.getBlockSnapshot(world, pos), Blocks.AIR.getDefaultState(), player, player.getActiveHand());
	MinecraftForge.EVENT_BUS.post(event);

	if (event.isCanceled()) return false;

	world.setBlockState(pos, state);
	world.notifyBlockUpdate(pos, state, state, 3);
	return true;
}
 
Example 7
Source File: ValkyrienUtils.java    From Valkyrien-Skies with Apache License 2.0 5 votes vote down vote up
public static Optional<PhysicsObject> getPhysicsObject(@Nullable World world,
    @Nullable BlockPos pos, boolean includePartiallyLoaded) {
    // No physics object manages a null world or a null pos.
    if (world != null && pos != null && world.isBlockLoaded(pos)) {
        IPhysicsChunk physicsChunk = (IPhysicsChunk) world.getChunk(pos);
        Optional<PhysicsObject> physicsObject = physicsChunk.getPhysicsObjectOptional();
        if (physicsObject.isPresent()) {
            if (includePartiallyLoaded || physicsObject.get()
                .isFullyLoaded()) {
                return physicsObject;
            }
        }
    }
    return Optional.empty();
}
 
Example 8
Source File: GTUtility.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * A way to update surrounding blocks. this does not update the original block!
 * 
 * @param world     - The world to update in
 * @param pos       - the block pos the update should originate
 * @param blockType - block type which can be null
 * @param sides     - the list of sides to iterate an update
 */
public static void updateNeighbors(World world, BlockPos pos, @Nullable Block blockType, RotationList sides) {
	if (blockType == null) {
		blockType = Blocks.AIR;
	}
	for (EnumFacing side : sides) {
		BlockPos newPos = pos.offset(side);
		if (world.isBlockLoaded(newPos)) {
			world.neighborChanged(newPos, blockType, pos);
		}
	}
}
 
Example 9
Source File: SyncMapTile.java    From OpenModsLib with MIT License 5 votes vote down vote up
public static ISyncMapProvider findOwner(World world, PacketBuffer input) {
	final BlockPos pos = input.readBlockPos();
	if (world != null) {
		if (world.isBlockLoaded(pos)) {
			final TileEntity tile = world.getTileEntity(pos);
			if (tile instanceof ISyncMapProvider) return (ISyncMapProvider)tile;
		}
	}

	return null;
}
 
Example 10
Source File: VSNode_TileEntity.java    From Valkyrien-Skies with Apache License 2.0 5 votes vote down vote up
@Nullable
@Deprecated
public static IVSNode getVSNode_TileEntity(World world, BlockPos pos) {
    if (world == null || pos == null) {
        throw new IllegalArgumentException("Null arguments");
    }
    boolean isChunkLoaded = world.isBlockLoaded(pos);
    if (!isChunkLoaded) {
        return null;
        // throw new IllegalStateException("VSNode_TileEntity wasn't loaded in the
        // world!");
    }
    TileEntity entity = world.getTileEntity(pos);
    if (entity == null) {
        return null;
        // throw new IllegalStateException("VSNode_TileEntity was null");
    }
    if (entity instanceof IVSNodeProvider) {
        IVSNode vsNode = ((IVSNodeProvider) entity).getNode();
        if (!vsNode.isValid()) {
            return null;
            // throw new IllegalStateException("IVSNode was not valid!");
        } else {
            return vsNode;
        }
    } else {
        return null;
        // throw new IllegalStateException("VSNode_TileEntity of different class");
    }
}
 
Example 11
Source File: FabricatorStopStartPacket.java    From EmergingTechnology with MIT License 5 votes vote down vote up
private FabricatorTileEntity getTileEntity(World world, BlockPos pos) {

            if (world == null)
                return null;
            if (!world.isBlockLoaded(pos))
                return null;
            if (world.getTileEntity(pos) == null)
                return null;
            if (world.getTileEntity(pos) instanceof FabricatorTileEntity == false)
                return null;

            return (FabricatorTileEntity) world.getTileEntity(pos);
        }
 
Example 12
Source File: WindGeneratorAnimationPacket.java    From EmergingTechnology with MIT License 5 votes vote down vote up
private WindTileEntity getTileEntity(World world, BlockPos pos) {

            if (world == null) return null;
            if (!world.isBlockLoaded(pos)) return null;
            if (world.getTileEntity(pos) == null) return null;
            if (world.getTileEntity(pos) instanceof WindTileEntity == false) return null;
           
            return (WindTileEntity) world.getTileEntity(pos);
        }
 
Example 13
Source File: HarvesterStopAnimationPacket.java    From EmergingTechnology with MIT License 5 votes vote down vote up
private HarvesterTileEntity getTileEntity(World world, BlockPos pos) {

            if (world == null)
                return null;
            if (!world.isBlockLoaded(pos))
                return null;
            if (world.getTileEntity(pos) == null)
                return null;
            if (world.getTileEntity(pos) instanceof HarvesterTileEntity == false)
                return null;

            return (HarvesterTileEntity) world.getTileEntity(pos);
        }
 
Example 14
Source File: BlockTofuTerrain.java    From TofuCraftReload with MIT License 5 votes vote down vote up
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) {
    if (!worldIn.isRemote && worldIn.getBlockState(pos) == BlockLoader.zundatofuTerrain.getDefaultState()) {
        if (!worldIn.isAreaLoaded(pos, 3))
            return; // Forge: prevent loading unloaded chunks when checking neighbor's light and spreading
        if (worldIn.getLightFromNeighbors(pos.up()) < 4 && worldIn.getBlockState(pos.up()).getLightOpacity(worldIn, pos.up()) > 2) {
            worldIn.setBlockState(pos, BlockLoader.tofuTerrain.getDefaultState());
        } else {
            if (worldIn.getLightFromNeighbors(pos.up()) >= 9) {
                for (int i = 0; i < 4; ++i) {
                    BlockPos blockpos = pos.add(rand.nextInt(3) - 1, rand.nextInt(5) - 3, rand.nextInt(3) - 1);

                    if (blockpos.getY() >= 0 && blockpos.getY() < 256 && !worldIn.isBlockLoaded(blockpos)) {
                        return;
                    }

                    IBlockState iblockstate = worldIn.getBlockState(blockpos.up());
                    IBlockState iblockstate1 = worldIn.getBlockState(blockpos);

                    if ((iblockstate1.getBlock() == BlockLoader.tofuTerrain || iblockstate1.getBlock() == BlockLoader.KINUTOFU) && worldIn.getLightFromNeighbors(blockpos.up()) >= 4 && iblockstate.getLightOpacity(worldIn, pos.up()) <= 2) {
                        worldIn.setBlockState(blockpos, BlockLoader.zundatofuTerrain.getDefaultState());
                    }
                }
            }
        }
    }
}
 
Example 15
Source File: InventoryUtils.java    From OpenModsLib with MIT License 4 votes vote down vote up
public static IItemHandler tryGetHandler(World world, BlockPos pos, EnumFacing side) {
	if (!world.isBlockLoaded(pos)) return null;
	final TileEntity te = world.getTileEntity(pos);

	return tryGetHandler(te, side);
}
 
Example 16
Source File: int3.java    From GT-Classic with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean isLoaded(World world) {
	return world.isBlockLoaded(pos);
}
 
Example 17
Source File: TileEntityDrawbridge.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean extendOneBlock(int position, FakePlayer player, boolean playPistonSoundInsteadOfPlaceSound)
{
    final int invPosition = this.isAdvanced() ? position : 0;
    World world = this.getWorld();
    BlockPos pos = this.getPos().offset(this.getFacing(), position + 1);
    ItemStack stack = this.itemHandlerDrawbridge.getStackInSlot(invPosition);
    IBlockState placementState = this.getPlacementStateForPosition(position, world, pos, player, stack);

    if (placementState != null &&
        stack.isEmpty() == false &&
        world.isBlockLoaded(pos, world.isRemote == false) &&
        world.getBlockState(pos).getBlock().isReplaceable(world, pos) &&
        world.mayPlace(placementState.getBlock(), pos, true, EnumFacing.UP, null) &&
        ((playPistonSoundInsteadOfPlaceSound && world.setBlockState(pos, placementState)) ||
         (playPistonSoundInsteadOfPlaceSound == false && BlockUtils.setBlockStateWithPlaceSound(world, pos, placementState, 3))))
    {
        // This extract will also clear the blockInfoTaken, if the slot becomes empty.
        // This will prevent item duping exploits by swapping the item to something else
        // after the drawbridge has taken the state and TE data from the world for a given block.
        stack = this.itemHandlerDrawbridge.extractItem(invPosition, 1, false);

        this.blockStatesPlaced[position] = placementState;
        this.numPlaced++;

        NBTTagCompound nbt = this.getPlacementTileNBT(position, stack);

        if (nbt != null && placementState.getBlock().hasTileEntity(placementState))
        {
            TileUtils.createAndAddTileEntity(world, pos, nbt);
        }

        if (playPistonSoundInsteadOfPlaceSound)
        {
            world.playSound(null, pos, SoundEvents.BLOCK_PISTON_EXTEND, SoundCategory.BLOCKS, 0.5f, 0.8f);
        }

        return true;
    }
    else if (stack.isEmpty() == false)
    {
        this.failedPlacements++;
    }

    return false;
}
 
Example 18
Source File: TileEntityDrawbridge.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean retractOneBlock(int position, FakePlayer player, boolean takeNonPlaced, boolean playPistonSound)
{
    World world = this.getWorld();
    BlockPos pos = this.getPos().offset(this.getFacing(), position + 1);

    if (world.isBlockLoaded(pos, world.isRemote == false) == false)
    {
        return false;
    }

    IBlockState state = world.getBlockState(pos);

    if ((takeNonPlaced || state == this.blockStatesPlaced[position]) &&
        state.getBlock().isAir(state, world, pos) == false &&
        state.getBlockHardness(world, pos) >= 0f)
    {
        ItemStack stack = BlockUtils.getPickBlockItemStack(world, pos, player, EnumFacing.UP);

        if (stack.isEmpty() == false)
        {
            NBTTagCompound nbt = null;
            final int invPosition = this.isAdvanced() ? position : 0;

            if (state.getBlock().hasTileEntity(state))
            {
                TileEntity te = world.getTileEntity(pos);

                if (te != null)
                {
                    TileUtils.storeTileEntityInStack(stack, te, false);
                    nbt = te.writeToNBT(new NBTTagCompound());
                    NBTUtils.removePositionFromTileEntityNBT(nbt);
                }
            }

            if (this.itemHandlerDrawbridge.insertItem(invPosition, stack, false).isEmpty())
            {
                this.blockInfoTaken[position] = new BlockInfo(state, nbt);
                this.numTaken++;

                if (playPistonSound)
                {
                    world.playSound(null, pos, SoundEvents.BLOCK_PISTON_CONTRACT, SoundCategory.BLOCKS, 0.5f, 0.7f);
                }
                else
                {
                    BlockUtils.playBlockBreakSound(world, pos);
                }

                BlockUtils.setBlockToAirWithoutSpillingContents(world, pos);

                this.blockStatesPlaced[position] = null;

                return true;
            }
        }
    }

    return false;
}
 
Example 19
Source File: BlockUtils.java    From OpenModsLib with MIT License 4 votes vote down vote up
public static TileEntity getTileInDirectionSafe(World world, BlockPos coord, EnumFacing direction) {
	BlockPos n = coord.offset(direction);
	return world.isBlockLoaded(n)? world.getTileEntity(n) : null;
}
 
Example 20
Source File: PositionUtils.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Gets the surrounding collision boxes for an EntityItem, when it is being pushed
 * out of blocks. This is identical to the vanilla method other than the vanilla
 * method not passing the entity to the addCollisionBoxToList() method, which
 * prevents having blocks that don't have any collision boxes for EntityItems,
 * when it comes to the pushing-out-of-blocks code.
 * @param world
 * @param bb
 * @param entity
 * @return
 */
public static List<AxisAlignedBB> getSurroundingCollisionBoxesForEntityItem(World world, AxisAlignedBB bb, @Nullable Entity entity)
{
    List<AxisAlignedBB> list = Lists.<AxisAlignedBB>newArrayList();
    int xMin = MathHelper.floor(bb.minX) - 1;
    int xMax = MathHelper.ceil( bb.maxX) + 1;
    int yMin = MathHelper.floor(bb.minY) - 1;
    int yMax = MathHelper.ceil( bb.maxY) + 1;
    int zMin = MathHelper.floor(bb.minZ) - 1;
    int zMax = MathHelper.ceil( bb.maxZ) + 1;
    BlockPos.PooledMutableBlockPos pos = BlockPos.PooledMutableBlockPos.retain();

    for (int x = xMin; x < xMax; x++)
    {
        for (int z = zMin; z < zMax; z++)
        {
            int edges = (x != xMin && x != xMax - 1 ? 0 : 1) + (z != zMin && z != zMax - 1 ? 0 : 1);

            if (edges != 2 && world.isBlockLoaded(pos.setPos(x, 64, z)))
            {
                for (int y = yMin; y < yMax; y++)
                {
                    if (edges <= 0 || y != yMin && y != yMax - 1)
                    {
                        pos.setPos(x, y, z);
                        IBlockState state;

                        if (x >= -30000000 && x < 30000000 && z >= -30000000 && z < 30000000)
                        {
                            state = world.getBlockState(pos);
                        }
                        else
                        {
                            state = Blocks.BEDROCK.getDefaultState();
                        }

                        state.addCollisionBoxToList(world, pos, bb, list, entity, false);
                    }
                }
            }
        }
    }

    pos.release();

    return list;
}