Advertisement
Corosus

Untitled

Apr 15th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. package hostileworlds;
  2.  
  3. import java.io.File;
  4. import java.lang.reflect.Field;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import java.util.logging.Level;
  9.  
  10. import net.minecraft.src.c_CoroAIUtil;
  11. import net.minecraftforge.common.Configuration;
  12. import cpw.mods.fml.common.FMLLog;
  13. import cpw.mods.fml.common.event.FMLPreInitializationEvent;
  14.  
  15. public class ModConfig {
  16.  
  17.     public static HashMap<String, String> valsString = new HashMap<String, String>();
  18.     public static HashMap<String, Integer> valsInteger = new HashMap<String, Integer>();
  19.     public static HashMap<String, Boolean> valsBoolean = new HashMap<String, Boolean>();
  20.    
  21.     public Configuration preInitConfig;
  22.     public static File saveFilePath;
  23.  
  24.     public ModConfig() {
  25.        
  26.     }
  27.    
  28.     /* Main Usage Methods Start */
  29.    
  30.     /* Main Init */
  31.     public void init(FMLPreInitializationEvent event) {
  32.         saveFilePath = event.getSuggestedConfigurationFile();
  33.        
  34.         initData();
  35.         rewriteConfigFile();
  36.     }
  37.    
  38.     /* Update Config Field Entirely */
  39.     public void updateField(String name, Object obj) {
  40.         inputField(name, obj);
  41.         try { c_CoroAIUtil.setPrivateValue(ModConfigFields.class, this, name, obj); } catch (Exception ex) { ex.printStackTrace(); }
  42.         //writeHashMapsToFile();
  43.         rewriteConfigFile();
  44.     }
  45.    
  46.     /* Main Usage Methods End */
  47.    
  48.     private void initData() {
  49.         ModConfig.valsString.clear();
  50.         ModConfig.valsInteger.clear();
  51.         ModConfig.valsBoolean.clear();
  52.        
  53.         updateHashMaps();
  54.     }
  55.    
  56.     private void updateHashMaps() {
  57.         Field[] fields = ModConfigFields.class.getDeclaredFields();
  58.        
  59.         for (int i = 0; i < fields.length; i++) {
  60.             Field field = fields[i];
  61.             String name = field.getName();
  62.             processField(name);
  63.         }
  64.     }
  65.    
  66.     /*public void writeHashMapsToFile() {
  67.         Iterator it = valsString.entrySet().iterator();
  68.         while (it.hasNext()) {
  69.             Map.Entry pairs = (Map.Entry)it.next();
  70.             String name = (String)pairs.getKey();
  71.             Object val = pairs.getValue();
  72.         }
  73.     }*/
  74.    
  75.     private void processField(String fieldName) {
  76.         try {
  77.             Object obj = getField(fieldName);
  78.             if (obj instanceof String) {
  79.                 ModConfig.valsString.put(fieldName, (String)obj);
  80.             } else if (obj instanceof Integer) {
  81.                 ModConfig.valsInteger.put(fieldName, (Integer)obj);
  82.             } else if (obj instanceof Boolean) {
  83.                 ModConfig.valsBoolean.put(fieldName, (Boolean)obj);
  84.             } else {
  85.                 //dbg("unhandled datatype, update initField");
  86.             }
  87.         } catch (Exception ex) { ex.printStackTrace(); }
  88.     }
  89.    
  90.     private Object getField(String name) {
  91.         try { return c_CoroAIUtil.getPrivateValue(ModConfigFields.class, this, name);
  92.         } catch (Exception ex) { ex.printStackTrace(); }
  93.         return null;
  94.     }
  95.    
  96.     private void inputField(String fieldName, Object obj) {
  97.         if (obj instanceof String) {
  98.             ModConfig.valsString.put(fieldName, (String)obj);
  99.         } else if (obj instanceof Integer) {
  100.             ModConfig.valsInteger.put(fieldName, (Integer)obj);
  101.         } else if (obj instanceof Boolean) {
  102.             ModConfig.valsBoolean.put(fieldName, (Boolean)obj);
  103.         } else {
  104.            
  105.         }
  106.     }
  107.    
  108.     private void rewriteConfigFile() {
  109.         if (saveFilePath.exists()) saveFilePath.delete();
  110.         preInitConfig = new Configuration(saveFilePath);
  111.         preInitConfig.load();
  112.        
  113.         Field[] fields = ModConfigFields.class.getDeclaredFields();
  114.        
  115.         for (int i = 0; i < fields.length; i++) {
  116.             Field field = fields[i];
  117.             String name = field.getName();
  118.             addToConfig(name);
  119.         }
  120.        
  121.         preInitConfig.save();
  122.     }
  123.    
  124.     /*  */
  125.     private void addToConfig(String name) {
  126.        
  127.         Object obj = getField(name);
  128.         if (obj instanceof String) {
  129.             obj = preInitConfig.get(Configuration.CATEGORY_GENERAL, name, (String)obj).getString();
  130.         } else if (obj instanceof Integer) {
  131.             obj = preInitConfig.get(Configuration.CATEGORY_GENERAL, name, (Integer)obj).getInt((Integer)obj);
  132.         } else if (obj instanceof Boolean) {
  133.             obj = preInitConfig.get(Configuration.CATEGORY_GENERAL, name, (Boolean)obj).getBoolean((Boolean)obj);
  134.         } else {
  135.             //dbg("unhandled datatype, update initField");
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement