Basic enhancement of the grid for overall usage
has some performance improvement replaced outdated classes
This commit is contained in:
@@ -18,9 +18,11 @@
|
||||
|
||||
package appeng.me;
|
||||
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -29,6 +31,7 @@ import appeng.api.networking.IGrid;
|
||||
import appeng.api.networking.IGridCache;
|
||||
import appeng.api.networking.IGridHost;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.IGridStorage;
|
||||
import appeng.api.networking.IMachineSet;
|
||||
import appeng.api.networking.events.MENetworkEvent;
|
||||
import appeng.api.networking.events.MENetworkPostCacheConstruction;
|
||||
@@ -37,86 +40,104 @@ import appeng.core.WorldSettings;
|
||||
import appeng.hooks.TickHandler;
|
||||
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<Class<? extends IGridHost>, MachineSet>();
|
||||
private final Map<Class<? extends IGridCache>, GridCacheWrapper> caches = new HashMap<Class<? extends IGridCache>, GridCacheWrapper>();
|
||||
private GridNode pivot;
|
||||
private int priority; // how import is this network?
|
||||
private GridStorage myStorage;
|
||||
|
||||
GridStorage myStorage;
|
||||
|
||||
final NetworkEventBus bus = new NetworkEventBus();
|
||||
final HashMap<Class<? extends IGridHost>, Set> Machines = new HashMap<Class<? extends IGridHost>, Set>();
|
||||
final HashMap<Class<? extends IGridCache>, GridCacheWrapper> caches = new HashMap<Class<? extends IGridCache>, GridCacheWrapper>();
|
||||
|
||||
GridNode pivot;
|
||||
int isImportant; // how import is this network?
|
||||
|
||||
public Grid(GridNode center) {
|
||||
public Grid( GridNode center )
|
||||
{
|
||||
this.pivot = center;
|
||||
|
||||
HashMap<Class<? extends IGridCache>, IGridCache> myCaches = AEApi.instance().registries().gridCache().createCacheInstance( this );
|
||||
for (Entry<Class<? extends IGridCache>, IGridCache> c : myCaches.entrySet())
|
||||
Map<Class<? extends IGridCache>, IGridCache> myCaches = AEApi.instance().registries().gridCache().createCacheInstance( this );
|
||||
for ( Entry<Class<? extends IGridCache>, IGridCache> c : myCaches.entrySet() )
|
||||
{
|
||||
bus.readClass( c.getKey(), c.getValue().getClass() );
|
||||
caches.put( c.getKey(), new GridCacheWrapper( c.getValue() ) );
|
||||
Class<? extends IGridCache> key = c.getKey();
|
||||
IGridCache value = c.getValue();
|
||||
Class<? extends IGridCache> valueClass = value.getClass();
|
||||
|
||||
this.eventBus.readClass( key, valueClass );
|
||||
this.caches.put( key, new GridCacheWrapper( value ) );
|
||||
}
|
||||
|
||||
postEvent( new MENetworkPostCacheConstruction() );
|
||||
this.postEvent( new MENetworkPostCacheConstruction() );
|
||||
|
||||
TickHandler.instance.addNetwork( this );
|
||||
center.setGrid( this );
|
||||
}
|
||||
|
||||
public Set<Class<? extends IGridHost>> getMachineClasses()
|
||||
public int getPriority()
|
||||
{
|
||||
return Machines.keySet();
|
||||
return this.priority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGridNode getPivot()
|
||||
public IGridStorage getMyStorage()
|
||||
{
|
||||
return pivot;
|
||||
return this.myStorage;
|
||||
}
|
||||
|
||||
public Map<Class<? extends IGridCache>, GridCacheWrapper> getCaches()
|
||||
{
|
||||
return this.caches;
|
||||
}
|
||||
|
||||
public Iterable<Class<? extends IGridHost>> getMachineClasses()
|
||||
{
|
||||
return this.machines.keySet();
|
||||
}
|
||||
|
||||
public int size()
|
||||
{
|
||||
int out = 0;
|
||||
for (Collection<?> x : Machines.values())
|
||||
for ( Collection<?> x : this.machines.values() )
|
||||
out += x.size();
|
||||
return out;
|
||||
}
|
||||
|
||||
public void remove(GridNode gridNode)
|
||||
public void remove( GridNode gridNode )
|
||||
{
|
||||
for (IGridCache c : caches.values())
|
||||
c.removeNode( gridNode, gridNode.getMachine() );
|
||||
for ( IGridCache c : this.caches.values() )
|
||||
{
|
||||
IGridHost machine = gridNode.getMachine();
|
||||
c.removeNode( gridNode, machine );
|
||||
}
|
||||
|
||||
Collection<IGridNode> nodes = Machines.get( gridNode.getMachineClass() );
|
||||
Class<? extends IGridHost> machineClass = gridNode.getMachineClass();
|
||||
Set<IGridNode> nodes = this.machines.get( machineClass );
|
||||
if ( nodes != null )
|
||||
nodes.remove( gridNode );
|
||||
|
||||
gridNode.setGridStorage( null );
|
||||
|
||||
if ( pivot == gridNode )
|
||||
if ( this.pivot == gridNode )
|
||||
{
|
||||
Iterator<IGridNode> n = getNodes().iterator();
|
||||
Iterator<IGridNode> n = this.getNodes().iterator();
|
||||
if ( n.hasNext() )
|
||||
pivot = (GridNode) n.next();
|
||||
this.pivot = ( GridNode ) n.next();
|
||||
else
|
||||
{
|
||||
pivot = null;
|
||||
this.pivot = null;
|
||||
TickHandler.instance.removeNetwork( this );
|
||||
myStorage.remove();
|
||||
this.myStorage.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void add(GridNode gridNode)
|
||||
public void add( GridNode gridNode )
|
||||
{
|
||||
Class<? extends IGridHost> mClass = gridNode.getMachineClass();
|
||||
Set<IGridNode> nodes = Machines.get( mClass );
|
||||
|
||||
MachineSet nodes = this.machines.get( mClass );
|
||||
if ( nodes == null )
|
||||
{
|
||||
Machines.put( mClass, nodes = new MachineSet( mClass ) );
|
||||
bus.readClass( mClass, mClass );
|
||||
nodes = new MachineSet( mClass );
|
||||
this.machines.put( mClass, nodes );
|
||||
this.eventBus.readClass( mClass, mClass );
|
||||
}
|
||||
|
||||
// handle loading grid storages.
|
||||
@@ -127,130 +148,135 @@ public class Grid implements IGrid
|
||||
|
||||
if ( grid == null )
|
||||
{
|
||||
myStorage = gs;
|
||||
myStorage.setGrid( this );
|
||||
this.myStorage = gs;
|
||||
this.myStorage.setGrid( this );
|
||||
|
||||
for (IGridCache gc : caches.values())
|
||||
gc.onJoin( myStorage );
|
||||
for ( IGridCache gc : this.caches.values() )
|
||||
gc.onJoin( this.myStorage );
|
||||
}
|
||||
else if ( grid != this )
|
||||
{
|
||||
if ( myStorage == null )
|
||||
if ( this.myStorage == null )
|
||||
{
|
||||
myStorage = WorldSettings.getInstance().getNewGridStorage();
|
||||
myStorage.setGrid( this );
|
||||
this.myStorage = WorldSettings.getInstance().getNewGridStorage();
|
||||
this.myStorage.setGrid( this );
|
||||
}
|
||||
|
||||
GridStorage tmp = new GridStorage();
|
||||
if ( !gs.hasDivided( myStorage ) )
|
||||
IGridStorage tmp = new GridStorage();
|
||||
if ( !gs.hasDivided( this.myStorage ) )
|
||||
{
|
||||
gs.addDivided( myStorage );
|
||||
gs.addDivided( this.myStorage );
|
||||
|
||||
for (IGridCache gc : ((Grid) grid).caches.values())
|
||||
for ( IGridCache gc : ( ( Grid ) grid ).caches.values() )
|
||||
gc.onSplit( tmp );
|
||||
|
||||
for (IGridCache gc : caches.values())
|
||||
for ( IGridCache gc : this.caches.values() )
|
||||
gc.onJoin( tmp );
|
||||
}
|
||||
}
|
||||
}
|
||||
else if ( myStorage == null )
|
||||
else if ( this.myStorage == null )
|
||||
{
|
||||
myStorage = WorldSettings.getInstance().getNewGridStorage();
|
||||
myStorage.setGrid( this );
|
||||
this.myStorage = WorldSettings.getInstance().getNewGridStorage();
|
||||
this.myStorage.setGrid( this );
|
||||
}
|
||||
|
||||
// update grid node...
|
||||
gridNode.setGridStorage( myStorage );
|
||||
gridNode.setGridStorage( this.myStorage );
|
||||
|
||||
// track node.
|
||||
nodes.add( gridNode );
|
||||
|
||||
for (IGridCache c : caches.values())
|
||||
c.addNode( gridNode, gridNode.getMachine() );
|
||||
for ( IGridCache cache : this.caches.values() )
|
||||
{
|
||||
IGridHost machine = gridNode.getMachine();
|
||||
cache.addNode( gridNode, machine );
|
||||
}
|
||||
|
||||
gridNode.gridProxy.gridChanged();
|
||||
gridNode.getGridProxy().gridChanged();
|
||||
// postEventTo( gridNode, networkChanged );
|
||||
}
|
||||
|
||||
@Override
|
||||
public IReadOnlyCollection<IGridNode> getNodes()
|
||||
@SuppressWarnings( "unchecked" )
|
||||
public <C extends IGridCache> C getCache( Class<? extends IGridCache> iface )
|
||||
{
|
||||
return new NodeIterable( Machines );
|
||||
return ( C ) this.caches.get( iface ).myCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MENetworkEvent postEvent( MENetworkEvent ev )
|
||||
{
|
||||
return this.eventBus.postEvent( this, ev );
|
||||
}
|
||||
|
||||
@Override
|
||||
public MENetworkEvent postEventTo( IGridNode node, MENetworkEvent ev )
|
||||
{
|
||||
return this.eventBus.postEventTo( this, ( GridNode ) node, ev );
|
||||
}
|
||||
|
||||
@Override
|
||||
public IReadOnlyCollection<Class<? extends IGridHost>> getMachinesClasses()
|
||||
{
|
||||
return new ReadOnlyCollection<Class<? extends IGridHost>>( Machines.keySet() );
|
||||
Set<Class<? extends IGridHost>> machineKeys = this.machines.keySet();
|
||||
|
||||
return new ReadOnlyCollection<Class<? extends IGridHost>>( machineKeys );
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMachineSet getMachines(Class<? extends IGridHost> c)
|
||||
public IMachineSet getMachines( Class<? extends IGridHost> c )
|
||||
{
|
||||
MachineSet s = (MachineSet) Machines.get( c );
|
||||
MachineSet s = this.machines.get( c );
|
||||
if ( s == null )
|
||||
return new MachineSet( c );
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <C extends IGridCache> C getCache(Class<? extends IGridCache> iface)
|
||||
public IReadOnlyCollection<IGridNode> getNodes()
|
||||
{
|
||||
return (C) caches.get( iface ).myCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MENetworkEvent postEventTo(IGridNode node, MENetworkEvent ev)
|
||||
{
|
||||
return bus.postEventTo( this, (GridNode) node, ev );
|
||||
}
|
||||
|
||||
@Override
|
||||
public MENetworkEvent postEvent(MENetworkEvent ev)
|
||||
{
|
||||
return bus.postEvent( this, ev );
|
||||
}
|
||||
|
||||
public void requestSave()
|
||||
{
|
||||
myStorage.markDirty();
|
||||
WorldSettings.getInstance().save();
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
for (IGridCache gc : caches.values())
|
||||
{
|
||||
// are there any nodes left?
|
||||
if ( pivot != null )
|
||||
gc.onUpdateTick();
|
||||
}
|
||||
}
|
||||
|
||||
public Iterable<GridCacheWrapper> getCacheWrappers()
|
||||
{
|
||||
return caches.values();
|
||||
return new GridNodeCollection( this.machines );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return pivot == null;
|
||||
return this.pivot == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGridNode getPivot()
|
||||
{
|
||||
return this.pivot;
|
||||
}
|
||||
|
||||
public void setPivot( GridNode pivot )
|
||||
{
|
||||
this.pivot = pivot;
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
for ( IGridCache gc : this.caches.values() )
|
||||
{
|
||||
// are there any nodes left?
|
||||
if ( this.pivot != null )
|
||||
gc.onUpdateTick();
|
||||
}
|
||||
}
|
||||
|
||||
public void saveState()
|
||||
{
|
||||
for (IGridCache c : caches.values())
|
||||
for ( IGridCache c : this.caches.values() )
|
||||
{
|
||||
c.populateGridStorage( myStorage );
|
||||
c.populateGridStorage( this.myStorage );
|
||||
}
|
||||
}
|
||||
|
||||
public void setImportantFlag(int i, boolean publicHasPower)
|
||||
public void setImportantFlag( int i, boolean publicHasPower )
|
||||
{
|
||||
int flag = 1 << i;
|
||||
isImportant = (isImportant & ~flag) | (publicHasPower ? flag : 0);
|
||||
this.priority = ( this.priority & ~flag ) | ( publicHasPower ? flag : 0 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user