Solved p.getEyeLocation().getBlock()

Discussion in 'Spigot Plugin Development' started by GekaLol2.0, Aug 24, 2017.

  1. Hello, a question someone could tell me is I'm doing wrong, looking at the chest and open it does not work
    Code (Text):
          @EventHandler
          public void onShifAndLeftClick(PlayerInteractEvent event) {
              Player p = event.getPlayer();
              if(p.isSneaking() && event.getClickedBlock().getType() == Material.CHEST && event.getAction() == Action.RIGHT_CLICK_BLOCK)
            {                          
                  Chest(p, p.getEyeLocation().getBlock()); //error//
                   p.sendMessage(color("&cNice"));
            }
          }
         
          public void Chest(Player p,Block block) {
                block.getType();
                final Chest chest = (Chest)block.getState();
               Inventory inv = chest.getInventory();
               ItemStack one = new ItemStack(Material.DIAMOND);
               inv.addItem(one);
          }      
    Code (Text):
    [23:53:48 ERROR]: Could not pass event PlayerInteractEvent to InventoryTweaks v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_144]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_144]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.block.CraftBlockState cannot be cast to org.bukkit.block.Chest
            at me.gabri.Main.Chest(Main.java:43) ~[?:?]
            at me.gabri.Main.onShifAndLeftClick(Main.java:36) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_144]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]
            ... 17 more
     
  2. Player#getEyeLocation#getBlock is the block where the players head is, not the clicked block. why you don't pass PlayerInteractEvent#getClickedBlock() ?
     
  3. Code (Text):
          @EventHandler
          public void onShifAndLeftClick(PlayerInteractEvent event) {
              Player p = event.getPlayer();
              if(p.isSneaking() && event.getClickedBlock().getType() == Material.CHEST && event.getAction() == Action.RIGHT_CLICK_BLOCK)
            {                        
                    BlockState block = event.getClickedBlock().getState();
                    block.getType();
                    Chest cofre = (Chest) block;
                   Inventory inv = cofre.getInventory();
                   ItemStack one = new ItemStack(Material.DIAMOND);
                   inv.addItem(one);
                   p.sendMessage(color("&cNice"));
            }
          }
    Thank you for your help