aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/commands/misc/PronounsCommand.java
blob: 5a4f14000dc6f0cb966e44a2751ec76585edd3e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2022 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.commands.misc;

import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.commands.ClientCommandBase;
import io.github.moulberry.notenoughupdates.util.MinecraftExecutor;
import io.github.moulberry.notenoughupdates.util.PronounDB;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;

import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class PronounsCommand extends ClientCommandBase {
	public PronounsCommand() {
		super("neupronouns");
	}

	@Override
	public String getCommandUsage(ICommandSender sender) {
		return "/neupronouns <username> [platform]";
	}

	@Override
	public void processCommand(ICommandSender sender, String[] args) throws CommandException {
		switch (args.length) {
			case 1:
				fetchPronouns("minecraft", args[0]);
				break;
			case 2:
				fetchPronouns(args[1], args[0]);
				break;
			default:
				sender.addChatMessage(new ChatComponentText("§4" + getCommandUsage(sender)));
		}
	}

	private void fetchPronouns(String platform, String user) {
		GuiNewChat nc = Minecraft.getMinecraft().ingameGUI.getChatGUI();
		int id = new Random().nextInt();
		nc.printChatMessageWithOptionalDeletion(new ChatComponentText("§e[NEU] Fetching Pronouns..."), id);

		CompletableFuture<Optional<PronounDB.PronounChoice>> pronouns;
		if ("minecraft".equals(platform)) {
			CompletableFuture<UUID> c = new CompletableFuture<>();
			NotEnoughUpdates.profileViewer.getPlayerUUID(user, uuidString -> {
				if (uuidString == null) {
					c.completeExceptionally(new NullPointerException());
				} else {
					c.complete(Utils.parseDashlessUUID(uuidString));
				}
			});
			pronouns = c.thenCompose(PronounDB::getPronounsFor);
		} else {
			pronouns = PronounDB.getPronounsFor(platform, user);
		}
		pronouns.handleAsync((pronounChoice, throwable) -> {
			if (throwable != null || !pronounChoice.isPresent()) {
				nc.printChatMessageWithOptionalDeletion(new ChatComponentText("§e[NEU] §4Failed to fetch pronouns."), id);
				return null;
			}
			PronounDB.PronounChoice betterPronounChoice = pronounChoice.get();
			nc.printChatMessageWithOptionalDeletion(new ChatComponentText(
				"§e[NEU] Pronouns for §b" + user + " §eon §b" + platform + "§e:"), id);
			betterPronounChoice.render().forEach(it -> nc.printChatMessage(new ChatComponentText("§e[NEU] §a" + it)));
			return null;
		}, MinecraftExecutor.INSTANCE);

	}
}