2013-12-27 23:59:59 +01:00
|
|
|
package appeng.helpers;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.Queue;
|
2014-01-22 05:52:33 +01:00
|
|
|
import java.util.concurrent.Callable;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
import net.minecraftforge.event.world.WorldEvent;
|
|
|
|
import appeng.api.networking.IGridNode;
|
2014-02-07 21:37:22 +01:00
|
|
|
import appeng.core.AELog;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.Grid;
|
|
|
|
import appeng.tile.AEBaseTile;
|
|
|
|
import appeng.util.Platform;
|
2014-02-09 02:34:52 +01:00
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent;
|
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent.Type;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
public class TickHandler
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
class HandlerRep
|
|
|
|
{
|
|
|
|
|
|
|
|
public Queue<AEBaseTile> tiles = new LinkedList();
|
|
|
|
|
2014-01-02 06:51:41 +01:00
|
|
|
public LinkedList<Grid> networks = new LinkedList();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
public void clear()
|
|
|
|
{
|
|
|
|
tiles = new LinkedList();
|
|
|
|
networks = new LinkedList();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
final public static TickHandler instance = new TickHandler();
|
|
|
|
|
2014-01-22 05:52:33 +01:00
|
|
|
final private Queue<Callable> callQueue = new LinkedList();
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
final private HandlerRep server = new HandlerRep();
|
|
|
|
final private HandlerRep client = new HandlerRep();
|
|
|
|
|
|
|
|
HandlerRep getRepo()
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
return server;
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2014-01-22 05:52:33 +01:00
|
|
|
public void addCallable(Callable c)
|
|
|
|
{
|
|
|
|
callQueue.add( c );
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public void addInit(AEBaseTile tile)
|
|
|
|
{
|
|
|
|
getRepo().tiles.add( tile );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addNetwork(Grid grid)
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
getRepo().networks.add( grid );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeNetwork(Grid grid)
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
getRepo().networks.remove( grid );
|
|
|
|
}
|
|
|
|
|
|
|
|
public Iterable<Grid> getGridList()
|
|
|
|
{
|
|
|
|
return getRepo().networks;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown()
|
|
|
|
{
|
|
|
|
getRepo().clear();
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
@SubscribeEvent
|
2013-12-27 23:59:59 +01:00
|
|
|
public void unloadWorld(WorldEvent.Unload ev)
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
{
|
2014-01-28 23:30:14 +01:00
|
|
|
LinkedList<IGridNode> toDestroy = new LinkedList();
|
|
|
|
|
|
|
|
for (Grid g : getRepo().networks)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
for (IGridNode n : g.getNodes())
|
|
|
|
{
|
|
|
|
if ( n.getWorld() == ev.world )
|
2014-01-28 23:30:14 +01:00
|
|
|
toDestroy.add( n );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-28 23:30:14 +01:00
|
|
|
|
|
|
|
for (IGridNode n : toDestroy)
|
|
|
|
n.destroy();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
@SubscribeEvent
|
|
|
|
public void onTick(TickEvent ev)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
if ( ev.type == Type.SERVER )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
HandlerRep repo = getRepo();
|
|
|
|
while (!repo.tiles.isEmpty())
|
|
|
|
{
|
|
|
|
AEBaseTile bt = repo.tiles.poll();
|
|
|
|
bt.onReady();
|
|
|
|
}
|
2014-01-22 05:52:33 +01:00
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
for (Grid g : getRepo().networks)
|
2014-01-22 05:52:33 +01:00
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
g.update();
|
2014-01-22 05:52:33 +01:00
|
|
|
}
|
2014-02-09 02:34:52 +01:00
|
|
|
|
|
|
|
Callable c = null;
|
|
|
|
while ((c = callQueue.poll()) != null)
|
2014-01-22 05:52:33 +01:00
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
c.call();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
2014-01-22 05:52:33 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|