Skip to content

Commit

Permalink
feat: add requesting codes and whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
okdargy committed Apr 7, 2024
1 parent cfacea7 commit 343d9d2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

implementation 'com.google.code.gson:gson:2.10.1'
}

processResources {
Expand Down
95 changes: 72 additions & 23 deletions src/main/java/co/greasygang/PlayerJoinHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,83 @@
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;

import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.text.MutableText;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.Style;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest;
import com.google.gson.*;

public class PlayerJoinHandler {
public static void init() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
MutableText text = Text.literal("Text");
Style textStyle = text.getStyle()
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://greasygang.co/minecraft"));
text.setStyle(textStyle);
handler.getPlayer().networkHandler.disconnect(Text.literal("[ Click Here Generate Config ]")
.setStyle(
Style.EMPTY
.withFormatting(Formatting.GREEN)
.withClickEvent(
new ClickEvent(ClickEvent.Action.OPEN_URL,
"https://greasygang.co/minecraft"))));

});
}
// first, check if the player is whitelisted
HttpRequest whitelistCheckRequest = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:4000/checkWhitelistByUuid?uuid=" + handler.getPlayer().getUuidAsString()))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();

HttpResponse<String> whitelistCheckResponse = null;

try {
whitelistCheckResponse = HttpClient.newHttpClient().send(whitelistCheckRequest,
HttpResponse.BodyHandlers.ofString());
} catch (IOException e) {
handler.getPlayer().networkHandler.disconnect(Text.of("§6An error occurred while trying to whitelist you on this server."));
e.printStackTrace();
} catch (InterruptedException e) {
handler.getPlayer().networkHandler.disconnect(Text.of("§6An error occurred while trying to whitelist you on this server."));
e.printStackTrace();
}

// parse response as json
Gson gson = new Gson();
JsonObject whitelistCheckResponseJson = gson.fromJson(whitelistCheckResponse.body(), JsonObject.class);

// print json response
System.out.println(whitelistCheckResponseJson);

// check if the player is whitelisted
if (whitelistCheckResponseJson.get("linked").getAsBoolean()) {
System.out.println("Player is whitelisted on greasygang.co!");
return;
}

// if the player is not whitelisted, generate a code and disconnect them
HttpRequest codeGenerationRequest = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:4000/createWhitelistCode?uuid=" + handler.getPlayer().getUuidAsString()))
.method("POST", HttpRequest.BodyPublishers.noBody())
.build();

// import java.io.IOException;
// import java.net.URI;
// import java.net.http.HttpClient;
// import java.net.http.HttpResponse;
// import java.net.http.HttpRequest;
HttpResponse<String> codeGenerationResponse = null;

try {
codeGenerationResponse = HttpClient.newHttpClient().send(codeGenerationRequest,
HttpResponse.BodyHandlers.ofString());
} catch (IOException e) {
handler.getPlayer().networkHandler.disconnect(Text.of("§6An error occurred while trying to generate a code for you on the server."));
e.printStackTrace();
} catch (InterruptedException e) {
handler.getPlayer().networkHandler.disconnect(Text.of("§6An error occurred while trying to generate a code for you on the server."));
e.printStackTrace();
}

// parse response as json
JsonObject codeGenerationResponseJson = gson.fromJson(codeGenerationResponse.body(), JsonObject.class);

// print json response
System.out.println(codeGenerationResponseJson);

// disconnect the player
handler.getPlayer().networkHandler.disconnect(Text.of("""
§6You are not whitelisted on this server!
§6Please visit §egreasygang.co §6to gain access to the server.
§6Your code is: §e""" + codeGenerationResponseJson.get("token").getAsString() + """
§c§lDO NOT SHARE THIS CODE WITH ANYBODY!"""));
});
}

// private static void main(String[] args) {
// // TODO: change to greasygang.co
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Whitelistlink implements ModInitializer {
public class WhitelistLink 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("whitelist-link");
public static final String MOD_ID = "whitelistlink";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

@Override
public void onInitialize() {
Expand Down
15 changes: 4 additions & 11 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"id": "whitelist-link",
"version": "${version}",
"name": "whitelist-link",
"description": "This is an example description! Tell everyone what your mod is about!",
"description": "Connects the greasygang.co whitelist to your Minecraft server",
"authors": [
"Me!"
"veryCrunchy, dargy"
],
"contact": {
"homepage": "https://fabricmc.net/",
Expand All @@ -16,18 +16,11 @@
"environment": "*",
"entrypoints": {
"main": [
"co.greasygang.Whitelistlink"
],
"client": [
"co.greasygang.WhitelistlinkClient"
"co.greasygang.WhitelistLink"
]
},
"mixins": [
"whitelist-link.mixins.json",
{
"config": "whitelist-link.client.mixins.json",
"environment": "client"
}
"whitelist-link.mixins.json"
],
"depends": {
"fabricloader": ">=0.15.9",
Expand Down

0 comments on commit 343d9d2

Please sign in to comment.