|
1 | 1 | package net.azisaba.interchatmod.fabric; |
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonObject; |
3 | 5 | import com.mojang.brigadier.arguments.StringArgumentType; |
4 | 6 | import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
5 | 7 | import net.azisaba.interchatmod.common.model.Guild; |
6 | 8 | import net.azisaba.interchatmod.common.model.GuildMember; |
7 | 9 | import net.azisaba.interchatmod.common.util.Constants; |
8 | 10 | import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; |
| 11 | +import net.minecraft.client.MinecraftClient; |
9 | 12 | import net.minecraft.command.CommandSource; |
10 | 13 | import net.minecraft.text.*; |
11 | 14 | import net.minecraft.util.Formatting; |
12 | 15 | import org.jetbrains.annotations.NotNull; |
13 | 16 |
|
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.URISyntaxException; |
| 20 | +import java.nio.file.Files; |
14 | 21 | import java.util.*; |
15 | 22 | import java.util.function.Consumer; |
16 | 23 | import java.util.stream.Stream; |
|
19 | 26 | import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.literal; |
20 | 27 |
|
21 | 28 | public class Commands { |
| 29 | + private static final Gson GSON = new Gson(); |
22 | 30 | public static final @NotNull Set<String> KNOWN_PLAYERS = new HashSet<>(); |
23 | 31 |
|
24 | 32 | public static LiteralArgumentBuilder<FabricClientCommandSource> builderGTell() { |
@@ -121,7 +129,7 @@ public static LiteralArgumentBuilder<FabricClientCommandSource> builderGuild() { |
121 | 129 | .then(literal("role") |
122 | 130 | .then(argument("member", StringArgumentType.word()) |
123 | 131 | .suggests((ctx, builder) -> { |
124 | | - var set = Mod.guildMembers.getOrDefault(Mod.client.getSelectedGuild(), Collections.emptySet()); |
| 132 | + Set<GuildMember> set = Mod.guildMembers.getOrDefault(Mod.client.getSelectedGuild(), Collections.emptySet()); |
125 | 133 | return CommandSource.suggestMatching(set.stream().map(GuildMember::name), builder); |
126 | 134 | }) |
127 | 135 | .then(argument("role", StringArgumentType.word()) |
@@ -195,6 +203,11 @@ public static LiteralArgumentBuilder<FabricClientCommandSource> builderGuild() { |
195 | 203 | ) |
196 | 204 | ) |
197 | 205 | ) |
| 206 | + .then(literal("upload_image") |
| 207 | + .then(argument("uuid", StringArgumentType.string()) |
| 208 | + .executes(ctx -> executeUploadImage(ctx.getSource(), StringArgumentType.getString(ctx, "uuid"))) |
| 209 | + ) |
| 210 | + ) |
198 | 211 | ; |
199 | 212 | } |
200 | 213 |
|
@@ -291,6 +304,31 @@ private static int executeChat(FabricClientCommandSource source, String guildNam |
291 | 304 | return 1; |
292 | 305 | } |
293 | 306 |
|
| 307 | + private static int executeUploadImage(FabricClientCommandSource source, String uuid) { |
| 308 | + File image = Mod.images.get(UUID.fromString(uuid)); |
| 309 | + if (image == null) { |
| 310 | + return 0; |
| 311 | + } |
| 312 | + source.sendFeedback(new TranslatableText("generic.uploading").formatted(Formatting.GRAY)); |
| 313 | + Thread thread = new Thread(() -> { |
| 314 | + try { |
| 315 | + byte[] data = Files.readAllBytes(image.toPath()); |
| 316 | + JsonObject obj = GSON.fromJson(Mod.uploadImage(data), JsonObject.class); |
| 317 | + if (!obj.has("uuid")) { |
| 318 | + MinecraftClient.getInstance().execute(() -> source.sendError(new TranslatableText("generic.upload_image_failed", new LiteralText(obj.toString()).formatted(Formatting.RED)))); |
| 319 | + return; |
| 320 | + } |
| 321 | + String imageUuid = obj.get("uuid").getAsString(); |
| 322 | + Mod.client.sendMessageToGuild(null, "https://" + ModConfig.getEffectiveApiHost() + "/interchat/image?key=" + imageUuid); |
| 323 | + } catch (IOException | URISyntaxException e) { |
| 324 | + throw new RuntimeException(e); |
| 325 | + } |
| 326 | + }); |
| 327 | + thread.setName("InterChat Upload Image Thread"); |
| 328 | + thread.start(); |
| 329 | + return 1; |
| 330 | + } |
| 331 | + |
294 | 332 | private static String roleTranslationKey(String role) { |
295 | 333 | return "generic.guild_role." + role.toLowerCase(Locale.ROOT); |
296 | 334 | } |
|
0 commit comments