commit ba1e60d0cec6e11390542bf84313e1218d07b45b Author: Ash B Date: Fri Jul 30 20:53:09 2021 +0100 Initial commit diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..b0e29ef --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,24 @@ +name: Build +on: [pull_request, push] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Validate gradle wrapper + uses: gradle/wrapper-validation-action@v1 + - name: Setup JDK 16 + uses: actions/setup-java@v1 + with: + java-version: 16 + - name: Ensure gradlew is executable + run: chmod +x ./gradlew + - name: Build with gradle + run: ./gradlew build + - name: Upload build artifacts + uses: actions/upload-artifact@v2 + with: + name: Artifacts + path: build/libs/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09cd281 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +# gradle + +.gradle/ +build/ +out/ +classes/ + +# eclipse + +*.launch + +# idea + +.idea/ +*.iml +*.ipr +*.iws + +# vscode + +.settings/ +.vscode/ +bin/ +.classpath +.project + +# macos + +*.DS_Store + +# fabric + +run/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..ed878a6 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Player Pronouns +Let players share their pronouns! + +## For players + +### Commands +To change your displayed pronouns, you can use the command `/pronouns`. +It will suggest pronouns that are configured by the server admins, along with the default set. By default, you do not have to pick one of the suggestions at all, however server owners may disable setting custom pronouns in case of abuse, although it is not recommended to do so permanently. + +## For server owners + +### Configuration +The mod should work out of the box without any configuration, however if you want player's pronouns to be visible, you probably want to use the placeholder somewhere. + +#### Adding custom pronouns (eg. neo-pronouns) +To add custom pronoun sets, you can use the `single` and `pairs` options in the config file. `single` is for singular options such as `any` or `ask` while `pairs` is for pronouns that come in pairs and are used in the form `a/b`, for example `they` and `them`. + +#### Displaying pronouns + +##### In chat with Styled Chat +[Styled Chat](https://modrinth.com/mod/styled-chat) allows you to customise the formatting of chat messages. +To configure pronouns to show up like this, you can set the `chat` style to the following: + +`<${player} [%playerpronouns:pronouns%]> ${message}` + +![](https://cdn.discordapp.com/attachments/859419898962116642/870732808367267881/in-chat.png) + +##### On the tab list with Styled Player List +[Styled Player List](https://modrinth.com/mod/styledplayerlist) allows you to customise the look and feel of the tab/player list, as well as customise the formatting used for players in the list. + +```json +{ + "_comment": "Ensure that you include all the other default config options", + "changePlayerName": true, + "playerNameFormat": "%player:displayname% (%playerpronouns:pronouns%)", + "updatePlayerNameEveryChatMessage": true +} +``` + +![](https://cdn.discordapp.com/attachments/859419898962116642/870739744286453820/2021-07-30_19.45.49.png) diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..f786f00 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,56 @@ +plugins { + id("fabric-loom") version "0.9.45" + id("io.github.juuxel.loom-quiltflower") version "1.2.1" + `maven-publish` +} + +version = "1.0.0-beta" +group = "io.github.ashisbored" + +repositories { + // needed for placeholder-api + maven { + name = "NucleoidMC" + url = uri("https://maven.nucleoid.xyz/") + } +} + +dependencies { + // Minecraft + minecraft(libs.minecraft) + mappings(variantOf(libs.yarn) { classifier("v2") }) + + // Fabric + modImplementation(libs.fabric.loader) + modImplementation(libs.fabric.api) + + // placeholder-api + modImplementation(libs.placeholder.api) + include(libs.placeholder.api) +} + +tasks.processResources { + inputs.property("version", project.version) + + filesMatching("fabric.mod.json") { + expand("version" to project.version) + } +} + +java { + sourceCompatibility = JavaVersion.VERSION_16 + targetCompatibility = JavaVersion.VERSION_16 + + withSourcesJar() +} + +tasks.withType { + options.encoding = "UTF-8" + options.release.set(16) +} + +tasks.jar { + from("LICENSE") { + rename { "${it}_${project.name}" } + } +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7454180 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..05679dc --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..744e882 --- /dev/null +++ b/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MSYS* | MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..ac1b06f --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/libs.versions.toml b/libs.versions.toml new file mode 100644 index 0000000..7e8c932 --- /dev/null +++ b/libs.versions.toml @@ -0,0 +1,17 @@ +[versions] +minecraft = "1.17.1" +yarn = "1.17.1+build.31" + +fabric-loader = "0.11.6" +fabric-api = "0.37.1+1.17" + +placeholder-api = "1.0.1+1.17" + +[libraries] +minecraft = { module = "com.mojang:minecraft", version.ref = "minecraft" } +yarn = { module = "net.fabricmc:yarn", version.ref = "yarn" } + +fabric-loader = { module = "net.fabricmc:fabric-loader", version.ref = "fabric-loader" } +fabric-api = { module = "net.fabricmc.fabric-api:fabric-api", version.ref = "fabric-api" } + +placeholder-api = { module = "eu.pb4:placeholder-api", version.ref = "placeholder-api" } diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..81ad5f4 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,25 @@ +pluginManagement { + repositories { + maven { + name = "FabricMC" + url = uri("https://maven.fabricmc.net/") + } + maven { + name = "Cotton" + url = uri("https://server.bbkr.space/artifactory/libs-release") + } + gradlePluginPortal() + } +} + +rootProject.name = "player-pronouns" + +enableFeaturePreview("VERSION_CATALOGS") + +dependencyResolutionManagement { + versionCatalogs { + create("libs") { + from(files("libs.versions.toml")) + } + } +} diff --git a/src/main/java/io/github/ashisbored/playerpronouns/Config.java b/src/main/java/io/github/ashisbored/playerpronouns/Config.java new file mode 100644 index 0000000..82185b1 --- /dev/null +++ b/src/main/java/io/github/ashisbored/playerpronouns/Config.java @@ -0,0 +1,79 @@ +package io.github.ashisbored.playerpronouns; + +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.mojang.datafixers.util.Pair; +import com.mojang.serialization.Codec; +import com.mojang.serialization.JsonOps; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.fabricmc.loader.api.FabricLoader; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; +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.STRING.listOf().fieldOf("single").forGetter(config -> config.single), + Codec.STRING.listOf().fieldOf("pairs").forGetter(config -> config.pairs) + ).apply(instance, Config::new)); + + private final boolean allowCustom; + private final List single; + private final List pairs; + + private Config(boolean allowCustom, List single, List pairs) { + this.allowCustom = allowCustom; + this.single = single; + this.pairs = pairs; + } + + private Config() { + this(true, Collections.emptyList(), Collections.emptyList()); + } + + public boolean allowCustom() { + return allowCustom; + } + + public List getSingle() { + return single; + } + + public List getPairs() { + return pairs; + } + + public static Config load() { + Path path = FabricLoader.getInstance().getConfigDir().resolve("player-pronouns.json"); + if (!Files.exists(path)) { + Config config = new Config(); + Optional result = CODEC.encodeStart(JsonOps.INSTANCE, config).result(); + if (result.isPresent()) { + try { + Files.writeString(path, new GsonBuilder().setPrettyPrinting().create().toJson(result.get())); + } catch (IOException e) { + PlayerPronouns.LOGGER.warn("Failed to save default config!", e); + } + } else { + PlayerPronouns.LOGGER.warn("Failed to save default config!"); + } + return new Config(); + } else { + try { + String s = Files.readString(path); + JsonParser parser = new JsonParser(); + JsonElement ele = parser.parse(s); + return CODEC.decode(JsonOps.INSTANCE, ele).map(Pair::getFirst).result().orElseGet(Config::new); + } catch (IOException e) { + PlayerPronouns.LOGGER.warn("Failed to load config!", e); + return new Config(); + } + } + } +} diff --git a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java new file mode 100644 index 0000000..84d04dd --- /dev/null +++ b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java @@ -0,0 +1,97 @@ +package io.github.ashisbored.playerpronouns; + +import eu.pb4.placeholders.PlaceholderAPI; +import eu.pb4.placeholders.PlaceholderResult; +import io.github.ashisbored.playerpronouns.command.PronounsCommand; +import io.github.ashisbored.playerpronouns.data.BinaryPronounDatabase; +import io.github.ashisbored.playerpronouns.data.PronounList; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; +import net.minecraft.util.WorldSavePath; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +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 BinaryPronounDatabase pronounDatabase; + public static Config config; + + @Override + public void onInitialize() { + LOGGER.info("Player Pronouns initialising..."); + + config = Config.load(); + PronounList.load(config); + + ServerLifecycleEvents.SERVER_STARTING.register(server -> { + try { + Path playerData = server.getSavePath(WorldSavePath.PLAYERDATA); + if (!Files.exists(playerData)) { + Files.createDirectories(playerData); + } + pronounDatabase = BinaryPronounDatabase.load(playerData.resolve("pronouns.dat")); + } catch (IOException e) { + LOGGER.error("Failed to create/load pronoun database!", e); + } + }); + + ServerLifecycleEvents.SERVER_STOPPING.register(server -> { + if (pronounDatabase != null) { + try { + savePronounDatabase(server); + } catch (IOException e) { + LOGGER.error("Failed to save pronoun database!", e); + } + } + }); + + //noinspection CodeBlock2Expr + CommandRegistrationCallback.EVENT.register((dispatcher, __) -> { + PronounsCommand.register(dispatcher); + }); + + PlaceholderAPI.register(new Identifier(MOD_ID, "pronouns"), ctx -> { + if (!ctx.hasPlayer()) { + return PlaceholderResult.invalid("missing player"); + } + ServerPlayerEntity player = ctx.getPlayer(); + if (pronounDatabase == null) { + return PlaceholderResult.value("Unknown"); + } + String pronouns = pronounDatabase.get(player.getUuid()); + return PlaceholderResult.value(Objects.requireNonNullElse(pronouns, "Unknown")); + }); + } + + private static void savePronounDatabase(MinecraftServer server) throws IOException { + Path playerData = server.getSavePath(WorldSavePath.PLAYERDATA); + if (!Files.exists(playerData)) { + Files.createDirectories(playerData); + } + pronounDatabase.save(playerData.resolve("pronouns.dat")); + } + + public static boolean setPronouns(ServerPlayerEntity player, String pronouns) { + if (pronounDatabase == null) return false; + + pronounDatabase.put(player.getUuid(), pronouns); + try { + savePronounDatabase(Objects.requireNonNull(player.getServer())); + } catch (IOException e) { + LOGGER.error("Failed to save pronoun database!", e); + } + + return true; + } +} diff --git a/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsArgument.java b/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsArgument.java new file mode 100644 index 0000000..02bc0c3 --- /dev/null +++ b/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsArgument.java @@ -0,0 +1,29 @@ +package io.github.ashisbored.playerpronouns.command; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import io.github.ashisbored.playerpronouns.data.PronounList; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; + +import java.util.Locale; + +public class PronounsArgument { + + public static RequiredArgumentBuilder pronouns(String name) { + return CommandManager.argument(name, StringArgumentType.greedyString()) + .suggests((ctx, builder) -> { + String remaining = builder.getRemainingLowerCase(); + + for (String pronouns : PronounList.get().getCalculatedPronounStrings()) { + if (pronouns.toLowerCase(Locale.ROOT).startsWith(remaining)) { + builder.suggest(pronouns); + } + } + + return builder.buildFuture(); + }); + } + + private PronounsArgument() { } +} diff --git a/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java b/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java new file mode 100644 index 0000000..7cd7472 --- /dev/null +++ b/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java @@ -0,0 +1,41 @@ +package io.github.ashisbored.playerpronouns.command; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import io.github.ashisbored.playerpronouns.PlayerPronouns; +import io.github.ashisbored.playerpronouns.data.PronounList; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.LiteralText; +import net.minecraft.util.Formatting; + +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import static io.github.ashisbored.playerpronouns.command.PronounsArgument.pronouns; +import static net.minecraft.server.command.CommandManager.literal; + +public class PronounsCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(literal("pronouns") + .then(pronouns("pronouns") + .executes(ctx -> { + ServerPlayerEntity player = ctx.getSource().getPlayer(); + String pronouns = getString(ctx, "pronouns"); + + if (!PlayerPronouns.config.allowCustom() && !PronounList.get().getCalculatedPronounStrings().contains(pronouns)) { + ctx.getSource().sendError(new LiteralText("Custom pronouns have been disabled by the server administrator.")); + return 0; + } + + if (!PlayerPronouns.setPronouns(player, pronouns)) { + ctx.getSource().sendError(new LiteralText("Failed to update pronouns, sorry")); + } else { + ctx.getSource().sendFeedback(new LiteralText("Updated your pronouns to " + pronouns + "!") + .formatted(Formatting.GREEN), false); + } + + return Command.SINGLE_SUCCESS; + }) + ) + ); + } +} diff --git a/src/main/java/io/github/ashisbored/playerpronouns/data/BinaryPronounDatabase.java b/src/main/java/io/github/ashisbored/playerpronouns/data/BinaryPronounDatabase.java new file mode 100644 index 0000000..79e680a --- /dev/null +++ b/src/main/java/io/github/ashisbored/playerpronouns/data/BinaryPronounDatabase.java @@ -0,0 +1,80 @@ +package io.github.ashisbored.playerpronouns.data; + +import it.unimi.dsi.fastutil.objects.Object2ObjectMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import org.jetbrains.annotations.Nullable; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.UUID; + +public class BinaryPronounDatabase { + private final Object2ObjectMap data; + + private BinaryPronounDatabase(Object2ObjectMap data) { + this.data = data; + } + + private BinaryPronounDatabase() { + this(new Object2ObjectOpenHashMap<>()); + } + + public void put(UUID uuid, @Nullable String pronouns) { + if (pronouns == null) { + this.data.remove(uuid); + } else { + this.data.put(uuid, pronouns); + } + } + + public @Nullable String get(UUID uuid) { + return this.data.get(uuid); + } + + public synchronized void save(Path path) throws IOException { + try (OutputStream os = Files.newOutputStream(path); + DataOutputStream out = new DataOutputStream(os)) { + + out.writeShort(0x4567); // some form of magic, idk + out.writeInt(data.size()); + + for (var entry : data.entrySet()) { + UUID uuid = entry.getKey(); + out.writeLong(uuid.getMostSignificantBits()); + out.writeLong(uuid.getLeastSignificantBits()); + out.writeUTF(entry.getValue()); + } + } + } + + public static BinaryPronounDatabase load(Path path) throws IOException { + if (!Files.exists(path)) { + return new BinaryPronounDatabase(); + } + + try (InputStream is = Files.newInputStream(path); + DataInputStream in = new DataInputStream(is)) { + + short magic = in.readShort(); + if (magic != 0x4567) { + throw new IOException("Invalid DB magic: " + magic); + } + + int size = in.readInt(); + Object2ObjectOpenHashMap data = new Object2ObjectOpenHashMap<>(); + for (int i = 0; i < size; i++) { + long mostSigBits = in.readLong(); + long leastSigBits = in.readLong(); + UUID uuid = new UUID(mostSigBits, leastSigBits); + String pronouns = in.readUTF(); + String old = data.put(uuid, pronouns); + if (old != null) { + throw new IOException("Duplicate UUID in database: " + uuid); + } + } + + return new BinaryPronounDatabase(data); + } + } +} diff --git a/src/main/java/io/github/ashisbored/playerpronouns/data/PronounList.java b/src/main/java/io/github/ashisbored/playerpronouns/data/PronounList.java new file mode 100644 index 0000000..e1b3dca --- /dev/null +++ b/src/main/java/io/github/ashisbored/playerpronouns/data/PronounList.java @@ -0,0 +1,90 @@ +package io.github.ashisbored.playerpronouns.data; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import io.github.ashisbored.playerpronouns.Config; +import io.github.ashisbored.playerpronouns.PlayerPronouns; +import net.minecraft.util.Pair; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.*; + +public class PronounList { + private static PronounList INSTANCE; + + private final List defaultSingle; + private final List defaultPairs; + private final List customSingle; + private final List customPairs; + private final List calculatedPronounStrings; + + public PronounList(List defaultSingle, List defaultPairs, List customSingle, List customPairs) { + this.defaultSingle = defaultSingle; + this.defaultPairs = defaultPairs; + this.customSingle = customSingle; + this.customPairs = customPairs; + this.calculatedPronounStrings = this.computePossibleCombinations(); + } + + public List getCalculatedPronounStrings() { + return this.calculatedPronounStrings; + } + + private List computePossibleCombinations() { + List ret = new ArrayList<>(); + ret.addAll(this.defaultSingle); + ret.addAll(this.customSingle); + List combinedPairs = new ArrayList<>(); + combinedPairs.addAll(this.defaultPairs); + combinedPairs.addAll(this.customPairs); + for (int i = 0; i < combinedPairs.size(); i++) { + for (int j = 0; j < combinedPairs.size(); j++) { + if (i == j) continue; + ret.add(combinedPairs.get(i) + "/" + combinedPairs.get(j)); + } + } + ret.sort(Comparator.naturalOrder()); + return ret; + } + + public static void load(Config config) { + if (INSTANCE != null) { + throw new IllegalStateException("PronounList has already been loaded!"); + } + + Pair, List> defaults = loadDefaults(); + INSTANCE = new PronounList( + defaults.getLeft(), + defaults.getRight(), + config.getSingle(), + config.getPairs() + ); + } + + public static PronounList get() { + if (INSTANCE == null) { + throw new IllegalStateException("PronounList has not been loaded!"); + } + return INSTANCE; + } + + private static Pair, List> loadDefaults() { + try (InputStream is = Objects.requireNonNull(PronounList.class.getResourceAsStream("/default_pronouns.json")); + InputStreamReader reader = new InputStreamReader(is)) { + JsonObject ele = new JsonParser().parse(reader).getAsJsonObject(); + JsonArray jsonSingle = ele.getAsJsonArray("single"); + JsonArray jsonPairs = ele.getAsJsonArray("pairs"); + List single = new ArrayList<>(); + List pairs = new ArrayList<>(); + jsonSingle.forEach(e -> single.add(e.getAsString())); + jsonPairs.forEach(e -> pairs.add(e.getAsString())); + return new Pair<>(single, pairs); + } catch (IOException e) { + PlayerPronouns.LOGGER.error("Failed to load default pronouns!", e); + return new Pair<>(Collections.emptyList(), Collections.emptyList()); + } + } +} diff --git a/src/main/resources/default_pronouns.json b/src/main/resources/default_pronouns.json new file mode 100644 index 0000000..4e62017 --- /dev/null +++ b/src/main/resources/default_pronouns.json @@ -0,0 +1,15 @@ +{ + "_comment": "See default_pronouns.txt", + "single": [ + "any", + "ask", + "avoid", + "other" + ], + "pairs": [ + "he", "him", + "it", "its", + "she", "her", + "they", "them" + ] +} diff --git a/src/main/resources/default_pronouns.txt b/src/main/resources/default_pronouns.txt new file mode 100644 index 0000000..ac5a479 --- /dev/null +++ b/src/main/resources/default_pronouns.txt @@ -0,0 +1,3 @@ +The default pronouns are sorted in the file alphabetically to avoid bias. +Pairs are pronouns that can be combined in the form a/b, and are ordered with both forms on the same line. +The list of default pronouns is based on the available pronouns on https://pronoundb.org/ diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..005d308 --- /dev/null +++ b/src/main/resources/fabric.mod.json @@ -0,0 +1,32 @@ +{ + "schemaVersion": 1, + "id": "playerpronouns", + "version": "${version}", + + "name": "Player Pronouns", + "description": "A server-side mod adding pronouns!", + + "authors": [ + "Ash (ashisbored)" + ], + "contact": { + "sources": "https://github.com/ashisbored/player-pronouns", + "issues": "https://github.com/ashisbored/player-pronouns/issues" + }, + + "license": "MIT", + + "environment": "*", + "entrypoints": { + "main": [ + "io.github.ashisbored.playerpronouns.PlayerPronouns" + ] + }, + + "depends": { + "fabricloader": ">=0.11.6", + "fabric": "*", + "minecraft": "1.17.x", + "java": ">=16" + } +}