diff --git a/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java b/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java index 0c3c51b..842badb 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java +++ b/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java @@ -54,4 +54,16 @@ public final class ComponentManager { val c = Component.getComponents().get(cl); return c != null && c.isEnabled(); } + + /** + * Will also return false if the component is not registered. + * + * @param cl The component class + * @return The component if it's registered and enabled + */ + @SuppressWarnings("unchecked") + public static T getIfEnabled(Class cl) { + val c = Component.getComponents().get(cl); + return c != null && c.isEnabled() ? (T) c : null; + } } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java index 2271d56..98fa241 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java @@ -1,10 +1,12 @@ package buttondevteam.lib.architecture; import lombok.Getter; +import lombok.val; import org.bukkit.configuration.ConfigurationSection; import java.util.HashMap; import java.util.function.Function; +import java.util.function.Supplier; /** * A config system @@ -73,4 +75,42 @@ public final class IHaveConfig { datamap.put(path, data = new ConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter)); return (ConfigData) data; } + + /** + * This method overload should only be used with primitves or String. + * + * @param path The path in config to use + * @param def The value to use by default + * @param The type of this variable (only use primitives or String) + * @return The data object that can be used to get or set the value + */ + @SuppressWarnings("unchecked") + public ConfigData getData(String path, Supplier def) { + ConfigData data = datamap.get(path); + if (data == null) { + val defval = def.get(); + datamap.put(path, data = new ConfigData<>(config, path, defval, defval)); + } + return (ConfigData) data; + } + + /** + * This method overload may be used with any class. + * + * @param path The path in config to use + * @param def The value to use by default + * @param getter A function that converts a primitive representation to the correct value + * @param setter A function that converts a value to a primitive representation + * @param The type of this variable (can be any class) + * @return The data object that can be used to get or set the value + */ + @SuppressWarnings("unchecked") + public ConfigData getData(String path, Supplier def, Function getter, Function setter) { + ConfigData data = datamap.get(path); + if (data == null) { + val defval = def.get(); + datamap.put(path, data = new ConfigData<>(config, path, defval, setter.apply(defval), getter, setter)); + } + return (ConfigData) data; + } }