Merge pull request #500 from thatsIch/enhancement-imchandler

Enhancement: put IMC handling into separate class
This commit is contained in:
thatsIch 2014-11-29 18:04:33 +01:00
commit 0fad0b9ff2
8 changed files with 135 additions and 80 deletions

View file

@ -20,18 +20,14 @@ package appeng.core;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.google.common.base.Stopwatch;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLInterModComms; import cpw.mods.fml.common.event.FMLInterModComms;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerAboutToStartEvent; import cpw.mods.fml.common.event.FMLServerAboutToStartEvent;
@ -39,13 +35,8 @@ import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.event.FMLServerStoppingEvent; import cpw.mods.fml.common.event.FMLServerStoppingEvent;
import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.NetworkRegistry;
import appeng.api.config.TunnelType; import com.google.common.base.Stopwatch;
import appeng.core.api.IIMCHandler;
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;
import appeng.core.crash.CrashEnhancement; import appeng.core.crash.CrashEnhancement;
import appeng.core.crash.CrashInfo; import appeng.core.crash.CrashInfo;
import appeng.core.features.AEFeature; import appeng.core.features.AEFeature;
@ -62,23 +53,9 @@ import appeng.util.Platform;
@Mod( modid = AppEng.modid, acceptedMinecraftVersions = "[1.7.10]", name = AppEng.name, version = AEConfig.VERSION, dependencies = AppEng.dependencies, guiFactory = "appeng.client.gui.config.AEConfigGuiFactory" ) @Mod( modid = AppEng.modid, acceptedMinecraftVersions = "[1.7.10]", name = AppEng.name, version = AEConfig.VERSION, dependencies = AppEng.dependencies, guiFactory = "appeng.client.gui.config.AEConfigGuiFactory" )
public class AppEng public class AppEng
{ {
private String configPath;
public String getConfigPath()
{
return configPath;
}
public final static String modid = "appliedenergistics2"; public final static String modid = "appliedenergistics2";
public final static String name = "Applied Energistics 2"; public final static String name = "Applied Energistics 2";
final HashMap<String, IIMCHandler> IMCHandlers = new HashMap<String, IIMCHandler>();
public static AppEng instance;
public final static String dependencies = public final static String dependencies =
// a few mods, AE should load after, probably. // a few mods, AE should load after, probably.
// required-after:AppliedEnergistics2API|all; // required-after:AppliedEnergistics2API|all;
// "after:gregtech_addon;after:Mekanism;after:IC2;after:ThermalExpansion;after:BuildCraft|Core;" + // "after:gregtech_addon;after:Mekanism;after:IC2;after:ThermalExpansion;after:BuildCraft|Core;" +
@ -89,24 +66,24 @@ public class AppEng
+ net.minecraftforge.common.ForgeVersion.minorVersion + "." // minorVersion + net.minecraftforge.common.ForgeVersion.minorVersion + "." // minorVersion
+ net.minecraftforge.common.ForgeVersion.revisionVersion + "." // revisionVersion + net.minecraftforge.common.ForgeVersion.revisionVersion + "." // revisionVersion
+ net.minecraftforge.common.ForgeVersion.buildVersion + ",)"; // buildVersion + net.minecraftforge.common.ForgeVersion.buildVersion + ",)"; // buildVersion
public static AppEng instance;
private final IMCHandler imcHandler;
private String configPath;
public AppEng() public AppEng()
{ {
instance = this; instance = this;
IMCHandlers.put( "blacklist-block-spatial", new IMCBlackListSpatial() ); this.imcHandler = new IMCHandler();
IMCHandlers.put( "whitelist-spatial", new IMCSpatial() );
IMCHandlers.put( "add-grindable", new IMCGrinder() );
IMCHandlers.put( "add-mattercannon-ammo", new IMCMatterCannon() );
for ( TunnelType type : TunnelType.values() )
{
IMCHandlers.put( "add-p2p-attunement-" + type.name().replace( '_', '-' ).toLowerCase(), new IMCP2PAttunement() );
}
FMLCommonHandler.instance().registerCrashCallable( new CrashEnhancement( CrashInfo.MOD_VERSION ) ); FMLCommonHandler.instance().registerCrashCallable( new CrashEnhancement( CrashInfo.MOD_VERSION ) );
} }
public String getConfigPath()
{
return configPath;
}
public boolean isIntegrationEnabled( IntegrationType Name ) public boolean isIntegrationEnabled( IntegrationType Name )
{ {
return IntegrationRegistry.INSTANCE.isEnabled( Name ); return IntegrationRegistry.INSTANCE.isEnabled( Name );
@ -117,13 +94,6 @@ public class AppEng
return IntegrationRegistry.INSTANCE.getInstance( Name ); return IntegrationRegistry.INSTANCE.getInstance( Name );
} }
private void startService( String serviceName, Thread thread )
{
thread.setName( serviceName );
thread.setPriority( Thread.MIN_PRIORITY );
thread.start();
}
@EventHandler @EventHandler
void PreInit( FMLPreInitializationEvent event ) void PreInit( FMLPreInitializationEvent event )
{ {
@ -158,6 +128,13 @@ public class AppEng
AELog.info( "PreInit ( end " + star.elapsed( TimeUnit.MILLISECONDS ) + "ms )" ); AELog.info( "PreInit ( end " + star.elapsed( TimeUnit.MILLISECONDS ) + "ms )" );
} }
private void startService( String serviceName, Thread thread )
{
thread.setName( serviceName );
thread.setPriority( Thread.MIN_PRIORITY );
thread.start();
}
@EventHandler @EventHandler
void Init( FMLInitializationEvent event ) void Init( FMLInitializationEvent event )
{ {
@ -190,24 +167,9 @@ public class AppEng
} }
@EventHandler @EventHandler
public void processIMC( FMLInterModComms.IMCEvent event ) public void handleIMCEvent( FMLInterModComms.IMCEvent event )
{ {
for ( IMCMessage m : event.getMessages() ) this.imcHandler.handleIMCEvent( event );
{
try
{
IIMCHandler handler = IMCHandlers.get( m.key );
if ( handler != null )
handler.post( m );
else
throw new RuntimeException( "Invalid IMC Called: " + m.key );
}
catch ( Throwable t )
{
AELog.warning( "Problem detected when processing IMC " + m.key + " from " + m.getSender() );
AELog.error( t );
}
}
} }
@EventHandler @EventHandler
@ -228,5 +190,4 @@ public class AppEng
{ {
evt.registerServerCommand( new AECommand( evt.getServer() ) ); evt.registerServerCommand( new AECommand( evt.getServer() ) );
} }
} }

View file

@ -0,0 +1,98 @@
/*
* 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.core;
import java.util.HashMap;
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
*/
public class IMCHandler
{
/**
* Contains the processors,
*
* is mutable,
* but write access only by the constructor
*/
private final Map<String, IIMCProcessor> processors;
/**
* Initializes the processors
*/
public IMCHandler()
{
this.processors = new HashMap<String, IIMCProcessor>();
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() );
for ( TunnelType type : TunnelType.values() )
{
this.processors.put( "add-p2p-attunement-" + type.name().replace( '_', '-' ).toLowerCase(), new IMCP2PAttunement() );
}
}
/**
* Tries to find every message matching the internal IMC keys.
* When found the corresponding handler will process the attached message.
*
* @param event Event carrying the identifier and message for the handlers
*/
public void handleIMCEvent( FMLInterModComms.IMCEvent event )
{
for ( FMLInterModComms.IMCMessage message : event.getMessages() )
{
final String key = message.key;
try
{
IIMCProcessor handler = this.processors.get( key );
if ( handler != null )
{
handler.process( message );
}
else
{
throw new RuntimeException( "Invalid IMC Called: " + key );
}
}
catch ( Throwable t )
{
AELog.warning( "Problem detected when processing IMC " + key + " from " + message.getSender() );
AELog.error( t );
}
}
}
}

View file

@ -20,9 +20,7 @@ package appeng.core.api;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
public interface IIMCHandler public interface IIMCProcessor
{ {
void process( IMCMessage m );
void post(IMCMessage m);
} }

View file

@ -22,14 +22,14 @@ import net.minecraft.block.Block;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.core.AELog; import appeng.core.AELog;
import appeng.core.api.IIMCHandler; import appeng.core.api.IIMCProcessor;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
public class IMCBlackListSpatial implements IIMCHandler public class IMCBlackListSpatial implements IIMCProcessor
{ {
@Override @Override
public void post(IMCMessage m) public void process( IMCMessage m )
{ {
ItemStack is = m.getItemStackValue(); ItemStack is = m.getItemStackValue();

View file

@ -54,14 +54,13 @@ package appeng.core.api.imc;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.core.api.IIMCHandler; import appeng.core.api.IIMCProcessor;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
public class IMCGrinder implements IIMCHandler public class IMCGrinder implements IIMCProcessor
{ {
@Override @Override
public void post(IMCMessage m) public void process( IMCMessage m )
{ {
NBTTagCompound msg = m.getNBTValue(); NBTTagCompound msg = m.getNBTValue();
NBTTagCompound inTag = (NBTTagCompound) msg.getTag( "in" ); NBTTagCompound inTag = (NBTTagCompound) msg.getTag( "in" );
@ -93,5 +92,4 @@ public class IMCGrinder implements IIMCHandler
else else
AEApi.instance().registries().grinder().addRecipe( in, out, turns ); AEApi.instance().registries().grinder().addRecipe( in, out, turns );
} }
} }

View file

@ -33,14 +33,14 @@ package appeng.core.api.imc;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.core.api.IIMCHandler; import appeng.core.api.IIMCProcessor;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
public class IMCMatterCannon implements IIMCHandler public class IMCMatterCannon implements IIMCProcessor
{ {
@Override @Override
public void post(IMCMessage m) public void process( IMCMessage m )
{ {
NBTTagCompound msg = m.getNBTValue(); NBTTagCompound msg = m.getNBTValue();
NBTTagCompound item = (NBTTagCompound) msg.getTag( "item" ); NBTTagCompound item = (NBTTagCompound) msg.getTag( "item" );

View file

@ -31,14 +31,14 @@ package appeng.core.api.imc;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.api.config.TunnelType; import appeng.api.config.TunnelType;
import appeng.core.api.IIMCHandler; import appeng.core.api.IIMCProcessor;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
public class IMCP2PAttunement implements IIMCHandler public class IMCP2PAttunement implements IIMCProcessor
{ {
@Override @Override
public void post(IMCMessage m) public void process( IMCMessage m )
{ {
String key = m.key.substring( "add-p2p-attunement-".length() ).replace( '-', '_' ).toUpperCase(); String key = m.key.substring( "add-p2p-attunement-".length() ).replace( '-', '_' ).toUpperCase();

View file

@ -25,14 +25,14 @@ package appeng.core.api.imc;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.core.AELog; import appeng.core.AELog;
import appeng.core.api.IIMCHandler; import appeng.core.api.IIMCProcessor;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
public class IMCSpatial implements IIMCHandler public class IMCSpatial implements IIMCProcessor
{ {
@Override @Override
public void post(IMCMessage m) public void process( IMCMessage m )
{ {
try try