From e1212cb609a9b81c7578be50a341c71b99b81f45 Mon Sep 17 00:00:00 2001 From: thatsIch Date: Thu, 27 Nov 2014 17:06:48 +0100 Subject: [PATCH] Enhancement: put IMC handling into separate class Clarify documentation --- src/main/java/appeng/core/AppEng.java | 79 ++++----------- src/main/java/appeng/core/IMCHandler.java | 98 +++++++++++++++++++ .../{IIMCHandler.java => IIMCProcessor.java} | 6 +- .../core/api/imc/IMCBlackListSpatial.java | 6 +- .../java/appeng/core/api/imc/IMCGrinder.java | 8 +- .../appeng/core/api/imc/IMCMatterCannon.java | 6 +- .../appeng/core/api/imc/IMCP2PAttunement.java | 6 +- .../java/appeng/core/api/imc/IMCSpatial.java | 6 +- 8 files changed, 135 insertions(+), 80 deletions(-) create mode 100644 src/main/java/appeng/core/IMCHandler.java rename src/main/java/appeng/core/api/{IIMCHandler.java => IIMCProcessor.java} (93%) diff --git a/src/main/java/appeng/core/AppEng.java b/src/main/java/appeng/core/AppEng.java index 5a92ce26..d67a7ac9 100644 --- a/src/main/java/appeng/core/AppEng.java +++ b/src/main/java/appeng/core/AppEng.java @@ -20,18 +20,14 @@ package appeng.core; import java.io.File; -import java.util.HashMap; import java.util.concurrent.TimeUnit; -import com.google.common.base.Stopwatch; - import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; 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.FMLPreInitializationEvent; 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.network.NetworkRegistry; -import appeng.api.config.TunnelType; -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 com.google.common.base.Stopwatch; + import appeng.core.crash.CrashEnhancement; import appeng.core.crash.CrashInfo; 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" ) public class AppEng { - - private String configPath; - - public String getConfigPath() - { - return configPath; - } - public final static String modid = "appliedenergistics2"; public final static String name = "Applied Energistics 2"; - - final HashMap IMCHandlers = new HashMap(); - - public static AppEng instance; - public final static String dependencies = - // a few mods, AE should load after, probably. // required-after:AppliedEnergistics2API|all; // "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.revisionVersion + "." // revisionVersion + net.minecraftforge.common.ForgeVersion.buildVersion + ",)"; // buildVersion + public static AppEng instance; + private final IMCHandler imcHandler; + private String configPath; public AppEng() { instance = this; - IMCHandlers.put( "blacklist-block-spatial", new IMCBlackListSpatial() ); - 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() ); - } + this.imcHandler = new IMCHandler(); FMLCommonHandler.instance().registerCrashCallable( new CrashEnhancement( CrashInfo.MOD_VERSION ) ); } + public String getConfigPath() + { + return configPath; + } + public boolean isIntegrationEnabled( IntegrationType Name ) { return IntegrationRegistry.INSTANCE.isEnabled( Name ); @@ -117,13 +94,6 @@ public class AppEng return IntegrationRegistry.INSTANCE.getInstance( Name ); } - private void startService( String serviceName, Thread thread ) - { - thread.setName( serviceName ); - thread.setPriority( Thread.MIN_PRIORITY ); - thread.start(); - } - @EventHandler void PreInit( FMLPreInitializationEvent event ) { @@ -158,6 +128,13 @@ public class AppEng 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 void Init( FMLInitializationEvent event ) { @@ -190,24 +167,9 @@ public class AppEng } @EventHandler - public void processIMC( FMLInterModComms.IMCEvent event ) + public void handleIMCEvent( FMLInterModComms.IMCEvent event ) { - for ( IMCMessage m : event.getMessages() ) - { - 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 ); - } - } + this.imcHandler.handleIMCEvent( event ); } @EventHandler @@ -228,5 +190,4 @@ public class AppEng { evt.registerServerCommand( new AECommand( evt.getServer() ) ); } - } diff --git a/src/main/java/appeng/core/IMCHandler.java b/src/main/java/appeng/core/IMCHandler.java new file mode 100644 index 00000000..5325584f --- /dev/null +++ b/src/main/java/appeng/core/IMCHandler.java @@ -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 . + */ + +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 processors; + + /** + * Initializes the processors + */ + public IMCHandler() + { + this.processors = new HashMap(); + + 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 ); + } + } + } +} diff --git a/src/main/java/appeng/core/api/IIMCHandler.java b/src/main/java/appeng/core/api/IIMCProcessor.java similarity index 93% rename from src/main/java/appeng/core/api/IIMCHandler.java rename to src/main/java/appeng/core/api/IIMCProcessor.java index 9862e563..3b8a3569 100644 --- a/src/main/java/appeng/core/api/IIMCHandler.java +++ b/src/main/java/appeng/core/api/IIMCProcessor.java @@ -20,9 +20,7 @@ package appeng.core.api; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; -public interface IIMCHandler +public interface IIMCProcessor { - - void post(IMCMessage m); - + void process( IMCMessage m ); } diff --git a/src/main/java/appeng/core/api/imc/IMCBlackListSpatial.java b/src/main/java/appeng/core/api/imc/IMCBlackListSpatial.java index 382fc12c..e3b5f30a 100644 --- a/src/main/java/appeng/core/api/imc/IMCBlackListSpatial.java +++ b/src/main/java/appeng/core/api/imc/IMCBlackListSpatial.java @@ -22,14 +22,14 @@ import net.minecraft.block.Block; import net.minecraft.item.ItemStack; import appeng.api.AEApi; import appeng.core.AELog; -import appeng.core.api.IIMCHandler; +import appeng.core.api.IIMCProcessor; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; -public class IMCBlackListSpatial implements IIMCHandler +public class IMCBlackListSpatial implements IIMCProcessor { @Override - public void post(IMCMessage m) + public void process( IMCMessage m ) { ItemStack is = m.getItemStackValue(); diff --git a/src/main/java/appeng/core/api/imc/IMCGrinder.java b/src/main/java/appeng/core/api/imc/IMCGrinder.java index 25971e80..af067d57 100644 --- a/src/main/java/appeng/core/api/imc/IMCGrinder.java +++ b/src/main/java/appeng/core/api/imc/IMCGrinder.java @@ -54,14 +54,13 @@ package appeng.core.api.imc; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import appeng.api.AEApi; -import appeng.core.api.IIMCHandler; +import appeng.core.api.IIMCProcessor; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; -public class IMCGrinder implements IIMCHandler +public class IMCGrinder implements IIMCProcessor { - @Override - public void post(IMCMessage m) + public void process( IMCMessage m ) { NBTTagCompound msg = m.getNBTValue(); NBTTagCompound inTag = (NBTTagCompound) msg.getTag( "in" ); @@ -93,5 +92,4 @@ public class IMCGrinder implements IIMCHandler else AEApi.instance().registries().grinder().addRecipe( in, out, turns ); } - } diff --git a/src/main/java/appeng/core/api/imc/IMCMatterCannon.java b/src/main/java/appeng/core/api/imc/IMCMatterCannon.java index 38fddc68..38da3d48 100644 --- a/src/main/java/appeng/core/api/imc/IMCMatterCannon.java +++ b/src/main/java/appeng/core/api/imc/IMCMatterCannon.java @@ -33,14 +33,14 @@ package appeng.core.api.imc; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import appeng.api.AEApi; -import appeng.core.api.IIMCHandler; +import appeng.core.api.IIMCProcessor; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; -public class IMCMatterCannon implements IIMCHandler +public class IMCMatterCannon implements IIMCProcessor { @Override - public void post(IMCMessage m) + public void process( IMCMessage m ) { NBTTagCompound msg = m.getNBTValue(); NBTTagCompound item = (NBTTagCompound) msg.getTag( "item" ); diff --git a/src/main/java/appeng/core/api/imc/IMCP2PAttunement.java b/src/main/java/appeng/core/api/imc/IMCP2PAttunement.java index 96c90546..02693adf 100644 --- a/src/main/java/appeng/core/api/imc/IMCP2PAttunement.java +++ b/src/main/java/appeng/core/api/imc/IMCP2PAttunement.java @@ -31,14 +31,14 @@ package appeng.core.api.imc; import net.minecraft.item.ItemStack; import appeng.api.AEApi; import appeng.api.config.TunnelType; -import appeng.core.api.IIMCHandler; +import appeng.core.api.IIMCProcessor; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; -public class IMCP2PAttunement implements IIMCHandler +public class IMCP2PAttunement implements IIMCProcessor { @Override - public void post(IMCMessage m) + public void process( IMCMessage m ) { String key = m.key.substring( "add-p2p-attunement-".length() ).replace( '-', '_' ).toUpperCase(); diff --git a/src/main/java/appeng/core/api/imc/IMCSpatial.java b/src/main/java/appeng/core/api/imc/IMCSpatial.java index f1d167e3..1d1f0488 100644 --- a/src/main/java/appeng/core/api/imc/IMCSpatial.java +++ b/src/main/java/appeng/core/api/imc/IMCSpatial.java @@ -25,14 +25,14 @@ package appeng.core.api.imc; import appeng.api.AEApi; import appeng.core.AELog; -import appeng.core.api.IIMCHandler; +import appeng.core.api.IIMCProcessor; import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; -public class IMCSpatial implements IIMCHandler +public class IMCSpatial implements IIMCProcessor { @Override - public void post(IMCMessage m) + public void process( IMCMessage m ) { try