Solved EventHandler can't be resolved to a type?

Discussion in 'Plugin Help/Development/Requests' started by Raydond123, Jan 5, 2015.

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

    Raydond123

    Hi, this is my first plugin that I'm making, so please bear with me if I learn a bit too slow. :p
    Anyways, I'm trying to make a small plugin for my server which only allows you to drop diamonds, but nothing else.

    Here is the code:
    Code:
    package me.Raydond123;
    
    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.entity.EventHandler;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NoDrop extends JavaPlugin{
      
        @Override
        public void onEnable() {
            getLogger().info("Trinket Drop (Version 1.0) has been enabled!");
        }
      
        @Override
        public void onDisable() {
            getLogger().info("Trinket Drop (Version 1.0) has been disabled!");
        }
      
      
        @EventHandler
      
        public void nodrop(PlayerDropItemEvent e)
        {
          
            Player p = e.getPlayer();
          
            if(e.getItemDrop().getItemStack().getType() == Material.DIAMOND)
            {
              
                e.setCancelled(false);
              
            } else
            {
              
                e.setCancelled(true);
              
                p.sendMessage(ChatColor.RED + "You can only drop trinkets!");
              
            }
          
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          
            if (cmd.getName().equalsIgnoreCase("tdrop") && sender instanceof Player ) {
              
                Player player = (Player) sender;
                player.sendMessage(ChatColor.RED + "This is Raydond's first plugin that works! - TrinketDrop v1.0");
              
                return true;
              
            }
          
            return false;
        }
      
    }
    
    Could someone please help?

    Thanks,
    Raymond.
     
  2. Offline

    bwfcwalshy Retired Staff

    @Raydond123 No need to use loggers, Bukkit does it for you.
     
  3. Invisible

    nverdier

    @Raydond123 Well you can change the
    Code:
     if(e.getItemDrop().getItemStack().getType() == Material.DIAMOND) {
                e.setCancelled(false);
            } else {
                e.setCancelled(true);
                p.sendMessage(ChatColor.RED + "You can only drop trinkets!");
            }
    to
    Code:
    if (!e.getItemDrop().getItemStack.getType() == Material.DIAMOND) {
    e.getCancelled(true);
    p.sendMessage("You can only drop trinkets!");
    }
     
  4. Offline

    bwfcwalshy Retired Staff

    @Raydond123 Not implementing Listener and I have no idea if you need to register Listeners if they are in the same class. If so you need to do that.
     
  5. Invisible

    nverdier

    @bwfcwalshy Yes, you have to register the Listener if it's in the same class with PluginManager#registerEvents(this, this)
     
  6. Offline

    Raydond123

    Hm.... Okay, I'll take off the loggers.
    @nverdier Okay, I've tried what you said on your first post but, it gives me getItemStack cannot be resolved or is not a field.
    And, what do you guys mean "implementing Listener"? Sorry, I'm very new to this.
     
  7. Offline

    Europia79

    There is a duplication glitch in 1.8.1 where players can drop items, open up a 2nd Minecraft client, log-in, and they will still have the dropped items in their inventory plus the items on the ground.

    My point is that be careful about allowing players to simply drop diamonds un-checked because they will be able to duplicate them with the glitch.
     
  8. Offline

    bwfcwalshy Retired Staff

    @Raydond123 You know how you extended Java plugin, add on to that and implement Listener.
     
  9. Offline

    Raydond123

    Okay, @bwfcwalshy , I added it but it gave me a bunch of errors. So I changed it to "implements Listener" with an "s".
    Now, it tells, Listener cannot be resolved to a type.
     
  10. Offline

    Europia79

    Code:java
    1. public class NoDrop extends JavaPlugin implements Listener {
    2. @Override
    3. public void onEnable() {
    4. // getLogger().info("Trinket Drop (Version 1.0) has been enabled!");
    5. Listener listener = this;
    6. Plugin plugin = this;
    7. getServer().getPluginManager().registerEvents(listener, plugin);
    8. }
     
    Last edited: Jan 5, 2015
  11. Invisible

    nverdier

    @Europia79 No need for Listener listener = this; and Plugin plugin = this; Just use
    Code:
    getServer().getPluginManager().registerEvents(this, this);
     
  12. Offline

    Raydond123

    Here's my code so far:

    Code:
    package me.Raydond123;
    
    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.entity.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NoDrop extends JavaPlugin implements Listener{
     
        @Override
        public void onEnable() {
            Plugin plugin = this;
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        @Override
        public void onDisable() {
        }
     
     
        @EventHandler
     
        public void nodrop(PlayerDropItemEvent e)
        {
         
            Player p = e.getPlayer();
         
            if (!e.getItemDrop().getItemStack.getType() == Material.DIAMOND) {
                e.setCancelled(true);
                p.sendMessage("You can only drop trinkets!");
                }
         
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            if (cmd.getName().equalsIgnoreCase("tdrop") && sender instanceof Player ) {
             
                Player player = (Player) sender;
                player.sendMessage(ChatColor.RED + "This is Raydond's first plugin that works! - TrinketDrop v1.0");
             
                return true;
             
            }
         
            return false;
        }
     
    }
    
    The following errors occur with this code:
    EventHandler cannot be resolved to a type line 23
    getItemStack cannot be resolved or is not a field line 30
    The import org.bukkit.entity.EventHandler cannot be resolved line 8
    The method getCancelled(boolean) is undefined for the type PlayerDropItemEvent line 31
     
    Last edited: Jan 5, 2015
  13. Invisible

    nverdier

    @Raydond123 Don't try to copy and paste exactly what I put. I made little errors in that just so you wouldn't do that.
     
  14. Offline

    Raydond123

    Sorry, my previous post was messed up because I put an extra line. It's fixed now.

    @nverdier Ohhhh okay. I'll learn from that. ;)

    New Code:
    Code:
    package me.Raydond123;
    
    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.entity.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NoDrop extends JavaPlugin implements Listener{
       
        @Override
        public void onEnable() {
            Plugin plugin = this;
            getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public void onDisable() {
        }
       
       
        @EventHandler
       
        public void nodrop(PlayerDropItemEvent e)
        {
           
            Player p = e.getPlayer();
           
            if (!(e.getItemDrop().getItemStack().getType() == Material.DIAMOND)) {
                e.setCancelled(true);
                p.sendMessage("You can only drop trinkets!");
                }
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (cmd.getName().equalsIgnoreCase("tdrop") && sender instanceof Player ) {
               
                Player player = (Player) sender;
                player.sendMessage(ChatColor.RED + "This is Raydond's first plugin that works! - TrinketDrop v1.0");
               
                return true;
               
            }
           
            return false;
        }
       
    }
    
    Okay now, we're back to the same two errors:

    EventHandler cannot be resolved to a type line 27
    The import org.bukkit.entity.EventHandler cannot be resolved line 8

    <The Edit button is your friend, posts merged by Eyamaz.>
     
    Last edited by a moderator: Jan 5, 2015
  15. Offline

    SuperOriginal

    Do you even understand what each part of your code does?

    Java pls.
     
  16. Offline

    Europia79

    @nverdier

    Yes, there's no need for that... You are right. I did that on purpose because it's more readable than (this, this) for newbies...

    So... like, if he gets into a situation where he wants to move the listener to another class, then it's more apparent which this needs to be changed to new ExampleListener()
     
  17. Invisible

    nverdier

  18. Offline

    Raydond123

    I still can't seem to figure out why the EventHandler isn't working.
    Anyone have any ideas?
     
  19. Offline

    _Filip

    u have to add bukkit as a library
     
  20. Offline

    Raydond123

    I've resorted to using Spigot as a reference library... I can't seem a place to download Bukkit as their download was taken down by a DMCA request...
     
  21. Offline

    RoboticPlayer

    I noticed that your error with the import says "org.bukkit.entity.EventHandler", but you should be importing "
    org.bukkit.event.EventHandler". That is likely your issue.
     
  22. Offline

    timtower Moderator Moderator

  23. Offline

    Raydond123

    Yep, that fixed it! Thanks for helping me out!
     
Thread Status:
Not open for further replies.

Share This Page