Solved Help with PlayerInteractEvent

Discussion in 'Plugin Development' started by DavidK1412, Jun 17, 2020.

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

    DavidK1412

    Hello, I recently joined to develop minecraft plugins, in my first plugin i want to make a playerinteractevent with signals, when the player do rightclick in a signal a command will be ejecuted, but, when I try to test in-game, i´ve an error and isn´t of the config.yml or another commands in the plugin, is of the event, I don´t have any problem with others events, only with the PlayerInteractEvent, i´ve searched for two hours the solution and, i don´t have answer for the problem, so, i´ve decided to ask here... Help please xD.
    Sorry for my english in this post, i´m spanish :(...

    Error:
    Code:
    [21:11:25] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to endersp v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1200) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:18) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_251]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_251]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
    Caused by: java.lang.NullPointerException
        at davidk1412.enderspa.cmds.CartelListener.alClickear(CartelListener.java:33) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_251]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        ... 18 more
    Code of Event:
    Code:
    package davidk1412.enderspa.cmds;
    
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import davidk1412.enderspa.enderspa;
    import net.md_5.bungee.api.ChatColor;
    
    public class CartelListener implements Listener{
       
        private enderspa plugin;
    
        public CartelListener(enderspa plugin) {
            this.plugin = plugin;
        }
       
        @EventHandler
        public void alClickear(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            Block bloque = event.getClickedBlock();
            if(bloque.getType() != null && bloque.getType().equals(Material.AIR)) {
                if(bloque != null && bloque.getType().equals(Material.SIGN) || bloque.getType().equals(Material.SIGN_POST) || bloque.getType().equals(Material.WALL_SIGN)) {
                    if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                        FileConfiguration config = plugin.getConfig();
                        double x = bloque.getX();
                        double y = bloque.getY();
                        double z = bloque.getZ();
                        String world = bloque.getWorld().getName();
                        if(config.contains("Comandos")) {
                            for(String key : config.getConfigurationSection("Comandos").getKeys(false)) {
                               
                               
                                double x1 = Double.valueOf(config.getString("Comandos."+key+".x"));
                                double y1 = Double.valueOf(config.getString("Comandos."+key+".y"));
                                double z1 = Double.valueOf(config.getString("Comandos."+key+".z"));
                                String world1 = config.getString("Comandos."+key+".world");
                                if(x == x1 && y == y1 && z == z1 && world.contentEquals(world1)) {
                                    event.setCancelled(true);
                                    List<String> comandos = config.getStringList("Comandos."+key+".commands");
                                    ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
                                    for(int i=0; i<comandos.size();i++ ) {
                                        if(comandos.get(i).startsWith("msg %player%")) {
                                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', comandos.get(i).replace("msg %player% ", "")));
                                        }else {
                                            Bukkit.dispatchCommand(console, comandos.get(i).replace("%player%", p.getName()));
                                        }
                                    }
                                   
                                   
                                    return;
                                }
    
                            }
                        }
                    }
                   
                }
            }
        }
    }
    Thanks for reading!.
     
  2. Online

    timtower Administrator Administrator Moderator

    @DavidK1412 You are checking the type of bloque before you check if it is null.
     
  3. Offline

    DavidK1412

    So, i´ve to change the order or delete the first line in this line of code?:
    Code:
    if(bloque.getType() != null && bloque.getType().equals(Material.AIR)) {
                if(bloque != null && bloque.getType().equals(Material.SIGN) || bloque.getType().equals(Material.SIGN_POST) || bloque.getType().equals(Material.WALL_SIGN)) {
    Or use an else in my code ?, this part of my event is too confuse for me.
     
  4. Online

    timtower Administrator Administrator Moderator

    @DavidK1412 Delete the first line.
    You have something weird there anyways as you check the same block for two different types.
     
  5. Offline

    DavidK1412

    It works, but now when I do a click (left) after use the cartel with command, the console write the same error, I think that's the event isn´t cancelled, but, in the line 47 (now 46 xd) i´ve event.setCancelled(true);.
     
Thread Status:
Not open for further replies.

Share This Page