Add and fix OverrideInjector unbinding

This commit is contained in:
James Seibel
2024-01-30 19:43:37 -06:00
parent 4ab2fef1ac
commit b622095883
4 changed files with 24 additions and 23 deletions
@@ -30,7 +30,7 @@ import java.util.HashMap;
* This is done so other mods can override our methods to improve features down the line.
*
* @author James Seibel
* @version 2022-9-8
* @version 2024-1-30
*/
public class OverrideInjector implements IOverrideInjector<IDhApiOverrideable>
{
@@ -38,7 +38,6 @@ public class OverrideInjector implements IOverrideInjector<IDhApiOverrideable>
private final HashMap<Class<? extends IDhApiOverrideable>, OverridePriorityListContainer> overrideContainerByInterface = new HashMap<>();
/**
* This is used to determine if an override is part of Distant Horizons'
* Core or not.
@@ -111,7 +110,15 @@ public class OverrideInjector implements IOverrideInjector<IDhApiOverrideable>
overrideContainer.addOverride(dependencyImplementation);
}
@Override
public void unbind(Class<? extends IDhApiOverrideable> dependencyInterface, IDhApiOverrideable dependencyImplementation)
{
OverridePriorityListContainer overrideContainer = this.overrideContainerByInterface.get(dependencyInterface);
if (overrideContainer != null)
{
overrideContainer.removeOverride(dependencyImplementation);
}
}
@@ -137,12 +144,13 @@ public class OverrideInjector implements IOverrideInjector<IDhApiOverrideable>
//==========//
// clearing //
//==========//
@Override
public void clear() { this.overrideContainerByInterface.clear(); }
}
@@ -48,17 +48,8 @@ public class OverridePriorityListContainer implements IBindable
/** @return true if the override was removed from the list, false otherwise. */
public boolean removeOverride(IDhApiOverrideable override)
{
if (this.overridePairList.contains(override))
{
this.overridePairList.remove(override);
sortList();
return true;
}
else
{
return false;
}
boolean overrideRemoved = this.overridePairList.removeIf((pair) -> pair.override.equals(override));
return overrideRemoved;
}
@@ -83,7 +83,7 @@ public interface IDependencyInjector<BindableType extends IBindable>
/** Removes all bound dependencies. */
public void clear();
void clear();
@@ -28,21 +28,22 @@ public interface IOverrideInjector<BindableType extends IBindable>
* All core overrides should have this priority. <Br>
* Should be lower than {@link IOverrideInjector#MIN_NON_CORE_OVERRIDE_PRIORITY}.
*/
public static final int CORE_PRIORITY = -1;
int CORE_PRIORITY = -1;
/**
* The lowest priority non-core overrides can have.
* Should be higher than {@link IOverrideInjector#CORE_PRIORITY}.
*/
public static final int MIN_NON_CORE_OVERRIDE_PRIORITY = 0;
int MIN_NON_CORE_OVERRIDE_PRIORITY = 0;
/** The priority given to overrides that don't explicitly define a priority. */
public static final int DEFAULT_NON_CORE_OVERRIDE_PRIORITY = 10;
int DEFAULT_NON_CORE_OVERRIDE_PRIORITY = 10;
/**
* See {@link IDependencyInjector#bind(Class, IBindable) bind(Class, IBindable)} for full documentation.
*
* @throws IllegalArgumentException if a non-Distant Horizons Override with the priority CORE is passed in or a invalid priority value.
* @throws IllegalArgumentException if a non-Distant Horizons Override with the priority {@link IOverrideInjector#CORE_PRIORITY} is passed in
* or an override is passed in with an invalid priority value.
* @throws IllegalStateException if another override with the given priority already has been bound.
* @see IDependencyInjector#bind(Class, IBindable)
*/
@@ -66,7 +67,8 @@ public interface IOverrideInjector<BindableType extends IBindable>
*/
<T extends IDhApiOverrideable> T get(Class<T> interfaceClass, int priority) throws ClassCastException;
/** Removes the given concrete {@link IDhApiOverrideable} bound to the given interface. */
void unbind(Class<? extends IDhApiOverrideable> dependencyInterface, IDhApiOverrideable dependencyImplementation);
/** Removes all bound overrides. */
void clear();