Applied-Energistics-2-tiler.../src/main/java/appeng/hooks/TickHandler.java

311 lines
7.8 KiB
Java
Raw Normal View History

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>.
*/
package appeng.hooks;
2015-03-26 11:07:26 +01:00
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
2014-08-07 08:47:42 +02:00
import java.util.concurrent.TimeUnit;
import net.minecraft.world.World;
2014-05-13 04:15:45 +02:00
import net.minecraftforge.event.world.ChunkEvent;
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;
import appeng.api.AEApi;
import appeng.api.networking.IGridNode;
import appeng.api.parts.CableRenderMode;
import appeng.api.util.AEColor;
import appeng.core.AEConfig;
import appeng.core.AELog;
import appeng.core.CommonHelper;
import appeng.core.sync.packets.PacketPaintedEntity;
import appeng.crafting.CraftingJob;
2014-07-07 04:04:58 +02:00
import appeng.entity.EntityFloatingItem;
import appeng.me.Grid;
import appeng.me.NetworkList;
import appeng.tile.AEBaseTile;
import appeng.util.Platform;
public class TickHandler
{
2015-01-01 22:13:10 +01:00
public static final TickHandler INSTANCE = new TickHandler();
final Queue<Callable> serverQueue = new LinkedList<Callable>();
final Multimap<World, CraftingJob> craftingJobs = LinkedListMultimap.create();
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>();
CableRenderMode crm = CableRenderMode.Standard;
public HashMap<Integer, PlayerColor> getPlayerColors()
{
if( Platform.isServer() )
2014-12-29 15:13:47 +01:00
return this.srvPlayerColors;
return this.cliPlayerColors;
}
public void addCallable( World w, Callable c )
{
if( w == null )
2014-12-29 15:13:47 +01:00
this.serverQueue.add( c );
else
{
2014-12-29 15:13:47 +01:00
Queue<Callable> queue = this.callQueue.get( w );
if( queue == null )
2014-12-29 15:13:47 +01:00
this.callQueue.put( w, queue = new LinkedList<Callable>() );
queue.add( c );
}
}
public void addInit( AEBaseTile tile )
{
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 );
}
HandlerRep getRepo()
{
if( Platform.isServer() )
return this.server;
return this.client;
}
public void addNetwork( Grid grid )
{
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 );
}
public void removeNetwork( Grid grid )
{
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 );
}
public Iterable<Grid> getGridList()
{
2014-12-29 15:13:47 +01:00
return this.getRepo().networks;
}
public void shutdown()
{
2014-12-29 15:13:47 +01:00
this.getRepo().clear();
}
@SubscribeEvent
public void unloadWorld( WorldEvent.Unload ev )
{
if( Platform.isServer() ) // for no there is no reason to care about this on the client...
{
2014-09-28 22:20:14 +02:00
LinkedList<IGridNode> toDestroy = new LinkedList<IGridNode>();
for( Grid g : this.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 )
2014-05-13 04:15:45 +02:00
{
for( Object te : load.getChunk().chunkTileEntityMap.values() )
2014-05-13 04:15:45 +02:00
{
if( te instanceof AEBaseTile )
2014-05-13 04:15:45 +02:00
{
( (AEBaseTile) te ).onChunkLoad();
2014-05-13 04:15:45 +02:00
}
}
}
@SubscribeEvent
public void onTick( TickEvent ev )
{
2014-07-07 04:04:58 +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 );
EntityFloatingItem.ageStatic = ( EntityFloatingItem.ageStatic + 1 ) % 60000;
CableRenderMode currentMode = AEApi.instance().partHelper().getCableRenderMode();
if( currentMode != this.crm )
{
2014-12-29 15:13:47 +01:00
this.crm = currentMode;
CommonHelper.proxy.triggerUpdates();
}
2014-07-07 04:04:58 +02:00
}
if( ev.type == Type.WORLD && ev.phase == Phase.END )
{
WorldTickEvent wte = (WorldTickEvent) ev;
synchronized( this.craftingJobs )
{
2014-12-29 15:13:47 +01:00
Collection<CraftingJob> jobSet = this.craftingJobs.get( wte.world );
if( !jobSet.isEmpty() )
{
int simTime = Math.max( 1, AEConfig.instance.craftingCalculationTimePerTick / jobSet.size() );
Iterator<CraftingJob> i = jobSet.iterator();
while( i.hasNext() )
{
CraftingJob cj = i.next();
if( !cj.simulateFor( simTime ) )
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-12-29 15:13:47 +01:00
this.tickColors( this.srvPlayerColors );
// ready tiles.
2014-12-29 15:13:47 +01:00
HandlerRep repo = this.getRepo();
while( !repo.tiles.isEmpty() )
{
AEBaseTile bt = repo.tiles.poll();
if( !bt.isInvalid() )
bt.onReady();
}
// tick networks.
for( Grid g : this.getRepo().networks )
g.update();
// cross world queue.
2014-12-29 15:13:47 +01:00
this.processQueue( this.serverQueue );
}
// world synced queue(s)
if( ev.type == Type.WORLD && ev.phase == Phase.START )
{
this.processQueue( this.callQueue.get( ( (WorldTickEvent) ev ).world ) );
}
}
private void tickColors( HashMap<Integer, PlayerColor> playerSet )
{
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 )
return;
2014-08-07 08:47:42 +02:00
Stopwatch sw = Stopwatch.createStarted();
Callable c = null;
while( ( c = queue.poll() ) != null )
{
try
{
c.call();
2014-08-07 08:47:42 +02:00
if( sw.elapsed( TimeUnit.MILLISECONDS ) > 50 )
2014-08-07 08:47:42 +02:00
break;
}
catch( Exception e )
{
AELog.error( e );
}
}
2014-08-07 08:47:42 +02:00
// long time = sw.elapsed( TimeUnit.MILLISECONDS );
// if ( time > 0 )
// AELog.info( "processQueue Time: " + time + "ms" );
}
public void registerCraftingSimulation( World world, CraftingJob craftingJob )
{
synchronized( this.craftingJobs )
{
2014-12-29 15:13:47 +01:00
this.craftingJobs.put( world, craftingJob );
}
}
2014-07-09 02:36:20 +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();
}
}
public static class PlayerColor
{
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 );
}
}
}