diff --git a/ci-pom.xml b/ci-pom.xml index 2ad782c..760f239 100644 --- a/ci-pom.xml +++ b/ci-pom.xml @@ -7,7 +7,7 @@ us.mytheria BlobLib - 1.697.48 + 1.697.52 pom.xml bloblib diff --git a/local-pom.xml b/local-pom.xml index a56de0d..eb1e95f 100644 --- a/local-pom.xml +++ b/local-pom.xml @@ -5,7 +5,7 @@ us.mytheria BlobLib - 1.697.48 + 1.697.52 pom.xml bloblib diff --git a/pom.xml b/pom.xml index 24d2a5d..7b1e8f7 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 us.mytheria BlobLib - 1.697.48 + 1.697.52 pom diff --git a/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilderCarrier.java b/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilderCarrier.java index 2959f1a..36dc4e6 100644 --- a/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilderCarrier.java +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilderCarrier.java @@ -48,9 +48,12 @@ public static InventoryBuilderCarrier BLOB_FROM_CONFIGURATION_S } } String locale = configurationSection.getString("Locale", "en_us"); + ConfigurationSection buttonsSection = configurationSection + .getConfigurationSection("Buttons"); + if (buttonsSection == null) + buttonsSection = configurationSection.createSection("Buttons"); BlobButtonManager buttonManager = BlobButtonManager - .fromConfigurationSection(configurationSection - .getConfigurationSection("Buttons"), locale); + .fromConfigurationSection(buttonsSection, locale); return new InventoryBuilderCarrier<>(title, size, buttonManager, null, reference, locale); } @@ -84,9 +87,12 @@ public static InventoryBuilderCarrier META_FROM_CONFIGURATI String type = configurationSection.isString("Type") ? configurationSection.getString("Type") : "DEFAULT"; String locale = configurationSection.getString("Locale", "en_us"); + ConfigurationSection buttonsSection = configurationSection + .getConfigurationSection("Buttons"); + if (buttonsSection == null) + buttonsSection = configurationSection.createSection("Buttons"); MetaBlobButtonManager buttonManager = MetaBlobButtonManager - .fromConfigurationSection(configurationSection - .getConfigurationSection("Buttons"), locale); + .fromConfigurationSection(buttonsSection, locale); return new InventoryBuilderCarrier<>(title, size, buttonManager, type, reference, locale); } diff --git a/src/main/java/us/mytheria/bloblib/entities/proxy/IManagerDirectorProxy.java b/src/main/java/us/mytheria/bloblib/entities/proxy/IManagerDirectorProxy.java index cf493dd..6ceff64 100644 --- a/src/main/java/us/mytheria/bloblib/entities/proxy/IManagerDirectorProxy.java +++ b/src/main/java/us/mytheria/bloblib/entities/proxy/IManagerDirectorProxy.java @@ -18,8 +18,8 @@ public void postWorld() { managerDirector.postWorld(); } - public void reload() { - managerDirector.reload(); + public void reloadAll() { + managerDirector.reloadAll(); } public IFileManager getFileManager() { diff --git a/src/main/java/us/mytheria/bloblib/entities/translatable/BlobTranslatableModder.java b/src/main/java/us/mytheria/bloblib/entities/translatable/BlobTranslatableModder.java index 9aebcb7..4b903b6 100644 --- a/src/main/java/us/mytheria/bloblib/entities/translatable/BlobTranslatableModder.java +++ b/src/main/java/us/mytheria/bloblib/entities/translatable/BlobTranslatableModder.java @@ -5,6 +5,8 @@ import java.util.Locale; import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @param The type of the translatable @@ -36,6 +38,51 @@ public BlobTranslatableModder replace(String old, String replacement) { return modify(s -> s.replace(old, replacement)); } + private BlobTranslatableModder regexReplace(String match, Function function) { + Pattern pattern = Pattern.compile(match); + return modify(s -> { + Matcher matcher = pattern.matcher(s); + StringBuilder sb = new StringBuilder(); + while (matcher.find()) { + matcher.appendReplacement(sb, function.apply(matcher.group(1))); + } + matcher.appendTail(sb); + return sb.toString(); + }); + } + + /** + * Will match all occurrences of the given regex and replace them with the + * result of the function by using a wildcard. + *

+ * Example: + * matchReplace("%flag@%", "@", s -> s); //Will set all flag placeholders as + * //whatever the flag is. + *

+ * + * @param match The regex to match + * @param wildcard The wildcard to use + * @param function The function to use + * @return The modified translatable + */ + public BlobTranslatableModder matchReplace(String match, String wildcard, Function function) { + String regex = match.replace(wildcard, "(.*?)"); + return regexReplace(regex, function); + } + + /** + * Will match all occurrences of the given regex and replace them with the + * result of the function. + * Will use '@' as the wildcard. + * + * @param match The regex to match + * @param function The function to use + * @return The modified translatable + */ + public BlobTranslatableModder matchReplace(String match, Function function) { + return matchReplace(match, "@", function); + } + /** * @return The translatable in lower case */ diff --git a/src/main/java/us/mytheria/bloblib/exception/KeySharingException.java b/src/main/java/us/mytheria/bloblib/exception/KeySharingException.java new file mode 100644 index 0000000..862e12b --- /dev/null +++ b/src/main/java/us/mytheria/bloblib/exception/KeySharingException.java @@ -0,0 +1,16 @@ +package us.mytheria.bloblib.exception; + +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + +public class KeySharingException extends RuntimeException { + public static KeySharingException DEFAULT(@NotNull String key) { + Objects.requireNonNull(key); + return new KeySharingException("The key " + key + " is already in use"); + } + + public KeySharingException(String message) { + super(message); + } +} diff --git a/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java b/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java index 901f7e5..ce4d904 100644 --- a/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java +++ b/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java @@ -41,7 +41,7 @@ protected void blobLibReload() { PluginManager.unloadAssets(this); //Loads assets PluginManager.loadAssets(this); - getManagerDirector().reload(); + getManagerDirector().reloadAll(); } /** diff --git a/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java b/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java index 0a20fc9..20f8977 100644 --- a/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java +++ b/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java @@ -16,7 +16,7 @@ public interface IManagerDirector { /** * Will do reload logic (asynchronously). */ - void reload(); + void reloadAll(); /** * Will get the file manager that's required by BlobLib. diff --git a/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java b/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java index 9bd8de4..fd95015 100644 --- a/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java +++ b/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java @@ -15,6 +15,7 @@ import us.mytheria.bloblib.entities.currency.WalletOwner; import us.mytheria.bloblib.entities.currency.WalletOwnerManager; import us.mytheria.bloblib.entities.proxy.BlobProxifier; +import us.mytheria.bloblib.exception.KeySharingException; import us.mytheria.bloblib.utilities.ResourceUtil; import java.io.File; @@ -41,9 +42,8 @@ public abstract class ManagerDirector implements IManagerDirector { */ public ManagerDirector(BlobPlugin plugin) { this.namespacedKeys = new HashMap<>(); - namespacedKeys.put("tangibleCurrencyKey", new NamespacedKey(plugin, "tangibleCurrencyKey")); - namespacedKeys.put("tangibleCurrencyDenomination", new NamespacedKey(plugin, "tangibleCurrencyDenomination")); this.plugin = plugin; + reloadNamespacedKeys(); this.pluginOperator = () -> plugin; this.blobFileManager = new BlobFileManager(this, "plugins/" + plugin.getName(), @@ -75,9 +75,8 @@ public ManagerDirector(BlobPlugin plugin) { @Deprecated public ManagerDirector(BlobPlugin plugin, String fileManagerPathname) { this.namespacedKeys = new HashMap<>(); - namespacedKeys.put("tangibleCurrencyKey", new NamespacedKey(plugin, "tangibleCurrencyKey")); - namespacedKeys.put("tangibleCurrencyDenomination", new NamespacedKey(plugin, "tangibleCurrencyDenomination")); this.plugin = plugin; + reloadNamespacedKeys(); this.pluginOperator = () -> plugin; this.blobFileManager = new BlobFileManager(this, fileManagerPathname, plugin); @@ -98,9 +97,8 @@ public ManagerDirector(BlobPlugin plugin, String fileManagerPathname) { */ public ManagerDirector(BlobPlugin plugin, BlobFileManager fileManager) { this.namespacedKeys = new HashMap<>(); - namespacedKeys.put("tangibleCurrencyKey", new NamespacedKey(plugin, "tangibleCurrencyKey")); - namespacedKeys.put("tangibleCurrencyDenomination", new NamespacedKey(plugin, "tangibleCurrencyDenomination")); this.plugin = plugin; + reloadNamespacedKeys(); this.pluginOperator = () -> plugin; this.blobFileManager = Objects.requireNonNull(fileManager, "BlobFileManager cannot be null!"); this.proxiedFileManager = BlobProxifier.PROXY(blobFileManager); @@ -387,6 +385,14 @@ public void addDirector(String objectName, public void reload() { } + /** + * Will reload ManagerDirector's defaults and do reload logic. + */ + public void reloadAll() { + reloadNamespacedKeys(); + reload(); + } + /** * Will retrieve a manager by providing a String 'key'. * @@ -751,6 +757,29 @@ public ManagerDirector registerTranslatableSnippet(String... fileNames) { return registerTranslatableSnippet(false, fileNames); } + /** + * Creates a new NamespacedKey. + * + * @param key The key + * @return The NamespacedKey + */ + public NamespacedKey createNamespacedKey(String key) { + if (namespacedKeys.containsKey(key)) + throw KeySharingException.DEFAULT(key); + NamespacedKey namespacedKey = new NamespacedKey(plugin, key); + namespacedKeys.put(key, namespacedKey); + return namespacedKey; + } + + /** + * Reloads all namespaced keys. + */ + public void reloadNamespacedKeys() { + namespacedKeys.clear(); + createNamespacedKey("tangibleCurrencyKey"); + createNamespacedKey("tangibleCurrencyDenomination"); + } + protected Set> getManagerEntry() { return managers.entrySet(); }