'placing' blocks on glass

Discussion in 'Plugin Development' started by sgavster, Dec 11, 2013.

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

    sgavster

    Hi!

    I was working on my plugin, and I want to make it so I can place a pressure plate on glass. I get this error though:
    error (open)

    11.12 13:52:15 [Server] INFO ... 15 more
    11.12 13:52:15 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_21]
    11.12 13:52:15 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_21]
    11.12 13:52:15 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_21]
    11.12 13:52:15 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_21]
    11.12 13:52:15 [Server] INFO at me.sgavster.MyPlugin.kits.KitDemo.onInteract(KitDemo.java:74) ~[?:?]
    11.12 13:52:15 [Server] INFO Caused by: java.lang.NullPointerException
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:453) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:541) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:651) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile:9) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:603) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:161) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477) [craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427) ~[craftbukkit_dev_2938.jar:git-Bukkit-1.6.4-R2.0-28-g373cbb7-b2945jnks]
    11.12 13:52:15 [Server] INFO org.bukkit.event.EventException
    11.12 13:52:15 [Server] INFO [13:52:15 ERROR]: Could not pass event PlayerInteractEvent to MyPlugin v0.0.1


    My code:

    Code:java
    1. else if(p.getItemInHand().getType().equals(Material.STONE_PLATE))
    2. {
    3. if(plugin.getDemo().contains(p.getName()))
    4. {
    5. plate.add(new Location(e.getClickedBlock().getWorld(), e.getClickedBlock().getLocation().getX(), e.getClickedBlock().getLocation().getY() + 1, e.getClickedBlock().getLocation().getZ()));//ERROR ON THIS LINE
    6. e.getClickedBlock().getRelative(BlockFace.UP).setType(Material.STONE_PLATE);
    7. if(p.getItemInHand().getAmount() == 1)
    8. {
    9. p.setItemInHand(null);
    10. }
    11. else
    12. {
    13. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    14. }
    15. p.sendMessage("You create a land mine! Punch it to remove it!");
    16. }
    17. }


    Thanks for any help!
     
  2. Offline

    L33m4n123

    Code:
    11.12 13:52:15 [Server] INFO at me.sgavster.MyPlugin.kits.KitDemo.onInteract(KitDemo.java:74) ~[?:?]
    11.12 13:52:15 [Server] INFO Caused by: java.lang.NullPointerException
    Whats line 74 in your code?
     
  3. Offline

    zeeveener

    Try setting the itemInHand to Material.AIR and see if that helps.

    Otherwise, there isn't enough information to go off of here.
     
  4. Offline

    sgavster

    L33m4n123 It's commented in the code..
    Code:java
    1. plate.add(new Location(e.getClickedBlock().getWorld(), e.getClickedBlock().getLocation().getX(), e.getClickedBlock().getLocation().getY() + 1, e.getClickedBlock().getLocation().getZ()));//ERROR ON THIS LINE

    zeeveener The error is commented in the code.. It should be plenty of information.
     
  5. Offline

    L33m4n123


    my bad didn't see that

    You're sure that plugin.getDemo() returns a list/Set/Map that actually contains anything?

    And check with something (be it debug messages) if the getClickedBlock returns a value and not null
     
  6. Offline

    zeeveener

    sgavster

    Check to make sure "e" isn't null.
    Check to make sure "e.getClickedBlock()" isn't null
    Check to make sure "e.getClickedBlock().getLocation()" isn't null

    Follow that system for every variable in that line. Simple debugging process and you should be able to narrow down your search field.
     
  7. Offline

    sgavster

    L33m4n123 Everything should work. I have this code:

    Code:java
    1. @EventHandler
    2. public void onPlace(BlockPlaceEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. Block b = e.getBlock();
    6. if(b.getType().equals(Material.STONE_PLATE))
    7. {
    8. if(plugin.getDemo().contains(p.getName()))
    9. {
    10. plate.add(b.getLocation());
    11. p.sendMessage("You create a land mine! Punch it to remove it!");
    12. }
    13. }
    14. }

    And it works great. The error isn't on the plugin.getDemo(...).

    zeeveener Why would I check if e isn't null? It's the event

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

    zeeveener

    sgavster

    Check everything that is referenced. Even if it seems redundant.
     
  9. Offline

    L33m4n123


    To see what exactly is null. And in this case maybe not if e.. but for the future if you are unsure



    So if the error is not in plugin.getDemo() it is in e.getClickedBlock().. you are most likely interacting with air. Which is handeled as null.. and get the location from that = NPE
     
  10. Offline

    sgavster

    L33m4n123 I'm getting a NPE on this now:

    Code:java
    1. if(e.getClickedBlock().getType() != null && e.getClickedBlock() != null)
     
  11. Offline

    L33m4n123

    Yes because e.getClickedBlock is already null.. so if you try to get the type you get an NPE

    Edit: So you need to switch it arround.. check first if e.getClickedBlock != null and then && e.getClickedBlock.getType()!=null
     
  12. Offline

    sgavster

    L33m4n123 That fixed the error but the block doesn't place :/
    Code:java
    1. if(e.getClickedBlock() != null && e.getClickedBlock().getType() != null)
    2. {
    3. Location u = new Location(e.getClickedBlock().getWorld(), e.getClickedBlock().getLocation().getX(), e.getClickedBlock().getLocation().getY() + 1, e.getClickedBlock().getLocation().getZ());
    4. plate.add(u);
    5. u.getBlock().setType(Material.STONE_PLATE);
    6. if(p.getItemInHand().getAmount() == 1)
    7. {
    8. p.setItemInHand(null);
    9. }
    10. else
    11. {
    12. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    13. }
    14. }
     
  13. Offline

    L33m4n123

    Well. Now you do not get the NPE.. But since e.getClickedBlock IS null [otherwise you would have not gotten the error] the code never reaches anything inside the if-clause
     
  14. Offline

    sgavster

    L33m4n123 Alright, how could I fix that?
     
  15. Offline

    L33m4n123

    You are certain that you are rightclicking a glass block?
     
  16. Offline

    sgavster

    L33m4n123 Yes, I am positive (well, it's stained, but I never added a check for what block it is.)
     
  17. Offline

    L33m4n123

    Hmm thats weird. Maybe just to be sure add an
    Code:java
    1. if(e.getAction == RIGHT_CLICK_BLOCK) {
    2. // your code
    3. }


    arround it. Since this is working for me without any issues
    Code:java
    1. @EventHandler
    2. public void interact(PlayerInteractEvent e) {
    3. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    4. e.getPlayer().sendMessage(e.getClickedBlock().toString());
    5. }
    6. }


    so it gets the clickedBlock without any issues
     
  18. Offline

    sgavster

    L33m4n123 That's basicly what I have:

    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    6. {
    7. if(p.getItemInHand().getType().equals(Material.FIREBALL))
    8. {
    9. if(plugin.getDemo().contains(p.getName()))
    10. {
    11. e.setCancelled(true);
    12. final Item i = p.getLocation().getWorld().dropItemNaturally(p.getLocation(), new ItemStack(Material.FIREBALL));
    13. i.setVelocity(new Vector(p.getVelocity().getX(), p.getVelocity().getY() + 4, p.getVelocity().getZ()));
    14. i.setVelocity(p.getLocation().getDirection());
    15. i.setPickupDelay(100000);
    16. if(p.getItemInHand().getAmount() == 1)
    17. {
    18. p.setItemInHand(null);
    19. }
    20. else
    21. {
    22. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    23. }
    24. Bukkit.getScheduler().runTaskLater(plugin, new Runnable()
    25. {
    26. public void run()
    27. {
    28. i.getWorld().createExplosion(i.getLocation(), 2, false);
    29. i.remove();
    30. }
    31. }, 100L);
    32. }
    33. }
    34. }
    35. else if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    36. {
    37. if(p.getItemInHand().getType().equals(Material.STONE_PLATE))
    38. {
    39. if(plugin.getDemo().contains(p.getName()))
    40. {
    41. if(e.getClickedBlock() != null && e.getClickedBlock().getType() != null)
    42. {
    43. Location u = new Location(e.getClickedBlock().getWorld(), e.getClickedBlock().getLocation().getX(), e.getClickedBlock().getLocation().getY() + 1, e.getClickedBlock().getLocation().getZ());
    44. plate.add(u);
    45. u.getBlock().setType(Material.STONE_PLATE);
    46. if(p.getItemInHand().getAmount() == 1)
    47. {
    48. p.setItemInHand(null);
    49. }
    50. else
    51. {
    52. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    53. }
    54. p.sendMessage("You create a land mine! Punch it to remove it!");
    55. }
    56. }
    57. }
    58. }
    59. else if(e.getAction().equals(Action.PHYSICAL))
    60. {
    61. if(e.getClickedBlock().getType().equals(Material.STONE_PLATE))
    62. {
    63. if(plate.contains(e.getClickedBlock().getLocation()))
    64. {
    65. if(!(plugin.getDemo().contains(p.getName())))
    66. {
    67. e.getClickedBlock().setType(Material.AIR);
    68. plate.remove(e.getClickedBlock().getLocation());
    69. e.getClickedBlock().getWorld().createExplosion(e.getClickedBlock().getLocation(), 2, false);
    70. }
    71. }
    72. }
    73. }
    74. }
     
  19. Offline

    zeeveener

    sgavster

    Remove the "e.getAction().equals(Action.RIGHT_CLICK_AIR) || "
     
  20. Offline

    sgavster

    zeeveener .. I need that?....
    If you look at the code, the code that I need help with has
    Code:java
    1. else if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
     
  21. Offline

    zeeveener


    Why? If you are placing a block on grass, you aren't clicking air.

    Edit: Nevermind...
     
  22. Offline

    sgavster

    zeeveener look at the edited post.. And actually look at the code :p
     
  23. Offline

    zeeveener

    sgavster

    Oh I see. You aren't getting called because it's already been called for the fireball method.
    The fact that you use an OR means that if ANY of the two are true, enter, otherwise don't. When you click the ground OR air, you are entering the fireball section. You never touch the "else if" for the problem code.
     
  24. Offline

    sgavster

    zeeveener It should be
    Code:java
    1. if(air || block) {
    2. do my fireball
    3. } else if(block) {
    4. dont do fireball do bomb
    5. }

    But, why is it not doing that? Is there a fix?
    EDIT: I see what you mean.. I'll just make a new event?
     
  25. Offline

    zeeveener

    sgavster

    As soon as the program enters the if statement, which is always in this case, it skips the else's. You can try merging the two blocks of code.

    Code:java
    1. if(air || block){
    2. if(fireball){}
    3. else if(other){}
    4. }


    You should check the Item in hand first, THEN check if it's via block or air or both. That is what I would do.
     
  26. Offline

    L33m4n123


    Just simply because it cannot be done the way you did it

    Because if you click Air OR a block it will do your fireball but under no circumstance goes in your else if statement. [Look at else-if statements and bollean logics if you don't get it].

    So you would need to check somewhere in your first if-statement if you rightclicked a block and that block is a glass block and you got a pressureplate in the hand do not do fireball but do your pressureplate
     
  27. Offline

    sgavster

    L33m4n123 zeeveener I made a new event, and it still doesn't work. I have no idea why..
    Code:java
    1. @EventHandler
    2. public void onCreate(PlayerInteractEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    6. {
    7. if(p.getItemInHand().getType().equals(Material.STONE_PLATE))
    8. {
    9. if(plugin.getDemo().contains(p.getName()))
    10. {
    11. if(e.getClickedBlock() != null && e.getClickedBlock().getType() != null)
    12. {
    13. Location u = new Location(e.getClickedBlock().getWorld(), e.getClickedBlock().getLocation().getX(), e.getClickedBlock().getLocation().getY() + 1, e.getClickedBlock().getLocation().getZ());
    14. plate.add(u);
    15. u.getBlock().setType(Material.STONE_PLATE);
    16. if(p.getItemInHand().getAmount() == 1)
    17. {
    18. p.setItemInHand(null);
    19. }
    20. else
    21. {
    22. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    23. }
    24. p.sendMessage("You create a land mine! Punch it to remove it!");
    25. }
    26. }
    27. }
    28. }
    29. }
     
  28. Offline

    zeeveener

    sgavster

    Set a test print at the beginning of each if() block. That will let you know where it stops.
    Example: Increment the value for each statement but use "System.out.println("TEST1");"
     
  29. Offline

    sgavster

    zeeveener For some reason, it doesn't trigger.. At all.. It doesn't even say "1" which is after if(e.getAction() blah blag)
    Yes, events are registered AND there is @EventHandler :/
     
  30. Offline

    zeeveener

    sgavster

    Can you show me where you registered the event?
    Also, try putting one of the test prints above the Player p line. See if the event is getting fired at all and it's not something with the getAction() method.
     
Thread Status:
Not open for further replies.

Share This Page