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

The following examples show how to use net.minecraft.block.Block#canPlaceBlockAt() . 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: 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;
	
}