[Unsolved] Need help with GTOTechnology tut plugin

Discussion in 'Plugin Development' started by Magix39, May 22, 2012.

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

    Magix39

    I am/was following the GTOTechnology tutorial because I would like to start making plugins, but it seems to be outdated. I do want to get the GTOTechnology plugin to work though so that I can see what I did wrong. When googleing the error I was getting, it lead to this thread. >>[Link]<< I did what was suggested by the first poster but was unable to get it all to work..mainly the "In your main class on the onEnable() put this" part.

    I ended up poking around a few more threads and tuts but all to no luck and slightly more confused then when I started. The error message says it has issues with lines 16 in ServerChatPlayerListener, and 13 in HelloWorld, but I don't understand why...

    Line 16 in ServerChatPlayerListener:
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
    Line 13 in HelloWorld:
    public final ServerChatPlayerListener playerListener = new ServerChatPlayerListener(this);

    HelloWorld Src
    Show Spoiler

    Code:
    package me.Magix39.HelloWorld;
    
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
    
    public class HelloWorld extends JavaPlugin
    {
        public static HelloWorld plugin;
        public Logger log;
        public final ServerChatPlayerListener playerListener = new ServerChatPlayerListener(this);
        
        @Override
        public void onDisable()
        {
            PluginDescriptionFile pdffile = this.getDescription();
            log = this.getLogger();
            log.info(pdffile.getName() + " is now disabled");
            
        }    
         
        @Override
        public void onEnable()
        {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.playerListener, this);
            
            PluginDescriptionFile pdffile = this.getDescription();
            log = this.getLogger();
            log.info(pdffile.getName() + " version " + pdffile.getVersion() + " is enabled.");
        
        }
        
    }
    
    

    ServerChatPlayerListener Src
    Show Spoiler

    Code:
    package me.Magix39.HelloWorld;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
    
    public class ServerChatPlayerListener implements Listener
    {
        public static HelloWorld plugin;
        public ServerChatPlayerListener(HelloWorld instance)
        {
            plugin = instance;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
        
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerChat(PlayerChatEvent chat)
        {
            Player p = chat.getPlayer();
            String message = chat.getMessage();
            String message_lower = message.toLowerCase();
            ChatColor RED = ChatColor.RED;
            ChatColor WHITE = ChatColor.WHITE;
            if(message_lower.contains("hi") && message_lower.contains("server"))
            {
                p.sendMessage(RED + "[Server] " + WHITE + "Hello " + p.getName());
                chat.setCancelled(true);
            }     
        }    
    }
    
    

    Plugin.yml
    Show Spoiler

    Code:
        name: HelloWorld
        main: me.Magix39.HelloWorld.HelloWorld
        version: 1.0
        description: >
                     Hello World!
        commands:
    
    

    Error message
    Show Spoiler

    Code:
    2012-05-22 14:42:35 [SEVERE] Could not load 'plugins\HelloWorld.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java: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>(ServerConfigurationManager.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 me.Magix39.HelloWorld.ServerChatPlayerListener.<init>(ServerChatPlayerListener.java:16)
        at me.Magix39.HelloWorld.HelloWorld.<init>(HelloWorld.java:13)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:144)
        ... 8 more
    
     
  2. Offline

    VeryBIgCorp

    Instantiate the listener object in your onCreate method.
     
  3. plugin,getServer() return null when the plugins onEnable() methode isn't called
     
  4. Offline

    VeryBIgCorp

    Well he's calling 'this' to pass in as a parameter, which doesn't exist until the constructor of HelloWorld is called, so it's returning null.
     
  5. no, this is not true, I have noticed in some of my other code that its not true, its even inside code for craftbukkit
     
  6. Offline

    Magix39

    Sadly, still confused...
    I assume line 13 in HelloWorld is ok, and that it is line 16 causing the issues. If so, what should I do if shouldn't use 'this' in line 16? I got line 16 from the other thread, and to my understanding it works..

    Or do you mean to do something like this, from the wiki:
    Code:
    public class LoginPlugin extends JavaPlugin {
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new LoginListener(), this);
        }
    }
    
    Doing that however makes it want something in the ServerChatPlayerListener(). One suggestion ig gives is to remove the arguments from the ServerChatPlayerListener, which I assume you can't do because you need the code in public ServerChatPlayerListener(HelloWorld instance).

    ---------------

    EDIT: What...what the hell.... I've just been messing with the code here and there and....I commented out...well here is the code that I changed:

    -----In ServerChatPlayerListener-----
    Code:
        public ServerChatPlayerListener()//HelloWorld instance)
        {
            //plugin = instance;
        //    plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
    -----In HelloWorld------
    Code:
    public final ServerChatPlayerListener playerListener = new ServerChatPlayerListener();//this);
    

    and it actually worked....I typed Hi server in game chat and I got the expected message back. I assume I shouldn't have or even could have safely removed what I did..could someone please explain:
    Do I really need and is it really ok to NOT have:
    plugin = instance;
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
     
Thread Status:
Not open for further replies.

Share This Page