2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
package appeng.hooks;
|
|
|
|
|
2015-03-26 11:07:26 +01:00
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
import java.util.Collection;
|
2014-07-21 05:45:08 +02:00
|
|
|
import java.util.HashMap;
|
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-08-07 08:47:42 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2014-03-02 09:35:11 +01:00
|
|
|
|
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;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-03-26 11:07:26 +01:00
|
|
|
import com.google.common.base.Stopwatch;
|
|
|
|
import com.google.common.collect.LinkedListMultimap;
|
|
|
|
import com.google.common.collect.Multimap;
|
|
|
|
|
2014-08-03 06:53:34 +02:00
|
|
|
import appeng.api.AEApi;
|
2014-03-02 09:35:11 +01:00
|
|
|
import appeng.api.networking.IGridNode;
|
2014-08-03 06:53:34 +02:00
|
|
|
import appeng.api.parts.CableRenderMode;
|
2014-07-21 05:45:08 +02:00
|
|
|
import appeng.api.util.AEColor;
|
2014-08-30 21:50:00 +02:00
|
|
|
import appeng.core.AEConfig;
|
2014-03-02 09:35:11 +01:00
|
|
|
import appeng.core.AELog;
|
2014-08-03 06:53:34 +02:00
|
|
|
import appeng.core.CommonHelper;
|
2014-07-21 05:45:08 +02:00
|
|
|
import appeng.core.sync.packets.PacketPaintedEntity;
|
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
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
public class TickHandler
|
|
|
|
{
|
|
|
|
|
2015-01-01 22:13:10 +01:00
|
|
|
public static final TickHandler INSTANCE = new TickHandler();
|
2014-09-29 09:54:34 +02:00
|
|
|
final Queue<Callable> serverQueue = new LinkedList<Callable>();
|
2015-04-03 08:54:31 +02:00
|
|
|
final Multimap<World, CraftingJob> craftingJobs = LinkedListMultimap.create();
|
2015-04-06 00:35:42 +02:00
|
|
|
private final WeakHashMap<World, Queue<Callable>> callQueue = new WeakHashMap<World, Queue<Callable>>();
|
|
|
|
private final HandlerRep server = new HandlerRep();
|
|
|
|
private final HandlerRep client = new HandlerRep();
|
|
|
|
private final HashMap<Integer, PlayerColor> cliPlayerColors = new HashMap<Integer, PlayerColor>();
|
|
|
|
private final HashMap<Integer, PlayerColor> srvPlayerColors = new HashMap<Integer, PlayerColor>();
|
2015-04-03 08:54:31 +02:00
|
|
|
CableRenderMode crm = CableRenderMode.Standard;
|
2014-07-21 05:45:08 +02:00
|
|
|
|
|
|
|
public HashMap<Integer, PlayerColor> getPlayerColors()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( Platform.isServer() )
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.srvPlayerColors;
|
|
|
|
return this.cliPlayerColors;
|
2014-07-21 05:45:08 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public void addCallable( World w, Callable c )
|
2014-07-21 05:45:08 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( w == null )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.serverQueue.add( c );
|
2014-07-08 06:54:28 +02:00
|
|
|
else
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
Queue<Callable> queue = this.callQueue.get( w );
|
2014-07-08 06:54:28 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( queue == null )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.callQueue.put( w, queue = new LinkedList<Callable>() );
|
2014-07-08 06:54:28 +02:00
|
|
|
|
|
|
|
queue.add( c );
|
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public void addInit( AEBaseTile tile )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
2014-12-29 15:13:47 +01:00
|
|
|
this.getRepo().tiles.add( tile );
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
HandlerRep getRepo()
|
|
|
|
{
|
|
|
|
if( Platform.isServer() )
|
|
|
|
return this.server;
|
|
|
|
return this.client;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addNetwork( Grid grid )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
2014-12-29 15:13:47 +01:00
|
|
|
this.getRepo().networks.add( grid );
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public void removeNetwork( Grid grid )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
2014-12-29 15:13:47 +01:00
|
|
|
this.getRepo().networks.remove( grid );
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Iterable<Grid> getGridList()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.getRepo().networks;
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.getRepo().clear();
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2015-04-03 08:54:31 +02:00
|
|
|
public void unloadWorld( WorldEvent.Unload ev )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +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
|
|
|
{
|
2014-09-28 22:20:14 +02:00
|
|
|
LinkedList<IGridNode> toDestroy = new LinkedList<IGridNode>();
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( Grid g : this.getRepo().networks )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridNode n : g.getNodes() )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( n.getWorld() == ev.world )
|
2014-03-02 09:35:11 +01:00
|
|
|
toDestroy.add( n );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridNode n : toDestroy )
|
2014-03-02 09:35:11 +01:00
|
|
|
n.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 04:15:45 +02:00
|
|
|
@SubscribeEvent
|
2015-04-03 08:54:31 +02:00
|
|
|
public void onChunkLoad( ChunkEvent.Load load )
|
2014-05-13 04:15:45 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( Object te : load.getChunk().chunkTileEntityMap.values() )
|
2014-05-13 04:15:45 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( te instanceof AEBaseTile )
|
2014-05-13 04:15:45 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
( (AEBaseTile) te ).onChunkLoad();
|
2014-05-13 04:15:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
@SubscribeEvent
|
2015-04-03 08:54:31 +02:00
|
|
|
public void onTick( TickEvent ev )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2014-07-07 04:04:58 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( ev.type == Type.CLIENT && ev.phase == Phase.START )
|
2014-07-07 04:04:58 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.tickColors( this.cliPlayerColors );
|
2015-04-03 08:54:31 +02:00
|
|
|
EntityFloatingItem.ageStatic = ( EntityFloatingItem.ageStatic + 1 ) % 60000;
|
2014-08-03 06:53:34 +02:00
|
|
|
CableRenderMode currentMode = AEApi.instance().partHelper().getCableRenderMode();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( currentMode != this.crm )
|
2014-08-03 06:53:34 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.crm = currentMode;
|
2014-08-03 06:53:34 +02:00
|
|
|
CommonHelper.proxy.triggerUpdates();
|
|
|
|
}
|
2014-07-07 04:04:58 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( ev.type == Type.WORLD && ev.phase == Phase.END )
|
2014-06-23 06:50:56 +02:00
|
|
|
{
|
|
|
|
WorldTickEvent wte = (WorldTickEvent) ev;
|
2015-04-03 08:54:31 +02:00
|
|
|
synchronized( this.craftingJobs )
|
2014-06-23 06:50:56 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
Collection<CraftingJob> jobSet = this.craftingJobs.get( wte.world );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !jobSet.isEmpty() )
|
2014-06-23 06:50:56 +02:00
|
|
|
{
|
2014-08-30 21:50:00 +02:00
|
|
|
int simTime = Math.max( 1, AEConfig.instance.craftingCalculationTimePerTick / jobSet.size() );
|
|
|
|
Iterator<CraftingJob> i = jobSet.iterator();
|
2015-04-03 08:54:31 +02:00
|
|
|
while( i.hasNext() )
|
2014-08-30 21:50:00 +02:00
|
|
|
{
|
|
|
|
CraftingJob cj = i.next();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !cj.simulateFor( simTime ) )
|
2014-08-30 21:50:00 +02:00
|
|
|
i.remove();
|
|
|
|
}
|
2014-06-23 06:50:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for no there is no reason to care about this on the client...
|
2015-04-03 08:54:31 +02:00
|
|
|
else if( ev.type == Type.SERVER && ev.phase == Phase.END )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.tickColors( this.srvPlayerColors );
|
2014-07-08 06:54:28 +02:00
|
|
|
// ready tiles.
|
2014-12-29 15:13:47 +01:00
|
|
|
HandlerRep repo = this.getRepo();
|
2015-04-03 08:54:31 +02:00
|
|
|
while( !repo.tiles.isEmpty() )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
|
|
|
AEBaseTile bt = repo.tiles.poll();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !bt.isInvalid() )
|
2014-07-11 00:25:04 +02:00
|
|
|
bt.onReady();
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2014-07-08 06:54:28 +02:00
|
|
|
// tick networks.
|
2015-04-03 08:54:31 +02:00
|
|
|
for( Grid g : this.getRepo().networks )
|
2014-03-02 09:35:11 +01:00
|
|
|
g.update();
|
|
|
|
|
2014-07-08 06:54:28 +02:00
|
|
|
// cross world queue.
|
2014-12-29 15:13:47 +01:00
|
|
|
this.processQueue( this.serverQueue );
|
2014-07-08 06:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// world synced queue(s)
|
2015-04-03 08:54:31 +02:00
|
|
|
if( ev.type == Type.WORLD && ev.phase == Phase.START )
|
2014-07-08 06:54:28 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.processQueue( this.callQueue.get( ( (WorldTickEvent) ev ).world ) );
|
2014-07-08 06:54:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
private void tickColors( HashMap<Integer, PlayerColor> playerSet )
|
2014-07-08 06:54:28 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
Iterator<PlayerColor> i = playerSet.values().iterator();
|
|
|
|
while( i.hasNext() )
|
|
|
|
{
|
|
|
|
PlayerColor pc = i.next();
|
|
|
|
if( pc.ticksLeft <= 0 )
|
|
|
|
i.remove();
|
|
|
|
pc.ticksLeft--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processQueue( Queue<Callable> queue )
|
|
|
|
{
|
|
|
|
if( queue == null )
|
2014-07-08 06:54:28 +02:00
|
|
|
return;
|
|
|
|
|
2014-08-07 08:47:42 +02:00
|
|
|
Stopwatch sw = Stopwatch.createStarted();
|
|
|
|
|
2014-07-08 06:54:28 +02:00
|
|
|
Callable c = null;
|
2015-04-03 08:54:31 +02:00
|
|
|
while( ( c = queue.poll() ) != null )
|
2014-07-08 06:54:28 +02:00
|
|
|
{
|
|
|
|
try
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2014-07-08 06:54:28 +02:00
|
|
|
c.call();
|
2014-08-07 08:47:42 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( sw.elapsed( TimeUnit.MILLISECONDS ) > 50 )
|
2014-08-07 08:47:42 +02:00
|
|
|
break;
|
2014-07-08 06:54:28 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
catch( Exception e )
|
2014-07-08 06:54:28 +02:00
|
|
|
{
|
|
|
|
AELog.error( e );
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-07 08:47:42 +02:00
|
|
|
|
|
|
|
// long time = sw.elapsed( TimeUnit.MILLISECONDS );
|
|
|
|
// if ( time > 0 )
|
|
|
|
// AELog.info( "processQueue Time: " + time + "ms" );
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public void registerCraftingSimulation( World world, CraftingJob craftingJob )
|
2014-06-23 06:50:56 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
synchronized( this.craftingJobs )
|
2014-06-23 06:50:56 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.craftingJobs.put( world, craftingJob );
|
2014-06-23 06:50:56 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-09 02:36:20 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
static class HandlerRep
|
|
|
|
{
|
|
|
|
|
|
|
|
public Queue<AEBaseTile> tiles = new LinkedList<AEBaseTile>();
|
|
|
|
|
|
|
|
public Collection<Grid> networks = new NetworkList();
|
|
|
|
|
|
|
|
public void clear()
|
|
|
|
{
|
|
|
|
this.tiles = new LinkedList<AEBaseTile>();
|
|
|
|
this.networks = new NetworkList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-06 00:35:42 +02:00
|
|
|
public static class PlayerColor
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
public final AEColor myColor;
|
|
|
|
protected final int myEntity;
|
|
|
|
protected int ticksLeft;
|
|
|
|
|
|
|
|
public PlayerColor( int id, AEColor col, int ticks )
|
|
|
|
{
|
|
|
|
this.myEntity = id;
|
|
|
|
this.myColor = col;
|
|
|
|
this.ticksLeft = ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PacketPaintedEntity getPacket()
|
|
|
|
{
|
|
|
|
return new PacketPaintedEntity( this.myEntity, this.myColor, this.ticksLeft );
|
|
|
|
}
|
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|