Java Code Examples for net.minecraft.block.Block#onBlockPlacedBy()

The following examples show how to use net.minecraft.block.Block#onBlockPlacedBy() . 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: DroneAIPlace.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Called to actually place the block, after the location is determined
 * and all permission checks have been made.
 *
 * @param stack The item stack that was used to place the block. This can be changed inside the method.
 * @param player The player who is placing the block. Can be null if the block is not being placed by a player.
 * @param side The side the player (or machine) right-clicked on.
 */
private boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata){
    Block block = Block.getBlockFromItem(stack.getItem());
    if(!world.setBlock(x, y, z, block, metadata, 3)) {
        return false;
    }

    block.onBlockPlacedBy(world, x, y, z, player, stack);
    block.onPostBlockPlaced(world, x, y, z, metadata);

    return true;
}
 
Example 2
Source File: ItemArcaneCake.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 4 votes vote down vote up
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World yWorld, int z, int par5, int par6, int par7, float par8, float par9, float par10) {
    Block block = yWorld.getBlock(z, par5, par6);

    if (block == Blocks.snow_layer && (yWorld.getBlockMetadata(z, par5, par6) & 7) < 1) {
        par7 = 1;
    } else if (block != Blocks.vine && block != Blocks.tallgrass && block != Blocks.deadbush) {
        if (par7 == 0) {
            --par5;
        }

        if (par7 == 1) {
            ++par5;
        }

        if (par7 == 2) {
            --par6;
        }

        if (par7 == 3) {
            ++par6;
        }

        if (par7 == 4) {
            --z;
        }

        if (par7 == 5) {
            ++z;
        }
    }

    if (!player.canPlayerEdit(z, par5, par6, par7, stack)) {
        return false;
    } else if (stack.stackSize == 0) {
        return false;
    } else {
        Block cake = ForbiddenBlocks.arcaneCake;
        if (yWorld.canPlaceEntityOnSide(cake, z, par5, par6, false, par7, (Entity) null, stack)) {
            int j1 = cake.onBlockPlaced(yWorld, z, par5, par6, par7, par8, par9, par10, 0);

            if (yWorld.setBlock(z, par5, par6, cake, j1, 3)) {
                if (yWorld.getBlock(z, par5, par6) == cake) {
                    cake.onBlockPlacedBy(yWorld, z, par5, par6, player, stack);
                    cake.onPostBlockPlaced(yWorld, z, par5, par6, j1);
                }

                yWorld.playSoundEffect((double) ((float) z + 0.5F), (double) ((float) par5 + 0.5F), (double) ((float) par6 + 0.5F), block.stepSound.soundName, (block.stepSound.getVolume() + 1.0F) / 2.0F, block.stepSound.getPitch() * 0.8F);
                --stack.stackSize;
            }
        }

        return true;
    }
}
 
Example 3
Source File: BackpackHelper.java    From WearableBackpacks with MIT License 4 votes vote down vote up
/** Attempts to place down a backpack, unequipping it
 *  if the specified entity is currently wearing it. */
public static boolean placeBackpack(World world, BlockPos pos, ItemStack stack,
                                    EntityLivingBase entity, boolean ignoreEntities) {
	
	EntityPlayer player = ((entity instanceof EntityPlayer) ? (EntityPlayer)entity : null);
	if ((player != null) && !player.canPlayerEdit(pos, EnumFacing.UP, stack))
		return false;
	// TODO: Should permission things be handled outside the API method?
	// TODO: For mobs, use mob griefing gamerule?
	
	Item item = stack.getItem();
	// Would use this instead, but gotta avoid depending on the rest of WearableBackpacks.
	//Block block = MiscUtils.getBlockFromItem(item);
	Block block = Block.REGISTRY.getObject(item.getRegistryName());
	if (ignoreEntities ? !block.canPlaceBlockAt(world, pos)
	                   : !world.mayPlace(block, pos, false, EnumFacing.UP, null))
		return false;
	
	// Actually go ahead and try to set the block in the world.
	IBlockState state = block.getStateForPlacement(
		world, pos, EnumFacing.UP, 0.5F, 0.5F, 0.5F,
		item.getMetadata(stack.getMetadata()), player, EnumHand.MAIN_HAND);
	if (!world.setBlockState(pos, state, 3) ||
	    (world.getBlockState(pos).getBlock() != block)) return false;
	block.onBlockPlacedBy(world, pos, state, entity, stack);
	
	double x = pos.getX() + 0.5;
	double y = pos.getY() + 0.5;
	double z = pos.getZ() + 0.5;
	SoundType sound = block.getSoundType(state, world, pos, entity);
	world.playSound(x, y, z, sound.getPlaceSound(), SoundCategory.BLOCKS,
	                (sound.getVolume() + 1.0F) / 2.0F, sound.getPitch() * 0.8F, false);

	// This is used to transfer the BlockEntityTag from Ctrl+pick-blocking to the world.
	boolean alreadyConfigured = ItemBlock.setTileEntityNBT(world, player, pos, stack);

	TileEntity tileEntity = world.getTileEntity(pos);
	if (tileEntity == null) return true;
	IBackpack placedBackpack = BackpackHelper.getBackpack(tileEntity);
	if (placedBackpack == null) return true;
	
	IBackpack carrierBackpack = BackpackHelper.getBackpack(entity);
	boolean isEquipped = ((carrierBackpack != null) && (carrierBackpack.getStack() == stack));
	
	ItemStack stackOrig = stack;
	// Create a copy of the stack with stackSize set to 1 and transfer it.
	stack = stack.copy();
	stack.setCount(1);
	if (stack.hasTagCompound() && stack.getTagCompound().hasKey("BlockEntityTag")) {
		stack.getTagCompound().removeTag("BlockEntityTag");
	}
	placedBackpack.setStack(stack);
	
	if (!alreadyConfigured) {
		// If the carrier had the backpack equipped, transfer data and unequip.
		if (isEquipped) {
			
			IBackpackType type = carrierBackpack.getType();
			IBackpackData data = carrierBackpack.getData();
			if ((data == null) && !world.isRemote) {
				LOG.error("Backpack data was null when placing down equipped backpack");
				data = type.createBackpackData(stack);
			}
			
			placedBackpack.setData(data);
			
			if (!world.isRemote)
				BackpackHelper.setEquippedBackpack(entity, ItemStack.EMPTY, null);
			
			type.onUnequip(entity, tileEntity, placedBackpack);
			
		// Otherwise create a fresh backpack data on the server.
		} else if (!world.isRemote) placedBackpack.setData(
			placedBackpack.getType().createBackpackData(stack));
	}
	
	// We only shrink the original stack here instead of earlier
	// as its information is still needed for other checks, and
	// shrinking it from 1 to 0 would effectively empty the stack.
	stackOrig.shrink(1);
	return true;
	
}