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

The following examples show how to use net.minecraft.world.World#isBlockModifiable() . These examples are extracted from open source projects. 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
public ActionResult<ItemStack> tryPickUpFluid(@Nonnull World world, @Nonnull EntityPlayer player,
		@Nonnull EnumHand hand, ItemStack itemstack, FluidStack fluidStack) {
	RayTraceResult mop = this.rayTrace(world, player, true);
	ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, itemstack, mop);
	if (ret != null)
		return ret;
	if (mop == null) {
		return ActionResult.newResult(EnumActionResult.PASS, itemstack);
	}
	BlockPos clickPos = mop.getBlockPos();
	if (world.isBlockModifiable(player, clickPos)) {
		FluidActionResult result = FluidUtil.tryPickUpFluid(itemstack, player, world, clickPos, mop.sideHit);
		if (result.isSuccess()) {
			ItemHandlerHelper.giveItemToPlayer(player, result.getResult());
			itemstack.shrink(1);
			return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
		}
	}
	return ActionResult.newResult(EnumActionResult.FAIL, itemstack);
}
 
Example 2
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos,
        EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
    ItemStack stack = player.getHeldItem(hand);
    ItemStack useStack = this.getItemForUse(stack, player);

    if (useStack.isEmpty() == false && world.isBlockModifiable(player, pos.offset(facing)))
    {
        EntityUtils.setHeldItemWithoutEquipSound(player, hand, useStack);
        EnumActionResult result = useStack.onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ);

        if (world.isRemote == false && player.capabilities.isCreativeMode == false && useStack.isEmpty() == false)
        {
            tryInsertItemsToNullifier(useStack, stack, player);
        }

        EntityUtils.setHeldItemWithoutEquipSound(player, hand, stack);

        return result;
    }

    return super.onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ);
}
 
Example 3
/**
 * Checks if the player is allowed to change this block. Creative mode players are always allowed to change blocks.
 */
public static boolean canChangeBlock(World world, BlockPos pos, EntityPlayer player, boolean allowTileEntities, float maxHardness)
{
    if (player.capabilities.isCreativeMode)
    {
        return true;
    }

    IBlockState state = world.getBlockState(pos);

    if (state.getBlock().isAir(state, world, pos))
    {
        return true;
    }

    float hardness = state.getBlockHardness(world, pos);

    return world.isBlockModifiable(player, pos) && hardness >= 0 && (hardness <= maxHardness || state.getMaterial().isLiquid()) &&
           (allowTileEntities || world.getTileEntity(pos) == null);
}
 
Example 4
Source Project: MiningGadgets   File: MiningGadget.java    License: MIT License 5 votes vote down vote up
public static boolean canMineBlock(ItemStack tool, World world, PlayerEntity player, BlockPos pos, BlockState state) {
    if (!player.isAllowEdit() || !world.isBlockModifiable(player, pos))
        return false;

    if(MinecraftForge.EVENT_BUS.post(new BlockEvent.BreakEvent(world, pos, state, player)))
        return false;

    return canMine(tool);
}
 
Example 5
@Override
public void onBlockDestroyed(ItemStack stack, World world, IBlockState state, BlockPos pos, EntityLivingBase entity) {
    if(entity instanceof EntityPlayer && !(entity instanceof FakePlayer)) {
        EntityPlayer entityPlayer = (EntityPlayer) entity;
        EnumFacing sideHit = ToolUtility.getSideHit(world, pos, entityPlayer);
        int damagePerBlockBreak = getToolDamagePerBlockBreak(stack);
        JackHammerMode jackHammerMode = MODE_SWITCH_BEHAVIOR.getModeFromItemStack(stack);
        EnumFacing horizontalFacing = entity.getHorizontalFacing();
        int xSizeExtend = (jackHammerMode.getHorizontalSize() - 1) / 2;
        int ySizeExtend = (jackHammerMode.getVerticalSize() - 1) / 2;
        for (int x = -xSizeExtend; x <= xSizeExtend; x++) {
            for (int y = -ySizeExtend; y <= ySizeExtend; y++) {
                //do not check center block - it's handled now
                if (x == 0 && y == 0) continue;
                BlockPos offsetPos = rotate(pos, x, y, sideHit, horizontalFacing);
                IBlockState blockState = world.getBlockState(offsetPos);
                if(world.isBlockModifiable(entityPlayer, offsetPos) &&
                    blockState.getBlock().canHarvestBlock(world, offsetPos, entityPlayer) &&
                    blockState.getPlayerRelativeBlockHardness(entityPlayer, world, offsetPos) > 0.0f &&
                    stack.canHarvestBlock(blockState)) {
                    GTUtility.harvestBlock(world, offsetPos, entityPlayer);
                    ((ToolMetaItem) stack.getItem()).damageItem(stack, damagePerBlockBreak, false);
                }
            }
        }
    }
}
 
Example 6
public ActionResult<ItemStack> tryPlaceFluid(@Nonnull World world, @Nonnull EntityPlayer player,
		@Nonnull EnumHand hand, ItemStack itemstack, FluidStack fluidStack) {
	RayTraceResult mop = this.rayTrace(world, player, false);
	ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, itemstack, mop);
	if (ret != null)
		return ret;
	if (mop == null || mop.typeOfHit != RayTraceResult.Type.BLOCK) {
		return ActionResult.newResult(EnumActionResult.PASS, itemstack);
	}
	BlockPos clickPos = mop.getBlockPos();
	if (world.isBlockModifiable(player, clickPos)) {
		BlockPos targetPos = clickPos.offset(mop.sideHit);
		if (player.canPlayerEdit(targetPos, mop.sideHit, itemstack)) {
			FluidActionResult result = FluidUtil.tryPlaceFluid(player, world, targetPos, itemstack, fluidStack);
			if (result.isSuccess() && !player.capabilities.isCreativeMode) {
				player.addStat(StatList.getObjectUseStats(this));
				itemstack.shrink(1);
				ItemStack emptyStack = new ItemStack(GTItems.testTube);
				if (itemstack.isEmpty()) {
					return ActionResult.newResult(EnumActionResult.SUCCESS, emptyStack);
				} else {
					ItemHandlerHelper.giveItemToPlayer(player, emptyStack);
					return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
				}
			}
		}
	}
	return ActionResult.newResult(EnumActionResult.FAIL, itemstack);
}
 
Example 7
private boolean shouldTryToPickUpBlock(World world, BlockPos pos, EnumFacing side, EntityPlayer player)
{
    if (world.isBlockModifiable(player, pos) == false)
    {
        return false;
    }

    if (player.capabilities.isCreativeMode)
    {
        return true;
    }

    IBlockState state = world.getBlockState(pos);

    if (BlackLists.isBlockAllowedForDolly(state) == false)
    {
        return false;
    }

    TileEntity te = world.getTileEntity(pos);

    // Unbreakable block, player not in creative mode
    if (state.getBlockHardness(world, pos) < 0)
    {
        if ((te instanceof TileEntityEnderUtilitiesInventory) == false)
        {
            return false;
        }

        TileEntityEnderUtilitiesInventory teinv = (TileEntityEnderUtilitiesInventory) te;
        // From the unbreakable blocks in this mod, only allow moving non-Creative mode storage blocks.
        return teinv.isCreative() == false && teinv.isMovableBy(player);
    }

    return te instanceof TileEntityEnderUtilitiesInventory || te instanceof IInventory ||
           (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side));
}
 
Example 8
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos,
        EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
    if (worldIn.getBlockState(pos).getBlock() == Blocks.ICE && worldIn.isBlockModifiable(player, pos))
    {
        if (worldIn.isRemote == false)
        {
            int num = 8;
            float velocity = 0.2f;
            // The Super variant (meta = 1) doesn't cause a block update
            int flag = player.getHeldItem(hand).getMetadata() == 1 ? 2 : 3;

            if (worldIn.provider.doesWaterVaporize() == false)
            {
                worldIn.setBlockState(pos, Blocks.WATER.getDefaultState(), flag);
            }
            else
            {
                worldIn.setBlockToAir(pos);
                num = 32;
                velocity = 0.4f;
            }

            Effects.spawnParticlesFromServer(worldIn.provider.getDimension(), pos.up(), EnumParticleTypes.SMOKE_LARGE, num, 0.5f, velocity);
            worldIn.playSound(null, player.getPosition(), SoundEvents.BLOCK_LAVA_EXTINGUISH, SoundCategory.MASTER, 0.8f, 1.0f);
        }

        return EnumActionResult.SUCCESS;
    }

    return EnumActionResult.PASS;
}
 
Example 9
Source Project: TofuCraftReload   File: ItemRiceSeed.java    License: MIT License 4 votes vote down vote up
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
{
    ItemStack itemstack = playerIn.getHeldItem(handIn);
    RayTraceResult raytraceresult = this.rayTrace(worldIn, playerIn, true);

    if (raytraceresult == null)
    {
        return new ActionResult<ItemStack>(EnumActionResult.PASS, itemstack);
    }
    else
    {
        if (raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK)
        {
            BlockPos blockpos = raytraceresult.getBlockPos();

            if (!worldIn.isBlockModifiable(playerIn, blockpos) || !playerIn.canPlayerEdit(blockpos.offset(raytraceresult.sideHit), raytraceresult.sideHit, itemstack))
            {
                return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
            }

            BlockPos blockpos1 = blockpos.up();
            IBlockState iblockstate = worldIn.getBlockState(blockpos);

            if (iblockstate.getMaterial() == Material.WATER && ((Integer)iblockstate.getValue(BlockLiquid.LEVEL)).intValue() == 0 && worldIn.isAirBlock(blockpos1))
            {
                // special case for handling block placement with water lilies
                net.minecraftforge.common.util.BlockSnapshot blocksnapshot = net.minecraftforge.common.util.BlockSnapshot.getBlockSnapshot(worldIn, blockpos1);
                worldIn.setBlockState(blockpos1, BlockLoader.RICECROP.getDefaultState());
                if (net.minecraftforge.event.ForgeEventFactory.onPlayerBlockPlace(playerIn, blocksnapshot, net.minecraft.util.EnumFacing.UP, handIn).isCanceled())
                {
                    blocksnapshot.restore(true, false);
                    return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
                }

                worldIn.setBlockState(blockpos1, BlockLoader.RICECROP.getDefaultState(), 11);

                if (playerIn instanceof EntityPlayerMP)
                {
                    CriteriaTriggers.PLACED_BLOCK.trigger((EntityPlayerMP)playerIn, blockpos1, itemstack);
                }

                if (!playerIn.capabilities.isCreativeMode)
                {
                    itemstack.shrink(1);
                }

                playerIn.addStat(StatList.getObjectUseStats(this));
                worldIn.playSound(playerIn, blockpos, SoundEvents.BLOCK_WATERLILY_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
                return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
            }
        }

        return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
    }
}
 
Example 10
Source Project: Sakura_mod   File: ItemRiceSeeds.java    License: MIT License 4 votes vote down vote up
@Override
  public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
      ItemStack itemstack = playerIn.getHeldItem(handIn);
      RayTraceResult raytraceresult = this.rayTrace(worldIn, playerIn, true);

      if (raytraceresult == null) {
          return new ActionResult<ItemStack>(EnumActionResult.PASS, itemstack);
      }
if (raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK) {
    BlockPos blockpos = raytraceresult.getBlockPos();

    if (!worldIn.isBlockModifiable(playerIn, blockpos) || !playerIn.canPlayerEdit(blockpos.offset(raytraceresult.sideHit), raytraceresult.sideHit, itemstack)) {
        return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
    }

    BlockPos blockpos1 = blockpos.up();
    IBlockState iblockstate = worldIn.getBlockState(blockpos);

    if (iblockstate.getMaterial() == Material.WATER && iblockstate.getValue(BlockLiquid.LEVEL).intValue() == 0 && worldIn.isAirBlock(blockpos1)) {
        // special case for handling block placement with water lilies
        net.minecraftforge.common.util.BlockSnapshot blocksnapshot = net.minecraftforge.common.util.BlockSnapshot.getBlockSnapshot(worldIn, blockpos1);
        worldIn.setBlockState(blockpos1, BlockLoader.RICECROP.getDefaultState());
        if (net.minecraftforge.event.ForgeEventFactory.onPlayerBlockPlace(playerIn, blocksnapshot, net.minecraft.util.EnumFacing.UP, handIn).isCanceled()) {
            blocksnapshot.restore(true, false);
            return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
        }

        worldIn.setBlockState(blockpos1, BlockLoader.RICECROP.getDefaultState(), 11);

        if (playerIn instanceof EntityPlayerMP) {
            CriteriaTriggers.PLACED_BLOCK.trigger((EntityPlayerMP) playerIn, blockpos1, itemstack);
        }

        if (!playerIn.capabilities.isCreativeMode) {
            itemstack.shrink(1);
        }

        playerIn.addStat(StatList.getObjectUseStats(this));
        worldIn.playSound(playerIn, blockpos, SoundEvents.BLOCK_WATERLILY_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
        return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
    }
}

return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
  }
 
Example 11
Source Project: Production-Line   File: ItemWaterHyacinth.java    License: MIT License 4 votes vote down vote up
@Nonnull
public ActionResult<ItemStack> onItemRightClick(@Nonnull ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
    RayTraceResult raytraceresult = this.rayTrace(worldIn, playerIn, true);

    if (raytraceresult == null) {
        return new ActionResult<>(EnumActionResult.PASS, itemStackIn);
    } else {
        if (raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK) {
            BlockPos blockpos = raytraceresult.getBlockPos();

            if (!worldIn.isBlockModifiable(playerIn, blockpos) || !playerIn.canPlayerEdit(blockpos.offset(raytraceresult.sideHit), raytraceresult.sideHit, itemStackIn)) {
                return new ActionResult<>(EnumActionResult.FAIL, itemStackIn);
            }

            BlockPos blockpos1 = blockpos.up();
            IBlockState iblockstate = worldIn.getBlockState(blockpos);

            if (iblockstate.getMaterial() == Material.WATER && iblockstate.getValue(BlockLiquid.LEVEL) == 0 && worldIn.isAirBlock(blockpos1)) {
                // special case for handling block placement with water lilies
                net.minecraftforge.common.util.BlockSnapshot blocksnapshot = net.minecraftforge.common.util.BlockSnapshot.getBlockSnapshot(worldIn, blockpos1);
                worldIn.setBlockState(blockpos1, PLBlocks.waterHyacinth.getDefaultState());
                if (net.minecraftforge.event.ForgeEventFactory.onPlayerBlockPlace(playerIn, blocksnapshot, net.minecraft.util.EnumFacing.UP).isCanceled()) {
                    blocksnapshot.restore(true, false);
                    return new ActionResult<>(EnumActionResult.FAIL, itemStackIn);
                }

                worldIn.setBlockState(blockpos1, PLBlocks.waterHyacinth.getDefaultState(), 11);

                if (!playerIn.capabilities.isCreativeMode) {
                    --itemStackIn.stackSize;
                }

                playerIn.addStat(StatList.getObjectUseStats(this));
                worldIn.playSound(playerIn, blockpos, SoundEvents.BLOCK_WATERLILY_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
                return new ActionResult<>(EnumActionResult.SUCCESS, itemStackIn);
            }
        }

        return new ActionResult<>(EnumActionResult.FAIL, itemStackIn);
    }
}
 
Example 12
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
{
    // If the player is standing inside a portal, then we try to activate the teleportation in onItemRightClick()
    if (EntityUtils.isEntityCollidingWithBlockSpace(world, player, Blocks.PORTAL))
    {
        return EnumActionResult.PASS;
    }

    Block block = world.getBlockState(pos).getBlock();

    // When right clicking on a Nether Portal block, shut down the portal
    if (block == Blocks.PORTAL)
    {
        if (world.isRemote == false && world.isBlockModifiable(player, pos))
        {
            world.destroyBlock(pos, false);
        }

        return EnumActionResult.SUCCESS;
    }

    if (world.isRemote)
    {
        return EnumActionResult.SUCCESS;
    }

    ItemStack stack = player.getHeldItem(hand);

    // When right clicking on Obsidian, try to light a Nether Portal
    if (block == Blocks.OBSIDIAN && world.isAirBlock(pos.offset(side)) && world.isBlockModifiable(player, pos.offset(side)) &&
        UtilItemModular.useEnderCharge(stack, ENDER_CHARGE_COST_PORTAL_ACTIVATION, true) &&
        Blocks.PORTAL.trySpawnPortal(world, pos.offset(side)))
    {
        UtilItemModular.useEnderCharge(stack, ENDER_CHARGE_COST_PORTAL_ACTIVATION, false);
        world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundEvents.ENTITY_BLAZE_SHOOT, SoundCategory.MASTER, 0.8f, 1.0f);

        return EnumActionResult.SUCCESS;
    }

    return EnumActionResult.PASS;
}
 
Example 13
private boolean tryPlaceDownBlock(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side)
{
    pos = pos.offset(side);

    if (this.isCarryingBlock(stack) == false || world.isBlockModifiable(player, pos) == false)
    {
        return false;
    }

    NBTTagCompound tagCarrying = NBTUtils.getCompoundTag(stack, "Carrying", false);
    String name = tagCarrying.getString("Block");
    int meta = tagCarrying.getByte("Meta");
    Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(name));

    try
    {
        if (block != null && block != Blocks.AIR && world.mayPlace(block, pos, false, side, player))
        {
            @SuppressWarnings("deprecation")
            IBlockState state = block.getStateFromMeta(meta);
            EnumFacing pickupFacing = EnumFacing.byIndex(tagCarrying.getByte("PickupFacing"));
            EnumFacing currentFacing = EntityUtils.getHorizontalLookingDirection(player);
            Rotation rotation = PositionUtils.getRotation(pickupFacing, currentFacing);
            state = state.withRotation(rotation);

            if (world.setBlockState(pos, state))
            {
                TileEntity te = world.getTileEntity(pos);

                if (te != null && tagCarrying.hasKey("te", Constants.NBT.TAG_COMPOUND))
                {
                    NBTTagCompound nbt = tagCarrying.getCompoundTag("te");
                    TileUtils.createAndAddTileEntity(world, pos, nbt, rotation, Mirror.NONE);
                }

                NBTUtils.removeCompoundTag(stack, null, "Carrying");
                return true;
            }
        }
    }
    catch (Exception e)
    {
        EnderUtilities.logger.warn("Failed to place down a block from the Dolly", e);
    }

    return false;
}
 
Example 14
/**
 * Checks if the player can edit the target block
 */
public boolean isTargetUsable(World world, BlockPos pos, EnumFacing side, EntityPlayer player, ItemStack stack)
{
    // Spawn safe zone checks etc.
    return world.isBlockModifiable(player, pos) && player.canPlayerEdit(pos, side, stack);
}
 
Example 15
public boolean placeBlockToPosition(ItemStack wandStack, World world, EntityPlayer player,
        BlockPos pos, EnumFacing side, IBlockState newState, int setBlockStateFlags, boolean requireItems, boolean dropItems)
{
    if (newState.getMaterial() == Material.AIR || world.getBlockState(pos) == newState ||
        (player.capabilities.isCreativeMode == false && world.isBlockModifiable(player, pos) == false))
    {
        return false;
    }

    boolean replace = WandOption.REPLACE_EXISTING.isEnabled(wandStack);
    boolean isAir = world.isAirBlock(pos);

    if (player.capabilities.isCreativeMode)
    {
        if (replace || isAir)
        {
            BlockUtils.setBlockStateWithPlaceSound(world, pos, newState, setBlockStateFlags);
            return true;
        }

        return false;
    }

    if (hasEnoughCharge(wandStack, player) == false || (isAir == false &&
        (replace == false || BlockUtils.canChangeBlock(world, pos, player, false, Configs.buildersWandMaxBlockHardness) == false)))
    {
        return false;
    }

    ItemStack targetStack = ItemStack.EMPTY;
    Block blockNew = newState.getBlock();

    if (requireItems)
    {
        // Simulate getting the item to build with
        targetStack = getAndConsumeBuildItem(wandStack, world, pos, newState, player, true);
    }

    if (requireItems == false || targetStack.isEmpty() == false)
    {
        // Break the existing block
        if (replace && isAir == false && player instanceof EntityPlayerMP)
        {
            if (dropItems)
            {
                BlockUtils.breakBlockAsPlayer(world, pos, (EntityPlayerMP) player, wandStack);

                // Couldn't break the existing block
                if (world.isAirBlock(pos) == false)
                {
                    return false;
                }
            }
            else
            {
                BlockUtils.setBlockToAirWithoutSpillingContents(world, pos, 2);
            }
        }

        // Check if we can place the block. If we don't require items, then we are moving an area
        // and in that case we always want to place the block.
        if (requireItems == false || BlockUtils.checkCanPlaceBlockAt(world, pos, side, blockNew))
        {
            if (requireItems)
            {
                // Actually consume the build item
                getAndConsumeBuildItem(wandStack, world, pos, newState, player, false);
            }

            BlockUtils.setBlockStateWithPlaceSound(world, pos, newState, setBlockStateFlags);

            if (player.capabilities.isCreativeMode == false)
            {
                UtilItemModular.useEnderCharge(wandStack, ENDER_CHARGE_COST, false);
            }

            return true;
        }
    }

    return false;
}
 
Example 16
public static boolean hasEditPermission(@Nonnull BlockPos pos, @Nonnull EntityPlayerMP player) {
	World world = player.getEntityWorld();

	if (!world.isBlockModifiable(player, pos)) return false;

	if (world.getBlockState(pos).getBlockHardness(world, pos) == -1.0F && !player.capabilities.isCreativeMode) return false;

	return player.isAllowEdit();
}