Compman and config API extension

Fancy way of saying: I added 3 methods
This commit is contained in:
Norbi Peti 2019-01-28 23:09:07 +01:00
parent a6effaaf43
commit 41d40fa415
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 52 additions and 0 deletions

View file

@ -54,4 +54,16 @@ public final class ComponentManager {
val c = Component.getComponents().get(cl); val c = Component.getComponents().get(cl);
return c != null && c.isEnabled(); 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 extends Component> T getIfEnabled(Class<T> cl) {
val c = Component.getComponents().get(cl);
return c != null && c.isEnabled() ? (T) c : null;
}
} }

View file

@ -1,10 +1,12 @@
package buttondevteam.lib.architecture; package buttondevteam.lib.architecture;
import lombok.Getter; import lombok.Getter;
import lombok.val;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import java.util.HashMap; import java.util.HashMap;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
/** /**
* A config system * 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)); datamap.put(path, data = new ConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter));
return (ConfigData<T>) data; return (ConfigData<T>) 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 <T> 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 <T> ConfigData<T> getData(String path, Supplier<T> 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<T>) 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 <T> 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 <T> ConfigData<T> getData(String path, Supplier<T> def, Function<Object, T> getter, Function<T, Object> 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<T>) data;
}
} }