@EventHandler cannot be resolved to a type! Pls Help!

Discussion in 'Plugin Development' started by thesciencekid, Sep 1, 2012.

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

    thesciencekid

    Hello everyone! I have an error with this code that said, "EventHandler cannot be resolved to a type":
    Code:
    package com.cadenfarley.bukkitplugin;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Bukkit_Plugin extends JavaPlugin{
        //defines this as a CraftBukkit Plugin.
        public static Bukkit_Plugin plugin;
        public class myPlayerListener implements Listener {
            @EventHandler
            public void onPlayerInteractBlock(PlayerInteractEvent evt){
                if(evt.getPlayer().getItemInHand().getTypeId() == Material.FISHING_ROD.getId()){
                    //maximal distance between player and thunder is 200 blocks
                    evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
                }
            }
        }
     
        //when the server shuts down
        @Override
        public void onDisable(){
     
        }
     
        //when the server starts up
        @Override
        public void onEnable(){
            System.out.println("Loading Bukkit_Plugin");
        }
     
     
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            //if the command is "hello"
            if(cmd.getName().equalsIgnoreCase("hello")){
                //send a message in rainbow colors
                player.sendMessage(ChatColor.RED + "G");
                player.sendMessage(ChatColor.GOLD + "r");
                player.sendMessage(ChatColor.YELLOW + "e");
                player.sendMessage(ChatColor.GREEN + "e");
                player.sendMessage(ChatColor.BLUE + "t");
                player.sendMessage(ChatColor.DARK_PURPLE + "i");
                player.sendMessage(ChatColor.RED + "n");
                player.sendMessage(ChatColor.GOLD + "g");
                player.sendMessage(ChatColor.YELLOW + "s ");
                player.sendMessage(ChatColor.GRAY + "from the Console!");
       
       
                return true;
            }
     
            if(cmd.getName().equalsIgnoreCase("h")){
                player.setHealth(20);
                player.sendMessage("Hooray! you have been healed!");
            }
     
            if(cmd.getName().equalsIgnoreCase("killme")){
                player.setHealth(0);
            }
     
            return false;
        }
     
     
    }
    the "@EventHandler" is the line with the error. I have looked this up on google many times, and I it looks like I did everything right. I used this page:wiki.bukkit.org/Introduction_to_the_New_Event_System. (delete the %3E at the end). In case you need it, here is my plugin.yml:
    Code:
    name: Bukkit_Plugin
    main: com.cadenfarley.bukkitplugin.Bukkit_Plugin
    version: 0.1
    commands:
      hello:
        description: derp
        usage: /hello
      h:
        description: derp
        usage: /h
      killme:
        description: derp
        usage: /killme
    Please note that I am a kid and a beginner at java, but I have experience with programming in Xcode(iPhone apps).

    Thanks much!
     
  2. Offline

    Sabersamus

    if you are using eclipse, try saving the file, then trying to import it. that usually helps for me
     
  3. Offline

    pqqqqq

    You are going to need to add EventHandler to your imports:
    Code:
    import org.bukkit.event.EventHandler;
     
  4. Offline

    thesciencekid

    Thanks pqqqqq, but it doesn't recognize the package. =(
     
  5. Offline

    pqqqqq

    Hmm, are you sure that:
    A) You have the proper version of bukkit
    B) You put the import in the right place
    C) The project is properly built?
     
  6. Offline

    sensus12

    You didn't register Events.

    Put it onEnable()

    PluginManager pm = this.getServer().getPluginManager();
    pm.registerEvents(this, this);
     
  7. Offline

    Sabersamus

    one quick thing i noticed,

    Code:java
    1.  
    2. public static Bukkit_Plugin plugin;
    3.  


    You never actually defined plugin, in your onEnable() method, put plugin = this;
     
  8. Offline

    Coelho

    Use an IDE.
     
  9. Offline

    thesciencekid

    Ok, now for some reason, Eclipse stopped throwing a fit about the import statement, but it was in the same place as before:
    package com.cadenfarley.bukkitplugin;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.EventHandler;

    public class Bukkit_Plugin extends JavaPlugin implements Listener{
    //defines this as a CraftBukkit Plugin.
    public static Bukkit_Plugin plugin;

    //when the server shuts down
    @Override
    public void onDisable(){

    }

    //when the server starts up
    @Override
    public void onEnable(){
    System.out.println("Loading Bukkit_Plugin");
    plugin = this;
    PluginManager pm = this.getServer().getPluginManager();
    pm.registerEvents(this, this);
    System.out.println("=====================================");
    System.out.println("Bukkit_Plugin has loaded successfuly!");
    System.out.println("=====================================");
    }

    public class myPlayerListener implements Listener {
    @EventHandler
    public void onPlayerInteractBlock(PlayerInteractEvent evt){
    if(evt.getPlayer().getItemInHand().getTypeId() == Material.FISHING_ROD.getId()){
    //maximal distance between player and thunder is 200 blocks
    evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
    }
    }
    }



    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    Player player = (Player) sender;
    //if the command is "hello"
    if(cmd.getName().equalsIgnoreCase("hello")){
    //send a message in rainbow colors
    player.sendMessage(ChatColor.RED + "G");
    player.sendMessage(ChatColor.GOLD + "r");
    player.sendMessage(ChatColor.YELLOW + "e");
    player.sendMessage(ChatColor.GREEN + "e");
    player.sendMessage(ChatColor.BLUE + "t");
    player.sendMessage(ChatColor.DARK_PURPLE + "i");
    player.sendMessage(ChatColor.RED + "n");
    player.sendMessage(ChatColor.GOLD + "g");
    player.sendMessage(ChatColor.YELLOW + "s ");
    player.sendMessage(ChatColor.GRAY + "from the Console!");


    return true;
    }

    if(cmd.getName().equalsIgnoreCase("h")){
    player.setHealth(20);
    player.sendMessage("Hooray! you have been healed!");
    }

    if(cmd.getName().equalsIgnoreCase("killme")){
    player.setHealth(0);
    }

    return false;
    }


    }

    now there are 2 gray errors, one is still on "EventHandler" and the other is on "registerEvents". btw, i have eclipse 3.7.1, and my bukkit jar is called: bukkit-1.3.1-R2.0.jar
     
  10. Offline

    sensus12

    @UP You should implement Listener:

    public class Bukkit_Plugin extends JavaPlugin implements Listener
     
  11. Offline

    Firefly

    Where's your @EventHandler anyways? :confused:
     
  12. Offline

    thesciencekid

    package com.cadenfarley.bukkitplugin;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.EventHandler;

    public class Bukkit_Plugin extends JavaPlugin implements Listener{
    //defines this as a CraftBukkit Plugin.
    public static Bukkit_Plugin plugin;

    //when the server shuts down
    @Override
    public void onDisable(){

    }

    //when the server starts up
    @Override
    public void onEnable(){
    System.out.println("Loading Bukkit_Plugin");
    plugin = this;
    PluginManager pm = this.getServer().getPluginManager();
    pm.registerEvents(this, this);
    System.out.println("=====================================");
    System.out.println("Bukkit_Plugin has loaded successfuly!");
    System.out.println("=====================================");
    }

    public class myPlayerListener implements Listener {
    @EventHandler
    public void onPlayerInteractBlock(PlayerInteractEvent evt){
    if(evt.getPlayer().getItemInHand().getTypeId() == Material.FISHING_ROD.getId()){
    //maximal distance between player and thunder is 200 blocks
    evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
    }
    }
    }



    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    Player player = (Player) sender;
    //if the command is "hello"
    if(cmd.getName().equalsIgnoreCase("hello")){
    //send a message in rainbow colors
    player.sendMessage(ChatColor.RED + "G");
    player.sendMessage(ChatColor.GOLD + "r");
    player.sendMessage(ChatColor.YELLOW + "e");
    player.sendMessage(ChatColor.GREEN + "e");
    player.sendMessage(ChatColor.BLUE + "t");
    player.sendMessage(ChatColor.DARK_PURPLE + "i");
    player.sendMessage(ChatColor.RED + "n");
    player.sendMessage(ChatColor.GOLD + "g");
    player.sendMessage(ChatColor.YELLOW + "s ");
    player.sendMessage(ChatColor.GRAY + "from the Console!");


    return true;
    }

    if(cmd.getName().equalsIgnoreCase("h")){
    player.setHealth(20);
    player.sendMessage("Hooray! you have been healed!");
    }

    if(cmd.getName().equalsIgnoreCase("killme")){
    player.setHealth(0);
    }

    return false;
    }


    }

    sorry about that

    This seems to work after all! Thank you! No errors, but it doesn't do what its supposed to do: If you are holding a fishing pole, strike lightning wherever the bobber lands.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  13. Offline

    Firefly

    Code:
    public class myPlayerListener implements Listener {
    @EventHandler
    public void onPlayerInteractBlock(PlayerInteractEvent evt){
    if(evt.getPlayer().getItemInHand().getTypeId() == Material.FISHING_ROD.getId()){
    //maximal distance between player and thunder is 200 blocks
    evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
    }
    Why are you trying to define a new class within a class?
     
  14. Offline

    thesciencekid

    ...... I feel dumb...... Well thanks ALOT TO ALL WHO TRIED TO HELP A YOUNG PROGRAMMER!!!! It works! The reason why I was trying to define a new class (I didn't know that at the time) was that I copied it as a code snippet and i didn't know that i already had that. Thanks again,
    Caden
     
Thread Status:
Not open for further replies.

Share This Page