Solved Could not pass event PlayerInteractEvent

Discussion in 'Spigot Plugin Development' started by Deathworld, Apr 29, 2017.

Thread Status:
Not open for further replies.
  1. Hey, I'm trying to make an unlimited anvils in my spawn, so I did that :
    Code (Text):
    package fr.dragonia;

    import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.AnvilInventory;

    public class UnlimitedAnvil implements Listener {

        @EventHandler(priority = EventPriority.MONITOR)
        public void onAnvilUse(PlayerInteractEvent e)
        {
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                Block block = e.getClickedBlock();
                Player player = e.getPlayer();
                WorldGuardPlugin wg = (WorldGuardPlugin) Bukkit.getServer().getPluginManager(). getPlugin("WorldGuard");
                if (block.getType() == Material.ANVIL /*&& !wg.canBuild(player, block ) */){
                    player.sendMessage("toz");
                    AnvilInventory anvil = (AnvilInventory) e.getClickedBlock().getState();
                    if(anvil.getName().equalsIgnoreCase("§3Renommez et réparez")) {
                        byte data = block.getData();
                        block.setType(Material.ANVIL);
                        System.out.print(data);
                        if ((data == 10) || (data == 2) || (data == 8)) {
                            block.setData((byte) 2);
                        }
                        if ((data == 9) || (data == 3) || (data == 11)) {
                            block.setData((byte) 3);
                        }
                    }
                }
            }
        }

    }
    Firstly, I didn't find how to look for an Location worldguard's region name, so I only checked the anvil name (or I tried to do it x)) and if player can build. If you have an idea to do in an different way, could you please help me ? :3 And secondly, I got an error when I right click on an anvil :
    Code (Text):
    Could not pass event PlayerInteractEvent to DragoniaUtils v0.0.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_121]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_121]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.block.CraftBlockState cannot be cast to org.bukkit.inventory.AnvilInventory
        at fr.dragonia.UnlimitedAnvil.onAnvilUse(UnlimitedAnvil.java:27) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        ... 17 more
     

    Thanks in advance ! :3
     
  2. In this line, you are casting a BlockState to an inventory, that's not possible
    AnvilInventory anvil = (AnvilInventory) e.getClickedBlock().getState();

    You need to cast it as a Block before, check if it's type is Anvil, and the get it's inventory casting it to an InventoryHolder
    Code (Text):
    Block b =l.getBlock();
    if(b.getType().equals(Material.ANVIL))
    {
        Inventory inv=((InventoryHolder)b).getInventory();
        //do your stuff
    }
    EDIT: About the WorldGuard part, you can get a set or regions in which the player is using
    Set<ProtectedRegion> regions = wd.getRegionManager(l.getWorld()).getApplicableRegions(l).getRegions();

    And using a for cycle to check if one of the ProtectedRegion name (using ProtectedRegion.getId() ) equals the one you need
     
  3. You'll also never get a block name from a placed block.

    As that info gets wiped when you place it in the world.

    As for getting a region.

    Hook wg like you have then do;

    ProtectedRegion rg = wg.getRegionManager().getRegion(someRegionName);

    You can also get a list of regions by using getApplicableRegions() by location, iterate over the found ones where your click event happened and see if the returned regions where your anvil is are the one you were looking for.

    rg.getId() gets the name of the region.


    Sent from my iPhone using Tapatalk
     
  4. Other than that, seems like AnvilInventory cannot have a custom name, so you might need another way for doing that (While chest maintain their custom name even if being placed)
     
  5. Thanks, but I'll focus on worldguard part. So with your tips I have that :
    Code (Text):

    public class UnlimitedAnvil implements Listener {

        @EventHandler(priority = EventPriority.MONITOR)
        public void onAnvilUse(PlayerInteractEvent e)
        {
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK)
              {
                Block block = e.getClickedBlock();
                Player player = e.getPlayer();
                WorldGuardPlugin wg = (WorldGuardPlugin) Bukkit.getServer().getPluginManager(). getPlugin("WorldGuard");
                if (block.getType() == Material.ANVIL){
                    player.sendMessage("toz");
                    ProtectedRegion rg = wg.getRegionManager(Bukkit.getWorld("Dragonia")).getRegion("spawn");
                    if(rg.getId().equalsIgnoreCase("spawn")){
                    byte data = block.getData();
                        block.setType(Material.ANVIL);
                        System.out.print(data);
                        if ((data == 10) || (data == 2) || (data == 8)) {
                            block.setData((byte) 2);
                        }
                        if ((data == 9) || (data == 3) || (data == 11)) {
                            block.setData((byte) 3);
                        }
                        player.sendMessage("debug");
                    }
                    else{
                        player.sendMessage("de bug");
                    }
                }
            }
        }

    }

    }

     
    But I got an error, when I right click in an anvil, I have this error :
    Code (Text):
    Could not pass event PlayerInteractEvent to DragoniaUtils v0.0.2
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_121]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_121]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
        at com.sk89q.worldguard.bukkit.ConfigurationManager.get(ConfigurationManager.java:222) ~[?:?]
        at com.sk89q.worldguard.bukkit.WorldGuardPlugin.getRegionManager(WorldGuardPlugin.java:1016) ~[?:?]
        at fr.dragonia.UnlimitedAnvil.onAnvilUse(UnlimitedAnvil.java:28) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.jar:git-Spigot-21fe707-e1ebe52]
        ... 17 more
     

    Could you please help me again ? :3
     
  6. at fr.dragonia.UnlimitedAnvil.onAnvilUse(UnlimitedAnvil.java:28) ~[?:?]

    what is line 28 of UnlimitedAnvil.java?
     
  7. One thing, you're getting the region using getRegion("spawn") then checking if the rg.getId() is "spawn"

    Rg will always be spawn since you specify it in get region (which looks for a region by name)

    You should do.

    for (ProtectedRegion rg:
    wg.getRegionManager().getApplicableRegions(e.getClickedBlock().getLocation))
    {
    If(rg.getId().equalsIgnoreCase("spawn"))
    //do stuff


    }
    What that does is gets ALL regions at the clicked block location (there could be multiple) then checks if the id is the one you specified.

    Sent from my iPhone using Tapatalk
     
    • Like Like x 1
  8. Thanks, right now I don't have any errors, thanks you so much ! :D
     
Thread Status:
Not open for further replies.