Solved Returning items with BlockPlaceEvent?

Discussion in 'Plugin Development' started by TheCrazyCrafter5, Jul 21, 2013.

Thread Status:
Not open for further replies.
  1. First off, here is a portion of my code; what's relavent.
    Code:java
    1. @EventHandler
    2. public void onBlockPlace(BlockPlaceEvent e){
    3. if (e.getPlayer().getItemInHand().getTypeId() == Material.REDSTONE_TORCH_ON.getId())
    4. e.setCancelled(true);
    5. }

    The code above stops the player from placing a redstone torch and it does it's job well. However, when it cancels the event it doesn't give the player their item back, which is the problem. Any ideas on how to give them their item back when the event cancels? Let's see if we can get this thread prefix to "Solved"!

    Feedback is greatly appreciated, thank you.
     
  2. Offline

    pope_on_dope

    the item is actually still in the player's inventory, but it is invisible. for it to reappear, the player's inventory needs to update some how. to avoid this, i think there's a .update() method which may apply to the player's inventory?

    EDIT: there is a deprecated method of player.updateInventory(). it still works, but there's no other replacement to avoid the deprecation. :L
     
    TheCrazyCrafter5 likes this.
  3. Offline

    Samthelord1

    player.updateInventory();

    EDIT: didn't see popes edit xD
     
    TheCrazyCrafter5 likes this.
  4. Thanks for your feedback.

    I just tested my plugin on my server again. After the torch disappeared from placing it and the event being cancelled, I clicked on the inventory spot where the torch was and it reappeared! You were right!

    EDIT: Ok, i'm gonna try the code and see if it works. Thanks for the help!
     
  5. Offline

    pope_on_dope

    try this and tell me what happens:
    Code:java
    1. @EventHandler
    2. public void onItemPlace(BlockPlaceEvent e){
    3. if (e.getPlayer().getItemInHand().getTypeId() == Material.REDSTONE_TORCH_ON.getId())
    4. e.setCancelled(true);
    5. e.getPlayer().updateInventory();
    6. //use it even though it's deprecated, it'll work
    7. }
     
  6. I'm not sure what deprecated means, could you explain? In eclipse I have a black line streaking through the code, but it's just a warning. Either way, the code worked!!! Thank very much! It is greatly appreciated. :D
     
    pope_on_dope likes this.
  7. Offline

    Samthelord1

    Warnings are nothing, don't worry. Eclipse can be a "idiot" sometimes. Aslong as it works :)
     
    TheCrazyCrafter5 likes this.
  8. Offline

    ZeusAllMighty11

    TheCrazyCrafter5

    A deprecated element/method is something which programmers mark to note to anyone also using it/it's api that the method is either not safe, or there is a better method to achieve so.

    I don't believe there is a better method in Bukkit, though
     
    TheCrazyCrafter5 likes this.
  9. Yeah, I guess that makes sense. Anyways, thanks for your feedback too Sam. I really appreciate it. :)

    Oh, ok. Thanks for explaining. Though, the "unsafe" aspect of that has me a bit worried. Anyways, thank you.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  10. Offline

    Samthelord1

    Don't worry, change the title to solved :)
     
    TheCrazyCrafter5 likes this.
  11. Ok. Oh yeah, I forgot! It was solved so quickly. :D

    This thread is officially solved, thanks to the awesome community! <3

    How do I change the title to solved, lol? D:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  12. Offline

    Samthelord1

    Edit post and change the prefix to solve of something
     
    TheCrazyCrafter5 likes this.
  13. I was trying to do that... :3

    I'm not seeing the option to change the title.... Hold on...


    Ok, I did it! I had to go to thread tools, then do it.

    Now it's "Officially Solved", lol. Once again, thank you guys for the help. :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page