Jump to content

[1.10] Placing a block when a vanilla item is used


Corey

Recommended Posts

[cross-posted from Minecraft Forum]

 

I'm in the process of adding a firepit - to make it, you click on the ground with a stick. That part was easy enough. I'm using PlayerInteractEvent.RightClickBlock.

 

The only problem is, it doesn't account for blocks that can be activated, such as doors or containers. If you right click on a door, for example, it places the stick as a firepit. Is there a way to check and see if the block you're right clicking on can be activated?

 

Alternatively, is there a better solution to this than using the RightClickBlock event? I had to copy a chunk of code from ItemBlock.java, and I'd much rather use code that's already there, rather than duplicate it into my own code.

 

PS. This is my first Minecraft mod, but I'm a well-established software engineer.

 

Current code is as follows. It's a bit rough around the edges - I'm in hack mode.

 

  @SubscribeEvent
  public void onRightClickBlock(PlayerInteractEvent.RightClickBlock event) {
    if(event.getItemStack() != null) {
      Item item = event.getItemStack().getItem();
      
      if(item == Items.STICK) {
        if(event.getWorld().getBlockState(event.getPos()).getBlock() == ModBlocks.FIRE_PIT) {
          
        } else {
          //TODO: Don't do this if it's something you can interact with... door, container, etc.
          
          World world = event.getWorld();
          BlockPos pos = event.getPos();
          ItemStack stack = event.getItemStack();
          EntityPlayer player = event.getEntityPlayer();
          EnumFacing facing = event.getFace();
          
          IBlockState iblockstate = world.getBlockState(pos);
          Block block = iblockstate.getBlock();
          
          if(!block.isReplaceable(world, pos)) {
            pos = pos.offset(facing);
          }
          
          if(stack.stackSize != 0 && player.canPlayerEdit(pos, facing, stack) && world.canBlockBePlaced(ModBlocks.FIRE_PIT, pos, false, facing, null, stack)) {
            IBlockState iblockstate1 = ModBlocks.FIRE_PIT.onBlockPlaced(world, pos, facing, 0, 0, 0, 0, player);
            
            if(placeBlockAt(stack, player, world, pos, iblockstate1)) {
                SoundType soundtype = world.getBlockState(pos).getBlock().getSoundType(world.getBlockState(pos), world, pos, player);
                world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
                
                event.setUseItem(Result.ALLOW);
            }
          }
        }
      }
    }
  }
  
  public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, BlockPos pos, IBlockState newState) {
    if(!world.setBlockState(pos, newState, 3)) {
      return false;
    }
    
    IBlockState state = world.getBlockState(pos);
    if(state.getBlock() == newState.getBlock()) {
      ItemBlock.setTileEntityNBT(world, player, pos, stack);
      newState.getBlock().onBlockPlacedBy(world, pos, state, player, stack);
    }
    
    return true;
  }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.