2014-03-02 09:35:11 +01:00
|
|
|
package appeng.hooks;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
2014-06-23 06:50:56 +02:00
|
|
|
import java.util.Iterator;
|
2014-03-02 09:35:11 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.Queue;
|
2014-07-08 06:54:28 +02:00
|
|
|
import java.util.WeakHashMap;
|
2014-03-02 09:35:11 +01:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
import net.minecraft.world.World;
|
2014-05-13 04:15:45 +02:00
|
|
|
import net.minecraftforge.event.world.ChunkEvent;
|
2014-03-02 09:35:11 +01:00
|
|
|
import net.minecraftforge.event.world.WorldEvent;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.core.AELog;
|
2014-06-23 06:50:56 +02:00
|
|
|
import appeng.crafting.CraftingJob;
|
2014-07-07 04:04:58 +02:00
|
|
|
import appeng.entity.EntityFloatingItem;
|
2014-03-02 09:35:11 +01:00
|
|
|
import appeng.me.Grid;
|
|
|
|
import appeng.me.NetworkList;
|
|
|
|
import appeng.tile.AEBaseTile;
|
|
|
|
import appeng.util.Platform;
|
2014-06-23 06:50:56 +02:00
|
|
|
|
|
|
|
import com.google.common.collect.LinkedListMultimap;
|
|
|
|
import com.google.common.collect.Multimap;
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent;
|
2014-04-18 02:44:51 +02:00
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
|
2014-03-02 09:35:11 +01:00
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent.Type;
|
2014-06-23 06:50:56 +02:00
|
|
|
import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent;
|
2014-03-02 09:35:11 +01:00
|
|
|
|
|
|
|
public class TickHandler
|
|
|
|
{
|
|
|
|
|
|
|
|
class HandlerRep
|
|
|
|
{
|
|
|
|
|
|
|
|
public Queue<AEBaseTile> tiles = new LinkedList();
|
|
|
|
|
|
|
|
public Collection<Grid> networks = new NetworkList();
|
|
|
|
|
|
|
|
public void clear()
|
|
|
|
{
|
|
|
|
tiles = new LinkedList();
|
|
|
|
networks = new NetworkList();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
final public static TickHandler instance = new TickHandler();
|
|
|
|
|
2014-07-08 06:54:28 +02:00
|
|
|
final private WeakHashMap<World, Queue<Callable>> callQueue = new WeakHashMap<World, Queue<Callable>>();
|
|
|
|
Queue<Callable> serverQueue = new LinkedList<Callable>();
|
2014-03-02 09:35:11 +01:00
|
|
|
|
|
|
|
final private HandlerRep server = new HandlerRep();
|
|
|
|
final private HandlerRep client = new HandlerRep();
|
|
|
|
|
|
|
|
HandlerRep getRepo()
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
return server;
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2014-07-08 06:54:28 +02:00
|
|
|
public void addCallable(World w, Callable c)
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2014-07-08 06:54:28 +02:00
|
|
|
if ( w == null )
|
|
|
|
serverQueue.add( c );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Queue<Callable> queue = callQueue.get( w );
|
|
|
|
|
|
|
|
if ( queue == null )
|
|
|
|
callQueue.put( w, queue = new LinkedList<Callable>() );
|
|
|
|
|
|
|
|
queue.add( c );
|
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addInit(AEBaseTile tile)
|
|
|
|
{
|
2014-05-08 21:41:39 +02:00
|
|
|
if ( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
|
|
|
getRepo().tiles.add( tile );
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addNetwork(Grid grid)
|
|
|
|
{
|
2014-05-08 21:41:39 +02:00
|
|
|
if ( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
2014-03-02 09:35:11 +01:00
|
|
|
getRepo().networks.add( grid );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeNetwork(Grid grid)
|
|
|
|
{
|
2014-05-08 21:41:39 +02:00
|
|
|
if ( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
2014-03-02 09:35:11 +01:00
|
|
|
getRepo().networks.remove( grid );
|
|
|
|
}
|
|
|
|
|
|
|
|
public Iterable<Grid> getGridList()
|
|
|
|
{
|
|
|
|
return getRepo().networks;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown()
|
|
|
|
{
|
|
|
|
getRepo().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void unloadWorld(WorldEvent.Unload ev)
|
|
|
|
{
|
2014-05-08 21:41:39 +02:00
|
|
|
if ( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
|
|
|
LinkedList<IGridNode> toDestroy = new LinkedList();
|
|
|
|
|
|
|
|
for (Grid g : getRepo().networks)
|
|
|
|
{
|
|
|
|
for (IGridNode n : g.getNodes())
|
|
|
|
{
|
|
|
|
if ( n.getWorld() == ev.world )
|
|
|
|
toDestroy.add( n );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (IGridNode n : toDestroy)
|
|
|
|
n.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 04:15:45 +02:00
|
|
|
@SubscribeEvent
|
|
|
|
public void onChunkLoad(ChunkEvent.Load load)
|
|
|
|
{
|
|
|
|
for (Object te : load.getChunk().chunkTileEntityMap.values())
|
|
|
|
{
|
|
|
|
if ( te instanceof AEBaseTile )
|
|
|
|
{
|
|
|
|
((AEBaseTile) te).onChunkLoad();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
@SubscribeEvent
|
|
|
|
public void onTick(TickEvent ev)
|
|
|
|
{
|
2014-07-07 04:04:58 +02:00
|
|
|
|
|
|
|
if ( ev.type == Type.CLIENT && ev.phase == Phase.START )
|
|
|
|
{
|
|
|
|
EntityFloatingItem.ageStatic = (EntityFloatingItem.ageStatic + 1) % 60000;
|
|
|
|
}
|
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
// rwar!
|
|
|
|
if ( ev.type == Type.WORLD && ev.phase == Phase.END )
|
|
|
|
{
|
|
|
|
WorldTickEvent wte = (WorldTickEvent) ev;
|
|
|
|
synchronized (craftingJobs)
|
|
|
|
{
|
|
|
|
Iterator<CraftingJob> i = craftingJobs.get( wte.world ).iterator();
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
CraftingJob cj = i.next();
|
|
|
|
if ( !cj.simulateFor( 5 ) )
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for no there is no reason to care about this on the client...
|
|
|
|
else if ( ev.type == Type.SERVER && ev.phase == Phase.END )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2014-07-08 06:54:28 +02:00
|
|
|
// ready tiles.
|
2014-03-02 09:35:11 +01:00
|
|
|
HandlerRep repo = getRepo();
|
|
|
|
while (!repo.tiles.isEmpty())
|
|
|
|
{
|
|
|
|
AEBaseTile bt = repo.tiles.poll();
|
2014-07-11 00:25:04 +02:00
|
|
|
if (! bt.isInvalid() )
|
|
|
|
bt.onReady();
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2014-07-08 06:54:28 +02:00
|
|
|
// tick networks.
|
2014-03-02 09:35:11 +01:00
|
|
|
for (Grid g : getRepo().networks)
|
|
|
|
g.update();
|
|
|
|
|
2014-07-08 06:54:28 +02:00
|
|
|
// cross world queue.
|
|
|
|
processQueue( serverQueue );
|
|
|
|
}
|
|
|
|
|
|
|
|
// world synced queue(s)
|
|
|
|
if ( ev.type == Type.WORLD && ev.phase == Phase.START )
|
|
|
|
{
|
|
|
|
processQueue( callQueue.get( ((WorldTickEvent) ev).world ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processQueue(Queue<Callable> queue)
|
|
|
|
{
|
|
|
|
if ( queue == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
Callable c = null;
|
|
|
|
while ((c = queue.poll()) != null)
|
|
|
|
{
|
|
|
|
try
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2014-07-08 06:54:28 +02:00
|
|
|
c.call();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
Multimap<World, CraftingJob> craftingJobs = LinkedListMultimap.create();
|
|
|
|
|
|
|
|
public void registerCraftingSimulation(World world, CraftingJob craftingJob)
|
|
|
|
{
|
|
|
|
synchronized (craftingJobs)
|
|
|
|
{
|
|
|
|
craftingJobs.put( world, craftingJob );
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 02:36:20 +02:00
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|