From b622095883c13beafa05477b21f26a15c11979a4 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Tue, 30 Jan 2024 19:43:37 -0600 Subject: [PATCH] Add and fix OverrideInjector unbinding --- .../DependencyInjection/OverrideInjector.java | 20 +++++++++++++------ .../OverridePriorityListContainer.java | 13 ++---------- .../IDependencyInjector.java | 2 +- .../IOverrideInjector.java | 12 ++++++----- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/OverrideInjector.java b/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/OverrideInjector.java index f858300e0..9fd6dd9d8 100644 --- a/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/OverrideInjector.java +++ b/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/OverrideInjector.java @@ -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 { @@ -38,7 +38,6 @@ public class OverrideInjector implements IOverrideInjector private final HashMap, 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 overrideContainer.addOverride(dependencyImplementation); } - + @Override + public void unbind(Class dependencyInterface, IDhApiOverrideable dependencyImplementation) + { + OverridePriorityListContainer overrideContainer = this.overrideContainerByInterface.get(dependencyInterface); + if (overrideContainer != null) + { + overrideContainer.removeOverride(dependencyImplementation); + } + } @@ -137,12 +144,13 @@ public class OverrideInjector implements IOverrideInjector + //==========// + // clearing // + //==========// + @Override public void clear() { this.overrideContainerByInterface.clear(); } - - - } diff --git a/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/OverridePriorityListContainer.java b/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/OverridePriorityListContainer.java index 4645c0ecf..143259ee8 100644 --- a/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/OverridePriorityListContainer.java +++ b/api/src/main/java/com/seibel/distanthorizons/coreapi/DependencyInjection/OverridePriorityListContainer.java @@ -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; } diff --git a/api/src/main/java/com/seibel/distanthorizons/coreapi/interfaces/dependencyInjection/IDependencyInjector.java b/api/src/main/java/com/seibel/distanthorizons/coreapi/interfaces/dependencyInjection/IDependencyInjector.java index 3855a6557..9bb3b4e12 100644 --- a/api/src/main/java/com/seibel/distanthorizons/coreapi/interfaces/dependencyInjection/IDependencyInjector.java +++ b/api/src/main/java/com/seibel/distanthorizons/coreapi/interfaces/dependencyInjection/IDependencyInjector.java @@ -83,7 +83,7 @@ public interface IDependencyInjector /** Removes all bound dependencies. */ - public void clear(); + void clear(); diff --git a/api/src/main/java/com/seibel/distanthorizons/coreapi/interfaces/dependencyInjection/IOverrideInjector.java b/api/src/main/java/com/seibel/distanthorizons/coreapi/interfaces/dependencyInjection/IOverrideInjector.java index b37883b59..982dda17b 100644 --- a/api/src/main/java/com/seibel/distanthorizons/coreapi/interfaces/dependencyInjection/IOverrideInjector.java +++ b/api/src/main/java/com/seibel/distanthorizons/coreapi/interfaces/dependencyInjection/IOverrideInjector.java @@ -28,21 +28,22 @@ public interface IOverrideInjector * All core overrides should have this priority.
* 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 */ T get(Class interfaceClass, int priority) throws ClassCastException; - + /** Removes the given concrete {@link IDhApiOverrideable} bound to the given interface. */ + void unbind(Class dependencyInterface, IDhApiOverrideable dependencyImplementation); /** Removes all bound overrides. */ void clear();