Skip to content

Commit

Permalink
Merge pull request #19 from AceKiron-Community/integration-pronoundb
Browse files Browse the repository at this point in the history
Integration pronoundb
  • Loading branch information
AceKiron authored Oct 28, 2022
2 parents d594c88 + a2e5125 commit 20bbfbf
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.acekironcommunity</groupId>
<artifactId>pronounmc</artifactId>
<version>1.3</version>
<version>1.4</version>
<packaging>jar</packaging>

<name>PronounMC</name>
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/io/github/acekironcommunity/pronounmc/MyPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.acekironcommunity.pronounmc.commands.AddPronounCommand;
import io.github.acekironcommunity.pronounmc.commands.GetPronounsCommand;
import io.github.acekironcommunity.pronounmc.commands.RemovePronounCommand;
import io.github.acekironcommunity.pronounmc.commands.UnusedCommand;
import io.github.acekironcommunity.pronounmc.handlers.ChatHandler;
import io.github.acekironcommunity.pronounmc.tabcompleters.PronounsTabCompleter;

Expand All @@ -13,10 +14,10 @@ public final class MyPlugin extends JavaPlugin {

@Override
public void onEnable() {
Utils.log("Starting up", true);

Utils.SetPlugin(this);

Utils.log("Starting up", true);

// Initialize the config
saveDefaultConfig();
getConfig().options().copyDefaults(true);
Expand All @@ -32,13 +33,22 @@ public void onEnable() {

PronounsTabCompleter pronounsTabCompleter = new PronounsTabCompleter();

getCommand("addpronoun").setExecutor(new AddPronounCommand());
getCommand("addpronoun").setTabCompleter(pronounsTabCompleter);

getCommand("removepronoun").setExecutor(new RemovePronounCommand());
getCommand("removepronoun").setTabCompleter(pronounsTabCompleter);
boolean pronounOverrideEnabled = Utils.getPronounOverrideEnabled();

getCommand("getpronouns").setExecutor(new GetPronounsCommand());

if (pronounOverrideEnabled) {
UnusedCommand cmdHandler = new UnusedCommand();

getCommand("addpronoun").setExecutor(cmdHandler);
getCommand("removepronoun").setExecutor(cmdHandler);
} else {
getCommand("addpronoun").setExecutor(new AddPronounCommand());
getCommand("addpronoun").setTabCompleter(pronounsTabCompleter);

getCommand("removepronoun").setExecutor(new RemovePronounCommand());
getCommand("removepronoun").setTabCompleter(pronounsTabCompleter);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@
import java.util.UUID;
import java.util.ArrayList;

import io.github.acekironcommunity.pronounmc.tpo.PronounDBOverrider;

public class PronounAPI {

private static HashMap<UUID, List<String>> cache = new HashMap<UUID, List<String>>();

private static File playerPronounsFile;
private static YamlConfiguration playerPronounsConfig;

private static List<String> pronouns;

private static List<String> pronounCodes;
private static boolean logChanges;
private static String pronounOverride;

public PronounAPI(MyPlugin plugin) {
Utils.log("Initialized PronounMC API.", true);

logChanges = plugin.getConfig().getBoolean("log-changes");
pronounOverride = Utils.getPronounOverride();

pronouns = plugin.getConfig().getStringList("available-pronouns");
pronounCodes = plugin.getConfig().getStringList("available-pronouns");

// Use a separate file for storing players' pronouns
playerPronounsFile = new File(plugin.getDataFolder(), "pronouns-db.yml");
Expand All @@ -46,7 +50,7 @@ public PronounAPI(MyPlugin plugin) {
}

public static List<String> getAllCodes() {
return (List<String>) ((ArrayList<String>) pronouns).clone();
return (List<String>) ((ArrayList<String>) pronounCodes).clone();
}

/*
Expand All @@ -58,9 +62,24 @@ private static List<String> fetchPronouns(UUID playerUUID) {
return cache.get(playerUUID);
}

List<String> codes = playerPronounsConfig.getStringList("pronouns-" + playerUUID);
List<String> codes;
Utils.log("Fetch pronouns for player with UUID " + playerUUID, true);

if (!Utils.getPronounOverrideEnabled()) {
codes = playerPronounsConfig.getStringList("pronouns-" + playerUUID);
} else {
switch (pronounOverride) {
case "pronoundb":
codes = PronounDBOverrider.fetch(playerUUID);
break;

default:
Utils.log(pronounOverride + " is not a supported third party overrider.", false);
codes = new ArrayList<String>();
break;
}
}

cache.put(playerUUID, codes);
return codes;
}
Expand Down Expand Up @@ -107,7 +126,7 @@ private static void save(UUID playerUUID, List<String> pronouns) {
Returns true if pronoun code is valid, otherwise returns false.
*/
public static boolean addPronouns(UUID playerUUID, String code) {
if (!pronouns.contains(code)) return false;
if (!pronounCodes.contains(code)) return false;

List<String> pronouns = fetchPronouns(playerUUID);

Expand All @@ -127,7 +146,7 @@ public static boolean addPronouns(UUID playerUUID, String code) {
Returns true if pronoun code is valid, otherwise returns false.
*/
public static boolean removePronouns(UUID playerUUID, String code) {
if (!pronouns.contains(code)) return false;
if (!pronounCodes.contains(code)) return false;

List<String> pronouns = fetchPronouns(playerUUID);

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/github/acekironcommunity/pronounmc/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ public class Utils {

private static String messagePrefix;
private static boolean verboseLogging;
private static boolean pronounOverrideEnabled;
private static String pronounOverride;

public static void SetPlugin(MyPlugin plugin) {
messagePrefix = plugin.getConfig().getString("message-prefix");
verboseLogging = plugin.getConfig().getBoolean("verbose-logging");
pronounOverrideEnabled = plugin.getConfig().getBoolean("enable-third-party-override");
pronounOverride = plugin.getConfig().getString("third-party-override");
}

public static boolean getPronounOverrideEnabled() {
return pronounOverrideEnabled;
}

public static String getPronounOverride() {
return pronounOverride;
}

public static String formatMessage(String message, boolean ok) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.acekironcommunity.pronounmc.commands;

import io.github.acekironcommunity.pronounmc.Utils;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class UnusedCommand implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
sender.sendMessage(Utils.formatMessage("This command is unavailable because the server operators enabled override from a third party.", false));
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.github.acekironcommunity.pronounmc.tpo;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;

import io.github.acekironcommunity.pronounmc.Utils;

public class PronounDBOverrider {

public static List<String> fetch(UUID playerUUID) {
try {
String responseContent = "";

URL url = new URL("https://pronoundb.org/api/v1/lookup?platform=minecraft&id=" + playerUUID);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("GET");

if (http.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in .readLine()) != null) {
response.append(inputLine);
}

in.close();

responseContent = response.toString();
}

Utils.log(responseContent, true);
http.disconnect();

// Translates pronoundb string to full pronoun
String pronoun;
String[] array = responseContent.split("\"");
switch (array[3]) {
case "unspecified": return new ArrayList<String>();
case "hh": return new ArrayList<String>(Arrays.asList("he", "him"));
case "hi": return new ArrayList<String>(Arrays.asList("he", "it"));
case "hs": return new ArrayList<String>(Arrays.asList("he", "she"));
case "ht": return new ArrayList<String>(Arrays.asList("he", "they"));
case "ih": return new ArrayList<String>(Arrays.asList("it", "him"));
case "ii": return new ArrayList<String>(Arrays.asList("it", "its"));
case "is": return new ArrayList<String>(Arrays.asList("it", "she"));
case "it": return new ArrayList<String>(Arrays.asList("it", "they"));
case "shh": return new ArrayList<String>(Arrays.asList("she", "he"));
case "sh": return new ArrayList<String>(Arrays.asList("she", "her"));
case "si": return new ArrayList<String>(Arrays.asList("she", "it"));
case "st": return new ArrayList<String>(Arrays.asList("she", "they"));
case "th": return new ArrayList<String>(Arrays.asList("they", "he"));
case "ti": return new ArrayList<String>(Arrays.asList("they", "it"));
case "ts": return new ArrayList<String>(Arrays.asList("they", "she"));
case "tt": return new ArrayList<String>(Arrays.asList("they", "them"));
case "any": return new ArrayList<String>(Arrays.asList("any"));
case "other": return new ArrayList<String>(Arrays.asList("other"));
case "ask": return new ArrayList<String>(Arrays.asList("ask"));
case "avoid": return new ArrayList<String>(Arrays.asList("username"));
}
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

}
5 changes: 4 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ handle-chat: true
message-prefix: "PronounMC: "

verbose-logging: false
log-changes: true
log-changes: true

enable-third-party-override: true
third-party-override: "pronoundb"
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: PronounMC
version: '${project.version}'
main: io.github.acekironcommunity.pronounmc.MyPlugin
api-version: 1.19
authors: [ AceKiron, Purrrpley ]
authors: [ AceKiron, Purrrpley, werken23 ]
description: See someone's pronouns
website: https://github.com/AceKiron-Community/pronounmc

Expand Down

0 comments on commit 20bbfbf

Please sign in to comment.