From 3d53fd1688b72a6ad37ce9ca21daa446703b5f4a Mon Sep 17 00:00:00 2001 From: Pyrrha Date: Fri, 17 Jun 2022 18:59:47 +1000 Subject: [PATCH 1/4] feat: add PronounDB.org syncing --- .../ashisbored/playerpronouns/Config.java | 11 +++- .../playerpronouns/PlayerPronouns.java | 53 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/ashisbored/playerpronouns/Config.java b/src/main/java/io/github/ashisbored/playerpronouns/Config.java index c0494b4..38467e4 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/Config.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/Config.java @@ -21,31 +21,38 @@ import java.util.Optional; public class Config { private static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( Codec.BOOL.fieldOf("allow_custom").forGetter(config -> config.allowCustom), + Codec.BOOL.fieldOf("enable_pronoundb_sync").forGetter(config -> config.enablePronounDBSync), Pronoun.CODEC.listOf().fieldOf("single").forGetter(config -> config.single), Pronoun.CODEC.listOf().fieldOf("pairs").forGetter(config -> config.pairs), Codec.STRING.optionalFieldOf("default_placeholder", "Unknown").forGetter(config -> config.defaultPlaceholder) ).apply(instance, Config::new)); private final boolean allowCustom; + private final boolean enablePronounDBSync; private final List single; private final List pairs; private final String defaultPlaceholder; - private Config(boolean allowCustom, List single, List pairs, String defaultPlaceholder) { + private Config(boolean allowCustom, boolean enablePronounDBSync, List single, List pairs, String defaultPlaceholder) { this.allowCustom = allowCustom; + this.enablePronounDBSync = enablePronounDBSync; this.single = single; this.pairs = pairs; this.defaultPlaceholder = defaultPlaceholder; } private Config() { - this(true, Collections.emptyList(), Collections.emptyList(), "Unknown"); + this(true, true, Collections.emptyList(), Collections.emptyList(), "Unknown"); } public boolean allowCustom() { return allowCustom; } + public boolean enablePronounDBSync() { + return enablePronounDBSync; + } + public List getSingle() { return single; } diff --git a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java index 6e324b6..8b7e83d 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java @@ -1,31 +1,67 @@ package io.github.ashisbored.playerpronouns; +import com.google.gson.JsonObject; import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderResult; import eu.pb4.placeholders.api.Placeholders; import io.github.ashisbored.playerpronouns.command.PronounsCommand; +import io.github.ashisbored.playerpronouns.data.Pronoun; import io.github.ashisbored.playerpronouns.data.PronounDatabase; import io.github.ashisbored.playerpronouns.data.PronounList; import io.github.ashisbored.playerpronouns.data.Pronouns; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; +import net.minecraft.util.JsonHelper; import net.minecraft.util.WorldSavePath; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; +import org.spongepowered.include.com.google.common.base.Charsets; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; public class PlayerPronouns implements ModInitializer { public static final Logger LOGGER = LogManager.getLogger(); public static final String MOD_ID = "playerpronouns"; + private static final Map PRONOUNDB_ID_MAP = new HashMap<>() {{ + // short pronoun set identifier map from https://pronoundb.org/docs + put("unspecified", "ask"); // default if unknown + put("hh", "he/him"); + put("hi", "he/it"); + put("hs", "he/she"); + put("ht", "he/they"); + 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("other", "other"); + put("ask", "ask"); + put("avoid", "avoid"); + }}; private static PronounDatabase pronounDatabase; public static Config config; @@ -59,6 +95,23 @@ public class PlayerPronouns implements ModInitializer { } }); + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + if(config.enablePronounDBSync()) { + String pronounDbUrl = "https://pronoundb.org/api/v1/lookup?platform=minecraft&id=%s".formatted(handler.getPlayer().getUuid()); + try(CloseableHttpClient client = HttpClients.createMinimal(); + CloseableHttpResponse resp = client.execute(new HttpGet(pronounDbUrl))) { + + if(resp.getStatusLine().getStatusCode() == 200) { + JsonObject json = JsonHelper.deserialize(new String(resp.getEntity().getContent().readAllBytes(), Charsets.UTF_8)); + String pronouns = PRONOUNDB_ID_MAP.getOrDefault(json.get("pronouns").getAsString(), "ask"); + setPronouns(handler.getPlayer(), new Pronouns(pronouns, PronounList.get().getCalculatedPronounStrings().get(pronouns))); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }); + //noinspection CodeBlock2Expr CommandRegistrationCallback.EVENT.register((dispatcher, registry, env) -> { PronounsCommand.register(dispatcher); From 49d01ef615bda02c6321d36878cd42ddba7c1756 Mon Sep 17 00:00:00 2001 From: Pyrrha Date: Fri, 17 Jun 2022 19:07:11 +1000 Subject: [PATCH 2/4] fix: switch from Log4J to SLF4J --- build.gradle.kts | 4 ++++ .../github/ashisbored/playerpronouns/PlayerPronouns.java | 8 +++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index c96096b..cb5dfb5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -42,6 +42,10 @@ dependencies { include(libs.fabric.permissions) } +loom { + runtimeOnlyLog4j.set(true) +} + tasks.processResources { inputs.property("version", project.version) diff --git a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java index 8b7e83d..b001596 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java @@ -5,7 +5,6 @@ import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderResult; import eu.pb4.placeholders.api.Placeholders; import io.github.ashisbored.playerpronouns.command.PronounsCommand; -import io.github.ashisbored.playerpronouns.data.Pronoun; import io.github.ashisbored.playerpronouns.data.PronounDatabase; import io.github.ashisbored.playerpronouns.data.PronounList; import io.github.ashisbored.playerpronouns.data.Pronouns; @@ -15,7 +14,6 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.JsonHelper; import net.minecraft.util.WorldSavePath; @@ -23,9 +21,9 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.spongepowered.include.com.google.common.base.Charsets; import java.io.IOException; @@ -36,7 +34,7 @@ import java.util.Map; import java.util.Objects; public class PlayerPronouns implements ModInitializer { - public static final Logger LOGGER = LogManager.getLogger(); + public static final Logger LOGGER = LoggerFactory.getLogger(PlayerPronouns.class); public static final String MOD_ID = "playerpronouns"; private static final Map PRONOUNDB_ID_MAP = new HashMap<>() {{ // short pronoun set identifier map from https://pronoundb.org/docs From df5d5a892744bc61b2431f21bb20b5185c380cf6 Mon Sep 17 00:00:00 2001 From: Pyrrha Date: Sat, 18 Jun 2022 13:29:38 +1000 Subject: [PATCH 3/4] fix: switch to Java 11's HttpClient --- .../playerpronouns/PlayerPronouns.java | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java index b001596..f989731 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java @@ -14,6 +14,7 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.JsonHelper; import net.minecraft.util.WorldSavePath; @@ -27,8 +28,14 @@ import org.slf4j.LoggerFactory; import org.spongepowered.include.com.google.common.base.Charsets; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Duration; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -95,16 +102,26 @@ public class PlayerPronouns implements ModInitializer { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { if(config.enablePronounDBSync()) { - String pronounDbUrl = "https://pronoundb.org/api/v1/lookup?platform=minecraft&id=%s".formatted(handler.getPlayer().getUuid()); - try(CloseableHttpClient client = HttpClients.createMinimal(); - CloseableHttpResponse resp = client.execute(new HttpGet(pronounDbUrl))) { - - if(resp.getStatusLine().getStatusCode() == 200) { - JsonObject json = JsonHelper.deserialize(new String(resp.getEntity().getContent().readAllBytes(), Charsets.UTF_8)); - String pronouns = PRONOUNDB_ID_MAP.getOrDefault(json.get("pronouns").getAsString(), "ask"); - setPronouns(handler.getPlayer(), new Pronouns(pronouns, PronounList.get().getCalculatedPronounStrings().get(pronouns))); - } - } catch (IOException e) { + var pronounDbUrl = "https://pronoundb.org/api/v1/lookup?platform=minecraft&id=%s" + .formatted(handler.getPlayer().getUuid()); + try { + var client = HttpClient.newBuilder() + .version(HttpClient.Version.HTTP_2) + .followRedirects(HttpClient.Redirect.NORMAL) + .build(); + var req = HttpRequest.newBuilder() + .uri(new URI(pronounDbUrl)) + .GET() + .timeout(Duration.ofSeconds(10)) + .build(); + client.sendAsync(req, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .thenAccept(body -> { + var json = JsonHelper.deserialize(body); + var pronouns = PRONOUNDB_ID_MAP.getOrDefault(json.get("pronouns").getAsString(), "ask"); + setPronouns(handler.getPlayer(), new Pronouns(pronouns, PronounList.get().getCalculatedPronounStrings().get(pronouns))); + }).join(); + } catch (URISyntaxException e) { throw new RuntimeException(e); } } From 7464fdb297b7669083ec16d01fccd69f8f80b374 Mon Sep 17 00:00:00 2001 From: Pyrrha Date: Sat, 18 Jun 2022 13:31:11 +1000 Subject: [PATCH 4/4] fix: don't throw runtime exceptions when failing to retrieve pronouns --- .../io/github/ashisbored/playerpronouns/PlayerPronouns.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java index f989731..dca63c9 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java @@ -122,7 +122,7 @@ public class PlayerPronouns implements ModInitializer { setPronouns(handler.getPlayer(), new Pronouns(pronouns, PronounList.get().getCalculatedPronounStrings().get(pronouns))); }).join(); } catch (URISyntaxException e) { - throw new RuntimeException(e); + e.printStackTrace(); } } });