下面列出了怎么用net.minecraftforge.fml.common.Loader的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Check if another mod is loaded.
*
* @param modId The modid to check.
* @param version The version of the mod to match (optional).
*/
public boolean isModLoaded(String modId, String version) {
boolean isLoaded = Loader.isModLoaded(modId); // Check for the modid...
if (isLoaded && version != null) { // Check for the specific version...
for (ModContainer modContainer : Loader.instance().getModList()) {
if (modContainer.getModId().equals(modId) && modContainer.getVersion().equals(version)) {
return true;
}
}
return false;
}
return isLoaded;
}
public static void initIC2Circuits() {
if (!Loader.isModLoaded(GTValues.MOD_ID_GTCX)) {
if (GTConfig.general.addBasicCircuitRecipes) {
int recipeId = IC2.config.getFlag(IC2_STEEL_MODE) ? 1921363733 : 1058514721;
recipes.overrideRecipe("shaped_item.itemPartCircuit_"
+ recipeId, GTMaterialGen.getIc2(Ic2Items.electricCircuit, 1), "CCC", "RIR", "CCC", 'C', Ic2Items.insulatedCopperCable.copy(), 'R', GTValues.DUST_REDSTONE, 'I', GTValues.INPUT_INGOT_ELECTRIC);
recipeId = IC2.config.getFlag(IC2_STEEL_MODE) ? -1911001323 : 1521116961;
recipes.overrideRecipe("shaped_item.itemPartCircuit_"
+ recipeId, GTMaterialGen.getIc2(Ic2Items.electricCircuit, 1), "CRC", "CIC", "CRC", 'C', Ic2Items.insulatedCopperCable.copy(), 'R', GTValues.DUST_REDSTONE, 'I', GTValues.INPUT_INGOT_ELECTRIC);
recipes.addRecipe(GTMaterialGen.getIc2(Ic2Items.electricCircuit, 2), "CCC", "III", "CCC", 'C', Ic2Items.insulatedCopperCable.copy(), 'I', GTValues.INPUT_INGOT_ELECTRIC);
recipes.addRecipe(GTMaterialGen.getIc2(Ic2Items.electricCircuit, 2), "CIC", "CIC", "CIC", 'C', Ic2Items.insulatedCopperCable.copy(), 'I', GTValues.INPUT_INGOT_ELECTRIC);
}
if (GTConfig.general.addAdvCircuitRecipes) {
recipes.overrideRecipe("shaped_item.itemPartCircuitAdv_-1948043137", GTMaterialGen.getIc2(Ic2Items.advancedCircuit, 1), "RGR", "LCL", "RGR", 'R', GTValues.DUST_REDSTONE, 'G', GTValues.DUST_GLOWSTONE, 'C', GTValues.CIRCUIT_BASIC, 'L', GTValues.INPUT_LAPIS_ANY);
recipes.overrideRecipe("shaped_item.itemPartCircuitAdv_-205948801", GTMaterialGen.getIc2(Ic2Items.advancedCircuit, 1), "RLR", "GCG", "RLR", 'R', GTValues.DUST_REDSTONE, 'G', GTValues.DUST_GLOWSTONE, 'C', GTValues.CIRCUIT_BASIC, 'L', GTValues.INPUT_LAPIS_ANY);
recipes.addRecipe(GTMaterialGen.getIc2(Ic2Items.advancedCircuit, 2), "RGR", "LCL", "RGR", 'R', GTValues.INPUT_INGOT_SILVER, 'G', GTValues.DUST_GLOWSTONE, 'C', GTValues.INPUT_CIRCUIT_BASIC_X2, 'L', GTValues.INPUT_LAPIS_ANY);
recipes.addRecipe(GTMaterialGen.getIc2(Ic2Items.advancedCircuit, 2), "RLR", "GCG", "RLR", 'R', GTValues.INPUT_INGOT_SILVER, 'G', GTValues.DUST_GLOWSTONE, 'C', GTValues.INPUT_CIRCUIT_BASIC_X2, 'L', GTValues.INPUT_LAPIS_ANY);
}
}
}
private static Map<File, Set<ModContainer>> makeModMap() {
Map<File, Set<ModContainer>> modMap = new HashMap<>();
for (ModContainer mod : Loader.instance().getModList()) {
Set<ModContainer> currentMods = modMap.getOrDefault(mod.getSource(), new HashSet<>());
currentMods.add(mod);
try {
modMap.put(mod.getSource().getCanonicalFile(), currentMods);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
try {
modMap.remove(Loader.instance().getMinecraftModContainer().getSource()); // Ignore minecraft jar (minecraft)
modMap.remove(Loader.instance().getIndexedModList().get("FML").getSource()); // Ignore forge jar (FML, forge)
} catch (NullPointerException ignored) {
// Workaround for https://github.com/MinecraftForge/MinecraftForge/issues/4919
}
return modMap;
}
public void copyAllModules(File directory) {
Map<String, ModContainer> modList = Loader.instance().getIndexedModList();
for (Map.Entry<String, ModContainer> entry : modList.entrySet()) {
for (ModuleInstance module : modules) {
InputStream stream = LibrarianLib.PROXY.getResource(entry.getKey(), "wizmodules/" + module.getNBTKey() + ".json");
if (stream == null) {
Wizardry.LOGGER.error(" > SOMETHING WENT WRONG! Could not read module " + module.getNBTKey() + " from mod jar of '" + entry.getKey() + "'! Report this to the devs on Github!");
continue;
}
try {
FileUtils.copyInputStreamToFile(stream, new File(directory + "/wizmodules/", module.getNBTKey() + ".json"));
Wizardry.LOGGER.info(" > Module " + module.getNBTKey() + " copied successfully from mod jar.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void copyAllRecipes(File directory)
{
Map<String, ModContainer> modList = Loader.instance().getIndexedModList();
for (Map.Entry<String, ModContainer> entry : modList.entrySet() ) {
for (String recipeName : getResourceListing(entry.getKey(), "fire_recipes")) {
if (recipeName.isEmpty()) continue;
InputStream stream = LibrarianLib.PROXY.getResource(entry.getKey(), "fire_recipes/" + recipeName);
if (stream == null) {
Wizardry.LOGGER.fatal(" > SOMETHING WENT WRONG! Could not read recipe " + recipeName + " from mod jar of '" + entry.getKey() + "'! Report this to the devs on Github!");
continue;
}
try {
FileUtils.copyInputStreamToFile(stream, new File(directory, recipeName));
Wizardry.LOGGER.info(" > Fire recipe " + recipeName + " copied successfully from mod jar.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void copyAllRecipes(File directory) {
Map<String, ModContainer> modList = Loader.instance().getIndexedModList();
for (Map.Entry<String, ModContainer> entry : modList.entrySet() ) {
for (String recipeName : getResourceListing(entry.getKey(), "fluid_recipes")) {
if (recipeName.isEmpty()) continue;
InputStream stream = LibrarianLib.PROXY.getResource(entry.getKey(), "fluid_recipes/" + recipeName);
if (stream == null) {
Wizardry.LOGGER.fatal(" > SOMETHING WENT WRONG! Could not read recipe " + recipeName + " from mod jar of '" + entry.getKey() + "'! Report this to the devs on Github!");
continue;
}
try {
FileUtils.copyInputStreamToFile(stream, new File(directory, recipeName));
Wizardry.LOGGER.info(" > Mana recipe " + recipeName + " copied successfully from mod jar.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/** Set the actual port used for mission control - not persisted, could be different each time the Mod is run.
* @param port the port currently in use for mission control.
*/
static public void setMissionControlPort(int port)
{
if (port != AddressHelper.missionControlPort)
{
AddressHelper.missionControlPort = port;
// Also update our metadata, for displaying to the user:
ModMetadata md = Loader.instance().activeModContainer().getMetadata();
if (port != -1)
md.description = "Talk to this Mod using port " + TextFormatting.GREEN + port;
else
md.description = TextFormatting.RED + "ERROR: No mission control port - check configuration";
// See if changing the port should lead to changing the login details:
//AuthenticationHelper.update(MalmoMod.instance.getModPermanentConfigFile());
}
}
@EventHandler
public static void init(FMLInitializationEvent event) {
OreRegistry.loadJson(new File(configDirectory, "OreRegistry.json"));
loadConfigs();
Recipes.init();
OreRegistry.doRecipes();
proxy.initOreModels();
proxy.registerColorHandlers();
FMLInterModComms.sendMessage("Waila", "register",
"exnihiloadscensio.compatibility.CompatWaila.callbackRegister");
if (Loader.isModLoaded("theoneprobe") && Config.doTOPCompat) {
CompatTOP.init();
}
}
@Override
public void perform(Object source, GuiCustom parent)
{
for (ModContainer mod : Loader.instance().getModList())
{
if (mod.getModId().equals(modid))
{
IModGuiFactory guiFactory = FMLClientHandler.instance().getGuiFactoryFor(mod);
if (guiFactory != null)
{
GuiScreen newScreen = guiFactory.createConfigGui(parent);
Minecraft.getMinecraft().displayGuiScreen(newScreen);
}
}
}
}
private void findClasspathMods() {
List<ModContainer> mods = Loader.instance().getActiveModList();
HashSet<String> searched = new HashSet<String>();
for (ModContainer mod : mods) {
File source = mod.getSource();
if(source == null || searched.contains(source.getAbsolutePath()))
continue;
searched.add(source.getAbsolutePath());
if (source.isFile()) {
CodeChickenCorePlugin.logger.debug("Found a mod container %s, examining for codechicken classes", source.getAbsolutePath());
try {
readFromZipFile(source);
} catch (Exception e) {
CodeChickenCorePlugin.logger.error("Failed to scan " + source.getAbsolutePath() + ", the zip file is invalid", e);
}
} else if (source.isDirectory()) {
CodeChickenCorePlugin.logger.debug("Found a minecraft related directory at %s, examining for codechicken classes", source.getAbsolutePath());
readFromDirectory(source, source);
}
}
}
public static void Init() {
registerCompressorRecipes();
registerCrasherRecipes();
registerAggregatorRecipe();
if (Loader.isModLoaded("crafttweaker"))
{
doDelayTask();
}
actions = null;
}
public static void preInit() {
if (Loader.isModLoaded("tconstruct")) {
TConstructCompat.preInit();
}
if (Loader.isModLoaded("flammpfeil.slashblade")){
SlashBlade.InitEventBus.register(new SlashBladeCompat());
}
if (Loader.isModLoaded("tfc")) {
TFCCompat.preInit();
}
}
@SubscribeEvent
public void HotSpringGen(Decorate event) {
if(Loader.isModLoaded("tfc"))
return;
BlockPos pos = event.getChunkPos().getBlock(event.getRand().nextInt(16) + 8, 0, event.getRand().nextInt(16) + 8);
BlockPos newPos = WorldUtil.findGround(event.getWorld(),pos, true, false, true);
Biome biome = event.getWorld().getBiome(pos);
if (newPos != null&&event.getWorld().provider instanceof WorldProviderSurface&&biome != Biomes.DESERT && biome != Biomes.DESERT_HILLS && event.getRand().nextFloat() < SakuraConfig.hotspring_weight / 10000.0F) {
new WorldGenHotSpring().generate(event.getWorld(), event.getRand(), newPos);
}
}
public static void Init() {
barrelRegister();
furnaceRegister();
L2ISRegister();
mortarRegister();
potRegister();
if (Loader.isModLoaded("crafttweaker"))
doDelayTask();
actions = null;
}
public void init(FMLInitializationEvent event) {
MinecraftForge.ORE_GEN_BUS.register(new WorldGenLoader());
MinecraftForge.TERRAIN_GEN_BUS.register(new WorldGenLoader());
new WorldGenLoader();
TileEntityRegistry.init();
KimonoLoader.Init();
SakuraRecipeRegister.Init();
if(Loader.isModLoaded("tfc")){
TFCCompat.registerTFCFuel();
}
network = NetworkRegistry.INSTANCE.newSimpleChannel(SakuraMain.MODID);
network.registerMessage(new PacketKeyMessageHandler(),PacketKeyMessage.class,0,Side.SERVER);
}
public void unload() {
this.moduleManager.unload();
this.apiManager.unload();
this.commandManager.unload();
this.friendManager.unload();
this.waypointManager.unload();
this.macroManager.unload();
this.tickRateManager.unload();
this.chatManager.unload();
this.ignoredManager.unload();
this.capeManager.unload();
this.joinLeaveManager.unload();
this.hudManager.unload();
this.animationManager.unload();
this.notificationManager.unload();
this.seppukuMainMenu.unload();
this.cameraManager.unload();
this.getEventManager().dispatchEvent(new EventUnload());
ModContainer seppukuModContainer = null;
for (ModContainer modContainer : Loader.instance().getActiveModList()) {
if (modContainer.getModId().equals("seppukumod")) {
seppukuModContainer = modContainer;
}
}
if (seppukuModContainer != null) {
Loader.instance().getActiveModList().remove(seppukuModContainer);
}
Display.setTitle(this.prevTitle);
Minecraft.getMinecraft().ingameGUI.getChatGUI().clearChatMessages(true);
System.gc();
}
public static void preInit() {
if (initialized) return;
ICLoaded = Loader.isModLoaded("ic2");
OCLoaded = Loader.isModLoaded("opencomputers");
CTLoaded = Loader.isModLoaded("crafttweaker");
DDLoaded = Loader.isModLoaded("dumpsterdiving");
ACLoaded = Loader.isModLoaded("alchemistry");
initialized = true;
}
public static boolean isModLoaded(String modid) {
if (isModLoadedCache.containsKey(modid)) {
return isModLoadedCache.get(modid);
}
boolean isLoaded = Loader.instance().getIndexedModList().containsKey(modid);
isModLoadedCache.put(modid, isLoaded);
return isLoaded;
}
@Mod.EventHandler
public void init(FMLInitializationEvent e) {
GTMaterialElement.init();
GTRecipeIterators.init();
GTTileCentrifuge.init();
GTTileUUMAssembler.init();
GTTileMagicEnergyConverter.init();
GTRecipe.initShapeless();
GTRecipe.initItems();
GTRecipe.initBlocks();
GTRecipe.initIC2();
GTRecipe.initIC2Circuits();
GTRecipe.initIC2Jetpacks();
GTRecipe.initIC2Overrides();
GTRecipe.initProcessing();
GTBedrockOreHandler.bedrockOresInit();
GameRegistry.registerWorldGenerator(new GTWorldGen(), 0);
MinecraftForge.EVENT_BUS.register(new GTEventOnLivingFall());
MinecraftForge.EVENT_BUS.register(new GTEventLootTableLoad());
MinecraftForge.EVENT_BUS.register(new GTEventCheckSpawn());
MinecraftForge.EVENT_BUS.register(new GTEventEntityViewRenderEvent());
MinecraftForge.EVENT_BUS.register(new GTEventPopulateChunk());
MinecraftForge.EVENT_BUS.register(new GTEventItemTooltip());
if (!Loader.isModLoaded(GTValues.MOD_ID_FASTLEAF)) {
MinecraftForge.EVENT_BUS.register(new GTEventNeighborNotifyEvent());
}
MinecraftForge.TERRAIN_GEN_BUS.register(new GTEventDecorateBiome());
IC2.saveManager.registerGlobal("IDSU_Storage", GTIDSUStorageManager.class, false);
proxy.init(e);
}
@Mod.EventHandler
public void postInit(FMLPostInitializationEvent e) {
proxy.postInit(e);
GTMaterialGen.postInitProperities();
GTRecipeIterators.postInit();
GTTileMatterFabricator.postInit();
GTTileMultiFusionReactor.postInit();
GTRecipeMods.postInit();
GTTileDisassembler.init();
if (GTConfig.modcompat.compatTwilightForest && Loader.isModLoaded(GTValues.MOD_ID_TFOREST)) {
GTTwilightForestHandler.initStalactites();
}
}
public static void initEntries() {
GTJeiHandler.addEntry(new GTJeiEntry(GTTileCentrifuge.RECIPE_LIST, GTBlocks.tileCentrifuge, GTGuiMachine.GTIndustrialCentrifugeGui.class, 78, 24, 20, 18));
if (!Loader.isModLoaded(GTValues.MOD_ID_GTCX)){
GTJeiHandler.addEntry(new GTJeiEntry(GTTileMultiFusionReactor.RECIPE_LIST, GTBlocks.tileFusionReactor, GTGuiMachine.GTFusionComputerGui.class, 110, 34, 25, 17));
}
}
public static void initIC2Jetpacks() {
if (GTConfig.general.harderJetpacks) {
String inputItem = Loader.isModLoaded(GTValues.MOD_ID_GTCX) ? "plateStainlessSteel"
: GTValues.INGOT_TITANIUM;
int id = IC2.config.getFlag(IC2_STEEL_MODE) ? -1657838234 : 176647782;
recipes.overrideRecipe("shaped_item.itemArmorJetpack_"
+ id, StackUtil.copyWithDamage(Ic2Items.jetpack, 18001), "ICI", "IFI", "R R", 'I', inputItem, 'C', GTValues.CIRCUIT_BASIC, 'F', Ic2Items.fuelCan.copy(), 'R', GTValues.DUST_REDSTONE);
id = IC2.config.getFlag(IC2_STEEL_MODE) ? -1370803315 : 463682701;
recipes.overrideRecipe("shaped_item.itemArmorJetpackElectric_"
+ id, GTMaterialGen.getIc2(Ic2Items.electricJetpack), "ICI", "IBI", "G G", 'I', inputItem, 'C', GTValues.CIRCUIT_ADVANCED, 'B', Ic2Items.batBox.copy(), 'G', Items.DRAGON_BREATH);
}
}
public void register(Configuration value) {
ModContainer mod = Loader.instance().activeModContainer();
Preconditions.checkNotNull(mod, "Can't register outside initialization");
final String modId = mod.getModId();
configs.put(modId, value);
}
@Override
public boolean getResult(List<String> arguments)
{
for (String modId : arguments)
{
if (Loader.isModLoaded(modId))
return false;
}
return true;
}
public static String[] getResourceListing(String mod, String path) {
List<String> all = Lists.newArrayList();
if (CraftingHelper.findFiles(Loader.instance().getIndexedModList().get(mod), "assets/" + mod + "/" + path, null,
(root, full) -> all.add(root.relativize(full).toString()), false, false))
return all.toArray(new String[0]);
return new String[0];
}
public void loadNewInternalManifest(String... categories) {
Map<String, ModContainer> modList = Loader.instance().getIndexedModList();
for (Map.Entry<String, ModContainer> entry : modList.entrySet()) {
for (String category : categories) {
try {
for (String fileName : ManaRecipes.getResourceListing(entry.getKey(), category)) {
if (fileName.isEmpty()) continue;
InputStream stream = LibrarianLib.PROXY.getResource(entry.getKey(), category + "/" + fileName);
if (stream == null) {
Wizardry.LOGGER.error(" > SOMETHING WENT WRONG! Could not read " + fileName + " in " + category + " from mod jar! Report this to the devs on Github!");
continue;
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(stream, Charset.defaultCharset()))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append('\n');
}
addItemToManifest(category, entry.getKey(), Files.getNameWithoutExtension(fileName), sb.toString().hashCode() + "");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@EventHandler
public static void postInit(FMLPostInitializationEvent event) {
if (Loader.isModLoaded("tconstruct") && Config.doTICCompat) {
CompatTConstruct.postInit();
}
if (Loader.isModLoaded("EnderIO") && Config.doEnderIOCompat) {
CompatEIO.postInit();
}
}
public static void addTooltip(ItemStack stack, World world, List<String> curInfo){
String info = "signals.tooltip." + stack.getItem().getUnlocalizedName();
String translatedInfo = I18n.format(info);
if(!translatedInfo.equals(info)) {
if(Signals.proxy.isSneakingInGui()) {
translatedInfo = TextFormatting.AQUA + translatedInfo;
if(!Loader.isModLoaded("IGWMod")) translatedInfo += " \\n \\n" + I18n.format("signals.tooltip.assistIGW");
curInfo.addAll(SignalsUtils.convertStringIntoList(translatedInfo, 60));
} else {
curInfo.add(TextFormatting.AQUA + I18n.format("signals.gui.tooltip.sneakForInfo"));
}
}
}
public static String proxyCallback() {
if (Loader.isModLoaded("jei")) {
jeiLoaded = true;
return JEIProxy.class.getName();
}
jeiLoaded = false;
return DummyProxy.class.getName();
}
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event) {
isIC2Loaded = Loader.isModLoaded("IndustrialCraft 2");
setupMeta();
PLConfig.init(event.getSuggestedConfigurationFile());
PLEntity.init();
proxy.preInit();
}