Solved PlayerInteractEvent doesn 't work

Discussion in 'Plugin Help/Development/Requests' started by ramptoetsenbord, Jul 29, 2017.

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

    ramptoetsenbord

    I 'm new in the develloping of pugins and I have a problem with my first plugin.
    I have a PlayerInteractEvent that should fire whenever a player clicks on a sign.
    But when I try it, it gives an error.

    My PlayerInteractEvent:
    Code:
    package me.bukkit.firstFlame.events.player;
    
    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.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.material.Sign;
    
    public class PlayerInteract implements Listener {
    
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent event) {
    
            Player player = event.getPlayer();
            Action action = event.getAction();
    
            if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
             
                Block block = event.getClickedBlock();
                player.sendMessage("step1 check");
             
                if (block.getType().equals(Material.SIGN_POST) || block.getType().equals(Material.WALL_SIGN)) {
                    Sign sign = (Sign) block.getState();
    
                    player.sendMessage("step2 Check");
                 
                    String txt = sign.toString();
    
                    if (txt.contains("Food")) {
    
                        player.sendMessage("step3 check");
                     
                     
                        Inventory foodInv = Bukkit.createInventory(null, 9, "Food");
                        ItemStack Food = new ItemStack(Material.COOKED_BEEF, 64);
    
                        foodInv.addItem(Food, Food, Food, Food, Food, Food, Food, Food, Food);
    
                        player.openInventory(foodInv);
    
                    }
                }
    
            }
    
        }
    
    }

    My Main Class:
    Code:
    package me.bukkit.firstFlame;
    
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.bukkit.firstFlame.commands.Admins;
    import me.bukkit.firstFlame.commands.Goodbye;
    import me.bukkit.firstFlame.commands.Hello;
    import me.bukkit.firstFlame.commands.Kit;
    import me.bukkit.firstFlame.events.block.BlockBreak;
    import me.bukkit.firstFlame.events.player.PlayerChat;
    import me.bukkit.firstFlame.events.player.PlayerJoin;
    import me.bukkit.firstFlame.events.player.PlayerInteract;
    
    public class server_survival extends JavaPlugin {
    
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
    
            registerCommands();
            registerEvents();
            registerConfig();
    
            logger.info(pdfFile.getName() + " has been enabled! Build: " + pdfFile.getVersion());
    
        }
    
        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
    
            logger.info(pdfFile.getName() + " has been disabled! Build: " + pdfFile.getVersion());
    
        }
    
        public void registerCommands() {
    
            getCommand("hello").setExecutor(new Hello());
            getCommand("goodbye").setExecutor(new Goodbye());
            getCommand("admins").setExecutor(new Admins(this));
            getCommand("kit").setExecutor(new Kit());
    
        }
    
        public void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
    
            pm.registerEvents(new BlockBreak(), this);
            pm.registerEvents(new PlayerChat(), this);
            pm.registerEvents(new PlayerJoin(this), this);
            pm.registerEvents(new PlayerInteract(), this);
        }
    
        private void registerConfig() {
         
            getConfig().options().copyDefaults(true);
            saveConfig();
         
    
        }
    }
    

    and the error I am recieving:
    Code:
    Could not pass event PlayerInteractEvent to ServerSurvival vBeta1.9.1
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:499) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:234) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PlayerInteractManager.a(PlayerInteractManager.java:448) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:944) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:37) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            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_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-7228328-af1c013]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_12_R1.block.CraftSign cannot be cast to org.bukkit.material.Sign
            at me.bukkit.firstFlame.events.player.PlayerInteract.onPlayerUse(PlayerInteract.java:29) ~[?:?]
            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:302) ~[spigot-1.12.jar:git-Spigot-7228328-af1c013]
            ... 17 more

    If (someoneCouldHelp == true) {
    player.sendMessage("That would be cool!");
    }
     
    Last edited: Jul 29, 2017
  2. Offline

    Machine Maker

    @ramptoetsenbord
    1. You are importing the material sign as in the item in your inventory. You need to import the block sign.
    2. Change
    Code:
      import org.bukkit.material.Sign;
    to
    Code:
     import org.bukkit.block.Sign;
     
    ramptoetsenbord likes this.
  3. Offline

    ramptoetsenbord

    Thank you, but now there is another problem.
    When I try to click the sign, it il only say step 1 check and step 2 check (it is in my code so i could check how far it goes)
    I also already tried to change between step 2 and 3 the code:
    Code:
    player.sendMessage("step2 Check");
                   
                    String txt1 = sign.getLine(1);
    
                    if (txt1.equalsIgnoreCase("Food")) {
    
                        player.sendMessage("step3 check");
    But it still doen 't work.
     
  4. Offline

    Machine Maker

    @ramptoetsenbord
    The lines on signs start at 0 and go to 3. Just like a list. I think that's your problem.
     
    ramptoetsenbord likes this.
  5. Offline

    ramptoetsenbord

    Thank you! It fully works now!
     
Thread Status:
Not open for further replies.

Share This Page