2012-03-30 10:11:13 -04:00
/*
2016-06-22 23:49:47 -04:00
* Minecraft Forge
2018-07-01 14:17:28 -07:00
* Copyright (c) 2016-2018.
2013-06-27 08:38:11 -04:00
*
2016-06-22 23:49:47 -04: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 10:11:13 -04:00
*/
2013-03-07 19:25:48 -05:00
2018-06-15 15:03:35 -04:00
package net.minecraftforge.fml ;
2012-04-02 18:09:06 -04:00
2019-01-13 22:51:36 -05:00
import net.minecraftforge.forgespi.language.IModInfo ;
2012-05-11 15:18:26 -04:00
2018-09-28 21:07:46 -04:00
import java.util.Collections ;
2018-06-10 21:12:46 -04:00
import java.util.HashMap ;
2018-06-15 15:03:35 -04:00
import java.util.IdentityHashMap ;
import java.util.List ;
2018-06-10 21:12:46 -04:00
import java.util.Map ;
2018-06-15 15:03:35 -04:00
import java.util.Optional ;
2018-06-10 21:12:46 -04:00
import java.util.function.Consumer ;
2018-06-15 15:03:35 -04:00
import java.util.function.Supplier ;
2019-01-20 17:36:38 +01:00
import java.util.regex.Pattern ;
2017-01-11 15:17:56 -08:00
2012-04-05 16:22:47 -04:00
/**
* The container that wraps around mods in the system.
2012-07-14 13:58:46 -04:00
* <p>
* The philosophy is that individual mod implementation technologies should not
2018-08-27 13:10:07 -04:00
* impact the actual loading and management of mod code. This class provides
2012-07-14 13:58:46 -04: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 17:01:27 -04:00
*
2012-04-05 16:22:47 -04:00
* @author cpw
2012-07-30 17:01:27 -04:00
*
2012-04-05 16:22:47 -04:00
*/
2012-03-30 10:11:13 -04:00
2018-06-10 21:12:46 -04:00
public abstract class ModContainer
2012-04-05 10:07:52 -04:00
{
2019-01-20 17:36:38 +01:00
private static final Pattern VALID_MODIDS = Pattern . compile ( "^[a-z0-9_-]{3,64}$" );
2018-06-10 21:12:46 -04:00
protected final String modId ;
protected final IModInfo modInfo ;
protected ModLoadingStage modLoadingStage ;
protected final Map < ModLoadingStage , Consumer < LifecycleEventProvider . LifecycleEvent >> triggerMap ;
2018-06-15 15:03:35 -04:00
protected final Map < ExtensionPoint , Supplier <?>> extensionPoints = new IdentityHashMap <> ();
2019-01-20 17:36:38 +01:00
2018-06-10 21:12:46 -04:00
public ModContainer ( IModInfo info )
{
2019-01-20 17:36:38 +01: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-10 21:12:46 -04:00
this . modId = info . getModId ();
this . modInfo = info ;
this . triggerMap = new HashMap <> ();
2018-06-19 14:04:05 -04:00
this . modLoadingStage = ModLoadingStage . CONSTRUCT ;
2013-12-17 10:51:00 -05:00
}
2012-07-14 13:58:46 -04:00
2012-04-05 16:22:47 -04:00
/**
2018-06-10 21:12:46 -04:00
* @return the modid for this mod
2012-04-05 16:22:47 -04:00
*/
2018-06-10 21:12:46 -04:00
public final String getModId ()
{
return modId ;
}
2012-07-14 13:58:46 -04:00
2012-04-05 16:22:47 -04:00
/**
2018-06-10 21:12:46 -04:00
* @return the resource prefix for the mod
2012-04-05 16:22:47 -04:00
*/
2018-06-10 21:12:46 -04:00
public final String getPrefix ()
{
return modId ;
}
2012-07-14 13:58:46 -04:00
2012-04-05 16:22:47 -04:00
/**
2018-06-10 21:12:46 -04:00
* @return The current loading stage for this mod
2012-04-05 16:22:47 -04:00
*/
2018-06-10 21:12:46 -04:00
public ModLoadingStage getCurrentState ()
{
return modLoadingStage ;
}
2012-07-14 13:58:46 -04:00
2012-04-05 16:22:47 -04:00
/**
2018-06-10 21:12:46 -04:00
* Transition the mod to this event if possible.
* @param event to transition to
2012-04-05 16:22:47 -04:00
*/
2018-09-28 21:07:46 -04:00
public final void transitionState ( LifecycleEventProvider . LifecycleEvent event , Consumer < List < ModLoadingException >> errorHandler )
2018-06-10 21:12:46 -04:00
{
2018-06-15 15:03:35 -04:00
if ( modLoadingStage == event . fromStage ())
2018-06-10 21:12:46 -04:00
{
try
{
triggerMap . getOrDefault ( modLoadingStage , e -> {}). accept ( event );
2018-09-29 01:29:35 -04:00
modLoadingStage = event . toStage ();
2018-06-10 21:12:46 -04:00
}
2018-09-28 21:07:46 -04:00
catch ( ModLoadingException e )
2018-06-10 21:12:46 -04:00
{
modLoadingStage = ModLoadingStage . ERROR ;
2018-09-28 21:07:46 -04:00
errorHandler . accept ( Collections . singletonList ( e ));
2018-06-10 21:12:46 -04:00
}
}
}
2012-07-14 13:58:46 -04:00
2012-04-05 16:22:47 -04:00
/**
2018-06-10 21:12:46 -04:00
* @return the modinfo used to create this mod instance
2012-04-05 16:22:47 -04:00
*/
2018-06-10 21:12:46 -04:00
public IModInfo getModInfo ()
{
return modInfo ;
}
2012-07-14 13:58:46 -04:00
2018-06-15 15:03:35 -04: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 16:22:47 -04:00
/**
2018-06-10 21:12:46 -04:00
* Does this mod match the supplied mod?
2012-07-30 17:01:27 -04:00
*
2018-06-10 21:12:46 -04:00
* @param mod to compare
* @return if the mod matches
2012-04-05 16:22:47 -04:00
*/
2018-06-10 21:12:46 -04:00
public abstract boolean matches ( Object mod );
2012-07-14 13:58:46 -04:00
2012-05-14 01:38:53 -04:00
/**
2018-06-10 21:12:46 -04:00
* @return the mod object instance
2012-05-14 01:38:53 -04:00
*/
2018-06-10 21:12:46 -04:00
public abstract Object getMod ();
2012-03-30 10:11:13 -04:00
}