Bukkit error using threads

Discussion in 'Plugin Development' started by Mike724, Oct 30, 2011.

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

    Mike724

    Code:
    161 recipes
    17 achievements
    17:45:12 [INFO] Starting minecraft server version Beta 1.8.1
    17:45:12 [INFO] Loading properties
    17:45:12 [INFO] Starting Minecraft server on *:25565
    17:45:12 [INFO] This server is running Craftbukkit version git-Bukkit-0.0.0-1131
    -g86b7fa8-b1337jnks (MC: 1.8.1)
    17:45:12 [INFO] Preparing level "world"
    17:45:12 [INFO] Default game type: 0
    17:45:12 [INFO] Preparing start region for level 0 (Seed: -4424826604672261202)
    17:45:13 [INFO] Preparing start region for level 1 (Seed: -4424826604672261202)
    17:45:13 [INFO] Preparing spawn area: 93%
    17:45:14 [INFO] [WebCmd] Creating server socket...
    17:45:14 [INFO] [WebCmd] Server socket created on port 7556
    17:45:14 [INFO] [WebCmd] WebCmd v1.0 by Mike724 is enabled.
    17:45:14 [INFO] Server permissions file permissions.yml is empty, ignoring it
    17:45:14 [INFO] Done (0.119s)! For help, type "help" or "?"
    >reload
    17:45:57 [INFO] [WebCmd] WebCmd v1.0 by Mike724 is disabled!
    17:45:57 [INFO] [WebCmd] Creating server socket...
    17:45:57 [SEVERE] [WebCmd] Could not listen to port 7556
    17:45:57 [INFO] [WebCmd] WebCmd v1.0 by Mike724 is enabled.
    17:45:57 [INFO] Server permissions file permissions.yml is empty, ignoring it
    17:45:57 [SEVERE] Exception in thread "Thread-8"
    17:45:57 [SEVERE] java.lang.NullPointerException
    17:45:57 [SEVERE]       at Mike724.WebCmd.socketConnector.run(socketConnector.ja
    va:20)
    17:45:57 [INFO] Reload complete.
    17:45:57 [WARNING] Can't keep up! Did the system time change, or is the server o
    verloaded?
    >
    Show Spoiler

    Code:
    package Mike724.WebCmd;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.ServerSocket;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class WebCmd extends JavaPlugin {
    	Logger log = Logger.getLogger("Minecraft");
    	//VARS NEEDED FOR SOCKET
    	public ServerSocket server;
    	public String message;
    	public String authKey;
    	public int port;
    	public int timeout;
    	public socketConnector scktconn;
    
    	public void onEnable() {
    		this.getConfig().options().copyDefaults(true);
    		saveConfig();
    		try {
    			authKey = SHA1(this.getConfig().getString("authKey"));
    		} catch (NoSuchAlgorithmException e1) {
    		} catch (UnsupportedEncodingException e1) {
    		}
    		port = this.getConfig().getInt("port");
    		timeout = this.getConfig().getInt("timeout");
    		log.info("[WebCmd] Creating server socket...");
    		try {
    			server = new ServerSocket(port);
    			log.info("[WebCmd] Server socket created on port "+port);
    		} catch (IOException e) {
    			log.severe("[WebCmd] Could not listen to port "+port);
    		}
    		log.info("[WebCmd] WebCmd v1.0 by Mike724 is enabled.");
    		scktconn = new socketConnector(this);
    		scktconn.start();
    	}
    	public void onDisable() {
    	    this.scktconn.close();
    	    try {
    	      this.scktconn.join(5000L);
    	    } catch (InterruptedException ex) {
    	    	log.severe("[WebCmd] Could not end socket connector thread!");
    	    }
    		log.info("[WebCmd] WebCmd v1.0 by Mike724 is disabled!");
    	}
        private static String convertToHex(byte[] data) {
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < data.length; i++) {
                int halfbyte = (data[i] >>> 4) & 0x0F;
                int two_halfs = 0;
                do {
                    if ((0 <= halfbyte) && (halfbyte <= 9))
                        buf.append((char) ('0' + halfbyte));
                    else
                        buf.append((char) ('a' + (halfbyte - 10)));
                    halfbyte = data[i] & 0x0F;
                } while(two_halfs++ < 1);
            }
            return buf.toString();
        }
        public static String SHA1(String text)
        throws NoSuchAlgorithmException, UnsupportedEncodingException  {
        MessageDigest md;
        md = MessageDigest.getInstance("SHA-1");
        byte[] sha1hash = new byte[40];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        sha1hash = md.digest();
        return convertToHex(sha1hash);
        }
    }
    


    Show Spoiler

    Code:
    package Mike724.WebCmd;
    
    import java.io.IOException;
    
    public class socketConnector extends Thread {
    	public WebCmd plugin;
    	public boolean active;
    	public socketConnector(WebCmd plugin)
    	{
    		active = true;
    		this.plugin = plugin;
    	}
    	public void run()
    	{
    		while(this.active == true)
    		{
    			try {
    				Thread t = new Thread(new connectionHandler(plugin, plugin.server.accept()));
    				t.start();
    			} catch (IOException e1) {
    			}
    		}
    	}
    	public void close()
    	{
    		active = false;
    	}
    }
    


    Not sure on how I can squash that error. The plugin works fine, its just on reload that it does it. :/
     
  2. I have no experience with extra threads but my guess it has something to do with what @Afforess was talking about here. I know this isn't Spout related Afforess but your an amazing dev so I thought you may be able to help him with this if you have time :)
     
    Mike724 likes this.
  3. Offline

    Trc202

    Make sure you are stopping all your threads in onDisable()
     
  4. Offline

    Afforess

    Welcome to the wonderful world of reloads. Either your plugin variable is null, or your plugin.server variable is null.
     
  5. Offline

    Mike724

    Great, so there is like nothing I can do?

    Gotta love reloads, should just block them, know how to do that?

    I tried to do that, look at the source.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
Thread Status:
Not open for further replies.

Share This Page