This commit is contained in:
2023-05-03 11:36:20 +02:00
parent 12320b6a09
commit 5aa8dce299
7 changed files with 247 additions and 138 deletions

View File

@@ -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) {