aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLulonaut <lulonaut@lulonaut.tech>2023-12-26 10:24:41 +0100
committerGitHub <noreply@github.com>2023-12-26 10:24:41 +0100
commitaccf3c3124126db1232f7aee2b25000d6e5d6038 (patch)
treeb36f05de67c9e7ff9bce20da6a82b8478504b10e
parentcdac22ba4543d16445148fe77e7adca08163ef54 (diff)
downloadNotEnoughUpdates-accf3c3124126db1232f7aee2b25000d6e5d6038.tar.gz
NotEnoughUpdates-accf3c3124126db1232f7aee2b25000d6e5d6038.tar.bz2
NotEnoughUpdates-accf3c3124126db1232f7aee2b25000d6e5d6038.zip
migrate pronoundb integration to new api version (#964)
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/profileviewer/BasicPage.java5
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/util/PronounDB.java210
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.kt2
3 files changed, 103 insertions, 114 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/BasicPage.java b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/BasicPage.java
index d10acea9..41e5fc5a 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/BasicPage.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/BasicPage.java
@@ -62,6 +62,7 @@ import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -197,9 +198,7 @@ public class BasicPage extends GuiProfileViewerPage {
.flatMap(it -> it); // Flatten: First optional is whether it loaded, second optional is whether it was successful
if (pronounChoice.isPresent()) {
PronounDB.PronounChoice pronouns = pronounChoice.get();
- if (pronouns.isConsciousChoice()) {
- getInstance().tooltipToDisplay = pronouns.render();
- }
+ getInstance().tooltipToDisplay = Collections.singletonList(pronouns.render());
}
}
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/PronounDB.java b/src/main/java/io/github/moulberry/notenoughupdates/util/PronounDB.java
index 4c0b73eb..c38b43a8 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/util/PronounDB.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/util/PronounDB.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2022 NotEnoughUpdates contributors
+ * Copyright (C) 2022-2023 NotEnoughUpdates contributors
*
* This file is part of NotEnoughUpdates.
*
@@ -19,160 +19,150 @@
package io.github.moulberry.notenoughupdates.util;
+import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
+import lombok.Getter;
import java.util.Arrays;
import java.util.List;
+import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
+/**
+ * pronoundb.org integration
+ */
public class PronounDB {
-
- private static boolean isDisabled() {
- JsonObject disabled = Constants.DISABLE;
- return disabled != null && disabled.has("pronoundb");
- }
-
/**
* Returns an Optional, since JVMs can be *very* funky with KeyStore loading
*/
public static CompletableFuture<Optional<JsonObject>> performPronouning(String platform, String id) {
- if (isDisabled()) return CompletableFuture.completedFuture(Optional.empty());
+ if (isDisabledByRepo()) return CompletableFuture.completedFuture(Optional.empty());
return NotEnoughUpdates.INSTANCE.manager.apiUtils
.request()
- .url("https://pronoundb.org/api/v1/lookup")
+ .url("https://pronoundb.org/api/v2/lookup")
.queryArgument("platform", platform)
- .queryArgument("id", id)
+ .queryArgument("ids", id)
.requestJson()
.handle((result, ex) -> Optional.ofNullable(result));
}
- public enum Pronoun {
- HE("he", "him", "his"),
- IT("it", "it", "its"),
- SHE("she", "her", "hers"),
- THEY("they", "them", "theirs");
+ private static boolean isDisabledByRepo() {
+ JsonObject disabled = Constants.DISABLE;
+ return disabled != null && disabled.has("pronoundb");
+ }
- private final String subject;
- private final String object;
- private final String possessive;
+ /**
+ * Get the preferred pronouns from the pronoundb.org api for the specified platform
+ */
+ public static CompletableFuture<Optional<PronounChoice>> getPronounsFor(String platform, String name) {
+ return performPronouning(platform, name).thenApply(it -> it.flatMap(jsonObject -> parsePronouns(jsonObject, name)));
+ }
- Pronoun(String subject, String object, String possessive) {
- this.subject = subject;
- this.object = object;
- this.possessive = possessive;
- }
+ private static Optional<PronounChoice> parsePronouns(JsonObject pronounObject, String name) {
+ if (pronounObject.has(name)) {
- public String getSubject() {
- return subject;
- }
+ List<String> set = JsonUtils.transformJsonArrayToList(Utils
+ .getElementOrDefault(pronounObject, name + ".sets.en", new JsonArray())
+ .getAsJsonArray(), JsonElement::getAsString);
- public String getObject() {
- return object;
- }
+ List<Pronoun> pronouns = set.stream().map(pronounId -> Arrays
+ .stream(Pronoun.values())
+ .filter(pronoun -> pronoun.id.equals(pronounId))
+ .findFirst()
+ .orElse(null)).filter(Objects::nonNull).collect(Collectors.toList());
+
+ if (pronouns.isEmpty()) {
+ return Optional.empty();
+ }
+
+ if (pronouns.size() >= 2) {
+ return Optional.of(new PronounChoice(pronouns.get(0), pronouns.get(1)));
+ } else {
+ return Optional.of(new PronounChoice(pronouns.get(0), null));
- public String getPossessive() {
- return possessive;
+ }
}
+ return Optional.empty();
+ }
+
+ /**
+ * Get the preferred pronouns from the pronoundb.org api for the minecraft platform
+ */
+ public static CompletableFuture<Optional<PronounChoice>> getPronounsFor(UUID minecraftPlayer) {
+ return getPronounsFor("minecraft", minecraftPlayer.toString() /* dashed UUID */);
+ }
+
+ public static void test(UUID uuid) {
+ System.out.println("Pronouning...");
+ getPronounsFor(uuid).thenAccept(it -> {
+ PronounChoice pronounsFor = it.get();
+ System.out.println(pronounsFor.render());
+ Utils.addChatMessage(pronounsFor.render());
+ });
}
- public enum PronounChoice {
- UNSPECIFIED("unspecified", "Unspecified"),
- HE("hh", Pronoun.HE),
- HEIT("hi", Pronoun.HE, Pronoun.IT),
- HESHE("hs", Pronoun.HE, Pronoun.SHE),
- HETHEY("ht", Pronoun.HE, Pronoun.THEY),
- ITHE("ih", Pronoun.IT, Pronoun.HE),
- IT("ii", Pronoun.IT),
- ITSHE("is", Pronoun.IT, Pronoun.SHE),
- ITTHEY("it", Pronoun.IT, Pronoun.THEY),
- SHEHE("shh", Pronoun.SHE, Pronoun.HE),
- SHE("sh", Pronoun.SHE),
- SHEIT("si", Pronoun.SHE, Pronoun.IT),
- SHETHEY("st", Pronoun.SHE, Pronoun.THEY),
- THEYHE("th", Pronoun.THEY, Pronoun.HE),
- THEYIT("ti", Pronoun.THEY, Pronoun.IT),
- THEYSHE("ts", Pronoun.THEY, Pronoun.SHE),
- THEY("tt", Pronoun.THEY),
+ @Getter
+ public enum Pronoun {
+ HE("he", "him", "his"),
+ IT("it", "it", "its"),
+ SHE("she", "her", "hers"),
+ THEY("they", "them", "theirs"),
ANY("any", "Any pronouns"),
- OTHER("other", "Other pronouns"),
- ASK("ask", "Ask me my pronouns"),
- AVOID("avoid", "Avoid pronouns, use my name");
+ OTHER("other", "Other"),
+ ASK("ask", "Ask for pronouns"),
+ AVOID("avoid", "Avoid pronouns");
+
private final String id;
- private List<Pronoun> pronouns = null;
- private String override = null;
+ private final String object;
+ private final String possessive;
+ private final String override;
- PronounChoice(String id, String override) {
- this.override = override;
+ Pronoun(String id, String object, String possessive) {
this.id = id;
+ this.object = object;
+ this.possessive = possessive;
+ this.override = null;
}
- PronounChoice(String id, Pronoun... pronouns) {
+ Pronoun(String id, String override) {
this.id = id;
- this.pronouns = Arrays.asList(pronouns);
- }
-
- public static Optional<PronounChoice> findPronounsForId(String id) {
- for (PronounChoice value : values()) {
- if (value.id.equals(id)) return Optional.of(value);
- }
- return Optional.empty();
- }
-
- public String getOverride() {
- return override;
- }
-
- public List<Pronoun> getPronounsInPreferredOrder() {
- return pronouns;
- }
-
- public String getId() {
- return id;
+ this.override = override;
+ this.object = null;
+ this.possessive = null;
}
+ }
- public List<String> render() {
- if (override != null)
- return Arrays.asList(override);
- return pronouns
- .stream()
- .map(pronoun -> pronoun.getSubject() + "/" + pronoun.getObject())
- .collect(Collectors.toList());
- }
+ @Getter
+ public static class PronounChoice {
+ private final Pronoun firstPronoun;
+ private final Pronoun secondPronoun;
- public boolean isConsciousChoice() {
- return this != UNSPECIFIED;
+ PronounChoice(Pronoun firstPronoun, Pronoun secondPronoun) {
+ this.firstPronoun = firstPronoun;
+ this.secondPronoun = secondPronoun;
}
- }
+ /**
+ * Convert the pronoun choice into a readable String for the user
+ *
+ * @see Pronoun
+ */
+ public String render() {
+ if (firstPronoun.override != null) {
+ return firstPronoun.override;
+ }
- public static Optional<PronounChoice> parsePronouns(JsonObject pronounObject) {
- if (pronounObject.has("pronouns")) {
- JsonElement pronouns = pronounObject.get("pronouns");
- if (pronouns.isJsonPrimitive() && pronouns.getAsJsonPrimitive().isString())
- return PronounChoice.findPronounsForId(pronouns.getAsString());
+ if (secondPronoun == null) {
+ return firstPronoun.id + "/" + firstPronoun.object;
+ } else {
+ return firstPronoun.id + "/" + secondPronoun.id;
+ }
}
- return Optional.empty();
- }
-
- public static CompletableFuture<Optional<PronounChoice>> getPronounsFor(String platform, String name) {
- return performPronouning(platform, name).thenApply(it -> it.flatMap(PronounDB::parsePronouns));
- }
-
- public static CompletableFuture<Optional<PronounChoice>> getPronounsFor(UUID minecraftPlayer) {
- return getPronounsFor("minecraft", minecraftPlayer.toString() /* dashed UUID */);
- }
-
- public static void test() {
- System.out.println("Pronouning...");
- getPronounsFor(UUID.fromString("842204e6-6880-487b-ae5a-0595394f9948")).thenAccept(it -> {
- PronounChoice pronounsFor = it.get();
- pronounsFor.render().forEach(System.out::println);
- });
}
}
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.kt
index cf55416b..e8c12442 100644
--- a/src/main/kotlin/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.kt
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.kt
@@ -225,7 +225,7 @@ class DevTestCommand {
reply(AQUA.toString() + "I would never search")
}.withHelp("Reset your search data to redisplay the search tutorial")
thenLiteralExecute("bluehair") {
- PronounDB.test()
+ PronounDB.test(MC.thePlayer.uniqueID)
}.withHelp("Test the pronoundb integration")
thenLiteral("opengui") {
thenArgumentExecute("class", StringArgumentType.string()) { className ->