From e5c8c5a1e00eb7d2f479cfb631faa7e7a92d0e0c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 30 May 2017 23:24:48 +0200 Subject: [PATCH] API for other plugins, improvements Needs testing --- .classpath | 20 ++++ .gitignore | 4 - .settings/org.eclipse.core.resources.prefs | 3 + .settings/org.eclipse.jdt.core.prefs | 98 +++++++++++++++++++ .settings/org.eclipse.m2e.core.prefs | 4 + pom.xml | 7 +- .../website/ButtonWebsiteModule.java | 28 +++--- src/buttondevteam/website/data/Stats.java | 5 - src/buttondevteam/website/io/IOHelper.java | 4 + src/buttondevteam/website/io/Response.java | 15 +++ src/buttondevteam/website/page/IndexPage.java | 13 +-- src/buttondevteam/website/page/Page.java | 11 ++- 12 files changed, 175 insertions(+), 37 deletions(-) create mode 100644 .classpath create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs delete mode 100644 src/buttondevteam/website/data/Stats.java create mode 100644 src/buttondevteam/website/io/Response.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..be86bf9 --- /dev/null +++ b/.classpath @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index 77d9874..7d0ab85 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ ################# *.pydevproject -.project .metadata/ bin/ tmp/ @@ -12,11 +11,8 @@ tmp/ *.swp *~.nib local.properties -.classpath -.settings/ .loadpath target/ -.project # External tool builders .externalToolBuilders/ diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..7a53139 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 +encoding/src=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..6aad022 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,98 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled +org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore +org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull +org.eclipse.jdt.core.compiler.annotation.nonnull.secondary= +org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault +org.eclipse.jdt.core.compiler.annotation.nonnullbydefault.secondary= +org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable +org.eclipse.jdt.core.compiler.annotation.nullable.secondary= +org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning +org.eclipse.jdt.core.compiler.problem.autoboxing=ignore +org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning +org.eclipse.jdt.core.compiler.problem.deadCode=warning +org.eclipse.jdt.core.compiler.problem.deprecation=warning +org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled +org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled +org.eclipse.jdt.core.compiler.problem.discouragedReference=ignore +org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore +org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore +org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore +org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled +org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore +org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning +org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning +org.eclipse.jdt.core.compiler.problem.forbiddenReference=ignore +org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning +org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled +org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning +org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning +org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore +org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore +org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning +org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore +org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled +org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled +org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning +org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore +org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning +org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning +org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore +org.eclipse.jdt.core.compiler.problem.nonnullParameterAnnotationDropped=warning +org.eclipse.jdt.core.compiler.problem.nonnullTypeVariableFromLegacyInvocation=warning +org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error +org.eclipse.jdt.core.compiler.problem.nullReference=warning +org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error +org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning +org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning +org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore +org.eclipse.jdt.core.compiler.problem.pessimisticNullAnalysisForFreeTypeVariables=warning +org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore +org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore +org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore +org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning +org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning +org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore +org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore +org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore +org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore +org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore +org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled +org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning +org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled +org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled +org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled +org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore +org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning +org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled +org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning +org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning +org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore +org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning +org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore +org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore +org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled +org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedImport=warning +org.eclipse.jdt.core.compiler.problem.unusedLabel=warning +org.eclipse.jdt.core.compiler.problem.unusedLocal=warning +org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore +org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled +org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning +org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning +org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml index 16cc6b5..ac0add6 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.github.tbmcplugins ButtonWebsiteModule - 0.0.1-SNAPSHOT + master-SNAPSHOT ButtonWebsiteModule Button Website Module @@ -82,5 +82,10 @@ commons-io 1.3.2 + + com.github.TBMCPlugins.ButtonCore + ButtonCore + master-SNAPSHOT + diff --git a/src/buttondevteam/website/ButtonWebsiteModule.java b/src/buttondevteam/website/ButtonWebsiteModule.java index 0593df0..639304a 100644 --- a/src/buttondevteam/website/ButtonWebsiteModule.java +++ b/src/buttondevteam/website/ButtonWebsiteModule.java @@ -2,32 +2,34 @@ package buttondevteam.website; import java.net.InetAddress; import java.net.InetSocketAddress; + +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import com.sun.net.httpserver.HttpServer; +import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.website.page.*; public class ButtonWebsiteModule extends JavaPlugin { + private static HttpServer server; + @Override public void onEnable() { try { - this.getLogger().info("Starting webserver..."); - HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10); - /* - * Reflections rf = new Reflections( new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(Page.class.getClassLoader())) - * .addClassLoader(Page.class.getClassLoader()).addScanners(new SubTypesScanner()) .filterInputsBy((String pkg) -> pkg.contains(Page.class.getPackage().getName()))); Set> pages = rf.getSubTypesOf(Page.class); for (Class page : pages) { try { if (Modifier.isAbstract(page.getModifiers())) continue; Page p = page.newInstance(); - * addPage(server, p); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } - */ //^^ This code would load the pages dynamically - But we'll only have like, one page... - addPage(server, new IndexPage()); - server.start(); - this.getLogger().info("Webserver started"); + 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")); } catch (Exception e) { - e.printStackTrace(); + TBMCCoreAPI.SendException("An error occured while starting the webserver!", e); } } - private static void addPage(HttpServer server, Page page) { + /** + * 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); } } diff --git a/src/buttondevteam/website/data/Stats.java b/src/buttondevteam/website/data/Stats.java deleted file mode 100644 index d051729..0000000 --- a/src/buttondevteam/website/data/Stats.java +++ /dev/null @@ -1,5 +0,0 @@ -package buttondevteam.website.data; - -public class Stats { - -} diff --git a/src/buttondevteam/website/io/IOHelper.java b/src/buttondevteam/website/io/IOHelper.java index 6ea09e1..7058c34 100644 --- a/src/buttondevteam/website/io/IOHelper.java +++ b/src/buttondevteam/website/io/IOHelper.java @@ -12,6 +12,10 @@ import com.google.gson.JsonParser; import com.sun.net.httpserver.HttpExchange; public class IOHelper { + public static void SendResponse(Response resp) throws IOException { + SendResponse(resp.code, resp.content, resp.exchange); + } + public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException { try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) { try (ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) { diff --git a/src/buttondevteam/website/io/Response.java b/src/buttondevteam/website/io/Response.java new file mode 100644 index 0000000..a7155aa --- /dev/null +++ b/src/buttondevteam/website/io/Response.java @@ -0,0 +1,15 @@ +package buttondevteam.website.io; + +import com.sun.net.httpserver.HttpExchange; + +public class Response { + public int code; + public String content; + public HttpExchange exchange; + + public Response(int code, String content, HttpExchange exchange) { + this.code = code; + this.content = content; + this.exchange = exchange; + } +} diff --git a/src/buttondevteam/website/page/IndexPage.java b/src/buttondevteam/website/page/IndexPage.java index fdb95fd..19a69d6 100644 --- a/src/buttondevteam/website/page/IndexPage.java +++ b/src/buttondevteam/website/page/IndexPage.java @@ -1,21 +1,14 @@ package buttondevteam.website.page; -import java.io.IOException; - -import com.google.gson.Gson; import com.sun.net.httpserver.HttpExchange; -import buttondevteam.website.data.Stats; -import buttondevteam.website.io.IOHelper; +import buttondevteam.website.io.Response; public class IndexPage extends Page { @Override - public void handlePage(HttpExchange exchange) throws IOException { - Gson gson = new Gson(); - Stats request = gson.fromJson(IOHelper.GetPOSTJSON(exchange), Stats.class); // TODO: Change to a request class - Stats response = new Stats(); - IOHelper.SendResponse(200, gson.toJson(response), exchange); + public Response handlePage(HttpExchange exchange) { + return new Response(200, "Hello world!", exchange); } @Override diff --git a/src/buttondevteam/website/page/Page.java b/src/buttondevteam/website/page/Page.java index 17a96ae..74a33a9 100644 --- a/src/buttondevteam/website/page/Page.java +++ b/src/buttondevteam/website/page/Page.java @@ -1,24 +1,24 @@ package buttondevteam.website.page; -import java.io.IOException; import java.io.PrintStream; import org.apache.commons.io.output.ByteArrayOutputStream; import com.sun.net.httpserver.*; import buttondevteam.website.io.IOHelper; +import buttondevteam.website.io.Response; /** * Add to {@link Main}.Pages */ public abstract class Page implements HttpHandler { public abstract String GetName(); - + @Override public void handle(HttpExchange exchange) { try { if (exchange.getRequestURI().getPath().equals("/" + GetName())) - handlePage(exchange); + IOHelper.SendResponse(handlePage(exchange)); else { IOHelper.SendResponse(404, "404 Not found", exchange); } @@ -37,5 +37,8 @@ public abstract class Page implements HttpHandler { } } - public abstract void handlePage(HttpExchange exchange) throws IOException; + /** + * The main logic of the endpoint. Use IOHelper to retrieve the message sent and other things. + */ + public abstract Response handlePage(HttpExchange exchange); }