Solved Code doesn't execute

Discussion in 'Spigot Plugin Development' started by cayote20, Mar 28, 2020.

Thread Status:
Not open for further replies.
  1. Hello! I handle event PlayerInteractEvent, the problem is described in the code by the comment (sorry for my English).
    Code (Text):
        public Events() {
            Bukkit.getPluginManager().registerEvents(this, Main.getInstance());
        }

        @EventHandler
        public void onClickBlock(PlayerInteractEvent event) {
            Block block = event.getClickedBlock();
            Player player = event.getPlayer();
            if(player.getInventory().getItemInMainHand().getType().equals(Material.STICK) && player.hasPermission("permission") && event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                switch(block.getType()) { // This line of code and the lines that are below aren't executed
                    case DARK_OAK_DOOR:
                    case ACACIA_DOOR:
                    case BIRCH_DOOR:
                    case IRON_TRAPDOOR:
                    case JUNGLE_DOOR:
                    case SPRUCE_DOOR:
                    case TRAP_DOOR:
                    case WOOD_DOOR:
                    case WOODEN_DOOR:
                    case IRON_DOOR:
                        event.setCancelled(true);
                        int random = new Random().nextInt(2);
                        if(random == 1) {
                            Location location = player.getLocation();
                            for(Player online : Bukkit.getOnlinePlayers()) {
                                online.playSound(location, Sound.BLOCK_ANVIL_FALL, 1f, 1f);
                            }
                            Openable door = (Openable) block;
                            door.setOpen(true);
                            player.sendMessage("");
                            break;
                        } else {
                            player.sendMessage("");
                        }
                }
            }
        }
     
  2. Your post is rather vague, so here's solutions for a variety of possible issues:

    1. `Main.getInstance()` is not properly referencing your main class. You may benefit from passing your main class into the constructor of events.
    2. The player who is interacting with the block doesn't have the `permission` permission as defined by your code.

    Try the above, I am confident one of them is your issue.
     
    • Funny Funny x 2
  3. Multiple things can be wrong here.
    The item in the main hand can be null and that throws a NPE or player testing doesn't have permission "permission". Print out each statement to see whats wrong.
    Also I suggest to use == on enums and to add a default case in the switch statement.
     
    • Agree Agree x 1
  4. 1. Main.getInstance has correct value
    2. The player who is interacting wi the block have the permission
     
  5. I added the default case and replaced "equals()" to "==" on enums, but nothing changed

    New code:
    Code (Text):
        public Events() {
            Bukkit.getPluginManager().registerEvents(this, Main.getInstance());
        }

        @EventHandler
        public void onClickBlock(PlayerInteractEvent event) {
            Block block = event.getClickedBlock();
            Player player = event.getPlayer();
            if(player.getInventory().getItemInMainHand().getType() == Material.STICK && player.hasPermission("Assassin.jackDoors") && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                switch(block.getType()) { // This line of code and the lines that are below aren't executed
                    case DARK_OAK_DOOR:
                    case ACACIA_DOOR:
                    case BIRCH_DOOR:
                    case IRON_TRAPDOOR:
                    case JUNGLE_DOOR:
                    case SPRUCE_DOOR:
                    case TRAP_DOOR:
                    case WOOD_DOOR:
                    case WOODEN_DOOR:
                    case IRON_DOOR:
                        event.setCancelled(true);
                        int random = new Random().nextInt(2);
                        if(random == 1) {
                            Location location = player.getLocation();
                            for(Player online : Bukkit.getOnlinePlayers()) {
                                online.playSound(location, Sound.BLOCK_ANVIL_FALL, 1f, 1f);
                            }
                            Openable door = (Openable) block;
                            door.setOpen(true);
                            player.sendMessage("");
                            break;
                        } else {
                            player.sendMessage("");
                        }
                    default:
                        break;
                }
            }
        }
     
  6. Your plugin may be throwing errors. Please add the nullptr checks as suggested.
     
  7.  
  8. UPD: I debugged the switch statement and I found out that it executes the code of the default case
     
  9. I tried, it doesn't trows NPE anyway
     
  10. I right click on the iron door with a stick in the main hand and it goes to the default case. Why?
     
  11. Also if you're doing some sort of "door lock/open permission" plugin its easier to have a static final Set of material (all doors you want) and check if the event material is in the set.
     
  12. No idea, easy solution though. Just print the blocktype to the console and repeat exactly what you did. Then you know which block type youre actually clicking.
     
  13. Code (Text):
    [21:12:11 ERROR]: Could not pass event PlayerInteractEvent to Assasin v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:235) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerInteractManager.a(PlayerInteractManager.java:458) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:953) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:37) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_241]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_12_R1.block.CraftBlock cannot be cast to org.bukkit.material.Openable
            at md.sintez.assassin.events.Events.onClickBlock(Events.java:47) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
            ... 17 more
    [21:12:11 ERROR]: Could not pass event PlayerInteractEvent to Assasin v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:235) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerInteractManager.a(PlayerInteractManager.java:458) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:953) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:37) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_241]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_12_R1.block.CraftBlock cannot be cast to org.bukkit.material.Openable
            at md.sintez.assassin.events.Events.onClickBlock(Events.java:47) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
            ... 17 more
    Do you have any ideas how to cast Block to Openable? :D
     
  14. Openable is in the material package. So try casting the material, not 100% sure though. Maybe you also imported org.bukkit.block.data.openable instead. I believe data.openable should also work, but don't cast the block, cast the blockdata to openable.
     

  15. Uhh it's not using the default case.

    You're casting a NMS block (somehow) to a bukkit Openable. Those two aren't compatible. Try debugging out what the class is of the getClickedBlock.
     
  16. It was using the default case due to my fault in one case, but now it gives me this exception
     
  17. It is an openable block, I understand that they are incompatible, so my question is the following: how from the block get an openable?
     
  18. Code (Text):
    Openable openable = (Openable) block.getBlockData()
    should work.
     
  19. What class is getClickedBlock (or your variable block)?
    This shouldn't be a CraftBlock as far as I know. Could you try using a newer version of spigot?
     
Thread Status:
Not open for further replies.