Added a bunch of functions
This commit is contained in:
94
mod/remappedSrc/com/xeovalyte/polarcraft/PolarcraftMod.java
Normal file
94
mod/remappedSrc/com/xeovalyte/polarcraft/PolarcraftMod.java
Normal file
@@ -0,0 +1,94 @@
|
||||
package com.xeovalyte.polarcraft;
|
||||
|
||||
import com.xeovalyte.polarcraft.util.messageFunctions;
|
||||
import com.xeovalyte.polarcraft.util.verifyFunction;
|
||||
|
||||
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.server.network.ServerPlayerEntity;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.io.File;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
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.
|
||||
// It is considered best practice to use your mod id as the logger's name.
|
||||
// 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");
|
||||
|
||||
public static String configChatMessageUrl = "https://example.com/api/minecraft/message/chattodiscord";
|
||||
public static String configGameMessageUrl = "https://example.com/api/minecraft/message/gametodiscord";
|
||||
public static String configVerifyUrl = "https://example.com/verifyminecraft";
|
||||
|
||||
@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;
|
||||
|
||||
verifyFunction.onPlayerJoin(player, server);
|
||||
});
|
||||
|
||||
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
|
||||
ServerPlayerEntity player = (ServerPlayerEntity) handler.player;
|
||||
messageFunctions.sendGameMessage(player.getUuid(), player.getDisplayName().getString() + " left the game");
|
||||
});
|
||||
|
||||
ServerMessageEvents.CHAT_MESSAGE.register((message, sender, params) -> {
|
||||
messageFunctions.sendChatMessage(message, sender);
|
||||
});
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
if (!CONFIG_FILE.exists()) {
|
||||
saveConfig();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
MyModConfig config = GSON.fromJson(FileUtils.readFileToString(CONFIG_FILE, StandardCharsets.UTF_8), MyModConfig.class);
|
||||
configChatMessageUrl = config.chatMessageUrl;
|
||||
configGameMessageUrl = config.gameMessageUrl;
|
||||
configVerifyUrl = config.verifyUrl;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void saveConfig() {
|
||||
try {
|
||||
FileUtils.writeStringToFile(CONFIG_FILE, GSON.toJson(new MyModConfig(configChatMessageUrl, configGameMessageUrl, configVerifyUrl)), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyModConfig {
|
||||
public String chatMessageUrl;
|
||||
public String gameMessageUrl;
|
||||
public String verifyUrl;
|
||||
|
||||
public MyModConfig(String chatMessageUrl, String gameMessageUrl,String verifyUrl) {
|
||||
this.chatMessageUrl = chatMessageUrl;
|
||||
this.gameMessageUrl = gameMessageUrl;
|
||||
this.verifyUrl = verifyUrl;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.xeovalyte.polarcraft.mixin;
|
||||
|
||||
import com.xeovalyte.polarcraft.util.messageFunctions;
|
||||
import net.minecraft.advancement.Advancement;
|
||||
import net.minecraft.advancement.PlayerAdvancementTracker;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Mixin(PlayerAdvancementTracker.class)
|
||||
public class MixinPlayerAdvancementTracker {
|
||||
@Shadow
|
||||
private ServerPlayerEntity owner;
|
||||
|
||||
@Inject(method = "grantCriterion",
|
||||
at = @At(
|
||||
target = "Lnet/minecraft/server/PlayerManager;broadcast(Lnet/minecraft/text/Text;Z)V",
|
||||
value = "INVOKE"
|
||||
)
|
||||
)
|
||||
|
||||
private void grantCriterion(Advancement advancement, String criterionName, CallbackInfoReturnable<Boolean> cir) {
|
||||
// messageFunctions.sendGameMessage(owner.getUuid(), " has completed the challenge [" + Objects.requireNonNull(advancement.getDisplay()).getTitle().getString() + "]");
|
||||
messageFunctions.sendGameMessage(owner.getUuid(), owner.getDisplayName().getString() + " has completed the advancement [" + Objects.requireNonNull(advancement.getDisplay()).getTitle().getString() + "]");
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.xeovalyte.polarcraft.mixin;
|
||||
|
||||
import com.xeovalyte.polarcraft.util.messageFunctions;
|
||||
import net.minecraft.entity.damage.DamageSource;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.Text;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
||||
@Mixin(ServerPlayerEntity.class)
|
||||
public class MixinPlayerEntity {
|
||||
@Inject(method = "onDeath",
|
||||
at = @At(
|
||||
target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;sendPacket(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/PacketCallbacks;)V",
|
||||
value = "INVOKE",
|
||||
ordinal = 0),
|
||||
locals = LocalCapture.CAPTURE_FAILSOFT
|
||||
)
|
||||
private void onDeath(DamageSource damageSource, CallbackInfo ci, boolean bl, Text text){
|
||||
ServerPlayerEntity serverPlayerEntity = (ServerPlayerEntity) (Object) this;
|
||||
|
||||
messageFunctions.sendGameMessage(serverPlayerEntity.getUuid(), text.getString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
package com.xeovalyte.polarcraft.util;
|
||||
|
||||
import com.xeovalyte.polarcraft.PolarcraftMod;
|
||||
|
||||
import net.minecraft.network.message.SignedMessage;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
|
||||
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.util.UUID;
|
||||
|
||||
public class messageFunctions {
|
||||
public static void sendGameMessage(UUID uuid, String message) {
|
||||
try {
|
||||
// Create a URL object for the server endpoint
|
||||
URL url = new URL(PolarcraftMod.configGameMessageUrl);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setDoOutput(true);
|
||||
|
||||
String requestBody = "{\"content\":\"" + message + "\", \"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);
|
||||
}
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendChatMessage( SignedMessage message, ServerPlayerEntity sender) {
|
||||
try {
|
||||
// Create a URL object for the server endpoint
|
||||
URL url = new URL(PolarcraftMod.configChatMessageUrl);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setDoOutput(true);
|
||||
|
||||
String requestBody = "{\"content\":\"" + message.getSignedContent() + "\", \"uuid\":\"" + sender.getUuid() + "\"}";
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
package com.xeovalyte.polarcraft.util;
|
||||
|
||||
import com.xeovalyte.polarcraft.PolarcraftMod;
|
||||
|
||||
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.util.UUID;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
public class verifyFunction {
|
||||
public static void onPlayerJoin(ServerPlayerEntity player, MinecraftServer server) {
|
||||
UUID uuid = player.getUuid();
|
||||
|
||||
try {
|
||||
URL url = new URL(PolarcraftMod.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);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
Gson gson = new Gson();
|
||||
JsonElement element = gson.fromJson(response.toString(), JsonElement.class);
|
||||
JsonObject jsonResponse = element.getAsJsonObject();
|
||||
|
||||
boolean whitelisted = jsonResponse.get("whitelisted").getAsBoolean();
|
||||
|
||||
if (!whitelisted) {
|
||||
int code = jsonResponse.get("code").getAsInt();
|
||||
|
||||
player.networkHandler.disconnect(Text.literal("Whitelist yourself by using this code: " + code));
|
||||
} else {
|
||||
server.getCommandManager().executeWithPrefix(server.getCommandSource(), "/lp user " + player.getUuid() + " meta set display " + "\"" + jsonResponse.get("username").getAsString() + "\"");
|
||||
|
||||
messageFunctions.sendGameMessage(player.getUuid(), jsonResponse.get("rawUsername").getAsString() + " joined the game");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
player.networkHandler.disconnect(Text.literal("There was an error while verifing your account"));
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user