Update to PronounDB's v2 API.

This commit is contained in:
PandaNinjas 2023-10-01 19:32:26 -07:00
parent a467a4d50d
commit 1cffd3895b
No known key found for this signature in database
GPG key ID: 63A34FF91DBE5FE1

View file

@ -40,27 +40,15 @@ public class PlayerPronouns implements ModInitializer, PronounsApi.PronounReader
private static final String USER_AGENT = "player-pronouns/1.0 (+https://ashhhleyyy.dev/projects/2021/player-pronouns)"; private static final String USER_AGENT = "player-pronouns/1.0 (+https://ashhhleyyy.dev/projects/2021/player-pronouns)";
private static final Map<String, String> PRONOUNDB_ID_MAP = new HashMap<>() {{ private static final Map<String, String> PRONOUNDB_ID_MAP = new HashMap<>() {{
// short pronoun set identifier map from https://pronoundb.org/docs // short pronoun set identifier map from https://pronoundb.org/wiki/api-docs
put("hh", "he/him"); put("he", "he/him");
put("hi", "he/it"); put("it", "it/its");
put("hs", "he/she"); put("she", "she/her");
put("ht", "he/they"); put("they", "they/them");
put("ih", "it/he");
put("ii", "it/its");
put("is", "it/she");
put("it", "it/they");
put("shh", "she/he");
put("sh", "she/her");
put("si", "she/it");
put("st", "she/they");
put("th", "they/he");
put("ti", "they/it");
put("ts", "they/she");
put("tt", "they/them");
put("any", "any"); put("any", "any");
put("other", "other");
put("ask", "ask"); put("ask", "ask");
put("avoid", "avoid"); put("avoid", "avoid");
put("other", "other");
}}; }};
private PronounDatabase pronounDatabase; private PronounDatabase pronounDatabase;
@ -101,7 +89,7 @@ public class PlayerPronouns implements ModInitializer, PronounsApi.PronounReader
var currentPronouns = pronounDatabase.get(handler.getPlayer().getUuid()); var currentPronouns = pronounDatabase.get(handler.getPlayer().getUuid());
if (currentPronouns != null && !currentPronouns.remote()) return; if (currentPronouns != null && !currentPronouns.remote()) return;
var pronounDbUrl = "https://pronoundb.org/api/v1/lookup?platform=minecraft&id=%s" var pronounDbUrl = "https://pronoundb.org/api/v2/lookup?platform=minecraft&ids=%s"
.formatted(handler.getPlayer().getUuid()); .formatted(handler.getPlayer().getUuid());
try { try {
var client = HttpClient.newBuilder() var client = HttpClient.newBuilder()
@ -122,10 +110,21 @@ public class PlayerPronouns implements ModInitializer, PronounsApi.PronounReader
if (currentPronouns2 != null && !currentPronouns2.remote()) return; if (currentPronouns2 != null && !currentPronouns2.remote()) return;
var json = JsonHelper.deserialize(body); var json = JsonHelper.deserialize(body);
var pronounsResponse = json.get("pronouns").getAsString(); StringBuilder pronouns = new StringBuilder("unspecified");
if (!"unspecified".equals(pronounsResponse)) { if (json.has(handler.getPlayer().getUuid().toString()) && json.getAsJsonObject(handler.getPlayer().getUuid().toString()).getAsJsonObject("sets").has("en")) {
var pronouns = PRONOUNDB_ID_MAP.getOrDefault(pronounsResponse, "ask"); var pronounsList = json.getAsJsonObject(handler.getPlayer().getUuid().toString()).getAsJsonObject("sets").getAsJsonArray("en");
setPronouns(handler.getPlayer(), new Pronouns(pronouns, PronounList.get().getCalculatedPronounStrings().get(pronouns), true)); if (pronounsList.size() == 1) {
pronouns = new StringBuilder(PRONOUNDB_ID_MAP.get(pronounsList.get(0).getAsString()));
} else {
pronouns = new StringBuilder();
for (var prounoun: pronounsList) {
pronouns.append(prounoun.getAsString()).append("/");
}
pronouns = new StringBuilder(pronouns.substring(0, pronouns.length() - 1)); // remove trailing slash
}
}
if (!"unspecified".contentEquals(pronouns)) {
setPronouns(handler.getPlayer(), new Pronouns(pronouns.toString(), PronounList.get().getCalculatedPronounStrings().get(pronouns.toString()), true));
} }
}, server); }, server);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {