mirror of
https://github.com/GTNewHorizons/EnderCore.git
synced 2026-11-19 19:49:48 +00:00
Add WorldCache and PermanentCache to persist ID based data
This commit is contained in:
1 parent
5f8c166fce
commit
fddcd36f29
7 files changed
+494
-298
No files matched your search
@@ -25,6 +25,7 @@ import com.enderio.core.common.enchant.EnchantAutoSmelt;
|
||||
import com.enderio.core.common.enchant.EnchantXPBoost;
|
||||
import com.enderio.core.common.imc.IMCRegistry;
|
||||
import com.enderio.core.common.util.EnderFileUtils;
|
||||
import com.enderio.core.common.util.PermanentCache;
|
||||
import com.enderio.core.common.util.TextureErrorRemover;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@@ -39,111 +40,99 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLServerStartingEvent;
|
||||
|
||||
@Mod(modid = EnderCore.MODID, name = EnderCore.NAME, version = EnderCore.VERSION, guiFactory = "com.enderio.core.common.config.BaseConfigFactory")
|
||||
public class EnderCore implements IEnderMod
|
||||
{
|
||||
public static final String MODID = "endercore";
|
||||
public static final String NAME = "EnderCore";
|
||||
public static final String BASE_PACKAGE = "com.enderio";
|
||||
public static final String VERSION = "@VERSION@";
|
||||
public class EnderCore implements IEnderMod {
|
||||
|
||||
public static final Logger logger = LogManager.getLogger(NAME);
|
||||
public static final Lang lang = new Lang(MODID);
|
||||
public static final String MODID = "endercore";
|
||||
public static final String NAME = "EnderCore";
|
||||
public static final String BASE_PACKAGE = "com.enderio";
|
||||
public static final String VERSION = "@VERSION@";
|
||||
|
||||
@Instance
|
||||
public static EnderCore instance;
|
||||
public static final Logger logger = LogManager.getLogger(NAME);
|
||||
public static final Lang lang = new Lang(MODID);
|
||||
|
||||
@SidedProxy(serverSide = "com.enderio.core.common.CommonProxy", clientSide = "com.enderio.core.client.ClientProxy")
|
||||
public static CommonProxy proxy;
|
||||
@Instance
|
||||
public static EnderCore instance;
|
||||
|
||||
public List<IConfigHandler> configs = Lists.newArrayList();
|
||||
@SidedProxy(serverSide = "com.enderio.core.common.CommonProxy", clientSide = "com.enderio.core.client.ClientProxy")
|
||||
public static CommonProxy proxy;
|
||||
|
||||
@EventHandler
|
||||
@SneakyThrows
|
||||
public void preInit(FMLPreInitializationEvent event)
|
||||
{
|
||||
if (event.getSide().isClient())
|
||||
{
|
||||
TextureErrorRemover.beginIntercepting();
|
||||
}
|
||||
public List<IConfigHandler> configs = Lists.newArrayList();
|
||||
|
||||
ConfigHandler.configFolder = event.getModConfigurationDirectory();
|
||||
ConfigHandler.enderConfigFolder = new File(ConfigHandler.configFolder.getPath() + "/" + MODID);
|
||||
ConfigHandler.configFile = new File(ConfigHandler.enderConfigFolder.getPath() + "/" + event.getSuggestedConfigurationFile().getName());
|
||||
|
||||
if (!ConfigHandler.configFile.exists() && event.getSuggestedConfigurationFile().exists())
|
||||
{
|
||||
FileUtils.copyFile(event.getSuggestedConfigurationFile(), ConfigHandler.configFile);
|
||||
EnderFileUtils.safeDelete(event.getSuggestedConfigurationFile());
|
||||
}
|
||||
|
||||
ConfigHandler.instance().initialize(ConfigHandler.configFile);
|
||||
Handlers.findPackages();
|
||||
|
||||
CompatabilityRegistry.INSTANCE.handle(event);
|
||||
OreDict.registerVanilla();
|
||||
|
||||
EnchantXPBoost.INSTANCE.register();
|
||||
EnchantAutoSmelt.INSTANCE.register();
|
||||
@EventHandler
|
||||
@SneakyThrows
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
if (event.getSide().isClient()) {
|
||||
TextureErrorRemover.beginIntercepting();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void init(FMLInitializationEvent event)
|
||||
{
|
||||
for (IConfigHandler c : configs)
|
||||
{
|
||||
c.initHook();
|
||||
}
|
||||
ConfigHandler.configFolder = event.getModConfigurationDirectory();
|
||||
ConfigHandler.enderConfigFolder = new File(ConfigHandler.configFolder.getPath() + "/" + MODID);
|
||||
ConfigHandler.configFile = new File(ConfigHandler.enderConfigFolder.getPath() + "/" + event.getSuggestedConfigurationFile().getName());
|
||||
|
||||
Handlers.register();
|
||||
CompatabilityRegistry.INSTANCE.handle(event);
|
||||
ClientCommandHandler.instance.registerCommand(CommandReloadConfigs.CLIENT);
|
||||
if (event.getSide().isServer())
|
||||
{
|
||||
((CommandHandler) MinecraftServer.getServer().getCommandManager()).registerCommand(CommandReloadConfigs.SERVER);
|
||||
}
|
||||
|
||||
IMCRegistry.INSTANCE.init();
|
||||
if (!ConfigHandler.configFile.exists() && event.getSuggestedConfigurationFile().exists()) {
|
||||
FileUtils.copyFile(event.getSuggestedConfigurationFile(), ConfigHandler.configFile);
|
||||
EnderFileUtils.safeDelete(event.getSuggestedConfigurationFile());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void postInit(FMLPostInitializationEvent event)
|
||||
{
|
||||
for (IConfigHandler c : configs)
|
||||
{
|
||||
c.postInitHook();
|
||||
}
|
||||
ConfigHandler.instance().initialize(ConfigHandler.configFile);
|
||||
Handlers.findPackages();
|
||||
|
||||
CompatabilityRegistry.INSTANCE.handle(event);
|
||||
ConfigHandler.instance().loadRightClickCrops();
|
||||
CompatabilityRegistry.INSTANCE.handle(event);
|
||||
OreDict.registerVanilla();
|
||||
|
||||
EnchantXPBoost.INSTANCE.register();
|
||||
EnchantAutoSmelt.INSTANCE.register();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void init(FMLInitializationEvent event) {
|
||||
for (IConfigHandler c : configs) {
|
||||
c.initHook();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onServerStarting(FMLServerStartingEvent event)
|
||||
{
|
||||
event.registerServerCommand(new CommandScoreboardInfo());
|
||||
Handlers.register();
|
||||
CompatabilityRegistry.INSTANCE.handle(event);
|
||||
ClientCommandHandler.instance.registerCommand(CommandReloadConfigs.CLIENT);
|
||||
if (event.getSide().isServer()) {
|
||||
((CommandHandler) MinecraftServer.getServer().getCommandManager()).registerCommand(CommandReloadConfigs.SERVER);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onIMCEvent(IMCEvent event)
|
||||
{
|
||||
IMCRegistry.INSTANCE.handleEvent(event);
|
||||
IMCRegistry.INSTANCE.init();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void postInit(FMLPostInitializationEvent event) {
|
||||
for (IConfigHandler c : configs) {
|
||||
c.postInitHook();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modid()
|
||||
{
|
||||
return MODID;
|
||||
}
|
||||
CompatabilityRegistry.INSTANCE.handle(event);
|
||||
ConfigHandler.instance().loadRightClickCrops();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name()
|
||||
{
|
||||
return NAME;
|
||||
}
|
||||
@EventHandler
|
||||
public void onServerStarting(FMLServerStartingEvent event) {
|
||||
event.registerServerCommand(new CommandScoreboardInfo());
|
||||
PermanentCache.saveCaches();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String version()
|
||||
{
|
||||
return VERSION;
|
||||
}
|
||||
@EventHandler
|
||||
public void onIMCEvent(IMCEvent event) {
|
||||
IMCRegistry.INSTANCE.handleEvent(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modid() {
|
||||
return MODID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String version() {
|
||||
return VERSION;
|
||||
}
|
||||
}
|
||||
@@ -145,7 +145,7 @@ public class SpecialTooltipHandler {
|
||||
}
|
||||
|
||||
public static void addShowDetailsTooltip(List list) {
|
||||
list.add(EnumChatFormatting.WHITE + "" + EnumChatFormatting.ITALIC + EnderCore.lang.localize("item.tooltip.showDetails"));
|
||||
list.add(EnumChatFormatting.WHITE + "" + EnumChatFormatting.ITALIC + EnderCore.lang.localize("tooltip.showDetails"));
|
||||
}
|
||||
|
||||
public static boolean showAdvancedTooltips() {
|
||||
@@ -169,7 +169,7 @@ public class SpecialTooltipHandler {
|
||||
int line = 1;
|
||||
while (!done) {
|
||||
String key = keyBase + line;
|
||||
String val = EnderCore.lang.localize(key, false);
|
||||
String val = EnderCore.lang.localizeExact(key);
|
||||
if(val == null || val.trim().length() < 0 || val.equals(key) || line > 12) {
|
||||
done = true;
|
||||
} else {
|
||||
|
||||
@@ -2,12 +2,14 @@ package com.enderio.core.client.render;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import com.enderio.core.api.client.render.VertexTransform;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.renderer.RenderBlocks;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.client.model.obj.GroupObject;
|
||||
|
||||
import com.enderio.core.api.client.render.VertexTransform;
|
||||
|
||||
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||
|
||||
public class TechneModelRenderer implements ISimpleBlockRenderingHandler {
|
||||
@@ -38,13 +40,21 @@ public class TechneModelRenderer implements ISimpleBlockRenderingHandler {
|
||||
@Override
|
||||
public void renderInventoryBlock(Block block, int metadata, int modelId, RenderBlocks renderer) {
|
||||
TechneUtil.vt = this.vt;
|
||||
TechneUtil.renderInventoryBlock(model, block, metadata, renderer);
|
||||
TechneUtil.renderInventoryBlock(model, getModelIcon(block, metadata), block, metadata, renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) {
|
||||
TechneUtil.vt = this.vt;
|
||||
return TechneUtil.renderWorldBlock(model, world, x, y, z, block, renderer);
|
||||
return TechneUtil.renderWorldBlock(model, getModelIcon(world, x, y, z, block), world, x, y, z, block, renderer);
|
||||
}
|
||||
|
||||
protected IIcon getModelIcon(Block block, int metadata) {
|
||||
return block.getIcon(0, metadata);
|
||||
}
|
||||
|
||||
protected IIcon getModelIcon(IBlockAccess world, int x, int y, int z, Block block) {
|
||||
return block.getIcon(world, x, y, z, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -38,6 +38,10 @@ public class Lang {
|
||||
* @return A localized string.
|
||||
*/
|
||||
public String localize(String unloc, Object... args) {
|
||||
// Compat for old EIO code
|
||||
if (args.length == 1 && args[0] instanceof Boolean && !((Boolean) args[0]).booleanValue()) {
|
||||
return localizeExact(unloc);
|
||||
}
|
||||
return localizeExact(addPrefix(unloc), args);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,194 +28,203 @@ import com.google.gson.reflect.TypeToken;
|
||||
* Where the objects go inbetween the brackets ( [] )
|
||||
*
|
||||
* @param <T>
|
||||
* The type of the object to be read in. If this type is generic in itself (ex. {@code HashMap}) you should use one of the TypeToken
|
||||
* constructors.
|
||||
* The type of the object to be read in. If this type is generic in
|
||||
* itself (ex. {@code HashMap}) you should use one of the TypeToken
|
||||
* constructors.
|
||||
*/
|
||||
public class JsonConfigReader<T> implements Iterable<T>
|
||||
{
|
||||
/**
|
||||
* A class representing some info about your mod.
|
||||
* <p>
|
||||
* <b>mainClass</b> is a class in your mod so that files can be copied out of your jar.<br>
|
||||
* <b>assetPath</b> is the path to your default json file. This includes the /modid part and whatever subfolders follow.
|
||||
*/
|
||||
@Value
|
||||
public static class ModToken
|
||||
{
|
||||
private Class<?> mainClass;
|
||||
private String assetPath;
|
||||
public class JsonConfigReader<T> implements Iterable<T> {
|
||||
|
||||
/**
|
||||
* A class representing some info about your mod.
|
||||
* <p>
|
||||
* <b>mainClass</b> is a class in your mod so that files can be copied out of
|
||||
* your jar.<br>
|
||||
* <b>assetPath</b> is the path to your default json file. This includes the
|
||||
* /modid part and whatever subfolders follow.
|
||||
*/
|
||||
@Value
|
||||
public static class ModToken {
|
||||
private Class<?> mainClass;
|
||||
private String assetPath;
|
||||
}
|
||||
|
||||
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private static final JsonParser parser = new JsonParser();
|
||||
private static final String DEFAULT_KEY = "data";
|
||||
|
||||
private File file;
|
||||
private JsonObject root;
|
||||
|
||||
private Class<T> type = null;
|
||||
private TypeToken<T> typeToken = null;
|
||||
|
||||
/**
|
||||
* An easy way to read in config files in JSON format
|
||||
*
|
||||
* @param mod
|
||||
* A {@link ModToken} object to hold data about your mod. This is
|
||||
* used to automatically copy the file from your jar if it does not
|
||||
* exist in the config folder
|
||||
* @param fullFileName
|
||||
* The full file path and name of your JSON config. Can be relative
|
||||
* to the working directory or absolute.
|
||||
* @param objClass
|
||||
* The type of the object being deserialized. Must be of the same
|
||||
* type as the generic type of this class.
|
||||
*/
|
||||
public JsonConfigReader(ModToken mod, String fullFileName, Class<T> objClass) {
|
||||
this(mod, new File(fullFileName), objClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* An easy way to read in config files in JSON format
|
||||
*
|
||||
* @param mod
|
||||
* A {@link ModToken} object to hold data about your mod. This is
|
||||
* used to automatically copy the file from your jar if it does not
|
||||
* exist in the config folder
|
||||
* @param file
|
||||
* The {@link File} representing your JSON config.
|
||||
* @param objClass
|
||||
* The type of the object being deserialized. Must be of the same
|
||||
* type as the generic type of this class.
|
||||
*/
|
||||
public JsonConfigReader(ModToken mod, File file, Class<T> objClass) {
|
||||
this.type = objClass;
|
||||
initialize(mod, file);
|
||||
}
|
||||
|
||||
/**
|
||||
* An easy way to read in config files in JSON format. Use this constructor if
|
||||
* the type of this class is generic in itself. (ex. {@code HashMap<K, V>})
|
||||
*
|
||||
* @param mod
|
||||
* A {@link ModToken} object to hold data about your mod. This is
|
||||
* used to automatically copy the file from your jar if it does not
|
||||
* exist in the config folder
|
||||
* @param fullFileName
|
||||
* The full file path and name of your JSON config. Can be relative
|
||||
* to the working directory or absolute.
|
||||
* @param objType
|
||||
* A {@link TypeToken} representing the type of the object to be
|
||||
* deserialized. Must be of the same type as the generic type of this
|
||||
* class.
|
||||
*/
|
||||
public JsonConfigReader(ModToken mod, String fullFileName, TypeToken<T> objType) {
|
||||
this(mod, new File(fullFileName), objType);
|
||||
}
|
||||
|
||||
/**
|
||||
* An easy way to read in config files in JSON format. Use this constructor if
|
||||
* the type of this class is generic in itself. (ex. {@code HashMap<K, V>})
|
||||
*
|
||||
* @param mod
|
||||
* A {@link ModToken} object to hold data about your mod. This is
|
||||
* used to automatically copy the file from your jar if it does not
|
||||
* exist in the config folder
|
||||
* @param file
|
||||
* The {@link File} representing your JSON config.
|
||||
* @param objType
|
||||
* A {@link TypeToken} representing the type of the object to be
|
||||
* deserialized. Must be of the same type as the generic type of this
|
||||
* class.
|
||||
*/
|
||||
public JsonConfigReader(ModToken mod, File file, TypeToken<T> objType) {
|
||||
this.typeToken = objType;
|
||||
initialize(mod, file);
|
||||
}
|
||||
|
||||
private void initialize(ModToken mod, File file) {
|
||||
this.file = file;
|
||||
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
String assetPath = mod.getAssetPath();
|
||||
if (!assetPath.endsWith("/")) {
|
||||
assetPath = assetPath + "/";
|
||||
}
|
||||
|
||||
EnderFileUtils.copyFromJar(mod.getMainClass(), assetPath + file.getName(), file);
|
||||
}
|
||||
|
||||
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private static final JsonParser parser = new JsonParser();
|
||||
private static final String DEFAULT_KEY = "data";
|
||||
refresh();
|
||||
}
|
||||
|
||||
private File file;
|
||||
private JsonObject root;
|
||||
|
||||
private Class<T> type = null;
|
||||
private TypeToken<T> typeToken = null;
|
||||
|
||||
/**
|
||||
* An easy way to read in config files in JSON format
|
||||
*
|
||||
* @param mod
|
||||
* A {@link ModToken} object to hold data about your mod. This is used to automatically copy the file from your jar if it does not
|
||||
* exist in the config folder
|
||||
* @param fullFileName
|
||||
* The full file path and name of your JSON config. Can be relative to the working directory or absolute.
|
||||
* @param objClass
|
||||
* The type of the object being deserialized. Must be of the same type as the generic type of this class.
|
||||
*/
|
||||
public JsonConfigReader(ModToken mod, String fullFileName, Class<T> objClass)
|
||||
{
|
||||
this(mod, new File(fullFileName), objClass);
|
||||
/**
|
||||
* Reparses the config file.
|
||||
*
|
||||
* @return The {@link JsonObject} read.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* If there is an exception while reading the file.
|
||||
*/
|
||||
public JsonObject parseFile() {
|
||||
try {
|
||||
return parser.parse(new FileReader(file)).getAsJsonObject();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An easy way to read in config files in JSON format
|
||||
*
|
||||
* @param mod
|
||||
* A {@link ModToken} object to hold data about your mod. This is used to automatically copy the file from your jar if it does not
|
||||
* exist in the config folder
|
||||
* @param file
|
||||
* The {@link File} representing your JSON config.
|
||||
* @param objClass
|
||||
* The type of the object being deserialized. Must be of the same type as the generic type of this class.
|
||||
*/
|
||||
public JsonConfigReader(ModToken mod, File file, Class<T> objClass)
|
||||
{
|
||||
this.type = objClass;
|
||||
initialize(mod, file);
|
||||
/**
|
||||
* Reparses the config file and stores the result in this class. Use this if
|
||||
* your JSON config was edited and you want to reload from disk.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* If there is an exception while reading the file.
|
||||
*/
|
||||
public void refresh() {
|
||||
this.root = parseFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* If this key exists in the current JSON data.
|
||||
*
|
||||
* @param key
|
||||
* The String key
|
||||
* @return True if this JSON has the given key. False otherwise.
|
||||
*/
|
||||
public boolean hasKey(String key) {
|
||||
return root.get(key) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from the {@link JsonObject} linked to the {@link String key} and
|
||||
* returns a List of all the elements contained in its array.
|
||||
*
|
||||
* @return A list of the generic type of this class containing the
|
||||
* deserialized elements from the passed key.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<T> getElements(String key) {
|
||||
JsonArray elements = root.get(key).getAsJsonArray();
|
||||
List<T> list = new ArrayList<T>();
|
||||
for (int i = 0; i < elements.size(); i++) {
|
||||
if (type == null) {
|
||||
list.add((T) gson.fromJson(elements.get(i), typeToken.getType()));
|
||||
} else {
|
||||
list.add(gson.fromJson(elements.get(i), type));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* An easy way to read in config files in JSON format. Use this constructor if the type of this class is generic in itself. (ex.
|
||||
* {@code HashMap<K, V>})
|
||||
*
|
||||
* @param mod
|
||||
* A {@link ModToken} object to hold data about your mod. This is used to automatically copy the file from your jar if it does not
|
||||
* exist in the config folder
|
||||
* @param fullFileName
|
||||
* The full file path and name of your JSON config. Can be relative to the working directory or absolute.
|
||||
* @param objType
|
||||
* A {@link TypeToken} representing the type of the object to be deserialized. Must be of the same type as the generic type of this
|
||||
* class.
|
||||
*/
|
||||
public JsonConfigReader(ModToken mod, String fullFileName, TypeToken<T> objType)
|
||||
{
|
||||
this(mod, new File(fullFileName), objType);
|
||||
}
|
||||
/**
|
||||
* Reads the default (defined by {@value #DEFAULT_KEY}) cached JsonObject in
|
||||
* this class and returns a List of all the elements contained in its array.
|
||||
* <p>
|
||||
* {@link #getElements(String)} should be used if your config has multiple
|
||||
* keys.
|
||||
*
|
||||
* @return A list of the generic type of this class containing the
|
||||
* deserialized elements from your JSON config's default key.
|
||||
*/
|
||||
public List<T> getElements() {
|
||||
return getElements(DEFAULT_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* An easy way to read in config files in JSON format. Use this constructor if the type of this class is generic in itself. (ex.
|
||||
* {@code HashMap<K, V>})
|
||||
*
|
||||
* @param mod
|
||||
* A {@link ModToken} object to hold data about your mod. This is used to automatically copy the file from your jar if it does not
|
||||
* exist in the config folder
|
||||
* @param file
|
||||
* The {@link File} representing your JSON config.
|
||||
* @param objType
|
||||
* A {@link TypeToken} representing the type of the object to be deserialized. Must be of the same type as the generic type of this
|
||||
* class.
|
||||
*/
|
||||
public JsonConfigReader(ModToken mod, File file, TypeToken<T> objType)
|
||||
{
|
||||
this.typeToken = objType;
|
||||
initialize(mod, file);
|
||||
}
|
||||
|
||||
private void initialize(ModToken mod, File file)
|
||||
{
|
||||
this.file = file;
|
||||
|
||||
if (!file.exists())
|
||||
{
|
||||
file.getParentFile().mkdirs();
|
||||
String assetPath = mod.getAssetPath();
|
||||
if (!assetPath.endsWith("/"))
|
||||
{
|
||||
assetPath = assetPath + "/";
|
||||
}
|
||||
|
||||
EnderFileUtils.copyFromJar(mod.getMainClass(), assetPath + file.getName(), file);
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reparses the config file.
|
||||
*
|
||||
* @return The {@link JsonObject} read.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* If there is an exception while reading the file.
|
||||
*/
|
||||
public JsonObject parseFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
return parser.parse(new FileReader(file)).getAsJsonObject();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reparses the config file and stores the result in this class. Use this if your JSON config was edited and you want to reload from disk.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* If there is an exception while reading the file.
|
||||
*/
|
||||
public void refresh()
|
||||
{
|
||||
this.root = parseFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from the {@link JsonObject} linked to the {@link String key} and returns a List of all the elements contained in its array.
|
||||
*
|
||||
* @return A list of the generic type of this class containing the deserialized elements from the passed key.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<T> getElements(String key)
|
||||
{
|
||||
JsonArray elements = root.get(key).getAsJsonArray();
|
||||
List<T> list = new ArrayList<T>();
|
||||
for (int i = 0; i < elements.size(); i++)
|
||||
{
|
||||
if (type == null)
|
||||
{
|
||||
list.add((T) gson.fromJson(elements.get(i), typeToken.getType()));
|
||||
}
|
||||
else
|
||||
{
|
||||
list.add(gson.fromJson(elements.get(i), type));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the default (defined by {@value #DEFAULT_KEY}) cached JsonObject in this class and returns a List of all the elements contained in its
|
||||
* array.
|
||||
* <p>
|
||||
* {@link #getElements(String)} should be used if your config has multiple keys.
|
||||
*
|
||||
* @return A list of the generic type of this class containing the deserialized elements from your JSON config's default key.
|
||||
*/
|
||||
public List<T> getElements()
|
||||
{
|
||||
return getElements(DEFAULT_KEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator()
|
||||
{
|
||||
return getElements().iterator();
|
||||
}
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return getElements().iterator();
|
||||
}
|
||||
}
|
||||
@@ -289,7 +289,7 @@ public class ItemUtil {
|
||||
if (item == null) {
|
||||
return null;
|
||||
}
|
||||
return EnderCore.lang.localize("item.darkSteel.tooltip.durability") + " " + (item.getMaxDamage() - item.getItemDamage()) + "/" + item.getMaxDamage();
|
||||
return EnderCore.lang.localize("tooltip.durability") + " " + (item.getMaxDamage() - item.getItemDamage()) + "/" + item.getMaxDamage();
|
||||
}
|
||||
|
||||
public static NBTTagCompound getOrCreateNBT(ItemStack stack) {
|
||||
|
||||
@@ -1,39 +1,223 @@
|
||||
package com.enderio.core.common.util;
|
||||
|
||||
import gnu.trove.map.TIntObjectMap;
|
||||
import gnu.trove.map.hash.TIntObjectHashMap;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.BitSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.DimensionManager;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
public class WorldCache<K, V> extends HashMap<K, V>
|
||||
{
|
||||
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
private transient File saveFile;
|
||||
/**
|
||||
* Untested way to save arbitrary to a game save. The name to ID map will be
|
||||
* hot-swapped when a new save is loaded.
|
||||
*/
|
||||
public class WorldCache<I> {
|
||||
|
||||
public WorldCache(String ident)
|
||||
{
|
||||
super();
|
||||
saveFile = new File(DimensionManager.getCurrentSaveRootDirectory().getAbsolutePath() + "/" + ident + ".json");
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
|
||||
public static final int MAX_ID = Short.MAX_VALUE;
|
||||
|
||||
protected String ident;
|
||||
protected boolean locked;
|
||||
|
||||
protected BitSet usedIDs = new BitSet(MAX_ID);
|
||||
protected Set<Integer> blockedIDs = Sets.newHashSet();
|
||||
|
||||
protected BiMap<I, String> objToName = HashBiMap.create();
|
||||
|
||||
protected BiMap<String, Integer> nameToID = HashBiMap.create();
|
||||
|
||||
public WorldCache(String ident) {
|
||||
this(ident, true);
|
||||
}
|
||||
|
||||
protected WorldCache(String ident, boolean registerToEventBus) {
|
||||
this.ident = ident;
|
||||
if (registerToEventBus) {
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@SneakyThrows
|
||||
public void onWorldSave(WorldEvent.Save event)
|
||||
{
|
||||
new FileWriter(saveFile).write(gson.toJson(this));
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onWorldSave(WorldEvent.Save event) {
|
||||
if (!event.world.isRemote && event.world.provider.dimensionId == 0) {
|
||||
saveData(getSaveFile());
|
||||
}
|
||||
|
||||
private void readData(){}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@SneakyThrows
|
||||
public void onWorldLoad(WorldEvent.Load event) {
|
||||
if (!event.world.isRemote && event.world.provider.dimensionId == 0) {
|
||||
loadData(getSaveFile());
|
||||
}
|
||||
locked = true;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
protected void loadData(File file) {
|
||||
if (!file.createNewFile()) {
|
||||
NBTTagCompound tag = null;
|
||||
try {
|
||||
tag = CompressedStreamTools.read(file);
|
||||
} catch (Exception e) {
|
||||
generateIDs();
|
||||
return;
|
||||
}
|
||||
if (tag.hasKey("ItemData")) {
|
||||
// name <-> id mappings
|
||||
NBTTagList list = tag.getTagList("ItemData", Constants.NBT.TAG_COMPOUND);
|
||||
for (int i = 0; i < list.tagCount(); i++) {
|
||||
NBTTagCompound dataTag = list.getCompoundTagAt(i);
|
||||
int id = dataTag.getInteger("V");
|
||||
String name = dataTag.getString("K");
|
||||
nameToID.put(name, id);
|
||||
if (objToName.values().contains(name)) {
|
||||
usedIDs.set(id, true);
|
||||
}
|
||||
}
|
||||
|
||||
// blocked ids
|
||||
for (int id : tag.getIntArray("BlockedItemIds")) {
|
||||
blockedIDs.add(id);
|
||||
}
|
||||
|
||||
blockOldIDs();
|
||||
mergeNewIDs();
|
||||
return;
|
||||
}
|
||||
}
|
||||
generateIDs();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
protected void saveData(File file) {
|
||||
NBTTagCompound data = new NBTTagCompound();
|
||||
|
||||
// name <-> id mappings
|
||||
NBTTagList dataList = new NBTTagList();
|
||||
for (String key : nameToID.keySet()) {
|
||||
NBTTagCompound tag = new NBTTagCompound();
|
||||
tag.setString("K", key);
|
||||
tag.setInteger("V", nameToID.get(key));
|
||||
dataList.appendTag(tag);
|
||||
}
|
||||
data.setTag("ItemData", dataList);
|
||||
// blocked ids
|
||||
data.setIntArray("BlockedItemIds", ArrayUtils.toPrimitive(blockedIDs.toArray(new Integer[0])));
|
||||
|
||||
CompressedStreamTools.write(data, file);
|
||||
}
|
||||
|
||||
protected void blockOldIDs() {
|
||||
for (String s : nameToID.keySet()) {
|
||||
if (!objToName.containsValue(s)) {
|
||||
blockedIDs.add(nameToID.get(s));
|
||||
}
|
||||
}
|
||||
Iterator<String> iter = nameToID.keySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
if (blockedIDs.contains(nameToID.get(iter.next()))) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void mergeNewIDs() {
|
||||
for (String s : objToName.values()) {
|
||||
Integer id = nameToID.get(s);
|
||||
if (id != null) {
|
||||
setID(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void generateIDs() {
|
||||
for (Entry<I, String> e : objToName.entrySet()) {
|
||||
if (!nameToID.containsKey(e.getValue())) {
|
||||
setID(e.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected int getNextAvailableID(String name) {
|
||||
Integer nextID = nameToID.get(name);
|
||||
if (nextID != null) {
|
||||
return nextID;
|
||||
} else {
|
||||
nextID = usedIDs.nextClearBit(0);
|
||||
}
|
||||
while (blockedIDs.contains(nextID) || nameToID.values().contains(nextID)) {
|
||||
usedIDs.nextClearBit(nextID++);
|
||||
}
|
||||
return nextID;
|
||||
}
|
||||
|
||||
protected void setID(String name) {
|
||||
setID(name, getNextAvailableID(name));
|
||||
}
|
||||
|
||||
protected void setID(String name, int id) {
|
||||
nameToID.put(name, id);
|
||||
usedIDs.set(id, true);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
protected File getSaveFile() {
|
||||
return new File(DimensionManager.getCurrentSaveRootDirectory().getAbsolutePath() + "/" + ident + ".json");
|
||||
}
|
||||
|
||||
public void addObject(I object, String name) {
|
||||
if (locked) {
|
||||
throw new IllegalStateException("Cannot add to world cache after world load!");
|
||||
}
|
||||
objToName.put(object, name);
|
||||
}
|
||||
|
||||
public int getID(I obj) {
|
||||
return nameToID.get(getName(obj));
|
||||
}
|
||||
|
||||
public String getName(I obj) {
|
||||
return objToName.get(obj);
|
||||
}
|
||||
|
||||
public I getObject(int id) {
|
||||
return getObject(nameToID.inverse().get(id));
|
||||
}
|
||||
|
||||
public I getObject(String string) {
|
||||
return objToName.inverse().get(string);
|
||||
}
|
||||
|
||||
public Iterator<I> getObjects() {
|
||||
return objToName.keySet().iterator();
|
||||
}
|
||||
|
||||
public TIntObjectMap<I> getEnumeratedObjects() {
|
||||
TIntObjectMap<I> ret = new TIntObjectHashMap<I>();
|
||||
for (int i = 0; i <= MAX_ID && usedIDs.nextSetBit(i) >= 0; i++) {
|
||||
if (usedIDs.get(i)) {
|
||||
ret.put(i, getObject(i));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user