OpenInvetory problem

Discussion in 'Spigot Plugin Development' started by SyncronicK, May 20, 2017.

Thread Status:
Not open for further replies.
  1. Hello, I have a problem with this code:

    Code (Text):

        @EventHandler
        public void onPunch(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if(player.getItemInHand().getType() != Material.DIAMOND_BLOCK && (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                openGUI(player);
                player.playSound(player.getLocation(), Sound.LEVEL_UP, 10, 1);
    }
     
    I want to open the inventory only if i'm click on the diamond block, not on only block.
     
  2. You want it to only open if you click the diamond block

    but you're using != (not equals)
    I see a problem
     
    • Agree Agree x 1
  3. Now the inventory open's only when I place the block.
     
  4. try to use player.openInventory(invname)
     
  5. No result man...
     
  6. That is because you are checking the item in the players hand and not the block they are interacting with. Instead of checking the ItemInhand() use e.getClickedBlock() and that will return the block they are interacting with :) Then check if this block is a diamond block and do what you need to do
     
  7. Right clicking with a block seems to imply placing it, wouldn't you agree?
     
  8. I want to open only on right click it if it's placed.
     
  9. I solved your problem above, take a look :p
     
  10. I get an error on console:
    Code (Text):

    Could not pass event PlayerInteractEvent to ProtectionStonesMenu v1.0.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:227) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:194) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:190) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:694) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer cannot be cast to org.bukkit.event.player.PlayerInteractEvent
            at com.protectionstones.menu.Main.onPunch(Main.java:91) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-d2856ae-8f0f4ed]
            ... 18 more
     
    PlayerInteractEvent:
    Code (Text):

        @EventHandler
        public void onPunch(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if((((PlayerInteractEvent) player).getClickedBlock().getType() == Material.GOLD_BLOCK && (event.getAction() == Action.RIGHT_CLICK_BLOCK))) {
                openGUI(player);
                player.playSound(player.getLocation(), Sound.LEVEL_UP, 10, 1);
     
     
  11. Code (Text):
    @EventHandler
        public void onPunch(PlayerInteractEvent e){
            Player p = e.getPlayer();
            Action a = e.getAction();
            if (a == Action.RIGHT_CLICK_BLOCK){
                if (e.getClickedBlock() != null){
                    if (e.getClickedBlock().getType() == Material.DIAMOND_BLOCK){
                        //Do the stuff you want
                    }
                    else {
                        return;
                    }
                }
                else {
                    return;
                }
            }
            else {
                return;
            }
        }
    Not too sure what you are doing with all the casting but try this out
     
  12. Tank
    Tanks man, you are awsome :D
     
    • Like Like x 1
  13. Tank
    Tanks man, you are awsome :D
     
Thread Status:
Not open for further replies.