2016-10-22 20:21:01 +00:00
|
|
|
package buttondevteam.website;
|
2016-10-22 19:20:28 +00:00
|
|
|
|
2016-10-22 20:21:01 +00:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
2017-05-30 21:24:48 +00:00
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
2016-10-22 20:21:01 +00:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import com.sun.net.httpserver.HttpServer;
|
2016-10-22 19:20:28 +00:00
|
|
|
|
2017-05-30 21:24:48 +00:00
|
|
|
import buttondevteam.lib.TBMCCoreAPI;
|
2016-10-22 20:21:01 +00:00
|
|
|
import buttondevteam.website.page.*;
|
|
|
|
|
|
|
|
public class ButtonWebsiteModule extends JavaPlugin {
|
2017-05-30 21:24:48 +00:00
|
|
|
private static HttpServer server;
|
|
|
|
|
2016-10-22 20:21:01 +00:00
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
|
|
|
try {
|
2017-05-30 21:24:48 +00:00
|
|
|
server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10);
|
|
|
|
addPage(new IndexPage());
|
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(this, () -> this.getLogger().info("Starting webserver..."));
|
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(this, server::start);
|
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(this, () -> this.getLogger().info("Webserver started"));
|
2016-10-22 20:21:01 +00:00
|
|
|
} catch (Exception e) {
|
2017-05-30 21:24:48 +00:00
|
|
|
TBMCCoreAPI.SendException("An error occured while starting the webserver!", e);
|
2016-10-22 20:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 21:24:48 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2016-10-22 20:21:01 +00:00
|
|
|
server.createContext("/" + page.GetName(), page);
|
|
|
|
}
|
2016-10-22 19:20:28 +00:00
|
|
|
}
|