diff --git a/mod/src/main/java/com/xeovalyte/polarcraft/PolarcraftMod.java b/mod/src/main/java/com/xeovalyte/polarcraft/PolarcraftMod.java index 81eb97c..267660a 100644 --- a/mod/src/main/java/com/xeovalyte/polarcraft/PolarcraftMod.java +++ b/mod/src/main/java/com/xeovalyte/polarcraft/PolarcraftMod.java @@ -2,23 +2,33 @@ package com.xeovalyte.polarcraft; import net.fabricmc.api.ModInitializer; +import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.network.message.SignedMessage; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.io.File; + +import com.google.gson.JsonObject; +import com.google.gson.JsonElement; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import java.util.UUID; -import com.google.gson.stream.JsonReader; -import java.io.StringReader; - +import net.fabricmc.fabric.api.message.v1.ServerMessageEvents; public class PolarcraftMod implements ModInitializer { // This logger is used to write text to the console and the log file. @@ -26,63 +36,159 @@ public class PolarcraftMod implements ModInitializer { // That way, it's clear which mod wrote info, warnings, and errors. public static final Logger LOGGER = LoggerFactory.getLogger("polarcraft-mod"); + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + private static final File CONFIG_FILE = new File(FabricLoader.getInstance().getConfigDir().toFile(), "polarcraft.json"); + + private String configWebhookUrl = "https://discordapp.com/api/webhooks/webhookid/webhooktoken"; + private String configVerifyUrl = "https://example.com/api/minecraft/verifyuuid"; + @Override public void onInitialize() { // This code runs as soon as Minecraft is in a mod-load-ready state. // However, some things (like resources) may still be uninitialized. // Proceed with mild caution. + loadConfig(); + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { ServerPlayerEntity player = (ServerPlayerEntity) handler.player; - LOGGER.info("Player {} joined the game.", player.getUuidAsString()); onPlayerJoin(player); }); - LOGGER.info("Hello Fabric world!"); + ServerMessageEvents.CHAT_MESSAGE.register((message, sender, params) -> { + onChatMessage(message, sender); + }); + + ServerMessageEvents.GAME_MESSAGE.register((server, message, overlay) -> { + onGameMessage(server, message); + }); + } + + private void loadConfig() { + if (!CONFIG_FILE.exists()) { + saveConfig(); + return; + } + + try { + MyModConfig config = GSON.fromJson(FileUtils.readFileToString(CONFIG_FILE, StandardCharsets.UTF_8), MyModConfig.class); + configWebhookUrl = config.webhookUrl; + configVerifyUrl = config.verifyUrl; + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void saveConfig() { + try { + FileUtils.writeStringToFile(CONFIG_FILE, GSON.toJson(new MyModConfig(configWebhookUrl, configVerifyUrl)), StandardCharsets.UTF_8); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static class MyModConfig { + public String webhookUrl; + public String verifyUrl; + + public MyModConfig(String webhookUrl, String verifyUrl) { + this.webhookUrl = webhookUrl; + this.verifyUrl = verifyUrl; + } + } + + private void onChatMessage( SignedMessage message, ServerPlayerEntity sender) { + try { + // Create a URL object for the server endpoint + URL url = new URL(configWebhookUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setDoOutput(true); + + String requestBody = "{\"content\":\"" + sender.getName().getString() + " > "+ message.getSignedContent() + "\"}"; + + OutputStream outputStream = connection.getOutputStream(); + outputStream.write(requestBody.getBytes()); + outputStream.flush(); + outputStream.close(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + StringBuilder response = new StringBuilder(); + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void onGameMessage( MinecraftServer server, Text message) { + try { + // Create a URL object for the server endpoint + URL url = new URL(configWebhookUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setDoOutput(true); + + String requestBody = "{\"content\":\"" + "**" + message.getString() + "**" + "\"}"; + + OutputStream outputStream = connection.getOutputStream(); + outputStream.write(requestBody.getBytes()); + outputStream.flush(); + outputStream.close(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + StringBuilder response = new StringBuilder(); + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } } private void onPlayerJoin(ServerPlayerEntity player) { UUID uuid = player.getUuid(); try { - // Create the POST request - URL url = new URL("http://localhost:3000/api/minecraft/verifyuuid"); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("POST"); - con.setRequestProperty("Content-Type", "application/json"); - con.setDoOutput(true); - - // Write the request body - String jsonInputString = "{\"uuid\":\"" + uuid + "\"}"; - con.getOutputStream().write(jsonInputString.getBytes()); - - // Read the response - BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); - StringBuilder responseBuilder = new StringBuilder(); - String responseLine; - while ((responseLine = in.readLine()) != null) { - responseBuilder.append(responseLine); + URL url = new URL(configVerifyUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setDoOutput(true); + + String requestBody = "{\"uuid\":\"" + uuid + "\"}"; + + OutputStream outputStream = connection.getOutputStream(); + outputStream.write(requestBody.getBytes()); + outputStream.flush(); + outputStream.close(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + StringBuilder response = new StringBuilder(); + while ((line = reader.readLine()) != null) { + response.append(line); } - String responseBody = responseBuilder.toString(); - - // Parse the response as a JSON object - JsonReader jsonReader = new JsonReader(new StringReader(responseBody)); - jsonReader.beginObject(); - boolean verified = false; - int code = -1; - while (jsonReader.hasNext()) { - String key = jsonReader.nextName(); - if (key.equals("verified")) { - verified = jsonReader.nextBoolean(); - } else if (key.equals("code")) { - code = jsonReader.nextInt(); - } else { - jsonReader.skipValue(); - } - } - jsonReader.endObject(); - - // Kick the player if verified is true + reader.close(); + + Gson gson = new Gson(); + JsonElement element = gson.fromJson(response.toString(), JsonElement.class); + JsonObject jsonResponse = element.getAsJsonObject(); + + boolean verified = jsonResponse.get("verified").getAsBoolean(); + if (!verified) { + int code = jsonResponse.get("code").getAsInt(); + player.networkHandler.disconnect(Text.literal("Whitelist yourself by using this code: " + code)); } } catch (IOException e) { diff --git a/web/components/Whitelist.vue b/web/components/Whitelist.vue new file mode 100644 index 0000000..15fbd7b --- /dev/null +++ b/web/components/Whitelist.vue @@ -0,0 +1,43 @@ + + + diff --git a/web/components/layout/Sidebar.vue b/web/components/layout/Sidebar.vue index ebb0fdf..fa61a99 100644 --- a/web/components/layout/Sidebar.vue +++ b/web/components/layout/Sidebar.vue @@ -4,10 +4,6 @@ Home - - - Whitelist - Team diff --git a/web/layouts/default.vue b/web/layouts/default.vue index 7bde4f5..036a131 100644 --- a/web/layouts/default.vue +++ b/web/layouts/default.vue @@ -2,8 +2,8 @@
+ + diff --git a/web/pages/index.vue b/web/pages/index.vue index 52939ac..0c9fc5c 100644 --- a/web/pages/index.vue +++ b/web/pages/index.vue @@ -1,6 +1,24 @@ @@ -10,4 +28,32 @@ definePageMeta({ }) const user = useState('user') + +const refreshUsername = async () => { + try { + const response = await $fetch('/api/minecraft/refreshusername') + + user.value.minecraft.username = response.username + + useToast().success('Username is ververst') + } catch (e) { + console.log(e) + + useToast().error(e.statusMessage) + } +} + +const removeWhitelist = async () => { + try { + await $fetch('/api/minecraft/removewhitelist') + + user.value.minecraft.uuid = null + user.value.minecraft.username = null + + useToast().success('Minecraft is niet meer gekoppeld') + } catch (e) { + console.log(e) + useToast().error(e.statusMessage) + } +} diff --git a/web/pages/whitelist.vue b/web/pages/whitelist.vue deleted file mode 100644 index 3a3259a..0000000 --- a/web/pages/whitelist.vue +++ /dev/null @@ -1,87 +0,0 @@ - - - diff --git a/web/tsconfig.json b/web/tsconfig.json index a746f2a..02a2d85 100644 --- a/web/tsconfig.json +++ b/web/tsconfig.json @@ -1,4 +1,5 @@ { // https://nuxt.com/docs/guide/concepts/typescript - "extends": "./.nuxt/tsconfig.json" + "extends": "./.nuxt/tsconfig.json", + "allowJS": true }