Type cannot be resolved or is not a field? [solved]

Discussion in 'Plugin Development' started by Killifactor, May 9, 2012.

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

    Killifactor

    Hello everyone, recently i followed a tutorial on how to make a basic java plugin for bukkit. After completely copying everything the way he did it (with the exception of changing a few things) i cannot seem to get it to work.

    Here is the main class: TD

    Code:
    package me.samkio.TD;
     
    import java.util.ArrayList;
     
    import org.bukkit.event.Event;
     
    import java.util.HashMap;
    import java.util.logging.Logger;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    import org.bukkit.event.EventPriority;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class TD extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        private final TDBlockListener blockListener = new TDBlockListener(this);
        public final HashMap<Player, ArrayList<Block>> tdUsers = new HashMap<Player, ArrayList<Block>>();
        public void onEnable() {
            //PluginManager pm = getServer().getPluginManager();
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, EventPriority.NORMAL, this);
            //pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener,
                    //EventPriority.NORMAL, this);
            log.info("TD STARTED");
        }
     
        public void onDisable() {
            log.info("TD DISABLED");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("TD")){
                toggleTD((Player) sender);
                return true;
            }
            return false;
        }
        public void toggleTD(Player player){
            if(enabled(player)){
                this.tdUsers.remove(player);
                player.sendMessage("TD Disabled");
            }else{
                this.tdUsers.put(player, null);
                player.sendMessage("TD Enabled");
            }
        }
        public boolean enabled(Player player){
            return this.tdUsers.containsKey(player);
        }
    }
    Here is the BlockListener: TDBlockListener

    Code:
    package me.samkio.TD;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
     
    public class TDBlockListener implements Listener {
        public static TD plugin;
     
        public TDBlockListener(TD instance) {
            plugin = instance;
        }
        public void onBlockBreak(BlockBreakEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            if(block.getType() == Material.TORCH && plugin.enabled(player)){
                player.sendMessage(ChatColor.RED + "You broke a torch.");
            }
        }
    }

    Here are the errors im receiving in the command prompt.

    Code:
    182 recipes
    27 achievements
    17:13:24 [INFO] Starting minecraft server version 1.2.5
    17:13:24 [INFO] Loading properties
    17:13:24 [INFO] Starting Minecraft server on *:25565
    17:13:24 [INFO] This server is running CraftBukkit version git-Bukkit-1.2.5-R1.0
    -b2149jnks (MC: 1.2.5) (Implementing API version 1.2.5-R1.0)
    17:13:25 [SEVERE] Could not load 'plugins\TorchDestroy.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.Error: Unresolved compilatio
    n problem:
            The import org.bukkit.event.block.BlockListener cannot be resolved
     
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:148)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:207)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:183)
            at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager.java:53)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:156)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:422)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.Error: Unresolved compilation problem:
            The import org.bukkit.event.block.BlockListener cannot be resolved
     
            at me.samkio.TD.TDBlockListener.<init>(TDBlockListener.java:9)
            at me.samkio.TD.TD.<init>(TD.java:20)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:144)
            ... 8 more
    17:13:25 [INFO] [WirelessRedstone] Loading WirelessRedstone v1.6b
    17:13:25 [INFO] Preparing level "world"
    17:13:25 [INFO] Default game type: 0
    17:13:25 [INFO] Preparing start region for level 0 (Seed: 4044050814232608057)
    17:13:25 [INFO] Preparing start region for level 1 (Seed: 4044050814232608057)
    17:13:26 [INFO] Preparing spawn area: 65%
    17:13:26 [INFO] Preparing start region for level 2 (Seed: 4044050814232608057)
    17:13:26 [INFO] [WirelessRedstone] Enabling WirelessRedstone v1.6b
    17:13:26 [INFO] WirelessRedstone: WirelessRedstone version 1.6b is loading...
    17:13:26 [INFO] WirelessRedstone: Any of the supported permissions plugins has b
    een detected! Defaulting to OP/Config files!
    17:13:26 [INFO] WirelessRedstone version 1.6b is enabled!
    17:13:26 [INFO] Server permissions file permissions.yml is empty, ignoring it
    17:13:26 [INFO] Done (1.746s)! For help, type "help" or "?"
    >
    And im also recieving and error in this line of code
    Code:
    pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, EventPriority.NORMAL, this);
    Where "Type" is underlined in red and when i mouse over it, it comes up with "Type cannot be resolved or is not a field".


    I've searched for these errors everywhere and cannot seem to come across any fixes. Any help is appreciated! Thanks :)
     
  2. Offline

    Gravity Retired Staff

  3. Offline

    CorrieKay

    youre using the old tutorial for listeners.

    When creating a listener, just implement Listener instead of extend whatever listener youre trying.

    then in front of the listener method, put your event annotation @EventHandler

    Then where you did pm.registerEvent, do this instead
    pm.registerEvents(blockListener, this);
     
  4. Offline

    Killifactor

    Thanks alot guys! Will try this in a few moments!

    Ok so i went through my code and cleaned any errors that existed. However, i still am getting errors from in the command prompt when im trying to run the server.

    Heres the new TD class file:

    Show Spoiler
    Code:
    package me.samkio.TD;
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.logging.Logger;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class TD extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        private final TDBlockListener blockListener = new TDBlockListener();
        public final HashMap<Player, ArrayList<Block>> tdUsers = new HashMap<Player, ArrayList<Block>>();
        public void onEnable() {
            //PluginManager pm = getServer().getPluginManager();
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(blockListener, this);
            //pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener,
                    //EventPriority.NORMAL, this);
            log.info("TD STARTED");
        }
     
        public void onDisable() {
            log.info("TD DISABLED");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("TD")){
                toggleTD((Player) sender);
                return true;
            }
            return false;
        }
        public void toggleTD(Player player){
            if(enabled(player)){
                this.tdUsers.remove(player);
                player.sendMessage("TD Disabled");
            }else{
                this.tdUsers.put(player, null);
                player.sendMessage("TD Enabled");
            }
        }
        public boolean enabled(Player player){
            return this.tdUsers.containsKey(player);
        }
    }


    Here is the new TDBlockListener class:

    Show Spoiler
    Code:
    package me.samkio.TD;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.BlockBreakEvent;
     
     
    public class TDBlockListener implements Listener {
        public static TD plugin;
       
        public TDBlockListener() {
            Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
        }
        @EventHandler(priority = EventPriority.NORMAL)
        public void onBlockBreak(final BlockBreakEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            if(block.getType() == Material.TORCH && plugin.enabled(player)){
                player.sendMessage(ChatColor.RED + "You broke a torch.");
            }
        }
    }


    And here is the errors in the command prompt:

    Show Spoiler
    Code:
    182 recipes
    27 achievements
    19:22:38 [INFO] Starting minecraft server version 1.2.5
    19:22:38 [INFO] Loading properties
    19:22:38 [INFO] Starting Minecraft server on *:25565
    19:22:39 [INFO] This server is running CraftBukkit version git-Bukkit-1.2.5-R1.0
    -b2149jnks (MC: 1.2.5) (Implementing API version 1.2.5-R1.0)
    19:22:39 [SEVERE] Could not load 'plugins\TorchDestroy.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:148)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:207)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:183)
            at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager.java:53)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:156)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:422)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
            at org.bukkit.plugin.SimplePluginManager.registerEvents(SimplePluginMana
    ger.java:485)
            at me.samkio.TD.TDBlockListener.<init>(TDBlockListener.java:18)
            at me.samkio.TD.TD.<init>(TD.java:16)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:144)
            ... 8 more
    19:22:39 [INFO] [WirelessRedstone] Loading WirelessRedstone v1.6b
    19:22:39 [INFO] Preparing level "world"
    19:22:39 [INFO] Default game type: 0
    19:22:39 [INFO] Preparing start region for level 0 (Seed: 4044050814232608057)
    19:22:40 [INFO] Preparing start region for level 1 (Seed: 4044050814232608057)
    19:22:40 [INFO] Preparing spawn area: 44%
    19:22:41 [INFO] Preparing start region for level 2 (Seed: 4044050814232608057)
    19:22:41 [INFO] Preparing spawn area: 52%
    19:22:41 [INFO] [WirelessRedstone] Enabling WirelessRedstone v1.6b
    19:22:41 [INFO] WirelessRedstone: WirelessRedstone version 1.6b is loading...
    19:22:41 [INFO] WirelessRedstone: Any of the supported permissions plugins has b
    een detected! Defaulting to OP/Config files!
    19:22:42 [INFO] WirelessRedstone version 1.6b is enabled!
    19:22:42 [INFO] Server permissions file permissions.yml is empty, ignoring it
    19:22:42 [INFO] Done (2.826s)! For help, type "help" or "?"
    >


    Any ideas on what i did wrong now?

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

    MrAverage

    I'm not as pro as a lot of these guys are, but I see a couple things that look funny. First off, it looks like your trying to use register events twice for your block listener: once in the main class, and once in the listener. So, you shouldn't need the ..

    Code:
    public TDBlockListener() {
            Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
        }
    .. in the listener class since it's already registered in the main class. Incidentally, that's also the block causing the null pointer as shown..

    Code:
    Caused by: java.lang.NullPointerException
            at org.bukkit.plugin.SimplePluginManager.registerEvents(SimplePluginMana
    ger.java:485)
            at me.samkio.TD.TDBlockListener.<init>(TDBlockListener.java:18)
    .. here. You defined 'plugin' in your listener, but it's not initialized to anything and is therefor null, hence the nullpointerexception.

    Get rid of that constructor in your listener and try it out. =) Hope that helps a little!
     
  6. Offline

    Killifactor

    Thank you so much! Works like a charm now! :D

    EDIT: Well the actual plugin compiles without error, but it doesn't work in game.

    This is the error i receive when breaking a torch in-game:

    Show Spoiler
    Code:
    20:07:54 [SEVERE] Could not pass event BlockBreakEvent to TD
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:303)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:459)
            at net.minecraft.server.ItemInWorldManager.breakBlock(ItemInWorldManager
    .java:220)
            at net.minecraft.server.ItemInWorldManager.dig(ItemInWorldManager.java:1
    55)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:537)
            at net.minecraft.server.Packet14BlockDig.handle(SourceFile:43)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
            at me.samkio.TD.TDBlockListener.onBlockBreak(TDBlockListener.java:20)
            at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301)
            ... 12 more
    >
     
  7. Offline

    MrAverage

    Awesome man! Good luck on your first plugin! If you get frustrated, just ctrl+shift+s and walk away for a few hours and come back to it with a fresh pair of eyes later.. trust me, it works! :)

    Are you still using if(block.getType() == Material.TORCH && plugin.enabled(player)) ?

    Remember, 'plugin' was null.. it needs to be initialized. Learn to look for null pointer exceptions in your error lists, and you will find 90% of your mistakes. What really helped me was reading about how to avoid them in various java tutorials on the net. You will learn to hate null pointers. =P

    Code:
    Caused by: java.lang.NullPointerException
            at me.samkio.TD.TDBlockListener.onBlockBreak(TDBlockListener.java:20)
    
    This is telling you that line 20 in your block listener is causing the issue. Honestly I don't see why you even need the && plugin.enabled(player) part so you could prolly just get rid of that. But please do remember that you have to both declare and initialize all objects in Java. What I have been doing for pointing other classes to the main class is something like this..

    Code:
    // declare a TD object like this in your main class
    public static TD td;
     
    // use the constructor in your main class to initialize the object you declared..
    public TD() {
        td = this;
    }
     
    // and declare a private object in your listener pointing to the object in the main class.
    private TD td = TD.td;
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  8. Offline

    Killifactor

    Thanks mate! Everything works now. I'm still pretty novice at java atm, but im learning as i go along :)
     
  9. Offline

    MrAverage

    No Problem man.. everyone here had to start somewhere. Just keep at it and you'll be amazed how quickly you pick it up.

    EDIT: don't forget to mark this as [solved]
     
  10. Offline

    V10lator

    MrAverage Just a side note: You shouln't use statics in bukkit (or at least set them to null in onDisable()). So a better way would be adding a constructor to the listener and handling the main classes instance over to it.
     
    MrAverage likes this.
  11. Offline

    MrAverage

    That makes better sense. I'll start doing it that way. Thanks for the tip man!
     
  12. Offline

    V10lator

  13. Offline

    MrAverage


    Very, very interesting read. I think I might be doing some big rewrites in the near future. With that new perspective, I realize my code probably leaks like a sieve on a /reload. I've always restarted servers personally, but I can see how high population server owners would be more inclined to reload.
     
Thread Status:
Not open for further replies.

Share This Page