Compare commits

...

9 Commits

Author SHA1 Message Date
Auxilor
af486580c1 Updated to 6.52.0 2023-02-27 16:31:19 +00:00
Auxilor
7955a94f14 Added missing annotations 2023-02-25 19:03:18 +00:00
Auxilor
c99a1bd50a loadListeners is no longer abstract 2023-02-25 19:02:14 +00:00
Auxilor
93364247de Merge branch 'master' into develop 2023-02-25 14:49:15 +00:00
Auxilor
a806ac039d Added regex validation for Ids 2023-02-23 21:15:12 +00:00
Auxilor
0ca2651af0 Added ListMap#append 2023-02-23 21:07:04 +00:00
Auxilor
6157fdcfa1 Improved DefaultMap 2023-02-23 21:04:01 +00:00
Auxilor
36cfcd24c2 Added DefaultMap 2023-02-23 20:51:21 +00:00
Auxilor
d4558db40c Added registry 2023-02-23 18:33:28 +00:00
7 changed files with 360 additions and 2 deletions

View File

@@ -713,7 +713,9 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike {
*
* @return A list of all listeners.
*/
protected abstract List<Listener> loadListeners();
protected List<Listener> loadListeners() {
return new ArrayList<>();
}
/**
* Useful for custom LangYml implementations.
@@ -862,6 +864,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike {
* @param pluginName The name.
* @return The plugin.
*/
@Nullable
public static EcoPlugin getPlugin(@NotNull final String pluginName) {
return Eco.get().getPluginByName(pluginName);
}
@@ -871,6 +874,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike {
*
* @return The set of names.
*/
@NotNull
public static Set<String> getPluginNames() {
return new HashSet<>(Eco.get().getLoadedPlugins());
}

View File

@@ -0,0 +1,147 @@
package com.willfp.eco.core.map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* A map with a default value.
*
* @param <K> The key type.
* @param <V> The value type.
*/
public class DefaultMap<K, V> implements Map<K, V> {
/**
* The map.
*/
private final Map<K, V> map;
/**
* The default value.
*/
private final V defaultValue;
/**
* Create a new default map.
*
* @param defaultValue The default value.
*/
public DefaultMap(@NotNull final V defaultValue) {
this.map = new HashMap<>();
this.defaultValue = defaultValue;
}
/**
* Create a new default map.
*
* @param map The map.
* @param defaultValue The default value.
*/
public DefaultMap(@NotNull final Map<K, V> map, @NotNull final V defaultValue) {
this.map = map;
this.defaultValue = defaultValue;
}
@Override
@NotNull
@SuppressWarnings("unchecked")
public V get(@Nullable final Object key) {
if (key == null) {
return defaultValue;
}
if (map.get(key) == null) {
map.put((K) key, defaultValue);
}
return map.get(key);
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsKey(@Nullable final Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(@Nullable final Object value) {
return map.containsValue(value);
}
@Override
public V put(@NotNull final K key, @Nullable final V value) {
return map.put(key, value);
}
@Override
public V remove(@NotNull final Object key) {
return map.remove(key);
}
@Override
public void putAll(@NotNull final Map<? extends K, ? extends V> m) {
map.putAll(m);
}
@Override
public void clear() {
map.clear();
}
@NotNull
@Override
public Set<K> keySet() {
return map.keySet();
}
@NotNull
@Override
public Collection<V> values() {
return map.values();
}
@NotNull
@Override
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}
/**
* Create a new nested map.
*
* @param <K> The key type.
* @param <K1> The nested key type.
* @param <V> The value type.
* @return The nested map.
*/
@NotNull
public static <K, K1, V> DefaultMap<K, Map<K1, V>> createNestedMap() {
return new DefaultMap<>(new HashMap<>());
}
/**
* Create a new nested list map.
*
* @param <K> The key type.
* @param <K1> The nested key type.
* @param <V> The value type.
* @return The nested list map.
*/
@NotNull
public static <K, K1, V> DefaultMap<K, ListMap<K1, V>> createNestedListMap() {
return new DefaultMap<>(new ListMap<>());
}
}

View File

@@ -0,0 +1,33 @@
package com.willfp.eco.core.map;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* Maps keys to lists of values.
*
* @param <K> The key type.
* @param <V> The value type.
*/
public class ListMap<K, V> extends DefaultMap<K, List<V>> {
/**
* Create a new list map.
*/
public ListMap() {
super(new ArrayList<>());
}
/**
* Append a value to a key.
*
* @param key The key.
* @param value The value.
*/
void append(@NotNull final K key,
@NotNull final V value) {
this.get(key).add(value);
}
}

View File

@@ -0,0 +1,36 @@
package com.willfp.eco.core.registry;
import org.jetbrains.annotations.NotNull;
/**
* An object that can be registered.
*
* @see Registry
*/
public interface Registrable {
/**
* Get the ID of the element.
*
* @return The ID.
*/
@NotNull
String getID();
/**
* Called when the element is registered.
* <p>
* This is called after registration.
*/
default void onRegister() {
// Do nothing by default.
}
/**
* Called when the element is removed.
* <p>
* This is called before removal.
*/
default void onRemove() {
// Do nothing by default.
}
}

View File

@@ -0,0 +1,111 @@
package com.willfp.eco.core.registry;
import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
/**
* A registry for {@link Registrable}s.
*
* @param <T> The type of {@link Registrable}.
*/
public abstract class Registry<T extends Registrable> {
/**
* The ID pattern.
*/
private static final Pattern ID_PATTERN = Pattern.compile("[a-z0-9_]{1,100}");
/**
* The registry.
*/
private final Map<String, T> registry = new HashMap<>();
/**
* Instantiate a new registry.
*/
protected Registry() {
}
/**
* Register a new element.
*
* @param element The element to register.
* @return The element.
*/
@NotNull
public T register(@NotNull final T element) {
Validate.isTrue(ID_PATTERN.matcher(element.getID()).matches(), "ID must match pattern: " + ID_PATTERN.pattern());
registry.put(element.getID(), element);
element.onRegister();
return element;
}
/**
* Remove an element.
*
* @param element The element.
* @return The element.
*/
public T remove(@NotNull final T element) {
element.onRemove();
registry.remove(element.getID());
return element;
}
/**
* Remove an element by ID.
*
* @param id The ID.
* @return The element.
*/
@Nullable
public T remove(@NotNull final String id) {
T element = registry.get(id);
if (element != null) {
element.onRemove();
}
return registry.remove(id);
}
/**
* Get an element by ID.
*
* @param id The ID.
* @return The element, or null if not found.
*/
@Nullable
public T get(@NotNull final String id) {
return registry.get(id);
}
/**
* Clear the registry.
*/
public void clear() {
for (T value : registry.values()) {
remove(value);
}
}
/**
* Get all elements.
*
* @return All elements.
*/
public Set<T> values() {
return Set.copyOf(registry.values());
}
}

View File

@@ -0,0 +1,27 @@
@file:JvmName("DefaultMapExtensions")
package com.willfp.eco.core.map
/**
* @see DefaultMap
*/
fun <K : Any, V : Any> defaultMap(defaultValue: V) =
DefaultMap<K, V>(defaultValue)
/**
* @see ListMap
*/
fun <K : Any, V : Any> listMap() =
ListMap<K, V>()
/**
* @see DefaultMap.createNestedMap
*/
fun <K : Any, K1 : Any, V : Any> nestedMap() =
DefaultMap.createNestedMap<K, K1, V>()
/**
* @see DefaultMap.createNestedListMap
*/
fun <K : Any, K1 : Any, V : Any> nestedListMap() =
DefaultMap.createNestedListMap<K, K1, V>()

View File

@@ -1,3 +1,3 @@
version = 6.51.4
version = 6.52.0
plugin-name = eco
kotlin.code.style = official