Delay Block Updates, and save the world!
This commit is contained in:
+45
-17
@@ -3,8 +3,10 @@ package appeng.hooks;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.event.world.ChunkEvent;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import appeng.api.networking.IGridNode;
|
||||
@@ -17,6 +19,7 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.Type;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent;
|
||||
|
||||
public class TickHandler
|
||||
{
|
||||
@@ -38,7 +41,8 @@ public class TickHandler
|
||||
|
||||
final public static TickHandler instance = new TickHandler();
|
||||
|
||||
final private Queue<Callable> callQueue = new LinkedList();
|
||||
final private WeakHashMap<World, Queue<Callable>> callQueue = new WeakHashMap<World, Queue<Callable>>();
|
||||
Queue<Callable> serverQueue = new LinkedList<Callable>();
|
||||
|
||||
final private HandlerRep server = new HandlerRep();
|
||||
final private HandlerRep client = new HandlerRep();
|
||||
@@ -50,9 +54,19 @@ public class TickHandler
|
||||
return client;
|
||||
}
|
||||
|
||||
public void addCallable(Callable c)
|
||||
public void addCallable(World w, Callable c)
|
||||
{
|
||||
callQueue.add( c );
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
||||
public void addInit(AEBaseTile tile)
|
||||
@@ -122,6 +136,7 @@ public class TickHandler
|
||||
if ( ev.type == Type.SERVER && ev.phase == Phase.END ) // for no there is no reason to care about this on the
|
||||
// client...
|
||||
{
|
||||
// ready tiles.
|
||||
HandlerRep repo = getRepo();
|
||||
while (!repo.tiles.isEmpty())
|
||||
{
|
||||
@@ -129,24 +144,37 @@ public class TickHandler
|
||||
bt.onReady();
|
||||
}
|
||||
|
||||
// tick networks.
|
||||
for (Grid g : getRepo().networks)
|
||||
{
|
||||
g.update();
|
||||
}
|
||||
|
||||
Callable c = null;
|
||||
while ((c = callQueue.poll()) != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
c.call();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AELog.error( e );
|
||||
}
|
||||
}
|
||||
// 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
|
||||
{
|
||||
c.call();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AELog.error( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user