Posted June 9, 20178 yr 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
June 9, 20178 yr 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.
June 9, 20178 yr 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; }
June 9, 20178 yr 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?
June 9, 20178 yr 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?
June 10, 20178 yr 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.
June 10, 20178 yr 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.