diff --git a/src/api/java/appeng/api/networking/IGridCacheFactory.java b/src/api/java/appeng/api/networking/IGridCacheFactory.java new file mode 100644 index 000000000..3bab888ee --- /dev/null +++ b/src/api/java/appeng/api/networking/IGridCacheFactory.java @@ -0,0 +1,43 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2013 AlgorithmX2 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package appeng.api.networking; + +import javax.annotation.Nonnull; + +/** + * A factory for grid cache implementations. + */ +@FunctionalInterface +public interface IGridCacheFactory { + + /** + * Creates a grid cache for the given grid. + * + * @param grid The grid for which the cache should be created. + * @return A new grid cache instance. + */ + @Nonnull + T createCache(IGrid grid); + +} diff --git a/src/api/java/appeng/api/networking/IGridCacheRegistry.java b/src/api/java/appeng/api/networking/IGridCacheRegistry.java index 6febca132..6c6cadb8a 100644 --- a/src/api/java/appeng/api/networking/IGridCacheRegistry.java +++ b/src/api/java/appeng/api/networking/IGridCacheRegistry.java @@ -36,10 +36,10 @@ public interface IGridCacheRegistry { * Register a new grid cache for use during operation, must be called during the * loading phase. * - * @param iface grid cache class + * @param iface grid cache class + * @param factory Factory for creating a new instance for each constructed grid */ - void registerGridCache(@Nonnull Class iface, - @Nonnull Class implementation); + void registerGridCache(@Nonnull Class iface, @Nonnull IGridCacheFactory factory); /** * requests a new INSTANCE of a grid cache for use, used internally diff --git a/src/main/java/appeng/core/Registration.java b/src/main/java/appeng/core/Registration.java index 3ee9f956e..32959e85a 100644 --- a/src/main/java/appeng/core/Registration.java +++ b/src/main/java/appeng/core/Registration.java @@ -130,14 +130,14 @@ final class Registration { final IRegistryContainer registries = api.registries(); final IGridCacheRegistry gcr = registries.gridCache(); - gcr.registerGridCache(ITickManager.class, TickManagerCache.class); - gcr.registerGridCache(IEnergyGrid.class, EnergyGridCache.class); - gcr.registerGridCache(IPathingGrid.class, PathGridCache.class); - gcr.registerGridCache(IStorageGrid.class, GridStorageCache.class); - gcr.registerGridCache(P2PCache.class, P2PCache.class); - gcr.registerGridCache(ISpatialCache.class, SpatialPylonCache.class); - gcr.registerGridCache(ISecurityGrid.class, SecurityCache.class); - gcr.registerGridCache(ICraftingGrid.class, CraftingGridCache.class); + gcr.registerGridCache(ITickManager.class, TickManagerCache::new); + gcr.registerGridCache(IEnergyGrid.class, EnergyGridCache::new); + gcr.registerGridCache(IPathingGrid.class, PathGridCache::new); + gcr.registerGridCache(IStorageGrid.class, GridStorageCache::new); + gcr.registerGridCache(P2PCache.class, P2PCache::new); + gcr.registerGridCache(ISpatialCache.class, SpatialPylonCache::new); + gcr.registerGridCache(ISecurityGrid.class, SecurityCache::new); + gcr.registerGridCache(ICraftingGrid.class, CraftingGridCache::new); registries.cell().addCellHandler(new BasicCellHandler()); registries.cell().addCellHandler(new CreativeCellHandler()); diff --git a/src/main/java/appeng/core/features/registries/GridCacheRegistry.java b/src/main/java/appeng/core/features/registries/GridCacheRegistry.java index 35f6d2fc4..ab53f7999 100644 --- a/src/main/java/appeng/core/features/registries/GridCacheRegistry.java +++ b/src/main/java/appeng/core/features/registries/GridCacheRegistry.java @@ -18,54 +18,57 @@ package appeng.core.features.registries; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import javax.annotation.Nonnull; + import appeng.api.networking.IGrid; import appeng.api.networking.IGridCache; +import appeng.api.networking.IGridCacheFactory; import appeng.api.networking.IGridCacheRegistry; import appeng.core.AELog; public final class GridCacheRegistry implements IGridCacheRegistry { - private final Map, Class> caches = new HashMap<>(); + + private final List> registry = new ArrayList<>(); @Override - public void registerGridCache(final Class iface, - final Class implementation) { - if (iface.isAssignableFrom(implementation)) { - this.caches.put(iface, implementation); - } else { - throw new IllegalArgumentException( - "Invalid setup, grid cache must either be the same class, or an interface that the implementation implements. Gotten: " - + iface + " and " + implementation); + public synchronized void registerGridCache(@Nonnull Class iface, + @Nonnull IGridCacheFactory factory) { + + if (registry.stream().anyMatch(r -> r.cacheClass.equals(iface))) { + AELog.debug("Overriding grid cache factory for cache type %s", iface); } + + registry.add(new GridCacheRegistration<>(iface, factory)); + } @Override - public HashMap, IGridCache> createCacheInstance(final IGrid g) { - final HashMap, IGridCache> map = new HashMap<>(); + public Map, IGridCache> createCacheInstance(final IGrid g) { + final Map, IGridCache> map = new HashMap<>(registry.size()); - for (final Class iface : this.caches.keySet()) { - try { - final Constructor c = this.caches.get(iface).getConstructor(IGrid.class); - map.put(iface, c.newInstance(g)); - } catch (final NoSuchMethodException e) { - AELog.error("Grid Caches must have a constructor with IGrid as the single param."); - throw new IllegalArgumentException(e); - } catch (final InvocationTargetException e) { - AELog.error("Grid Caches must have a constructor with IGrid as the single param."); - throw new IllegalStateException(e); - } catch (final InstantiationException e) { - AELog.error("Grid Caches must have a constructor with IGrid as the single param."); - throw new IllegalStateException(e); - } catch (final IllegalAccessException e) { - AELog.error("Grid Caches must have a constructor with IGrid as the single param."); - throw new IllegalStateException(e); - } + for (GridCacheRegistration registration : registry) { + map.put(registration.cacheClass, registration.factory.createCache(g)); } return map; } + + private static class GridCacheRegistration { + + private final Class cacheClass; + + private final IGridCacheFactory factory; + + public GridCacheRegistration(Class cacheClass, IGridCacheFactory factory) { + this.cacheClass = cacheClass; + this.factory = factory; + } + + } + } diff --git a/src/main/java/appeng/me/Grid.java b/src/main/java/appeng/me/Grid.java index 1c86bba91..40caef967 100644 --- a/src/main/java/appeng/me/Grid.java +++ b/src/main/java/appeng/me/Grid.java @@ -42,7 +42,7 @@ import appeng.util.ReadOnlyCollection; public class Grid implements IGrid { private final NetworkEventBus eventBus = new NetworkEventBus(); private final Map, MachineSet> machines = new HashMap<>(); - private final Map, GridCacheWrapper> caches = new HashMap<>(); + private final Map, GridCacheWrapper> caches; private GridNode pivot; private int priority; // how import is this network? private GridStorage myStorage; @@ -52,6 +52,7 @@ public class Grid implements IGrid { final Map, IGridCache> myCaches = AEApi.instance().registries().gridCache() .createCacheInstance(this); + this.caches = new HashMap<>(myCaches.size()); for (final Entry, IGridCache> c : myCaches.entrySet()) { final Class key = c.getKey(); final IGridCache value = c.getValue(); diff --git a/src/main/java/appeng/me/cache/TickManagerCache.java b/src/main/java/appeng/me/cache/TickManagerCache.java index 2134682c5..c20605055 100644 --- a/src/main/java/appeng/me/cache/TickManagerCache.java +++ b/src/main/java/appeng/me/cache/TickManagerCache.java @@ -21,7 +21,6 @@ package appeng.me.cache; import java.util.HashMap; import java.util.PriorityQueue; -import appeng.helpers.Reflected; import com.google.common.base.Preconditions; import net.minecraft.crash.CrashReport; @@ -47,7 +46,6 @@ public class TickManagerCache implements ITickManager { private long currentTick = 0; - @Reflected public TickManagerCache(@SuppressWarnings("unused") final IGrid g) { }