Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

[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;
  }

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.