Fixed DefaultMap and ListMap, adding tasks to lifecycle events, and packet injection
This commit is contained in:
@@ -35,7 +35,6 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Logger;
|
||||
@@ -139,27 +138,27 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
|
||||
/**
|
||||
* The tasks to run on enable.
|
||||
*/
|
||||
private final Map<LifecyclePosition, List<Runnable>> onEnable = new ListMap<>();
|
||||
private final ListMap<LifecyclePosition, Runnable> onEnable = new ListMap<>();
|
||||
|
||||
/**
|
||||
* The tasks to run on disable.
|
||||
*/
|
||||
private final Map<LifecyclePosition, List<Runnable>> onDisable = new ListMap<>();
|
||||
private final ListMap<LifecyclePosition, Runnable> onDisable = new ListMap<>();
|
||||
|
||||
/**
|
||||
* The tasks to run on reload.
|
||||
*/
|
||||
private final Map<LifecyclePosition, List<Runnable>> onReload = new ListMap<>();
|
||||
private final ListMap<LifecyclePosition, Runnable> onReload = new ListMap<>();
|
||||
|
||||
/**
|
||||
* The tasks to run on load.
|
||||
*/
|
||||
private final Map<LifecyclePosition, List<Runnable>> onLoad = new ListMap<>();
|
||||
private final ListMap<LifecyclePosition, Runnable> onLoad = new ListMap<>();
|
||||
|
||||
/**
|
||||
* The tasks to run after load.
|
||||
*/
|
||||
private final Map<LifecyclePosition, List<Runnable>> afterLoad = new ListMap<>();
|
||||
private final ListMap<LifecyclePosition, Runnable> afterLoad = new ListMap<>();
|
||||
|
||||
/**
|
||||
* Create a new plugin.
|
||||
@@ -453,7 +452,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
|
||||
*/
|
||||
public final void onEnable(@NotNull final LifecyclePosition position,
|
||||
@NotNull final Runnable task) {
|
||||
this.onEnable.get(position).add(task);
|
||||
this.onEnable.append(position, task);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -495,7 +494,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
|
||||
*/
|
||||
public final void onDisable(@NotNull final LifecyclePosition position,
|
||||
@NotNull final Runnable task) {
|
||||
this.onDisable.get(position).add(task);
|
||||
this.onDisable.append(position, task);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -527,7 +526,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
|
||||
*/
|
||||
public final void onLoad(@NotNull final LifecyclePosition position,
|
||||
@NotNull final Runnable task) {
|
||||
this.onLoad.get(position).add(task);
|
||||
this.onLoad.append(position, task);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -591,7 +590,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
|
||||
*/
|
||||
public final void afterLoad(@NotNull final LifecyclePosition position,
|
||||
@NotNull final Runnable task) {
|
||||
this.afterLoad.get(position).add(task);
|
||||
this.afterLoad.append(position, task);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -619,7 +618,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
|
||||
* @param task The task.
|
||||
*/
|
||||
public final void onReload(@NotNull final Runnable task) {
|
||||
this.afterLoad(LifecyclePosition.END, task);
|
||||
this.onReload(LifecyclePosition.END, task);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -630,7 +629,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
|
||||
*/
|
||||
public final void onReload(@NotNull final LifecyclePosition position,
|
||||
@NotNull final Runnable task) {
|
||||
this.onReload.get(position).add(task);
|
||||
this.onReload.append(position, task);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* A map with a default value.
|
||||
@@ -23,7 +24,7 @@ public class DefaultMap<K, V> implements Map<K, V> {
|
||||
/**
|
||||
* The default value.
|
||||
*/
|
||||
private final V defaultValue;
|
||||
private final Supplier<V> defaultValue;
|
||||
|
||||
/**
|
||||
* Create a new default map.
|
||||
@@ -31,8 +32,16 @@ public class DefaultMap<K, V> implements Map<K, V> {
|
||||
* @param defaultValue The default value.
|
||||
*/
|
||||
public DefaultMap(@NotNull final V defaultValue) {
|
||||
this.map = new HashMap<>();
|
||||
this.defaultValue = defaultValue;
|
||||
this(() -> defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new default map.
|
||||
*
|
||||
* @param defaultValue The default value.
|
||||
*/
|
||||
public DefaultMap(@NotNull final Supplier<V> defaultValue) {
|
||||
this(new HashMap<>(), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,7 +50,19 @@ public class DefaultMap<K, V> implements Map<K, V> {
|
||||
* @param map The map.
|
||||
* @param defaultValue The default value.
|
||||
*/
|
||||
public DefaultMap(@NotNull final Map<K, V> map, @NotNull final V defaultValue) {
|
||||
public DefaultMap(@NotNull final Map<K, V> map,
|
||||
@NotNull final V defaultValue) {
|
||||
this(map, () -> 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 Supplier<V> defaultValue) {
|
||||
this.map = map;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
@@ -51,11 +72,11 @@ public class DefaultMap<K, V> implements Map<K, V> {
|
||||
@SuppressWarnings("unchecked")
|
||||
public V get(@Nullable final Object key) {
|
||||
if (key == null) {
|
||||
return defaultValue;
|
||||
return defaultValue.get();
|
||||
}
|
||||
|
||||
if (map.get(key) == null) {
|
||||
map.put((K) key, defaultValue);
|
||||
map.put((K) key, defaultValue.get());
|
||||
}
|
||||
|
||||
return map.get(key);
|
||||
|
||||
@@ -16,7 +16,7 @@ public class ListMap<K, V> extends DefaultMap<K, List<V>> {
|
||||
* Create a new list map.
|
||||
*/
|
||||
public ListMap() {
|
||||
super(new ArrayList<>());
|
||||
super(ArrayList::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,9 +25,8 @@ public class ListMap<K, V> extends DefaultMap<K, List<V>> {
|
||||
* @param key The key.
|
||||
* @param value The value.
|
||||
*/
|
||||
void append(@NotNull final K key,
|
||||
@NotNull final V value) {
|
||||
public void append(@NotNull final K key,
|
||||
@NotNull final V value) {
|
||||
this.get(key).add(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user