Jump to content

PlayerInteractEvent - Is the player placing or using a block?


Delfil

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

"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?

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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?

 

 

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • i notice a change if i add the min and max ram in the line like this for example:    # Xmx and Xms set the maximum and minimum RAM usage, respectively. # They can take any number, followed by an M or a G. # M means Megabyte, G means Gigabyte. # For example, to set the maximum to 3GB: -Xmx3G # To set the minimum to 2.5GB: -Xms2500M # A good default for a modded server is 4GB. # Uncomment the next line to set it. -Xmx10240M -Xms8192M    i need to make more experiments but for now this apparently works.
    • Selamat datang di OLXTOTO, situs slot gacor terpanas yang sedang booming di industri perjudian online. Jika Anda mencari pengalaman bermain yang luar biasa, maka OLXTOTO adalah tempat yang tepat untuk Anda. Dapatkan sensasi tidak biasa dengan variasi slot online terlengkap dan peluang memenangkan jackpot slot maxwin yang sering. Di sini, Anda akan merasakan keseruan yang luar biasa dalam bermain judi slot. DAFTAR OLXTOTO DISINI LOGIN OLXTOTO DISINI AKUN PRO OLXTOTO DISINI   Slot Gacor untuk Sensasi Bermain Maksimal Olahraga cepat dan seru dengan slot gacor di OLXTOTO. Rasakan sensasi bermain maksimal dengan mesin slot yang memberikan kemenangan beruntun. Temukan keberuntungan Anda di antara berbagai pilihan slot gacor yang tersedia dan rasakan kegembiraan bermain judi slot yang tak terlupakan. Situs Slot Terpercaya dengan Pilihan Terlengkap OLXTOTO adalah situs slot terpercaya yang menawarkan pilihan terlengkap dalam perjudian online. Nikmati berbagai genre dan tema slot online yang menarik, dari slot klasik hingga slot video yang inovatif. Dipercaya oleh jutaan pemain, OLXTOTO memberikan pengalaman bermain yang aman dan terjamin.   Jackpot Slot Maxwin Sering Untuk Peluang Besar Di OLXTOTO, kami tidak hanya memberikan hadiah slot biasa, tapi juga memberikan kesempatan kepada pemain untuk memenangkan jackpot slot maxwin yang sering. Dengan demikian, Anda dapat meraih keberuntungan besar dan memenangkan ribuan rupiah sebagai hadiah jackpot slot maxwin kami. Jackpot slot maxwin merupakan peluang besar bagi para pemain judi slot untuk meraih keuntungan yang lebih besar. Dalam permainan kami, Anda tidak harus terpaku pada kemenangan biasa saja. Kami hadir dengan jackpot slot maxwin yang sering, sehingga Anda memiliki peluang yang lebih besar untuk meraih kemenangan besar dengan hadiah yang menggiurkan. Dalam permainan judi slot, pengalaman bermain bukan hanya tentang keseruan dan hiburan semata. Kami memahami bahwa para pemain juga menginginkan kesempatan untuk meraih keberuntungan besar. Oleh karena itu, OLXTOTO hadir dengan jackpot slot maxwin yang sering untuk memberikan peluang besar kepada para pemain kami. Peluang Besar Menang Jackpot Slot Maxwin Peluang menang jackpot slot maxwin di OLXTOTO sangatlah besar. Anda tidak perlu khawatir tentang batasan atau pembatasan dalam meraih jackpot tersebut. Kami ingin memberikan kesempatan kepada semua pemain kami untuk merasakan sensasi menang dalam jumlah yang luar biasa. Jackpot slot maxwin kami dibuka untuk semua pemain judi slot di OLXTOTO. Anda memiliki peluang yang sama dengan pemain lainnya untuk memenangkan hadiah jackpot yang besar. Kami percaya bahwa semua orang memiliki kesempatan untuk meraih keberuntungan besar, dan itulah mengapa kami menyediakan jackpot slot maxwin yang sering untuk memenuhi harapan dan keinginan Anda.  
    • LOGIN DAN DAFTAR DISINI SEKARANG !!!! Blacktogel adalah situs judi slot online yang menjadi pilihan banyak penggemar judi slot gacor di Indonesia. Dengan platform yang sangat user-friendly dan berbagai macam permainan slot yang tersedia, Blacktogel menjadi tempat yang tepat untuk penggemar judi slot online. Dalam artikel ini, kami akan membahas tentang Blacktogel dan keunggulan situs slot gacor online yang disediakan.  
    • Situs bandar slot online Gacor dengan bonus terbesar saat ini sedang menjadi sorotan para pemain judi online. Dengan persaingan yang semakin ketat dalam industri perjudian online, pemain mencari situs yang tidak hanya menawarkan permainan slot yang gacor (sering memberikan kemenangan), tetapi juga bonus terbesar yang bisa meningkatkan peluang menang. Daftar disini : https://gesit.io/googlegopek
    • Situs bandar slot online Gacor dengan bonus terbesar saat ini sedang menjadi sorotan para pemain judi online. Dengan persaingan yang semakin ketat dalam industri perjudian online, pemain mencari situs yang tidak hanya menawarkan permainan slot yang gacor (sering memberikan kemenangan), tetapi juga bonus terbesar yang bisa meningkatkan peluang menang. Daftar disini : https://gesit.io/googlegopek
  • Topics

×
×
  • Create New...

Important Information

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