From 3eb9f6b0e19bfd563e45b5da68d51ae52b78f944 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 2 Apr 2021 14:09:31 +0100 Subject: [PATCH] Fix loading as an extension and config refactor --- README.md | 7 + pom.xml | 50 +++++- .../rtm516/FloodgatePlaceholders/Config.java | 110 ++++--------- .../ExpansionConfig.java | 146 ++++++++++++++++++ .../rtm516/FloodgatePlaceholders/Main.java | 9 +- .../FloodgatePlaceholders/Placeholder.java | 108 +++++++++---- .../FloodgatePlaceholders/PluginConfig.java | 111 +++++++++++++ src/main/resources/config.yml | 4 +- src/main/resources/plugin.yml | 2 +- 9 files changed, 425 insertions(+), 122 deletions(-) create mode 100644 src/main/java/com/rtm516/FloodgatePlaceholders/ExpansionConfig.java create mode 100644 src/main/java/com/rtm516/FloodgatePlaceholders/PluginConfig.java diff --git a/README.md b/README.md index d037fb0..2b98127 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,13 @@ Java: ![image](https://user-images.githubusercontent.com/5401186/80527741-c1519f00-898c-11ea-8b0a-999b455b77af.png) +## Download +You can download the plugin/extension from GitHub releases or eCloud + +https://api.extendedclip.com/expansions/floodgate/ or `/papi ecloud download Floodgate` ingame + +https://github.com/rtm516/FloodgatePlaceholders/releases/ + ## Placeholders: - Device: - `%floodgate_device%` diff --git a/pom.xml b/pom.xml index 1737d99..43be5f7 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.rtm516 FloodgatePlaceholders - 1.2.1 + 1.3.0 FloodgatePlaceholders @@ -50,6 +50,54 @@ + + com.google.code.maven-replacer-plugin + replacer + 1.5.3 + + + add-version + process-sources + + replace + + + + ${project.basedir}/src/main/java/com/rtm516/FloodgatePlaceholders/Placeholder.java + + + + String VERSION = ".*" + String VERSION = "${project.version}" + + + String NAME = ".*" + String NAME = "${project.name}" + + + + + + + remove-version + process-classes + + replace + + + + ${project.basedir}/src/main/java/com/rtm516/FloodgatePlaceholders/Placeholder.java + + + + String VERSION = ".*" + String VERSION = "DEV" + + + + + + diff --git a/src/main/java/com/rtm516/FloodgatePlaceholders/Config.java b/src/main/java/com/rtm516/FloodgatePlaceholders/Config.java index a7a3dae..c6168c7 100644 --- a/src/main/java/com/rtm516/FloodgatePlaceholders/Config.java +++ b/src/main/java/com/rtm516/FloodgatePlaceholders/Config.java @@ -1,111 +1,53 @@ package com.rtm516.FloodgatePlaceholders; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import lombok.Getter; +public interface Config { + boolean isSpecificDeviceDescriptors(); -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.logging.Level; -import java.util.logging.Logger; + DevicePlaceholders getDevice(); -@Getter -public class Config { - @JsonProperty(value = "specific-device-descriptors") - private boolean specificDeviceDescriptors; + GenericPlaceholders getLocale(); - @JsonProperty(value = "device") - private DevicePlaceholders device; + GenericPlaceholders getVersion(); - @JsonProperty(value = "locale") - private GenericPlaceholders locale; + GenericPlaceholders getXboxUsername(); - @JsonProperty(value = "version") - private GenericPlaceholders version; + GenericPlaceholders getXboxXuid(); - @JsonProperty(value = "xbox-username") - private GenericPlaceholders xboxUsername; + interface DevicePlaceholders { + String getJava(); - @JsonProperty(value = "xbox-xuid") - private GenericPlaceholders xboxXuid; + String getGeneric(); - @Getter - public static class DevicePlaceholders { - @JsonProperty("java") - private String java; + String getUnknown(); - @JsonProperty("generic") - private String generic; + String getGoogle(); - @JsonProperty("unknown") - private String unknown; + String getIOS(); - @JsonProperty("google") - private String google; + String getOSX(); - @JsonProperty("ios") - private String iOS; + String getAmazon(); - @JsonProperty("osx") - private String OSX; + String getGearVR(); - @JsonProperty("amazon") - private String amazon; + String getHololens(); - @JsonProperty("gearvr") - private String gearVR; + String getUwp(); - @JsonProperty("hololens") - private String hololens; + String getWin32(); - @JsonProperty("uwp") - private String uwp; + String getDedicated(); - @JsonProperty("win32") - private String win32; + String getPs4(); - @JsonProperty("dedicated") - private String dedicated; + String getNX(); - @JsonProperty("ps4") - private String ps4; - - @JsonProperty("nx") - private String NX; - - @JsonProperty("xbox") - private String xbox; + String getXbox(); } - @Getter - public static class GenericPlaceholders { - @JsonProperty("found") - private String found; - - @JsonProperty("none") - private String none; - } + interface GenericPlaceholders { + String getFound(); - public static Config load(Logger logger, Path configPath) { - Config config = null; - try { - try { - if (!configPath.toFile().exists()) - Files.copy(Config.class.getClassLoader().getResourceAsStream("config.yml"), configPath); - } catch (Exception e) { - logger.log(Level.SEVERE, "Error while creating config.yml", e); - } - - config = new ObjectMapper(new YAMLFactory()).readValue(Files.readAllBytes(configPath), Config.class); - } catch (Exception e) { - logger.log(Level.SEVERE, "Error while loading config.yml", e); - } - - if (config == null) { - throw new RuntimeException("Failed to load config file! Try to delete the data folder of FloodgatePlaceholders"); - } - - return config; + String getNone(); } } diff --git a/src/main/java/com/rtm516/FloodgatePlaceholders/ExpansionConfig.java b/src/main/java/com/rtm516/FloodgatePlaceholders/ExpansionConfig.java new file mode 100644 index 0000000..83d29ba --- /dev/null +++ b/src/main/java/com/rtm516/FloodgatePlaceholders/ExpansionConfig.java @@ -0,0 +1,146 @@ +package com.rtm516.FloodgatePlaceholders; + +public class ExpansionConfig implements Config { + private Placeholder expansion; + + public ExpansionConfig(Placeholder expansion) { + this.expansion = expansion; + } + + @Override + public boolean isSpecificDeviceDescriptors() { + return (Boolean) expansion.get("specific-device-descriptors", true); + } + + @Override + public DevicePlaceholders getDevice() { + return new ExpansionDevicePlaceholders(this, "device"); + } + + @Override + public GenericPlaceholders getLocale() { + return new ExpansionGenericPlaceholders(this, "locale"); + } + + @Override + public GenericPlaceholders getVersion() { + return new ExpansionGenericPlaceholders(this, "version"); + } + + @Override + public GenericPlaceholders getXboxUsername() { + return new ExpansionGenericPlaceholders(this, "xbox-username"); + } + + @Override + public GenericPlaceholders getXboxXuid() { + return new ExpansionGenericPlaceholders(this, "xbox-xuid"); + } + + private class ExpansionDevicePlaceholders implements DevicePlaceholders { + + private final ExpansionConfig config; + private final String parent; + + public ExpansionDevicePlaceholders(ExpansionConfig config, String parent) { + this.config = config; + this.parent = parent; + } + + @Override + public String getJava() { + return config.expansion.getString(parent + ".java", ""); + } + + @Override + public String getGeneric() { + return config.expansion.getString(parent + ".generic", ""); + } + + @Override + public String getUnknown() { + return config.expansion.getString(parent + ".unknown", ""); + } + + @Override + public String getGoogle() { + return config.expansion.getString(parent + ".google", ""); + } + + @Override + public String getIOS() { + return config.expansion.getString(parent + ".ios", ""); + } + + @Override + public String getOSX() { + return config.expansion.getString(parent + ".osx", ""); + } + + @Override + public String getAmazon() { + return config.expansion.getString(parent + ".fireos", ""); + } + + @Override + public String getGearVR() { + return config.expansion.getString(parent + ".gearvr", ""); + } + + @Override + public String getHololens() { + return config.expansion.getString(parent + ".hololens", ""); + } + + @Override + public String getUwp() { + return config.expansion.getString(parent + ".uwp", ""); + } + + @Override + public String getWin32() { + return config.expansion.getString(parent + ".win32", ""); + } + + @Override + public String getDedicated() { + return config.expansion.getString(parent + ".dedicated", ""); + } + + @Override + public String getPs4() { + return config.expansion.getString(parent + ".ps4", ""); + } + + @Override + public String getNX() { + return config.expansion.getString(parent + ".nx", ""); + } + + @Override + public String getXbox() { + return config.expansion.getString(parent + ".xbox", ""); + } + } + + private static class ExpansionGenericPlaceholders implements GenericPlaceholders { + + private final ExpansionConfig config; + private final String parent; + + public ExpansionGenericPlaceholders(ExpansionConfig config, String parent) { + this.config = config; + this.parent = parent; + } + + @Override + public String getFound() { + return config.expansion.getString(parent + ".found", ""); + } + + @Override + public String getNone() { + return config.expansion.getString(parent + ".none", ""); + } + } +} diff --git a/src/main/java/com/rtm516/FloodgatePlaceholders/Main.java b/src/main/java/com/rtm516/FloodgatePlaceholders/Main.java index e1c9dc1..1aec79d 100644 --- a/src/main/java/com/rtm516/FloodgatePlaceholders/Main.java +++ b/src/main/java/com/rtm516/FloodgatePlaceholders/Main.java @@ -6,8 +6,11 @@ public class Main extends JavaPlugin { + public static final String NAME = "FloodgatePlaceholders"; + public static final String VERSION = "DEV"; + @Getter - private Config configuration; + private PluginConfig configuration; @Override public void onLoad() { @@ -15,7 +18,7 @@ public void onLoad() { getDataFolder().mkdir(); } - configuration = Config.load(getLogger(), getDataFolder().toPath().resolve("config.yml")); + configuration = PluginConfig.load(getLogger(), getDataFolder().toPath().resolve("config.yml")); } @Override @@ -23,7 +26,7 @@ public void onEnable() { saveDefaultConfig(); if (Bukkit.getPluginManager().getPlugin("floodgate") != null && Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { - new Placeholder(this).register(); + new Placeholder(getConfiguration()).register(); } else { getLogger().warning("Floodgate 2.0 or PlaceholderAPI not found! Disabling plugin."); getServer().getPluginManager().disablePlugin(this); diff --git a/src/main/java/com/rtm516/FloodgatePlaceholders/Placeholder.java b/src/main/java/com/rtm516/FloodgatePlaceholders/Placeholder.java index d8502a4..b43d34c 100644 --- a/src/main/java/com/rtm516/FloodgatePlaceholders/Placeholder.java +++ b/src/main/java/com/rtm516/FloodgatePlaceholders/Placeholder.java @@ -1,16 +1,25 @@ package com.rtm516.FloodgatePlaceholders; +import me.clip.placeholderapi.expansion.Configurable; import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.geysermc.floodgate.api.FloodgateApi; import org.geysermc.floodgate.api.player.FloodgatePlayer; -public class Placeholder extends PlaceholderExpansion { +import java.util.HashMap; +import java.util.Map; - private Main plugin; +public class Placeholder extends PlaceholderExpansion implements Configurable { - public Placeholder(Main plugin){ - this.plugin = plugin; + private Config config; + + public Placeholder() { + config = new ExpansionConfig(this); + } + + public Placeholder(Config config){ + this.config = config; } @Override @@ -20,12 +29,17 @@ public boolean persist(){ @Override public boolean canRegister(){ - return true; + return Bukkit.getPluginManager().isPluginEnabled(getRequiredPlugin()); + } + + @Override + public String getRequiredPlugin() { + return "floodgate"; } @Override public String getAuthor(){ - return plugin.getDescription().getAuthors().toString(); + return "rtm516"; } @Override @@ -35,7 +49,7 @@ public String getIdentifier(){ @Override public String getVersion(){ - return plugin.getDescription().getVersion(); + return Main.VERSION; } @Override @@ -54,30 +68,30 @@ public String onPlaceholderRequest(Player player, String identifier) { case "locale_upper": if (floodgatePlayer != null) { boolean upper = identifier.endsWith("_upper"); - return plugin.getConfiguration().getLocale().getFound().replace("%locale%", upper ? floodgatePlayer.getLanguageCode().toUpperCase() : floodgatePlayer.getLanguageCode().toLowerCase()); + return config.getLocale().getFound().replace("%locale%", upper ? floodgatePlayer.getLanguageCode().toUpperCase() : floodgatePlayer.getLanguageCode().toLowerCase()); } else { - return plugin.getConfiguration().getLocale().getNone(); + return config.getLocale().getNone(); } case "version": if (floodgatePlayer != null) { - return plugin.getConfiguration().getVersion().getFound().replace("%version%", floodgatePlayer.getVersion()); + return config.getVersion().getFound().replace("%version%", floodgatePlayer.getVersion()); } else { - return plugin.getConfiguration().getVersion().getNone(); + return config.getVersion().getNone(); } case "username": if (floodgatePlayer != null) { - return plugin.getConfiguration().getXboxUsername().getFound().replace("%username%", floodgatePlayer.getUsername()); + return config.getXboxUsername().getFound().replace("%username%", floodgatePlayer.getUsername()); } else { - return plugin.getConfiguration().getXboxUsername().getNone(); + return config.getXboxUsername().getNone(); } case "xuid": if (floodgatePlayer != null) { - return plugin.getConfiguration().getXboxXuid().getFound().replace("%xuid%", floodgatePlayer.getXuid()); + return config.getXboxXuid().getFound().replace("%xuid%", floodgatePlayer.getXuid()); } else { - return plugin.getConfiguration().getXboxXuid().getNone(); + return config.getXboxXuid().getNone(); } } @@ -93,41 +107,73 @@ public String onPlaceholderRequest(Player player, String identifier) { private String getPlayerDeviceString(Player player) { FloodgatePlayer floodgatePlayer = FloodgateApi.getInstance().getPlayer(player.getUniqueId()); if (floodgatePlayer != null) { - if (plugin.getConfiguration().isSpecificDeviceDescriptors()) { + if (config.isSpecificDeviceDescriptors()) { switch (floodgatePlayer.getDeviceOs()) { case GOOGLE: - return plugin.getConfiguration().getDevice().getGoogle().replace("&", "§"); + return config.getDevice().getGoogle().replace("&", "§"); case IOS: - return plugin.getConfiguration().getDevice().getIOS().replace("&", "§"); + return config.getDevice().getIOS().replace("&", "§"); case OSX: - return plugin.getConfiguration().getDevice().getOSX().replace("&", "§"); + return config.getDevice().getOSX().replace("&", "§"); case AMAZON: - return plugin.getConfiguration().getDevice().getAmazon().replace("&", "§"); + return config.getDevice().getAmazon().replace("&", "§"); case GEARVR: - return plugin.getConfiguration().getDevice().getGearVR().replace("&", "§"); + return config.getDevice().getGearVR().replace("&", "§"); case HOLOLENS: - return plugin.getConfiguration().getDevice().getHololens().replace("&", "§"); + return config.getDevice().getHololens().replace("&", "§"); case UWP: - return plugin.getConfiguration().getDevice().getUwp().replace("&", "§"); + return config.getDevice().getUwp().replace("&", "§"); case WIN32: - return plugin.getConfiguration().getDevice().getWin32().replace("&", "§"); + return config.getDevice().getWin32().replace("&", "§"); case DEDICATED: - return plugin.getConfiguration().getDevice().getDedicated().replace("&", "§"); + return config.getDevice().getDedicated().replace("&", "§"); case PS4: - return plugin.getConfiguration().getDevice().getPs4().replace("&", "§"); + return config.getDevice().getPs4().replace("&", "§"); case NX: - return plugin.getConfiguration().getDevice().getNX().replace("&", "§"); + return config.getDevice().getNX().replace("&", "§"); case XBOX: - return plugin.getConfiguration().getDevice().getXbox().replace("&", "§"); + return config.getDevice().getXbox().replace("&", "§"); default: - return plugin.getConfiguration().getDevice().getUnknown().replace("&", "§"); + return config.getDevice().getUnknown().replace("&", "§"); } }else{ - return plugin.getConfiguration().getDevice().getGeneric().replace("&", "§"); + return config.getDevice().getGeneric().replace("&", "§"); } } else { - return plugin.getConfiguration().getDevice().getJava().replace("&", "§"); + return config.getDevice().getJava().replace("&", "§"); } } + @Override + public Map getDefaults() { + if (!(config instanceof ExpansionConfig)) { + return null; + } + + final Map defaults = new HashMap<>(); + defaults.put("device.java", "&8[&aJava&8]"); + defaults.put("device.generic", "&8[&7Bedrock&8]"); + defaults.put("device.unknown", "&8[&aUnknown&8]"); + defaults.put("device.google", "&8[&aAndroid&8]"); + defaults.put("device.ios", "&8[&bi&fOS&8]"); + defaults.put("device.osx", "&8[&fOS&bX&8]"); + defaults.put("device.amazon", "&8[&6Amazon&8]"); + defaults.put("device.gearvr", "&8[&7Gear&fVR&8]"); + defaults.put("device.hololens", "&8[&7Holo&fLens&8]"); + defaults.put("device.uwp", "&8[&3UWP&8]"); + defaults.put("device.win32", "&8[&3Win&f32&8]"); + defaults.put("device.dedicated", "&8[&aDED&8]"); + defaults.put("device.ps4", "&8[&3PS4&8]"); + defaults.put("device.nx", "&8[&eSwitch&8]"); + defaults.put("device.xbox", "&8[&2Xbox&8]"); + defaults.put("locale.found", "&8[&6%locale%&8]"); + defaults.put("locale.none", "&8[&6N/A&8]"); + defaults.put("version.found", "&8[&6%version%&8]"); + defaults.put("version.none", "&8[&6N/A&8]"); + defaults.put("xbox-username.found", "&8[&6%username%&8]"); + defaults.put("xbox-username.none", "&8[&6N/A&8]"); + defaults.put("xbox-xuid.found", "&8[&6%xuid%&8]"); + defaults.put("xbox-xuid.none", "&8[&6N/A&8]"); + return defaults; + } } diff --git a/src/main/java/com/rtm516/FloodgatePlaceholders/PluginConfig.java b/src/main/java/com/rtm516/FloodgatePlaceholders/PluginConfig.java new file mode 100644 index 0000000..5500816 --- /dev/null +++ b/src/main/java/com/rtm516/FloodgatePlaceholders/PluginConfig.java @@ -0,0 +1,111 @@ +package com.rtm516.FloodgatePlaceholders; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import lombok.Getter; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.logging.Level; +import java.util.logging.Logger; + +@Getter +public class PluginConfig implements Config { + @JsonProperty(value = "specific-device-descriptors") + private boolean specificDeviceDescriptors; + + @JsonProperty(value = "device") + private PluginDevicePlaceholders device; + + @JsonProperty(value = "locale") + private PluginGenericPlaceholders locale; + + @JsonProperty(value = "version") + private PluginGenericPlaceholders version; + + @JsonProperty(value = "xbox-username") + private PluginGenericPlaceholders xboxUsername; + + @JsonProperty(value = "xbox-xuid") + private PluginGenericPlaceholders xboxXuid; + + @Getter + public static class PluginDevicePlaceholders implements DevicePlaceholders { + @JsonProperty("java") + private String java; + + @JsonProperty("generic") + private String generic; + + @JsonProperty("unknown") + private String unknown; + + @JsonProperty("google") + private String google; + + @JsonProperty("ios") + private String iOS; + + @JsonProperty("osx") + private String OSX; + + @JsonProperty("amazon") + private String amazon; + + @JsonProperty("gearvr") + private String gearVR; + + @JsonProperty("hololens") + private String hololens; + + @JsonProperty("uwp") + private String uwp; + + @JsonProperty("win32") + private String win32; + + @JsonProperty("dedicated") + private String dedicated; + + @JsonProperty("ps4") + private String ps4; + + @JsonProperty("nx") + private String NX; + + @JsonProperty("xbox") + private String xbox; + } + + @Getter + public static class PluginGenericPlaceholders implements GenericPlaceholders { + @JsonProperty("found") + private String found; + + @JsonProperty("none") + private String none; + } + + public static PluginConfig load(Logger logger, Path configPath) { + PluginConfig pluginConfig = null; + try { + try { + if (!configPath.toFile().exists()) + Files.copy(PluginConfig.class.getClassLoader().getResourceAsStream("config.yml"), configPath); + } catch (Exception e) { + logger.log(Level.SEVERE, "Error while creating config.yml", e); + } + + pluginConfig = new ObjectMapper(new YAMLFactory()).readValue(Files.readAllBytes(configPath), PluginConfig.class); + } catch (Exception e) { + logger.log(Level.SEVERE, "Error while loading config.yml", e); + } + + if (pluginConfig == null) { + throw new RuntimeException("Failed to load config file! Try to delete the data folder of FloodgatePlaceholders"); + } + + return pluginConfig; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 07306e7..6160822 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -9,7 +9,7 @@ device: google: "&8[&aAndroid&8]" ios: "&8[&bi&fOS&8]" osx: "&8[&fOS&bX&8]" - fireos: "&8[&6F&fireOS&8]" + amazon: "&8[&6Amazon&8]" gearvr: "&8[&7Gear&fVR&8]" hololens: "&8[&7Holo&fLens&8]" uwp: "&8[&3UWP&8]" @@ -17,7 +17,7 @@ device: dedicated: "&8[&aDED&8]" ps4: "&8[&3PS4&8]" nx: "&8[&eSwitch&8]" - xboxOne: "&8[&2Xbox&8]" + xbox: "&8[&2Xbox&8]" # The locale format (none for java users) locale: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 54dc1c6..4df620e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,4 +3,4 @@ main: com.rtm516.FloodgatePlaceholders.Main version: ${project.version} author: rtm516 api-version: 1.15 -depend: [floodgate,PlaceholderAPI] \ No newline at end of file +depend: [floodgate, PlaceholderAPI] \ No newline at end of file