Skip to content

Commit 165914a

Browse files
committed
Add a uuid command
1 parent cf5b908 commit 165914a

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

src/main/java/dev/ithundxr/railwaystweaks/commands/RailwaysTweaksCommands.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package dev.ithundxr.railwaystweaks.commands;
22

33
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.arguments.StringArgumentType;
45
import com.mojang.brigadier.builder.ArgumentBuilder;
56
import com.mojang.brigadier.context.CommandContext;
67
import com.simibubi.create.Create;
78
import com.simibubi.create.content.trains.entity.CarriageContraptionEntity;
89
import dev.ithundxr.railwaystweaks.RailwaysTweaks;
910
import dev.ithundxr.railwaystweaks.mixin.compat.tconstruct.SimpleChannelAccessor;
11+
import dev.ithundxr.railwaystweaks.utils.UUIDFinder;
1012
import me.pepperbell.simplenetworking.C2SPacket;
1113
import me.pepperbell.simplenetworking.S2CPacket;
1214
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
@@ -40,10 +42,18 @@ public static void init() {
4042
.executes(ctx -> avgMSPT(ctx.getSource())));
4143
});
4244

45+
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
46+
dispatcher.register(literal("uuid")
47+
.then(Commands.argument("player_name", StringArgumentType.string())
48+
.executes(RailwaysTweaksCommands::getPlayerUUID)
49+
)
50+
);
51+
});
52+
4353
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
4454
dispatcher.register(literal("railwaystweaks")
4555
.then(
46-
literal("opac-party")
56+
literal("opac-party")
4757
.then(Commands.argument("player_uuid", UuidArgument.uuid())
4858
.executes(RailwaysTweaksCommands::getPlayerPartyName)
4959
)
@@ -132,11 +142,23 @@ private static int getPlayerPartyName(CommandContext<CommandSourceStack> ctx) {
132142
IServerPartyAPI partyAPI = api.getPartyManager().getPartyByMember(uuid);
133143

134144
if (partyAPI != null) {
135-
ctx.getSource().sendSuccess(() -> Component.literal(partyAPI.getDefaultName() + "\n" + partyAPI.getId()), true);
145+
ctx.getSource().sendSuccess(() -> Component.literal(partyAPI.getDefaultName() + "\n" + partyAPI.getId()), false);
136146
return 0;
137147
} else {
138148
ctx.getSource().sendFailure(Component.literal("Failed to get a party uuid from this player"));
139149
return 1;
140150
}
141151
}
152+
153+
private static int getPlayerUUID(CommandContext<CommandSourceStack> ctx) {
154+
String name = StringArgumentType.getString(ctx, "player_name");
155+
156+
if (name != null) {
157+
UUIDFinder.findUuid(name, (uuid) -> {
158+
if (uuid != null)
159+
ctx.getSource().sendSuccess(() -> Component.literal(uuid.toString()), false);
160+
});
161+
}
162+
return 0;
163+
}
142164
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package dev.ithundxr.railwaystweaks.utils;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
6+
import java.io.IOException;
7+
import java.net.HttpURLConnection;
8+
import java.net.URL;
9+
import java.util.Scanner;
10+
import java.util.UUID;
11+
import java.util.concurrent.CompletableFuture;
12+
import java.util.function.Consumer;
13+
14+
public class UUIDFinder {
15+
private static final String MOJANG_UUID_API = "https://api.mojang.com/users/profiles/minecraft/";
16+
private static final int TIMEOUT_IN_SECS = 3;
17+
18+
private static CompletableFuture<UUID> uuidFuture;
19+
20+
/**
21+
* Takes a users name and finds a matching UUID
22+
*/
23+
public static void findUuid(String playerName, Consumer<UUID> finish) {
24+
// Asynchronously search for a users UUID
25+
uuidFuture = CompletableFuture.supplyAsync(() -> UUIDFinder.getUuidFromPlayer(playerName));
26+
uuidFuture.thenAccept(finish);
27+
}
28+
29+
private static UUID getUuidFromPlayer(String playerName) {
30+
UUID uuid;
31+
32+
try {
33+
URL uuidGetRequest = new URL(MOJANG_UUID_API + playerName);
34+
var httpURLConnection = (HttpURLConnection) uuidGetRequest.openConnection();
35+
httpURLConnection.setRequestMethod("GET");
36+
37+
int responseCode = httpURLConnection.getResponseCode();
38+
if (responseCode != 200)
39+
uuidFuture.completeExceptionally(new IOException("Failed to get a valid UUID for the player"));
40+
41+
var stringBuilder = new StringBuilder();
42+
var scanner = new Scanner(uuidGetRequest.openStream());
43+
while (scanner.hasNext())
44+
stringBuilder.append(scanner.nextLine());
45+
scanner.close();
46+
47+
var uuidObject = (JsonObject) JsonParser.parseString(stringBuilder.toString());
48+
String id = uuidObject.get("id").getAsString();
49+
50+
// Format into a proper uuid (accepted) uuid format complete with '-' characters
51+
String idFormatted = id.substring(0, 8) + "-" + id.substring(8, 12) + "-"
52+
+ id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32);
53+
54+
uuid = UUID.fromString(idFormatted);
55+
56+
} catch (IOException e) {
57+
uuid = null;
58+
}
59+
return uuid;
60+
}
61+
}

0 commit comments

Comments
 (0)