Skip to content

Commit

Permalink
Adds KeySharingException
Browse files Browse the repository at this point in the history
Adds BlobTranslatableModder#matchReplace
  • Loading branch information
anjoismysign committed Jan 3, 2024
1 parent 9af164b commit 2909d8b
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ci-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.48</version>
<version>1.697.52</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.48</version>
<version>1.697.52</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.48</version>
<version>1.697.52</version>
<packaging>pom</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ public static InventoryBuilderCarrier<InventoryButton> 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);
}
Expand Down Expand Up @@ -84,9 +87,12 @@ public static InventoryBuilderCarrier<MetaInventoryButton> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public void postWorld() {
managerDirector.postWorld();
}

public void reload() {
managerDirector.reload();
public void reloadAll() {
managerDirector.reloadAll();
}

public IFileManager getFileManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.util.Locale;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @param <T, R> The type of the translatable
Expand Down Expand Up @@ -36,6 +38,51 @@ public BlobTranslatableModder<T, R> replace(String old, String replacement) {
return modify(s -> s.replace(old, replacement));
}

private BlobTranslatableModder<T, R> regexReplace(String match, Function<String, String> 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.
* <p>
* Example:
* matchReplace("%flag@%", "@", s -> s); //Will set all flag placeholders as
* //whatever the flag is.
* <p>
*
* @param match The regex to match
* @param wildcard The wildcard to use
* @param function The function to use
* @return The modified translatable
*/
public BlobTranslatableModder<T, R> matchReplace(String match, String wildcard, Function<String, String> 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<T, R> matchReplace(String match, Function<String, String> function) {
return matchReplace(match, "@", function);
}

/**
* @return The translatable in lower case
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected void blobLibReload() {
PluginManager.unloadAssets(this);
//Loads assets
PluginManager.loadAssets(this);
getManagerDirector().reload();
getManagerDirector().reloadAll();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 35 additions & 6 deletions src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -387,6 +385,14 @@ public <T extends BlobObject> 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'.
*
Expand Down Expand Up @@ -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<Map.Entry<String, Manager>> getManagerEntry() {
return managers.entrySet();
}
Expand Down

0 comments on commit 2909d8b

Please sign in to comment.