2012-03-30 14:11:13 +00:00
/ *
2016-06-23 03:49:47 +00:00
* Minecraft Forge
2018-07-01 21:17:28 +00:00
* Copyright ( c ) 2016 - 2018 .
2013-06-27 12:38:11 +00:00
*
2016-06-23 03:49:47 +00:00
* This library 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 version 2 . 1
* of the License .
*
* This library 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 this library ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2012-03-30 14:11:13 +00:00
* /
2013-03-08 00:25:48 +00:00
2018-06-15 19:03:35 +00:00
package net.minecraftforge.fml ;
2012-04-02 22:09:06 +00:00
2019-01-14 03:51:36 +00:00
import net.minecraftforge.forgespi.language.IModInfo ;
2012-05-11 19:18:26 +00:00
2018-09-29 01:07:46 +00:00
import java.util.Collections ;
2018-06-11 01:12:46 +00:00
import java.util.HashMap ;
2018-06-15 19:03:35 +00:00
import java.util.IdentityHashMap ;
import java.util.List ;
2018-06-11 01:12:46 +00:00
import java.util.Map ;
2018-06-15 19:03:35 +00:00
import java.util.Optional ;
2018-06-11 01:12:46 +00:00
import java.util.function.Consumer ;
2018-06-15 19:03:35 +00:00
import java.util.function.Supplier ;
2019-01-20 16:36:38 +00:00
import java.util.regex.Pattern ;
2017-01-11 23:17:56 +00:00
2012-04-05 20:22:47 +00:00
/ * *
* The container that wraps around mods in the system .
2012-07-14 17:58:46 +00:00
* < p >
* The philosophy is that individual mod implementation technologies should not
2018-08-27 17:10:07 +00:00
* impact the actual loading and management of mod code . This class provides
2012-07-14 17:58:46 +00:00
* a mechanism by which we can wrap actual mod code so that the loader and other
* facilities can treat mods at arms length .
* < / p >
2012-07-30 21:01:27 +00:00
*
2012-04-05 20:22:47 +00:00
* @author cpw
2012-07-30 21:01:27 +00:00
*
2012-04-05 20:22:47 +00:00
* /
2012-03-30 14:11:13 +00:00
2018-06-11 01:12:46 +00:00
public abstract class ModContainer
2012-04-05 14:07:52 +00:00
{
2019-01-20 16:36:38 +00:00
private static final Pattern VALID_MODIDS = Pattern . compile ( " ^[a-z0-9_-]{3,64}$ " ) ;
2018-06-11 01:12:46 +00:00
protected final String modId ;
protected final IModInfo modInfo ;
protected ModLoadingStage modLoadingStage ;
protected final Map < ModLoadingStage , Consumer < LifecycleEventProvider . LifecycleEvent > > triggerMap ;
2018-06-15 19:03:35 +00:00
protected final Map < ExtensionPoint , Supplier < ? > > extensionPoints = new IdentityHashMap < > ( ) ;
2019-01-20 16:36:38 +00:00
2018-06-11 01:12:46 +00:00
public ModContainer ( IModInfo info )
{
2019-01-20 16:36:38 +00:00
if ( ! VALID_MODIDS . matcher ( info . getModId ( ) ) . matches ( ) )
throw new IllegalArgumentException ( " Invalid modid " + info . getModId ( ) + " ! Mod ids need to be lowercase alphanumeric characters or '-'/'_' and need to be between 3 and 64 chars long. " ) ;
2018-06-11 01:12:46 +00:00
this . modId = info . getModId ( ) ;
this . modInfo = info ;
this . triggerMap = new HashMap < > ( ) ;
2018-06-19 18:04:05 +00:00
this . modLoadingStage = ModLoadingStage . CONSTRUCT ;
2013-12-17 15:51:00 +00:00
}
2012-07-14 17:58:46 +00:00
2012-04-05 20:22:47 +00:00
/ * *
2018-06-11 01:12:46 +00:00
* @return the modid for this mod
2012-04-05 20:22:47 +00:00
* /
2018-06-11 01:12:46 +00:00
public final String getModId ( )
{
return modId ;
}
2012-07-14 17:58:46 +00:00
2012-04-05 20:22:47 +00:00
/ * *
2018-06-11 01:12:46 +00:00
* @return the resource prefix for the mod
2012-04-05 20:22:47 +00:00
* /
2018-06-11 01:12:46 +00:00
public final String getPrefix ( )
{
return modId ;
}
2012-07-14 17:58:46 +00:00
2012-04-05 20:22:47 +00:00
/ * *
2018-06-11 01:12:46 +00:00
* @return The current loading stage for this mod
2012-04-05 20:22:47 +00:00
* /
2018-06-11 01:12:46 +00:00
public ModLoadingStage getCurrentState ( )
{
return modLoadingStage ;
}
2012-07-14 17:58:46 +00:00
2012-04-05 20:22:47 +00:00
/ * *
2018-06-11 01:12:46 +00:00
* Transition the mod to this event if possible .
* @param event to transition to
2012-04-05 20:22:47 +00:00
* /
2018-09-29 01:07:46 +00:00
public final void transitionState ( LifecycleEventProvider . LifecycleEvent event , Consumer < List < ModLoadingException > > errorHandler )
2018-06-11 01:12:46 +00:00
{
2018-06-15 19:03:35 +00:00
if ( modLoadingStage = = event . fromStage ( ) )
2018-06-11 01:12:46 +00:00
{
try
{
triggerMap . getOrDefault ( modLoadingStage , e - > { } ) . accept ( event ) ;
2018-09-29 05:29:35 +00:00
modLoadingStage = event . toStage ( ) ;
2018-06-11 01:12:46 +00:00
}
2018-09-29 01:07:46 +00:00
catch ( ModLoadingException e )
2018-06-11 01:12:46 +00:00
{
modLoadingStage = ModLoadingStage . ERROR ;
2018-09-29 01:07:46 +00:00
errorHandler . accept ( Collections . singletonList ( e ) ) ;
2018-06-11 01:12:46 +00:00
}
}
}
2012-07-14 17:58:46 +00:00
2012-04-05 20:22:47 +00:00
/ * *
2018-06-11 01:12:46 +00:00
* @return the modinfo used to create this mod instance
2012-04-05 20:22:47 +00:00
* /
2018-06-11 01:12:46 +00:00
public IModInfo getModInfo ( )
{
return modInfo ;
}
2012-07-14 17:58:46 +00:00
2018-06-15 19:03:35 +00:00
@SuppressWarnings ( " unchecked " )
public < T > Optional < T > getCustomExtension ( ExtensionPoint point ) {
return Optional . ofNullable ( ( T ) extensionPoints . getOrDefault ( point , ( ) - > null ) . get ( ) ) ;
}
public < T > void registerExtensionPoint ( ExtensionPoint point , Supplier < T > extension )
{
extensionPoints . put ( point , extension ) ;
}
2012-04-05 20:22:47 +00:00
/ * *
2018-06-11 01:12:46 +00:00
* Does this mod match the supplied mod ?
2012-07-30 21:01:27 +00:00
*
2018-06-11 01:12:46 +00:00
* @param mod to compare
* @return if the mod matches
2012-04-05 20:22:47 +00:00
* /
2018-06-11 01:12:46 +00:00
public abstract boolean matches ( Object mod ) ;
2012-07-14 17:58:46 +00:00
2012-05-14 05:38:53 +00:00
/ * *
2018-06-11 01:12:46 +00:00
* @return the mod object instance
2012-05-14 05:38:53 +00:00
* /
2018-06-11 01:12:46 +00:00
public abstract Object getMod ( ) ;
2012-03-30 14:11:13 +00:00
}