Merge pull request #4441 from AppliedEnergistics/cleanups
Minor Cleanups / Refactoring
This commit is contained in:
@@ -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<T extends IGridCache> {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
}
|
||||
@@ -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<? extends IGridCache> iface,
|
||||
@Nonnull Class<? extends IGridCache> implementation);
|
||||
<T extends IGridCache> void registerGridCache(@Nonnull Class<T> iface, @Nonnull IGridCacheFactory<T> factory);
|
||||
|
||||
/**
|
||||
* requests a new INSTANCE of a grid cache for use, used internally
|
||||
|
||||
@@ -32,6 +32,7 @@ import javax.annotation.Nonnull;
|
||||
import com.mojang.blaze3d.matrix.MatrixStack;
|
||||
|
||||
import net.minecraft.client.renderer.IRenderTypeBuffer;
|
||||
import net.minecraft.crash.CrashReportCategory;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
@@ -371,4 +372,14 @@ public interface IPart extends ICustomCableConnection {
|
||||
*/
|
||||
void getBoxes(final IPartCollisionHelper bch);
|
||||
|
||||
/**
|
||||
* This will be used by the core to add information about this part to a crash
|
||||
* report if it is attached to a host that caused a crash during tick
|
||||
* processing.
|
||||
*
|
||||
* @param section The crash report section the information will be added to.
|
||||
*/
|
||||
default void addEntityCrashInfo(final CrashReportCategory section) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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<? extends IGridCache>, Class<? extends IGridCache>> caches = new HashMap<>();
|
||||
|
||||
private final List<GridCacheRegistration<?>> registry = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void registerGridCache(final Class<? extends IGridCache> iface,
|
||||
final Class<? extends IGridCache> 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 <T extends IGridCache> void registerGridCache(@Nonnull Class<T> iface,
|
||||
@Nonnull IGridCacheFactory<T> 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<Class<? extends IGridCache>, IGridCache> createCacheInstance(final IGrid g) {
|
||||
final HashMap<Class<? extends IGridCache>, IGridCache> map = new HashMap<>();
|
||||
public Map<Class<? extends IGridCache>, IGridCache> createCacheInstance(final IGrid g) {
|
||||
final Map<Class<? extends IGridCache>, IGridCache> map = new HashMap<>(registry.size());
|
||||
|
||||
for (final Class<? extends IGridCache> iface : this.caches.keySet()) {
|
||||
try {
|
||||
final Constructor<? extends IGridCache> 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<T extends IGridCache> {
|
||||
|
||||
private final Class<T> cacheClass;
|
||||
|
||||
private final IGridCacheFactory<T> factory;
|
||||
|
||||
public GridCacheRegistration(Class<T> cacheClass, IGridCacheFactory<T> factory) {
|
||||
this.cacheClass = cacheClass;
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ import appeng.util.ReadOnlyCollection;
|
||||
public class Grid implements IGrid {
|
||||
private final NetworkEventBus eventBus = new NetworkEventBus();
|
||||
private final Map<Class<? extends IGridHost>, MachineSet> machines = new HashMap<>();
|
||||
private final Map<Class<? extends IGridCache>, GridCacheWrapper> caches = new HashMap<>();
|
||||
private final Map<Class<? extends IGridCache>, 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<Class<? extends IGridCache>, IGridCache> myCaches = AEApi.instance().registries().gridCache()
|
||||
.createCacheInstance(this);
|
||||
this.caches = new HashMap<>(myCaches.size());
|
||||
for (final Entry<Class<? extends IGridCache>, IGridCache> c : myCaches.entrySet()) {
|
||||
final Class<? extends IGridCache> key = c.getKey();
|
||||
final IGridCache value = c.getValue();
|
||||
|
||||
+3
-12
@@ -39,7 +39,6 @@ import appeng.me.cache.helpers.TickTracker;
|
||||
|
||||
public class TickManagerCache implements ITickManager {
|
||||
|
||||
private final IGrid myGrid;
|
||||
private final HashMap<IGridNode, TickTracker> alertable = new HashMap<>();
|
||||
private final HashMap<IGridNode, TickTracker> sleeping = new HashMap<>();
|
||||
private final HashMap<IGridNode, TickTracker> awake = new HashMap<>();
|
||||
@@ -47,12 +46,7 @@ public class TickManagerCache implements ITickManager {
|
||||
|
||||
private long currentTick = 0;
|
||||
|
||||
public TickManagerCache(final IGrid g) {
|
||||
this.myGrid = g;
|
||||
}
|
||||
|
||||
public long getCurrentTick() {
|
||||
return this.currentTick;
|
||||
public TickManagerCache(@SuppressWarnings("unused") final IGrid g) {
|
||||
}
|
||||
|
||||
public long getAvgNanoTime(final IGridNode node) {
|
||||
@@ -66,7 +60,7 @@ public class TickManagerCache implements ITickManager {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return tt.getAvgNanos();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -146,7 +140,7 @@ public class TickManagerCache implements ITickManager {
|
||||
|
||||
Preconditions.checkNotNull(tr);
|
||||
|
||||
final TickTracker tt = new TickTracker(tr, gridNode, (IGridTickable) machine, this.currentTick, this);
|
||||
final TickTracker tt = new TickTracker(tr, gridNode, (IGridTickable) machine, this.currentTick);
|
||||
|
||||
if (tr.canBeAlerted) {
|
||||
this.alertable.put(gridNode, tt);
|
||||
@@ -184,9 +178,6 @@ public class TickManagerCache implements ITickManager {
|
||||
if (tt == null) {
|
||||
return false;
|
||||
}
|
||||
// throw new RuntimeException(
|
||||
// "Invalid alerted device, this node is not marked as alertable, or part of
|
||||
// this grid." );
|
||||
|
||||
// set to awake, this is for sanity.
|
||||
this.sleeping.remove(node);
|
||||
|
||||
+4
-12
@@ -25,9 +25,8 @@ import net.minecraft.crash.CrashReportCategory;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.ticking.IGridTickable;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.api.parts.IPart;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.me.cache.TickManagerCache;
|
||||
import appeng.parts.AEBasePart;
|
||||
|
||||
public class TickTracker implements Comparable<TickTracker> {
|
||||
|
||||
@@ -35,13 +34,10 @@ public class TickTracker implements Comparable<TickTracker> {
|
||||
private final IGridTickable gt;
|
||||
private final IGridNode node;
|
||||
|
||||
private final long LastFiveTicksTime = 0;
|
||||
|
||||
private long lastTick;
|
||||
private int currentRate;
|
||||
|
||||
public TickTracker(final TickingRequest req, final IGridNode node, final IGridTickable gt, final long currentTick,
|
||||
final TickManagerCache tickManagerCache) {
|
||||
public TickTracker(final TickingRequest req, final IGridNode node, final IGridTickable gt, final long currentTick) {
|
||||
this.request = req;
|
||||
this.gt = gt;
|
||||
this.node = node;
|
||||
@@ -49,10 +45,6 @@ public class TickTracker implements Comparable<TickTracker> {
|
||||
this.setLastTick(currentTick);
|
||||
}
|
||||
|
||||
public long getAvgNanos() {
|
||||
return (this.LastFiveTicksTime / 5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@Nonnull final TickTracker t) {
|
||||
int next = Long.compare(this.getNextTick(), t.getNextTick());
|
||||
@@ -72,8 +64,8 @@ public class TickTracker implements Comparable<TickTracker> {
|
||||
}
|
||||
|
||||
public void addEntityCrashInfo(final CrashReportCategory crashreportcategory) {
|
||||
if (this.getGridTickable() instanceof AEBasePart) {
|
||||
final AEBasePart part = (AEBasePart) this.getGridTickable();
|
||||
if (this.getGridTickable() instanceof IPart) {
|
||||
final IPart part = (IPart) this.getGridTickable();
|
||||
part.addEntityCrashInfo(crashreportcategory);
|
||||
}
|
||||
|
||||
|
||||
@@ -182,6 +182,7 @@ public abstract class AEBasePart implements IPart, IGridProxyable, IActionHost,
|
||||
return this.getItemStack().hasDisplayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addEntityCrashInfo(final CrashReportCategory crashreportcategory) {
|
||||
crashreportcategory.addDetail("Part Side", this.getSide());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user