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

Heyo everyone, got a quick question here. Is onBlockActivated() in a Block class, an alright place to store the events that happen when a user right clicks with an item onto it? Or should I be using an Item's onItemUse() instead? Keep in mind I am trying to do custom events for items like Leather, which I am unsure if you can add custom events to vanilla items.

 

Any help is great! Thanks :D

If you control the Block but not all the Items, it's best to handle the interactions in Block#onBlockActivated. If you control the Items but not the Block, it's best to handle the interactions in Item#onItemUse. If you control neither the Block nor the Items, you can handle the interactions using PlayerInteractEvent.RightClickBlock.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

  • Author

When I tried using Block#onBlockActivated, I was able to successfully use the item on the block, which took the item from the inventory and put it into the block. This works great, but once I implemented a way to take the item with a hand with no item being held, it never took the item out of the inventory even though the adding section of the method was firing.

 

12 hours ago, Choonster said:

If you control the Block but not all the Items, it's best to handle the interactions in Block#onBlockActivated. If you control the Items but not the Block, it's best to handle the interactions in Item#onItemUse. If you control neither the Block nor the Items, you can handle the interactions using PlayerInteractEvent.RightClickBlock.

@Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
        // do something
        if (!worldIn.isRemote) {
            System.out.print("Call\n");
            EnumFacing enumFacing = state.getValue(FACING);
            EnumPart enumPart = state.getValue(PART);
            TileEntityTanningRack tile = getTileEntity(worldIn, pos);

            if (state.getValue(PART) != EnumPart.LEFT) {
                BlockPos newPos = getDirectionOffset(enumFacing, EnumPart.LEFT, enumPart, pos);
                worldIn.getBlockState(newPos).getBlock().onBlockActivated(worldIn, newPos, worldIn.getBlockState(newPos), playerIn, hand, heldItem, side, hitX, hitY, hitZ);
                tile.markDirty();
                return true;
            }
            IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);

            boolean flag = false;
            if(heldItem != null){
                if(ModItems.ItemValidForTanningRack(heldItem.getItem())){
                    if(itemHandler.getStackInSlot(0) == null){
                        // remove item from player
                        ItemStack itemStack = heldItem.splitStack(1);
                        itemHandler.insertItem(0, itemStack, false);
                        flag = true;
                    }else if(!flag && itemHandler.getStackInSlot(0) != null){
                        // give player the item
                        ItemStack itemStack = itemHandler.extractItem(0, 1, false);
                        if(!playerIn.inventory.addItemStackToInventory(itemStack)){
                            // fail
                            itemHandler.insertItem(0, itemStack, false);
                            tile.markDirty();
                            return false;
                        }
                    }
                }else{
                    if(itemHandler.getStackInSlot(0) == null) return false;
                    // give player the item
                    ItemStack itemStack = itemHandler.extractItem(0, 1, false);
                    if(!playerIn.inventory.addItemStackToInventory(itemStack)){
                        // fail
                        itemHandler.insertItem(0, itemStack, false);
                        tile.markDirty();
                        return false;
                    }
                }
                tile.markDirty();
                return true;
            }
            else if(!flag && heldItem == null){
                if(itemHandler.getStackInSlot(0) == null) return false;
                // give player the item
                ItemStack itemStack = itemHandler.extractItem(0, 1, false);
                if(!playerIn.inventory.addItemStackToInventory(itemStack)){
                    // fail
                    itemHandler.insertItem(0, itemStack, false);
                    return false;
                }
            }
        }

        System.out.print("USED\n");

        return false;
    }

 

Can you clarify, exactly what doesn't happen as expected? Is a condition returning false/true when it shouldn't? Or something to do with adding to an inventory isn't working correctly?

  • Author
Just now, Jay Avery said:

Can you clarify, exactly what doesn't happen as expected? Is a condition returning false/true when it shouldn't? Or something to do with adding to an inventory isn't working correctly?

Well, basically nothing happens other than if I spam right click on the block, the item will flicker for a split second (it has a custom renderer to display the held item). Not sure what's going on. Also, is this method supposed to be calling 4 times at once?

6 hours ago, Nomnomab said:

Also, is this method supposed to be calling 4 times at once?

The method gets called on the client and the server. For each of those, it's likely that it also gets called once for each player hand (main hand and off hand) since the player could have the needed item in either hand. So, four times total.

  • Author
1 minute ago, Daeruin said:

The method gets called on the client and the server. For each of those, it's likely that it also gets called once for each player hand (main hand and off hand) since the player could have the needed item in either hand. So, four times total.

Ouch, that might be why it freaks out

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.