I have an ItemBlock that I want to be able to right click another block with and do some things if the block is a certain type of block. I have everything working except for one thing: my code in the ItemBlock's onItemUse() method only seems to be running when I place the ItemBlock, not just any time I right click a block with it. Here is my class file:
As you can see, I have a sysout in the onItemUse() method. Yet I only see the sysout when I place the block (it being an ItemBlock). For example right clicking a lit torch with the ItemBlock does not work if I right clicked the torch on the top face, as my custom ItemBlock cannot be placed there. I don't think it makes much sense that onItemUse() is dependent on if the block was placed, so I'm wondering if anyone know why this might be happening.
I can't use that one, I just had to make an entire thread relating to this. onItemUse() should work when right clicking a block, no matter if you can actually place that block there, correct? I need to use that method because it gives the position of the block that was right clicked, which is essential for what I'm trying to do.
I can't use that one, I just had to make an entire thread relating to this. onItemUse() should work when right clicking a block, no matter if you can actually place that block there, correct? I need to use that method because it gives the position of the block that was right clicked, which is essential for what I'm trying to do.
You could use the player's look vector to determine the position, because onItemUse is only called when placing the block due to the enumActionResult in ItemBlock#getItemUseAction.
EDIT: As Choonster said in your first thread: ItemBlock#onItemUse is only called when the block was placed successfully.
You could use the player's look vector to determine the position, because onItemUse is only called when placing the block due to the enumActionResult in ItemBlock#getItemUseAction.
EDIT: As Choonster said in your first thread: ItemBlock#onItemUse is only called when the block was placed successfully.
Agh. Thanks for the explanation, it's just I'm very confused and have switched between onItemUse() and onItemRightClick() many times. At this point I would really like a working code chunk, which I can understand once it works, rather than trying to make the method before understanding.
What I have been trying to do it this:
- I have an in world block that extends BlockTorch. It may be placed on a wall or floor.
- I have an ItemBlock that places a Block that extends BlockTorch as it's in world Block. Basically a torch ItemBlock.
- When I right click on the BlockTorch with the ItemBlock, I need to cancel the placement of the Block and do some things with the ItemBlock ItemStack. This can be pseudocode for now, as I already have the working code for this scetion.
- This has to be done in the ItemBlock class, as the BlockTorch may not be the same block every time, and needs to support Blocks that I do not have the code to.
I hope this made sense, and again, this doesn't have to be fully realized code, and I recognize I'm asking for a lot. But if someone could explain what methods I need to override, what methods I need to use, and the basic structure of the code, it would be very helpful and I will understand better. Currently I've been trying to get this to work, but all these similar but different methods are confusing me.
I do not know if this works flawlessly for blocks that do not have a selection box -- specifically air, fire, etc. In your case, detecting clicking on fire is very important. You may want to set hitPos to rtr.getBlockPos().offset(rtr.sideHit, 1) and see how that works.
Rollback Post to RevisionRollBack
Click this banner for a list of illegal mod distributors -- only download from legal sites!
I do not know if this works flawlessly for blocks that do not have a selection box -- specifically air, fire, etc. In your case, detecting clicking on fire is very important. You may want to set hitPos to rtr.getBlockPos().offset(rtr.sideHit, 1) and see how that works.
Thank you, this is close to what I expected as a solution. Unfortunately, my current code:
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
System.out.println("Called onItemRightClick()");
RayTraceResult rtr = this.rayTrace(worldIn, playerIn, true);
if (rtr == null) {
return ActionResult.newResult(EnumActionResult.FAIL, itemStackIn);
} else if (rtr.typeOfHit == RayTraceResult.Type.BLOCK) {
// Get the registry name of the right clicked block
BlockPos hitPos = rtr.getBlockPos();
ResourceLocation rl = worldIn.getBlockState(hitPos).getBlock().getRegistryName();
// If the registry name matches on in the config
if (ModConfig.inWorldLightItems.contains(rl)) {
// If sneaking, just place the block normally
if (!playerIn.isSneaking()) {
lightTorch(playerIn, hand);
return ActionResult.newResult(EnumActionResult.PASS, itemStackIn);
}
}
}
return ActionResult.newResult(EnumActionResult.PASS, itemStackIn);
}
Has the opposite problem; now it only works when the block place fails, before it only worked if the block place succeeded. Are there any other methods I need to override?
Ah thank you, this is what I had been looking for. This method is called before the other one, so I have to make sure the other is called if my conditions are met. I had to make a few changes for my mod, but it works! I'm glad to finally have this issue solved, thanks!
I have an ItemBlock that I want to be able to right click another block with and do some things if the block is a certain type of block. I have everything working except for one thing: my code in the ItemBlock's onItemUse() method only seems to be running when I place the ItemBlock, not just any time I right click a block with it. Here is my class file:
https://gist.github.com/WolfieWaffle/33508cc809d283b666227b41e10b567e
As you can see, I have a sysout in the onItemUse() method. Yet I only see the sysout when I place the block (it being an ItemBlock). For example right clicking a lit torch with the ItemBlock does not work if I right clicked the torch on the top face, as my custom ItemBlock cannot be placed there. I don't think it makes much sense that onItemUse() is dependent on if the block was placed, so I'm wondering if anyone know why this might be happening.
use the Item#onItemRightClick method, it is also fired when right clicking air.
I can't use that one, I just had to make an entire thread relating to this. onItemUse() should work when right clicking a block, no matter if you can actually place that block there, correct? I need to use that method because it gives the position of the block that was right clicked, which is essential for what I'm trying to do.
You could use the player's look vector to determine the position, because onItemUse is only called when placing the block due to the enumActionResult in ItemBlock#getItemUseAction.
EDIT: As Choonster said in your first thread: ItemBlock#onItemUse is only called when the block was placed successfully.
Agh. Thanks for the explanation, it's just I'm very confused and have switched between onItemUse() and onItemRightClick() many times. At this point I would really like a working code chunk, which I can understand once it works, rather than trying to make the method before understanding.
What I have been trying to do it this:
- I have an in world block that extends BlockTorch. It may be placed on a wall or floor.
- I have an ItemBlock that places a Block that extends BlockTorch as it's in world Block. Basically a torch ItemBlock.
- When I right click on the BlockTorch with the ItemBlock, I need to cancel the placement of the Block and do some things with the ItemBlock ItemStack. This can be pseudocode for now, as I already have the working code for this scetion.
- This has to be done in the ItemBlock class, as the BlockTorch may not be the same block every time, and needs to support Blocks that I do not have the code to.
I hope this made sense, and again, this doesn't have to be fully realized code, and I recognize I'm asking for a lot. But if someone could explain what methods I need to override, what methods I need to use, and the basic structure of the code, it would be very helpful and I will understand better. Currently I've been trying to get this to work, but all these similar but different methods are confusing me.
Maybe use the PlayerInteractEvent, it is fired regardless of the itemblock placing it's block in the world or not.
Is that part of the Player class or Item class?
The Item class has a method for getting the player's look vector. Call this from onItemRightClick before returning an ActionResult.
Then check that it hit a block like this:
I do not know if this works flawlessly for blocks that do not have a selection box -- specifically air, fire, etc. In your case, detecting clicking on fire is very important. You may want to set hitPos to rtr.getBlockPos().offset(rtr.sideHit, 1) and see how that works.
Thank you, this is close to what I expected as a solution. Unfortunately, my current code:
Has the opposite problem; now it only works when the block place fails, before it only worked if the block place succeeded. Are there any other methods I need to override?
Well... you could have one call the other. I don't think it would cause any looping or anything terrible.
Ah thank you, this is what I had been looking for. This method is called before the other one, so I have to make sure the other is called if my conditions are met. I had to make a few changes for my mod, but it works! I'm glad to finally have this issue solved, thanks!