Files
DummyCore/DummyCore/Core/Core.java
T

202 lines
7.2 KiB
Java
Raw Normal View History

2015-05-03 14:07:22 +03:00
package DummyCore.Core;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
2015-09-14 14:27:37 +03:00
import net.minecraft.creativetab.CreativeTabs;
import net.minecraftforge.common.config.Configuration;
2015-05-03 14:07:22 +03:00
import DummyCore.CreativeTabs.CreativePageBlocks;
import DummyCore.CreativeTabs.CreativePageItems;
import DummyCore.Utils.IDummyConfig;
2015-09-14 14:27:37 +03:00
import DummyCore.Utils.LoadingUtils;
2015-05-03 14:07:22 +03:00
import DummyCore.Utils.Notifier;
public class Core {
2015-09-14 14:27:37 +03:00
public static final ArrayList<DCMod> registeredMods = new ArrayList<DCMod>();
public static File mcDir;
public static boolean isModRegistered(Class<?> mod)
{
for(DCMod dcm : registeredMods)
if(dcm.modClass.equals(mod))
return true;
return false;
}
public static DCMod getModFromClass(Class<?> c)
{
for(DCMod dcm : registeredMods)
if(dcm.modClass.equals(c))
return dcm;
return null;
}
private static void registerMod(Class<?> c, String name, boolean addCreativeTabs)
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
if(!isModRegistered(c))
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
DCMod mod = new DCMod(c,name);
if(addCreativeTabs)
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
mod.blocks = new CreativePageBlocks(name);
mod.items = new CreativePageItems(name);
2015-05-03 14:07:22 +03:00
}
2015-09-14 14:27:37 +03:00
registeredMods.add(mod);
2015-05-03 14:07:22 +03:00
}else
{
2015-09-14 14:27:37 +03:00
Notifier.notifyError(name+"[classPath:"+c+"]Already has a DummyCore mod associated with it(is already registered), ignoring");
2015-05-03 14:07:22 +03:00
}
}
2015-09-14 14:27:37 +03:00
private static void registerConfigurationFileForMod(Class<?> c, String path)
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
try
{
if(!isModRegistered(c))
{
LoadingUtils.makeACrash("[DummyCore]Catched an attempt to register configuration file for not registered mod, this should not be possible and many things will go wrong as a result!", new IllegalStateException(c+" Is not a valid DCMod!"), false);
}else
{
DCMod mod = getModFromClass(c);
File file = new File(path,mod.ufName+".cfg");
if(file.isDirectory())
file.delete();
if(!file.exists())
file.createNewFile();
Configuration cfg = new Configuration(file);
cfg.save();
mod.injectFMLConfig(cfg);
Notifier.notifySimple("Configuration File for mod "+mod+" was successfully created with path "+path+mod.ufName+".cfg");
}
}
catch(IOException e)
{
DCMod mod = getModFromClass(c);
LoadingUtils.makeACrash("[DummyCore]Could not create a config file for mod "+mod+" - check your file system!", e, true);
}
2015-05-03 14:07:22 +03:00
}
/**
2015-09-14 14:27:37 +03:00
* Use this in you Pre-Initialization functions. This will register your mod in the DummyCore system and automatically create all config and .lang files.
2015-05-03 14:07:22 +03:00
* From DummyCore v1.1 you no longer need to use IMCEvent to register all blocks and items.
* @param c - class file of your mod. MUST be registered from the mod itself. Use getClass().
* @param modname - this name will be used to name CreativeTabs, config and .lang files.
* @param configPath - the path to your configuration file. You can use FMLPreInitialisationEvent.getModConfigurationDirectory().getAbsolutePath() to get your path.
2015-09-14 14:27:37 +03:00
* @param config - the initialized! object, that implements IDummyConfig.
2015-05-03 14:07:22 +03:00
* @version From DummyCore 1.0.
* @Warning From DummyCore 1.1 no longer the function registerConfigurationHandler must be called.
*
*/
2015-09-14 14:27:37 +03:00
public static void registerModAbsolute(Class<?> c, String modname, String configPath, IDummyConfig config)
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
registerModAbsolute(c,modname,configPath,config,true);
2015-05-03 14:07:22 +03:00
}
/**
2015-09-14 14:27:37 +03:00
* Use this in you Pre-Initialization functions. This will register your mod in the DummyCore system and automatically create all config and .lang files.
* From DummyCore v1.1 you no longer need to use IMCEvent to register all blocks and items.
* @param c - class file of your mod. MUST be registered from the mod itself. Use getClass().
* @param modname - this name will be used to name CreativeTabs, config and .lang files.
* @param configPath - the path to your configuration file. You can use FMLPreInitialisationEvent.getModConfigurationDirectory().getAbsolutePath() to get your path.
* @param config - the initialized! object, that implements IDummyConfig.
* @param addCreativeTabs if the custom creative tabs for your mod should be created.
* @version From DummyCore 2.0.
*
2015-05-03 14:07:22 +03:00
*/
2015-09-14 14:27:37 +03:00
public static void registerModAbsolute(Class<?> c, String modname, String configPath, IDummyConfig config, boolean addCreativeTabs)
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
registerMod(c,modname,addCreativeTabs);
registerConfigurationFileForMod(c,configPath);
registerConfigurationHandler(config,c);
loadConfigForMod(c);
2015-05-03 14:07:22 +03:00
}
/**
* Used to get the config file of the mod using .class file.
* @param c - the class file of the mod.
* @return The corresponding config file of the mod.
2015-09-14 14:27:37 +03:00
* @version From DummyCore 2.0
2015-05-03 14:07:22 +03:00
*/
public static Configuration getConfigFileForMod(Class<?> c)
{
2015-09-14 14:27:37 +03:00
if(!isModRegistered(c))
{
Notifier.notifyError("Catched an attempt to get configuration for a not registered mod, things are about go wery wrong! Offendor: "+c);
return null;
}
return getModFromClass(c).fmlCfg;
2015-05-03 14:07:22 +03:00
}
/**
* Used to register new Configuration Handlers of the mod.
* @param config - the initialised! object, that implements IDummyConfig.
* @param c - the class file of the mod. Use getClass(). Should only be called in the mod itself.
* @throws RuntimeException
2015-09-14 14:27:37 +03:00
* @version From DummyCore 2.0
2015-05-03 14:07:22 +03:00
*/
2015-09-14 14:27:37 +03:00
private static void registerConfigurationHandler(IDummyConfig config, Class<?> c)
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
if(!isModRegistered(c))
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
LoadingUtils.makeACrash("[DummyCore]Catched an attempt to register IDummyConfig for not registered mod, this should not be possible and many things will go wrong as a result!", new IllegalStateException(c+" Is not a valid DCMod!"), false);
2015-05-03 14:07:22 +03:00
}else
{
2015-09-14 14:27:37 +03:00
DCMod mod = getModFromClass(c);
mod.injectConfig(config);
Notifier.notifySimple("IDummyConfing for mod "+mod+" was successfully created");
2015-05-03 14:07:22 +03:00
}
}
2015-09-14 14:27:37 +03:00
public static void loadConfigForMod(Class<?> c)
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
if(!isModRegistered(c))
2015-05-03 14:07:22 +03:00
{
2015-09-14 14:27:37 +03:00
LoadingUtils.makeACrash("[DummyCore]Catched an attempt to load IDummyConfig for not registered mod, this should not be possible and many things will go wrong as a result!", new IllegalStateException(c+" Is not a valid DCMod!"), false);
2015-05-03 14:07:22 +03:00
}else
{
2015-09-14 14:27:37 +03:00
DCMod mod = getModFromClass(c);
mod.fmlCfg.load();
mod.cfg.load(mod.fmlCfg);
mod.fmlCfg.save();
2015-05-03 14:07:22 +03:00
}
}
/**
* Use this to get the creativetab of all mod items.
* @param c - the .class file of the mod.
* @return The corresponding Creative Tab
2015-09-14 14:27:37 +03:00
* @version From DummyCore 2.0
2015-05-03 14:07:22 +03:00
*/
public static CreativeTabs getItemTabForMod(Class<?> c)
{
2015-09-14 14:27:37 +03:00
if(!isModRegistered(c))
{
Notifier.notifyError("Catched an attempt to get CreativeTabs for a not registered mod, things are about go wery wrong! Offendor: "+c);
return null;
}
return getModFromClass(c).items;
2015-05-03 14:07:22 +03:00
}
/**
* Use this to get the creativetab of all mod blocks.
* @param c - the .class file of the mod.
* @return The corresponding Creative Tab
2015-09-14 14:27:37 +03:00
* @version From DummyCore 2.0
2015-05-03 14:07:22 +03:00
*/
public static CreativeTabs getBlockTabForMod(Class<?> c)
{
2015-09-14 14:27:37 +03:00
if(!isModRegistered(c))
{
Notifier.notifyError("Catched an attempt to get CreativeTabs for a not registered mod, things are about go wery wrong! Offendor: "+c);
return null;
}
return getModFromClass(c).blocks;
2015-05-03 14:07:22 +03:00
}
}