Solved How do i make a chest do something on open?

Discussion in 'Plugin Development' started by Chintzi, Nov 16, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Chintzi

    So basically what the titles says but what i am trying to do is:

    If a chest is named "LootChest" then if it's right click don't open the chest inventory menu you but break the chest instead of opening it Is there any way of doing this?
     
  2. Offline

    JordyPwner

    If i get it good you mean: When you right click a chest called "LootChest" you want it to not open and break?
     
    Chintzi likes this.
  3. Offline

    mythbusterma

    Chintzi

    You could listen for the interact event, then cancel it and break the block and cancel the event instead.
     
    Chintzi likes this.
  4. Offline

    Chintzi

    Exactly :D
     
  5. Offline

    JordyPwner

    Then do what @mythbuserma said :p
     
  6. Offline

    Chintzi

    But how? Like what event. do i use?

    Think i found it:
    Code:java
    1. if (event.getClickedBlock().equals(Material.CHEST)) {


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

    JordyPwner

    check if the clickedblock isnt null else you will get a NPE
     
  8. Offline

    Chintzi

    nvm it didnt work D:

    What do you mean? this is what i have so far:

    Code:java
    1. if (event.getClickedBlock().equals(Material.CHEST)) {
    2. player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1.0F, 2.0F);
    3. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 1));
    4. }


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

    JordyPwner

    Do: if(event.getClickedBlock() != null){
    Before checking if clicking a chest
     
  10. Offline

    Chintzi

    Didnt work D:

    Code:java
    1. if (event.getClickedBlock() != null) {
    2. if (event.getClickedBlock().equals(Material.CHEST)) {
    3. player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1.0F, 2.0F);
    4. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 1));
    5. }
    6. }
     
  11. Offline

    JordyPwner

    Tried debugging?
     
  12. Offline

    Chintzi

    ? whats debugging
     
  13. Offline

    JordyPwner

    send a message after each line. See What line get reached and what now like:

    Code:java
    1. if (event.getClickedBlock() != null) {
    2. System.out.println("Debug 1");
    3. if (event.getClickedBlock().equals(Material.CHEST)) {
    4. System.out.println("Debug 2");
    5. player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1.0F, 2.0F);
    6. System.out.println("Debug 3");
    7. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 1));
    8. System.out.println("Debug 4");
    9. }
    10. }
     
  14. Offline

    shohouku

    Code:java
    1. @EventHandler
    2. public void onInteractBlock(PlayerInteractEvent event) {
    3. if(event.getClickedBlock() != null)
    4. System.out.println(1);
    5. if (event.getClickedBlock().getType() != null) {
    6. System.out.println(2;
    7. if (event.getClickedBlock().getType().equals(Material.CHEST)) {
    8. System.out.println(3);
    9. player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1.0F, 2.0F);
    10. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 1));
    11. }
    12. }
    13. }
     
  15. Offline

    Chintzi

    Got this D:
    16.11 03:42:40 [Server] ERROR Could not pass event PlayerInteractEvent to SkieCraft v0.5.016.11 03:42:40[Server]INFO Debug 1

    That was for debug 1

    Hello?
     
  16. Offline

    mythbusterma

  17. Offline

    Chintzi

    Whats bump?

    Isa
    Isn't this what bukkit forums is for help. Besides if i knew how to do it i would be asking

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

    JordyPwner

    Full error please? Also remind to tagh me
     
  19. Offline

    Chintzi

    Whats Tagh mean? and heres the logs:

    Code:java
    1. 16.11 04:10:34 [Server] INFO Unknown command. Type "help" for help.
    2. 16.11 04:10:27 [Server] INFO Implements: org.bukkit.entity.Player
    3. 16.11 04:10:27 [Server] INFO Inherits: org.bukkit.craftbukkit.v1_7_R3.entity.CraftHumanEntity, org.bukkit.craftbukkit.v1_7_R3.entity.CraftLivingEntity, org.bukkit.craftbukkit.v1_7_R3.entity.CraftEntity
    4. 16.11 04:10:27 [Server] INFO Method getPlayer='§b§l{§4§lOwner§b§l}§b§4§4Spiky2006§r' type Name: org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer
    5. 16.11 04:10:27 [Server] INFO Implements: java.io.Serializable, java.lang.Comparable
    6. 16.11 04:10:27 [Server] INFO Method isCancelled='false' type Name: java.lang.Boolean
    7. 16.11 04:10:27 [Server] INFO Implements: java.io.Serializable, java.lang.Comparable
    8. 16.11 04:10:27 [Server] INFO Method isAsynchronous='false' type Name: java.lang.Boolean
    9. 16.11 04:10:27 [Server] INFO Inherits: org.bukkit.inventory.InventoryView
    10. 16.11 04:10:27 [Server] INFO Method getView='org.bukkit.craftbukkit.v1_7_R3.inventory.CraftInventoryView@17c2a057' type Name: org.bukkit.craftbukkit.v1_7_R3.inventory.CraftInventoryView
    11. 16.11 04:10:27 [Server] INFO Implements: org.bukkit.inventory.Inventory
    12. 16.11 04:10:27 [Server] INFO Method getInventory='org.bukkit.craftbukkit.v1_7_R3.inventory.CraftInventory@256154b5' type Name: org.bukkit.craftbukkit.v1_7_R3.inventory.CraftInventory
    13. 16.11 04:10:27 [Server] INFO Implements: java.util.List, java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable
    14. 16.11 04:10:27 [Server] INFO Inherits: java.util.AbstractList, java.util.AbstractCollection
    15. 16.11 04:10:27 [Server] INFO Method getViewers='[CraftPlayer{name=spiky2006}]' type Name: java.util.ArrayList
    16. 16.11 04:10:27 [Server] INFO Implements: org.bukkit.event.Cancellable
    17. 16.11 04:10:27 [Server] INFO Inherits: org.bukkit.event.inventory.InventoryEvent, org.bukkit.event.Event
    18. 16.11 04:10:27 [Server] INFO Event Name: org.bukkit.event.inventory.InventoryOpenEvent
    19. 16.11 04:10:27 [Server] INFO Inherits: java.lang.Enum
    20. 16.11 04:10:27 [Server] INFO Method getBlockFace='UP' type Name: org.bukkit.block.BlockFace
    21. 16.11 04:10:27 [Server] INFO Inherits: java.lang.Enum
    22. 16.11 04:10:27 [Server] INFO Method useItemInHand='DEFAULT' type Name: org.bukkit.event.Event$Result
    23. 16.11 04:10:27 [Server] INFO Inherits: java.lang.Enum
    24. 16.11 04:10:27 [Server] INFO Method useInteractedBlock='ALLOW' type Name: org.bukkit.event.Event$Result
    25. 16.11 04:10:27 [Server] INFO Implements: java.io.Serializable, java.lang.Comparable
    26. 16.11 04:10:27 [Server] INFO Method hasItem='false' type Name: java.lang.Boolean
    27. 16.11 04:10:27 [Server] INFO Implements: java.io.Serializable, java.lang.Comparable
    28. 16.11 04:10:27 [Server] INFO Method isCancelled='false' type Name: java.lang.Boolean
    29. 16.11 04:10:27 [Server] INFO Inherits: java.lang.Enum
    30. 16.11 04:10:27 [Server] INFO Method getMaterial='AIR' type Name: org.bukkit.Material
    31. 16.11 04:10:27 [Server] INFO Implements: java.io.Serializable, java.lang.Comparable
    32. 16.11 04:10:27 [Server] INFO Method hasBlock='true' type Name: java.lang.Boolean
    33. 16.11 04:10:27 [Server] INFO Implements: java.io.Serializable, java.lang.Comparable
    34. 16.11 04:10:27 [Server] INFO Method isBlockInHand='false' type Name: java.lang.Boolean
    35. 16.11 04:10:27 [Server] INFO Implements: org.bukkit.block.Block
    36. 16.11 04:10:27 [Server] INFO Method getClickedBlock='CraftBlock{chunk=CraftChunk{x=-1z=0},x=-2,y=90,z=4,type=CHEST,data=3}' type Name: org.bukkit.craftbukkit.v1_7_R3.block.CraftBlock
    37. 16.11 04:10:27 [Server] INFO Inherits: java.lang.Enum
    38. 16.11 04:10:27 [Server] INFO Method getAction='RIGHT_CLICK_BLOCK' type Name: org.bukkit.event.block.Action
    39. 16.11 04:10:27 [Server] INFO Implements: java.io.Serializable, java.lang.Comparable
    40. 16.11 04:10:27 [Server] INFO Method isAsynchronous='false' type Name: java.lang.Boolean
    41. 16.11 04:10:27 [Server] INFO Implements: org.bukkit.entity.Player
    42. 16.11 04:10:27 [Server] INFO Inherits: org.bukkit.craftbukkit.v1_7_R3.entity.CraftHumanEntity, org.bukkit.craftbukkit.v1_7_R3.entity.CraftLivingEntity, org.bukkit.craftbukkit.v1_7_R3.entity.CraftEntity
    43. 16.11 04:10:27 [Server] INFO Method getPlayer='§b§l{§4§lOwner§b§l}§b§4§4Spiky2006§r' type Name: org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer
    44. 16.11 04:10:27 [Server] INFO Implements: org.bukkit.event.Cancellable
    45. 16.11 04:10:27 [Server] INFO Inherits: org.bukkit.event.player.PlayerEvent, org.bukkit.event.Event
    46. 16.11 04:10:27 [Server] INFO Event Name: org.bukkit.event.player.PlayerInteractEvent
    47. 16.11 04:10:27 [Server] INFO ... 15 more
    48. 16.11 04:10:27 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    49. 16.11 04:10:27 [Server] INFO at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_55]
    50. 16.11 04:10:27 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_55]
    51. 16.11 04:10:27 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_55]
    52. 16.11 04:10:27 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_55]
    53. 16.11 04:10:27 [Server] INFO at net.wrightnz.minecraft.skiecraft.listeners.PlayerListener.onPlayerInteract(PlayerListener.java:91) ~[?:?]
    54. 16.11 04:10:27 [Server] INFO Caused by: java.lang.NullPointerException
    55. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    56. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    57. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    58. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    59. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    60. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    61. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    62. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.handle(SourceFile:9) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    63. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.a(SourceFile:60) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    64. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:629) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    65. 16.11 04:10:27 [Server] INFO at net.minecraft.server.v1_7_R3.PlayerInteractManager.interact(PlayerInteractManager.java:374) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    66. 16.11 04:10:27 [Server] INFO at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:226) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    67. 16.11 04:10:27 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    68. 16.11 04:10:27 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    69. 16.11 04:10:27 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    70. 16.11 04:10:27 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    71. 16.11 04:10:27 [Server] INFO org.bukkit.event.EventException
    72. 16.11 04:10:27 [Server] ERROR Could not pass event PlayerInteractEvent to SkieCraft v0.5.0
    73. 16.11 04:10:27 [Server] INFO Debug 1
     
  20. Use == for checking the clicked block, not .equals()
     
  21. Offline

    mythbusterma

    Chintzi

    What's with all that reflection stuff?

    Furthermore, these are not forums for teaching Java. These are forums for helping with the peculiarities in Bukkit. You can read the Bukkit plugin tutorial and the very first line is you should have a through understanding of Java before starting.
     
  22. Offline

    Chintzi

    Kk cheers :D

    Oli_Gig
    When i do the == it says Incomparable types Block & Material, so how do i turn Material.CHEST into a Block?

    Code:
    Code:java
    1. if (event.getClickedBlock() == Material.CHEST) {


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  23. Online

    timtower Administrator Administrator Moderator

    Removed a discussion. Feel free to dislike each other. But keep it nice and to yourself please.

    Chintzi Please use the edit button instead of double posting.
     
  24. Offline

    mythbusterma

  25. Offline

    Chintzi

    Fair enough, thanks
     
  26. Offline

    Gingerbreadman

    Chintzi Try this
    Code:java
    1. if(event.getClickedBlock().getType() == Material.CHEST) {
     
  27. Offline

    Chintzi

  28. Offline

    shohouku

    Did you register your events?

    Code:java
    1.  
    2. public class ChestInteract extends JavaPlugin implements Listener {
    3. @Override
    4. public void onEnable() {
    5. getServer().getPluginManager().registerEvents(this, this);
    6. Bukkit.getServer().getPluginManager().registerEvents(new PlayerStats(), this);
    7. PluginDescriptionFile pdfFile = this.getDescription();
    8. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been Enabled!");
    9. }
    10. @Override
    11. public void onDisable() {
    12. PluginDescriptionFile pdfFile = this.getDescription();
    13. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been Disabled!");
    14. }
    15. @EventHandler
    16. public void onInteractBlock(PlayerInteractEvent event) {
    17. if(event.getClickedBlock() != null)
    18. System.out.println(1);
    19. if (event.getClickedBlock().getType() != null) {
    20. System.out.println(2;
    21. if (event.getClickedBlock().getType().equals(Material.CHEST)) {
    22. System.out.println(3);
    23. player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1.0F, 2.0F);
    24. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 1));
    25. }
    26. }
    27. }
    28.  
    29.  
     
  29. Offline

    mythbusterma

    shohouku

    Why did you include that code......? And the color codes...?
     
  30. Offline

    Chintzi

    Thank you! Thank you! Thank you! it worked :D but one other thing how do i set it to be a specific chest with a name so like if the chest was called LootChest then do it
     
Thread Status:
Not open for further replies.

Share This Page