From 71d1a652b4ea654e475dd760bf8962f17572ac22 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 24 Aug 2021 01:21:02 +0200 Subject: [PATCH] Update to Chroma-Core 1.0 --- pom.xml | 4 +-- src/buttondevteam/website/AcmeClient.java | 2 +- .../website/ButtonWebsiteModule.java | 17 +++++++----- src/buttondevteam/website/LoginCommand.java | 26 ++++++++----------- src/buttondevteam/website/WebUser.java | 7 +++-- src/buttondevteam/website/io/IOHelper.java | 10 +++---- .../website/page/BuildNotificationsPage.java | 5 ++-- src/buttondevteam/website/page/Page.java | 6 +++-- 8 files changed, 37 insertions(+), 40 deletions(-) diff --git a/pom.xml b/pom.xml index 18dac52..cd09ce1 100644 --- a/pom.xml +++ b/pom.xml @@ -119,8 +119,8 @@ provided - com.github.TBMCPlugins.ButtonCore - ButtonCore + com.github.TBMCPlugins.ChromaCore + Chroma-Core ${branch}-SNAPSHOT provided diff --git a/src/buttondevteam/website/AcmeClient.java b/src/buttondevteam/website/AcmeClient.java index ffeef47..13a0e09 100644 --- a/src/buttondevteam/website/AcmeClient.java +++ b/src/buttondevteam/website/AcmeClient.java @@ -301,7 +301,7 @@ public class AcmeClient { */ public static void main(String... args) { if (args.length == 0) { - TBMCCoreAPI.SendException("Error while doing ACME!", new Exception("No domains given")); + LOG.error("Error while doing ACME!", new Exception("No domains given")); return; } diff --git a/src/buttondevteam/website/ButtonWebsiteModule.java b/src/buttondevteam/website/ButtonWebsiteModule.java index 3c6ffbc..df04447 100644 --- a/src/buttondevteam/website/ButtonWebsiteModule.java +++ b/src/buttondevteam/website/ButtonWebsiteModule.java @@ -1,7 +1,7 @@ package buttondevteam.website; import buttondevteam.lib.TBMCCoreAPI; -import buttondevteam.lib.chat.TBMCChatAPI; +import buttondevteam.lib.architecture.ButtonPlugin; import buttondevteam.website.io.IOHelper; import buttondevteam.website.page.*; import com.sun.net.httpserver.HttpServer; @@ -13,7 +13,6 @@ import org.bouncycastle.openssl.PEMKeyPair; import org.bouncycastle.openssl.PEMParser; import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; import org.bukkit.Bukkit; -import org.bukkit.plugin.java.JavaPlugin; import javax.net.ssl.*; import java.io.*; @@ -29,7 +28,7 @@ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -public class ButtonWebsiteModule extends JavaPlugin { +public class ButtonWebsiteModule extends ButtonPlugin { private static HttpsServer server; /** * For ACME validation and user redirection @@ -110,13 +109,13 @@ public class ButtonWebsiteModule extends JavaPlugin { }); enabled = true; } catch (Exception e) { - TBMCCoreAPI.SendException("An error occurred while starting the webserver!", e); + TBMCCoreAPI.SendException("An error occurred while starting the webserver!", e, this); enabled = false; //It's not even enabled yet, so we need a variable } } @Override - public void onEnable() { + public void pluginEnable() { if (!enabled) { getServer().getPluginManager().disablePlugin(this); return; @@ -126,8 +125,8 @@ public class ButtonWebsiteModule extends JavaPlugin { addPage(new ProfilePage()); addPage(new BuildNotificationsPage()); addPage(new BridgePage()); - TBMCCoreAPI.RegisterUserClass(WebUser.class); - TBMCChatAPI.AddCommand(this, LoginCommand.class); + TBMCCoreAPI.RegisterUserClass(WebUser.class, WebUser::new); + getCommand2MC().registerCommand(new LoginCommand()); Bukkit.getScheduler().runTaskAsynchronously(this, () -> { this.getLogger().info("Starting webserver..."); server.setExecutor( @@ -145,6 +144,10 @@ public class ButtonWebsiteModule extends JavaPlugin { }); } + @Override + protected void pluginDisable() { + } + private static boolean httpstarted = false; /** diff --git a/src/buttondevteam/website/LoginCommand.java b/src/buttondevteam/website/LoginCommand.java index b2f5324..4e63bd0 100644 --- a/src/buttondevteam/website/LoginCommand.java +++ b/src/buttondevteam/website/LoginCommand.java @@ -1,27 +1,23 @@ package buttondevteam.website; import buttondevteam.lib.chat.CommandClass; -import buttondevteam.lib.chat.PlayerCommandBase; +import buttondevteam.lib.chat.ICommand2; +import buttondevteam.lib.chat.ICommand2MC; import buttondevteam.website.page.LoginPage; import org.bukkit.Bukkit; import org.bukkit.entity.Player; -@CommandClass -public class LoginCommand extends PlayerCommandBase { - @Override //TODO: Ask about linking already existing accounts, to prevent linking someone else's - public boolean OnCommand(Player player, String s, String[] strings) { +@CommandClass(helpText = {// + "§6---- Login ----", // + "This command allows you to log in to our website using your Minecraft account.", // + "If you are already logged in to the site, you can connect your MC account to it.", // + "This is good for getting Minecraft rewards if you're a patron for example." // +}) +public class LoginCommand extends ICommand2MC { + //TODO: Ask about linking already existing accounts, to prevent linking someone else's + public boolean def(Player player) { String state = LoginPage.generateState("minecraft", player.getUniqueId().toString()).toString(); Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"\",{\"text\":\"Please \",\"color\":\"aqua\"},{\"text\":\"Click Here\",\"color\":\"aqua\",\"bold\":true,\"underlined\":true,\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://server.figytuna.com/login?type=minecraft&state=" + state + "\"}},{\"text\":\" to log in to our site using your Minecraft account.\",\"color\":\"aqua\",\"bold\":false,\"underlined\":false}]"); return true; } - - @Override - public String[] GetHelpText(String s) { - return new String[]{// - "§6---- Login ----", // - "This command allows you to log in to our website using your Minecraft account.", // - "If you are already logged in to the site, you can connect your MC account to it.", // - "This is good for getting Minecraft rewards if you're a patron for example." // - }; - } } diff --git a/src/buttondevteam/website/WebUser.java b/src/buttondevteam/website/WebUser.java index ad95eac..b976cc9 100644 --- a/src/buttondevteam/website/WebUser.java +++ b/src/buttondevteam/website/WebUser.java @@ -2,8 +2,8 @@ package buttondevteam.website; import java.util.UUID; +import buttondevteam.lib.architecture.ConfigData; import buttondevteam.lib.player.ChromaGamerBase; -import buttondevteam.lib.player.PlayerData; import buttondevteam.lib.player.UserClass; @UserClass(foldername = "web") @@ -16,7 +16,6 @@ public class WebUser extends ChromaGamerBase { return uuid; } - public PlayerData sessionID() { - return data(new UUID(0, 0)); //It's used with toString() directly, so can't be null - } + //It's used with toString() directly, so can't be null + public ConfigData sessionID = getConfig().getData("sessionID", new UUID(0, 0)); } diff --git a/src/buttondevteam/website/io/IOHelper.java b/src/buttondevteam/website/io/IOHelper.java index 09ab235..5dfd80a 100644 --- a/src/buttondevteam/website/io/IOHelper.java +++ b/src/buttondevteam/website/io/IOHelper.java @@ -84,16 +84,14 @@ public class IOHelper { */ public static void LoginUser(HttpExchange exchange, WebUser user) { Bukkit.getLogger().fine("Logging in user: " + user); - user.sessionID().set(UUID.randomUUID()); - user.save(); + user.sessionID.set(UUID.randomUUID()); new Cookies(2).add(new Cookie("user_id", user.getUUID() + "")) - .add(new Cookie("session_id", user.sessionID().get().toString())).AddHeaders(exchange); + .add(new Cookie("session_id", user.sessionID.get().toString())).AddHeaders(exchange); Bukkit.getLogger().fine("Logged in user."); } public static void LogoutUser(HttpExchange exchange, WebUser user) { - user.sessionID().set(new UUID(0, 0)); - user.save(); + user.sessionID.set(new UUID(0, 0)); SendLogoutHeaders(exchange); } @@ -147,7 +145,7 @@ public class IOHelper { return null; WebUser user = ChromaGamerBase.getUser(cookies.get("user_id").getValue(), WebUser.class); if (user != null && cookies.get("session_id") != null - && cookies.get("session_id").getValue().equals(user.sessionID().get().toString())) { + && cookies.get("session_id").getValue().equals(user.sessionID.get().toString())) { if (cookies.getExpireTimeParsed().minusYears(1).isBefore(ZonedDateTime.now(ZoneId.of("GMT")))) LoginUser(exchange, user); return user; diff --git a/src/buttondevteam/website/page/BuildNotificationsPage.java b/src/buttondevteam/website/page/BuildNotificationsPage.java index 05d5e15..23ef3b9 100644 --- a/src/buttondevteam/website/page/BuildNotificationsPage.java +++ b/src/buttondevteam/website/page/BuildNotificationsPage.java @@ -1,6 +1,5 @@ package buttondevteam.website.page; -import buttondevteam.core.component.updater.PluginUpdater; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.website.io.IOHelper; import buttondevteam.website.io.Response; @@ -52,8 +51,8 @@ public class BuildNotificationsPage extends Page { if (signatures != null && signatures.size() > 0 && post.containsKey("payload") && verifySignature(payload.getBytes(StandardCharsets.UTF_8), Base64.getDecoder().decode(signatures.get(0)))) { - Bukkit.getPluginManager() - .callEvent(new PluginUpdater.UpdatedEvent(gson.fromJson(payload, JsonObject.class))); + /*Bukkit.getPluginManager() + .callEvent(new PluginUpdater.UpdatedEvent(gson.fromJson(payload, JsonObject.class)));*/ return new Response(200, "All right", exchange); } } catch (Exception e) { diff --git a/src/buttondevteam/website/page/Page.java b/src/buttondevteam/website/page/Page.java index 81134fe..77255ec 100644 --- a/src/buttondevteam/website/page/Page.java +++ b/src/buttondevteam/website/page/Page.java @@ -1,6 +1,7 @@ package buttondevteam.website.page; import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.website.ButtonWebsiteModule; import buttondevteam.website.io.IOHelper; import buttondevteam.website.io.Response; import com.sun.net.httpserver.HttpExchange; @@ -25,7 +26,8 @@ public abstract class Page implements HttpHandler { IOHelper.SendResponse(404, "404 Not found: " + exchange.getRequestURI().getPath(), exchange); } } catch (Exception e) { - TBMCCoreAPI.SendException("Internal Server Error in ButtonWebsiteModule!", e); + ButtonWebsiteModule module = ButtonWebsiteModule.getPlugin(ButtonWebsiteModule.class); + TBMCCoreAPI.SendException("Internal Server Error in ButtonWebsiteModule!", e, module); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream str = new PrintStream(baos); @@ -34,7 +36,7 @@ public abstract class Page implements HttpHandler { str.print(""); IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange); } catch (Exception e1) { - TBMCCoreAPI.SendException("Exception while sending Internal Server Error in ButtonWebsiteModule!", e1); + TBMCCoreAPI.SendException("Exception while sending Internal Server Error in ButtonWebsiteModule!", e1, module); } } }