From 4dbd0ee8f84f3c1f8f7e9d9336cbda705f146526 Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Sun, 17 Jul 2022 19:38:53 +0100 Subject: [PATCH] feat(pronoundb): don't replace user-set pronouns --- .../ashisbored/playerpronouns/PlayerPronouns.java | 12 ++++++++++-- .../playerpronouns/command/PronounsCommand.java | 4 ++-- .../playerpronouns/data/BinaryPronounDatabase.java | 2 +- .../ashisbored/playerpronouns/data/Pronouns.java | 6 ++++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java index 15fa49b..d796eb1 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java @@ -94,6 +94,10 @@ public class PlayerPronouns implements ModInitializer { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { 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" .formatted(handler.getPlayer().getUuid()); try { @@ -109,13 +113,17 @@ public class PlayerPronouns implements ModInitializer { client.sendAsync(req, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::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 pronounsResponse = json.get("pronouns").getAsString(); if (!"unspecified".equals(pronounsResponse)) { 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) { e.printStackTrace(); } diff --git a/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java b/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java index cfab162..233a4c3 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java @@ -33,9 +33,9 @@ public class PronounsCommand { Pronouns pronouns; if (pronounTexts.containsKey(pronounsString)) { - pronouns = new Pronouns(pronounsString, pronounTexts.get(pronounsString)); + pronouns = new Pronouns(pronounsString, pronounTexts.get(pronounsString), false); } else { - pronouns = new Pronouns(pronounsString, Text.literal(pronounsString)); + pronouns = new Pronouns(pronounsString, Text.literal(pronounsString), false); } if (!PlayerPronouns.setPronouns(player, pronouns)) { diff --git a/src/main/java/io/github/ashisbored/playerpronouns/data/BinaryPronounDatabase.java b/src/main/java/io/github/ashisbored/playerpronouns/data/BinaryPronounDatabase.java index 057b5da..b9f2fd3 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/data/BinaryPronounDatabase.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/data/BinaryPronounDatabase.java @@ -58,7 +58,7 @@ public class BinaryPronounDatabase { if (pronounStrings.containsKey(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); } diff --git a/src/main/java/io/github/ashisbored/playerpronouns/data/Pronouns.java b/src/main/java/io/github/ashisbored/playerpronouns/data/Pronouns.java index f7d16dc..a5971a9 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/data/Pronouns.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/data/Pronouns.java @@ -7,10 +7,12 @@ import xyz.nucleoid.codecs.MoreCodecs; public record Pronouns( String raw, - Text formatted + Text formatted, + boolean remote ) { public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( 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)); }