ButtonWebsiteModule/src/buttondevteam/website/ButtonWebsiteModule.java

45 lines
1.3 KiB
Java
Raw Normal View History

package buttondevteam.website;
2016-10-22 19:20:28 +00:00
import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import com.sun.net.httpserver.HttpServer;
2016-10-22 19:20:28 +00:00
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.website.page.*;
public class ButtonWebsiteModule extends JavaPlugin {
public static final int PORT = 8080;
private static HttpServer server;
public ButtonWebsiteModule() {
try {
server = HttpServer.create(new InetSocketAddress((InetAddress) null, PORT), 10);
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while starting the webserver!", e);
}
}
@Override
public void onEnable() {
addPage(new IndexPage());
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
this.getLogger().info("Starting webserver...");
((Runnable) server::start).run(); // Totally normal way of calling a method
this.getLogger().info("Webserver started");
Thread t = new Thread(() -> AcmeClient.main("server.figytuna.com"));
t.setContextClassLoader(getClass().getClassLoader());
t.start();
});
}
/**
* Adds a new page/endpoint to the website. This method needs to be called before the server finishes loading (onEnable).
*/
public static void addPage(Page page) {
server.createContext("/" + page.GetName(), page);
}
2016-10-22 19:20:28 +00:00
}