Jump to content

PlayerInteractEvent - Is the player placing or using a block?


Recommended Posts

Posted

Hi,

I want to detect if the event is placing blocks or using the clicked block (like opening a chest), but I can't find out how to do this.

I'm now using the PlayerInteractEvent where I check for

(e.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK)

But I can't find out if the player is placing a block or using a block.

Any idea how to do this?

 

(Im using Forge 1.7.2)

Edit: this part of the mod is serverside only.

 

I'll explain this a bit more.

What I basically want, is to know if not-cancelling this event results in a block placement or an interaction with a block (like opening the gui from a chest or crafting table).

 

Maybe I can make my question more clear this way:

Lets say I want to implement the following method:

/**
* given that:
*     event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK
*
* returns:
*     true if the event results in a block placement.
*     false if the event does not results in a block placement, but in results in a block interaction.
**/
boolean doesEventCauseBlockPlacement(PlayerInteractEvent event);

 

Then I want to ask, how do I implement this?

Posted

Think about it a bit.  You know the player involved.  Hint - look for what is in his hand.

 

As far as what you are trying to do, its a little unclear.  You want to place a block by right clicking it like normal or you want multiple blocks to be placed.  No need to do this from events, do it from your custom block.

 

I doubt that is what you are meaning though.  Explain more.

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

Thanks for your responses.

 

For example, I want to cancel all block placements in a certain area, but I still want players inside this area to be able to use chests, or the inverted case.

 

When I use:

event.useBlock = Result.DENY;

The player can't place blocks any more, but the player also can't use blocks like chests and crafting tables any more.

I want to be able to disable or block placing, or inventory opening or both.

 

When I look for the item in the players hand, I've to check if it is a block or an item what can place a block, if the block the player is clicking isn't a block with a GUI and if the player isn't shift clicking the block. And this for each possible mod.. There must be an easier way.

Posted

If you were just doing this with a custom block then you could just program it in the block itself.

 

However that does not seem to be the case.

 

You will need to check the event to see what was clicked and what was in hand and decide based upon areas.  Its not that hard.  Maybe 6 lines of code and you are done.

 

You will need to have your areas defined somehow.  That could be a bit more invovled.

 

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

Thanks again for your response,

 

I already got the zones implemented, so that wont be a problem.

 

If you were just doing this with a custom block then you could just program it in the block itself.

 

However that does not seem to be the case.

This isn't the case, indeed. I want to be able to block all blocks, also from Minecraft and other mods.

 

You will need to check the event to see what was clicked and what was in hand and decide based upon areas.  Its not that hard.  Maybe 6 lines of code and you are done.

I guess you are suggesting

event.entityPlayer.getItemInUse()

But I forgot to tell, this is a server side mod, and this method is a client side method.

Do you got a suggestion to find the current equipped item?

 

I also wonder about how you exclude all items and blocks who can be used to place blocks, could you point me to the methods you got in mind to implement this?

 

I tried something like:

if (Block.getBlockById(Item.getIdFromItem(p.ENTITY.getItemInUse().getItem()))!=null){
System.out.println("This is a block?");
} else {
System.out.println("This is an item?");
// Check here if this is an item who can place a block by right clicking.
}

But this won't work on the server.

Posted

Use the following

 

ItemStack itemstack = player.inventory.getCurrentItem();

 

you will need to do a null check though.

 

 

Your statements about client/server appear to be wrong.  Probably a Syntax Statement.

 

Why are you using BlockID?

 

I can't tell what you are asking

 

  " I also wonder about how you exclude all items and blocks who can be used to place blocks, could you point me to the methods you got in mind to implement this? "

 

Try saying it a different way.  There are no items used to place blocks.  All Blocks are used to place blocks.

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

"Think about it a bit.  You know the player involved.  Hint - look for what is in his hand."

I think I went off track here, my apology's. I'm now trying to write a function to find out if the current selected itemstack is able to place a block, which isn't relevant to the original question.

 

The question was:

"I want to handle placing blocks and using a block with right clicking (like opening a chest) differently, but I can't find out how to do this."

I'll explain this a bit more.

What I basically want, is to know if not-cancelling this event results in a block placement or an interaction with a block (like opening the gui from a chest or crafting table).

 

Maybe I can make my question more clear this way:

Lets say I want to implement the following method:

/**
* given that:
*     event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK
*
* returns:
*     true if the event results in a block placement.
*     false if the event does not results in a block placement, but in results in a block interaction.
**/
boolean doesEventCauseBlockPlacement(PlayerInteractEvent event);

 

Then I want to ask, how do I implement this?

Posted

I think you want to selectively cancel the event (assuming it is cancelable).  In other words, if you see that it is something you want to prevent you cancel it, otherwise you don't (and the vanilla processing will continue as expected). 

 

So the key is the test for what you want.  Are you having trouble with detecting just the action that you care about?

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Are you having trouble with detecting just the action that you care about?

 

y, Im having trouble with that part. (See the post before yours.)

I want to detect the difference between placing blocks and using a block within the PlayerInteractEvent event.

Posted

I haven't done it this way,but I traced out the code and I think it will work.  You will have to test it out.

 

If a block is in the players hand and you want to deny them the ability to place it, set the result of the event to false or:

 

"event.useItem = Result.DENY;"

 

Don't cancel the event.  That should block placement of the block only but allow the GUI to open.

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

I haven't tried

event.useItem = Result.DENY;

yet, because I was expecting it would be done with event.useBlock, since it is about blocks :$

This works indeed for only cancelling block placement, and leaving events for opening gui's alone.

However this isn't a way to detect the difference between placing blocks and using blocks, this did solve my problem, so thank you very much :)

 

A way to detect the difference between block placing and block using would still help me out.

Posted

I haven't tried

event.useItem = Result.DENY;

yet, because I was expecting it would be done with event.useBlock, since it is about blocks :$

This works indeed for only cancelling block placement, and leaving events for opening gui's alone.

However this isn't a way to detect the difference between placing blocks and using blocks, this did solve my problem, so thank you very much :)

 

A way to detect the difference between block placing and block using would still help me out.

 

I think the first thing is to get a good understanding of how placing really works.  You might want to just trace the operation with some console statements, or read carefully through the code.  But I think (not certain, you'll have to investigate) that there is the action field in the event that has possibility of being value RIGHT_CLICK_AIR.  Is it possible that that is the action posted when there is a block place event?

 

It seems to me that RIGHT_CLICK_AIR is the action to look for, as you can see it is posted in the processPlayerBlockPlacement() method of the NethandlerPlayServer.

 

Anyway, just a thought.  RIGHT_CLICK_AIR seems to have relationship to the moment of placing a block.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

While it seems strange that RIGHT_CLICK_AIR causes block placement, because right clicking in the air normally won't place any blocks, I tried it out:

// Test in method who handles the PlayerInteractEvent:
if (e.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR){
System.out.println("RIGHT_CLICK_AIR");
e.setCanceled(true);
}

 

And it seems like placing blocks on blocks wont post any PlayerInteractEvent with the value action set to PlayerInteractEvent.Action.RIGHT_CLICK_AIR. However, right clicking without selecting the air does.

  • 2 weeks later...
Posted

Any more thoughts on this?

 

I see a number of forum suggestions to use PlayerInteractEvent.action == RIGHT_CLICK_BLOCK to detect block placing, however, if the client is standing too close to the block placement site, RIGHT_CLICK_BLOCK will still trigger without any block being created.

 

Any way to access Block.onPostBlockPlaced?

 

 

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.