2014-11-27 17:06:48 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
2015-05-16 20:48:32 +02:00
|
|
|
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
2014-11-27 17:06:48 +01:00
|
|
|
*
|
|
|
|
* 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.core;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2015-08-10 13:34:31 +02:00
|
|
|
import java.util.Locale;
|
2014-11-27 17:06:48 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import cpw.mods.fml.common.event.FMLInterModComms;
|
|
|
|
|
|
|
|
import appeng.api.config.TunnelType;
|
|
|
|
import appeng.core.api.IIMCProcessor;
|
|
|
|
import appeng.core.api.imc.IMCBlackListSpatial;
|
|
|
|
import appeng.core.api.imc.IMCGrinder;
|
|
|
|
import appeng.core.api.imc.IMCMatterCannon;
|
|
|
|
import appeng.core.api.imc.IMCP2PAttunement;
|
|
|
|
import appeng.core.api.imc.IMCSpatial;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the delegation of the corresponding IMC messages to the suitable IMC processors
|
2015-08-10 13:34:31 +02:00
|
|
|
*
|
|
|
|
* @author thatsIch
|
|
|
|
* @version rv3 - 10.08.2015
|
|
|
|
* @since rv1
|
2014-11-27 17:06:48 +01:00
|
|
|
*/
|
|
|
|
public class IMCHandler
|
|
|
|
{
|
2015-05-16 20:48:32 +02:00
|
|
|
private static final int INITIAL_PROCESSORS_CAPACITY = 20;
|
|
|
|
|
2014-11-27 17:06:48 +01:00
|
|
|
/**
|
|
|
|
* Contains the processors,
|
|
|
|
*
|
2015-08-10 13:34:31 +02:00
|
|
|
* is mutable, but write access only by the constructor
|
2014-11-27 17:06:48 +01:00
|
|
|
*/
|
|
|
|
private final Map<String, IIMCProcessor> processors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the processors
|
|
|
|
*/
|
|
|
|
public IMCHandler()
|
|
|
|
{
|
2015-05-16 20:48:32 +02:00
|
|
|
this.processors = new HashMap<String, IIMCProcessor>( INITIAL_PROCESSORS_CAPACITY );
|
2014-11-27 17:06:48 +01:00
|
|
|
|
|
|
|
this.processors.put( "blacklist-block-spatial", new IMCBlackListSpatial() );
|
|
|
|
this.processors.put( "whitelist-spatial", new IMCSpatial() );
|
|
|
|
this.processors.put( "add-grindable", new IMCGrinder() );
|
|
|
|
this.processors.put( "add-mattercannon-ammo", new IMCMatterCannon() );
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final TunnelType type : TunnelType.values() )
|
2014-11-27 17:06:48 +01:00
|
|
|
{
|
2015-08-10 13:34:31 +02:00
|
|
|
this.processors.put( "add-p2p-attunement-" + type.name().replace( '_', '-' ).toLowerCase( Locale.ENGLISH ), new IMCP2PAttunement() );
|
2014-11-27 17:06:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-10 13:34:31 +02:00
|
|
|
* Tries to find every message matching the internal IMC keys. When found the corresponding handler will process the attached message.
|
2014-11-27 17:06:48 +01:00
|
|
|
*
|
|
|
|
* @param event Event carrying the identifier and message for the handlers
|
|
|
|
*/
|
2015-09-25 23:10:56 +02:00
|
|
|
public void handleIMCEvent( final FMLInterModComms.IMCEvent event )
|
2014-11-27 17:06:48 +01:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final FMLInterModComms.IMCMessage message : event.getMessages() )
|
2014-11-27 17:06:48 +01:00
|
|
|
{
|
|
|
|
final String key = message.key;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-08-10 13:34:31 +02:00
|
|
|
final IIMCProcessor handler = this.processors.get( key );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( handler != null )
|
2014-11-27 17:06:48 +01:00
|
|
|
{
|
|
|
|
handler.process( message );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-23 10:14:35 +01:00
|
|
|
throw new IllegalStateException( "Invalid IMC Called: " + key );
|
2014-11-27 17:06:48 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final Exception t )
|
2014-11-27 17:06:48 +01:00
|
|
|
{
|
|
|
|
AELog.warning( "Problem detected when processing IMC " + key + " from " + message.getSender() );
|
|
|
|
AELog.error( t );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|