ForgePatch/src/main/java/net/minecraftforge/fml/ModContainer.java

139 lines
3.8 KiB
Java
Raw Normal View History

2012-03-30 14:11:13 +00:00
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* 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
*/
package net.minecraftforge.fml;
import net.minecraftforge.fml.language.IModInfo;
import java.util.ArrayList;
2018-06-11 01:12:46 +00:00
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
2018-06-11 01:12:46 +00:00
import java.util.Map;
import java.util.Optional;
2018-06-11 01:12:46 +00:00
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* The container that wraps around mods in the system.
* <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
* a mechanism by which we can wrap actual mod code so that the loader and other
* facilities can treat mods at arms length.
* </p>
*
* @author cpw
*
*/
2012-03-30 14:11:13 +00:00
2018-06-11 01:12:46 +00:00
public abstract class ModContainer
{
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;
protected final Map<ExtensionPoint, Supplier<?>> extensionPoints = new IdentityHashMap<>();
protected final List<Throwable> modLoadingError;
2018-06-11 01:12:46 +00:00
public ModContainer(IModInfo info)
{
this.modId = info.getModId();
this.modInfo = info;
this.triggerMap = new HashMap<>();
this.modLoadingError = new ArrayList<>();
this.modLoadingStage = ModLoadingStage.CONSTRUCT;
}
/**
2018-06-11 01:12:46 +00:00
* @return the modid for this mod
*/
2018-06-11 01:12:46 +00:00
public final String getModId()
{
return modId;
}
/**
2018-06-11 01:12:46 +00:00
* @return the resource prefix for the mod
*/
2018-06-11 01:12:46 +00:00
public final String getPrefix()
{
return modId;
}
/**
2018-06-11 01:12:46 +00:00
* @return The current loading stage for this mod
*/
2018-06-11 01:12:46 +00:00
public ModLoadingStage getCurrentState()
{
return modLoadingStage;
}
/**
2018-06-11 01:12:46 +00:00
* Transition the mod to this event if possible.
* @param event to transition to
*/
2018-06-11 01:12:46 +00:00
public final void transitionState(LifecycleEventProvider.LifecycleEvent event)
{
if (modLoadingStage == event.fromStage())
2018-06-11 01:12:46 +00:00
{
try
{
triggerMap.getOrDefault(modLoadingStage, e->{}).accept(event);
modLoadingStage = event.toStage();
}
catch (RuntimeException e)
{
modLoadingError.add(e);
2018-06-11 01:12:46 +00:00
modLoadingStage = ModLoadingStage.ERROR;
}
}
}
/**
2018-06-11 01:12:46 +00:00
* @return the modinfo used to create this mod instance
*/
2018-06-11 01:12:46 +00:00
public IModInfo getModInfo()
{
return modInfo;
}
@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);
}
/**
2018-06-11 01:12:46 +00:00
* Does this mod match the supplied mod?
*
2018-06-11 01:12:46 +00:00
* @param mod to compare
* @return if the mod matches
*/
2018-06-11 01:12:46 +00:00
public abstract boolean matches(Object mod);
/**
2018-06-11 01:12:46 +00:00
* @return the mod object instance
*/
2018-06-11 01:12:46 +00:00
public abstract Object getMod();
2012-03-30 14:11:13 +00:00
}