feat(pronoundb): don't replace user-set pronouns

This commit is contained in:
Ashhhleyyy 2022-07-17 19:38:53 +01:00
parent 1e4d041ef4
commit 4dbd0ee8f8
Signed by: ash
GPG key ID: 83B789081A0878FB
4 changed files with 17 additions and 7 deletions

View file

@ -94,6 +94,10 @@ public class PlayerPronouns implements ModInitializer {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
if(config.enablePronounDBSync()) { if(config.enablePronounDBSync()) {
// Do not override player-set pronouns
var currentPronouns = pronounDatabase.get(handler.getPlayer().getUuid());
if (currentPronouns != null && !currentPronouns.remote()) return;
var pronounDbUrl = "https://pronoundb.org/api/v1/lookup?platform=minecraft&id=%s" var pronounDbUrl = "https://pronoundb.org/api/v1/lookup?platform=minecraft&id=%s"
.formatted(handler.getPlayer().getUuid()); .formatted(handler.getPlayer().getUuid());
try { try {
@ -109,13 +113,17 @@ public class PlayerPronouns implements ModInitializer {
client.sendAsync(req, HttpResponse.BodyHandlers.ofString()) client.sendAsync(req, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body) .thenApply(HttpResponse::body)
.thenAcceptAsync(body -> { .thenAcceptAsync(body -> {
// Do not override player-set pronouns (repeated to prevent race-condition from network delays)
var currentPronouns2 = pronounDatabase.get(handler.getPlayer().getUuid());
if (currentPronouns2 != null && !currentPronouns2.remote()) return;
var json = JsonHelper.deserialize(body); var json = JsonHelper.deserialize(body);
var pronounsResponse = json.get("pronouns").getAsString(); var pronounsResponse = json.get("pronouns").getAsString();
if (!"unspecified".equals(pronounsResponse)) { if (!"unspecified".equals(pronounsResponse)) {
var pronouns = PRONOUNDB_ID_MAP.getOrDefault(pronounsResponse, "ask"); var pronouns = PRONOUNDB_ID_MAP.getOrDefault(pronounsResponse, "ask");
setPronouns(handler.getPlayer(), new Pronouns(pronouns, PronounList.get().getCalculatedPronounStrings().get(pronouns))); setPronouns(handler.getPlayer(), new Pronouns(pronouns, PronounList.get().getCalculatedPronounStrings().get(pronouns), true));
} }
}, server).join(); }, server);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -33,9 +33,9 @@ public class PronounsCommand {
Pronouns pronouns; Pronouns pronouns;
if (pronounTexts.containsKey(pronounsString)) { if (pronounTexts.containsKey(pronounsString)) {
pronouns = new Pronouns(pronounsString, pronounTexts.get(pronounsString)); pronouns = new Pronouns(pronounsString, pronounTexts.get(pronounsString), false);
} else { } else {
pronouns = new Pronouns(pronounsString, Text.literal(pronounsString)); pronouns = new Pronouns(pronounsString, Text.literal(pronounsString), false);
} }
if (!PlayerPronouns.setPronouns(player, pronouns)) { if (!PlayerPronouns.setPronouns(player, pronouns)) {

View file

@ -58,7 +58,7 @@ public class BinaryPronounDatabase {
if (pronounStrings.containsKey(entry.getValue())) { if (pronounStrings.containsKey(entry.getValue())) {
formatted = pronounStrings.get(entry.getValue()); formatted = pronounStrings.get(entry.getValue());
} }
pronouns.put(entry.getKey(), new Pronouns(entry.getValue(), formatted)); pronouns.put(entry.getKey(), new Pronouns(entry.getValue(), formatted, false));
} }
return new PalettePronounDatabase(pronouns); return new PalettePronounDatabase(pronouns);
} }

View file

@ -7,10 +7,12 @@ import xyz.nucleoid.codecs.MoreCodecs;
public record Pronouns( public record Pronouns(
String raw, String raw,
Text formatted Text formatted,
boolean remote
) { ) {
public static final Codec<Pronouns> CODEC = RecordCodecBuilder.create(instance -> instance.group( public static final Codec<Pronouns> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("raw").forGetter(Pronouns::raw), Codec.STRING.fieldOf("raw").forGetter(Pronouns::raw),
MoreCodecs.TEXT.fieldOf("formatted").forGetter(Pronouns::formatted) MoreCodecs.TEXT.fieldOf("formatted").forGetter(Pronouns::formatted),
Codec.BOOL.optionalFieldOf("remote", false).forGetter(Pronouns::remote)
).apply(instance, Pronouns::new)); ).apply(instance, Pronouns::new));
} }