JZTech101

Untitled

Oct 5th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. package idfix;
  2.  
  3. import java.io.File;
  4. import java.util.HashMap;
  5.  
  6. import net.minecraft.block.Block;
  7. import net.minecraft.enchantment.Enchantment;
  8. import net.minecraft.item.Item;
  9. import net.minecraft.item.ItemBlock;
  10. import net.minecraft.src.BaseMod;
  11. import net.minecraftforge.common.Configuration;
  12. import net.minecraftforge.common.Property;
  13. import cpw.mods.fml.common.Mod;
  14. import cpw.mods.fml.common.Mod.Instance;
  15. import cpw.mods.fml.common.network.NetworkMod;
  16.  
  17. @Mod(modid = "Idfix", name = "Idfix", version = "1.1.0")
  18. @NetworkMod(clientSideRequired = true, serverSideRequired = true)
  19. public class Idfix {
  20. static Configuration config;
  21.  
  22. @Instance("Idfix")
  23. public static Idfix instance;
  24.  
  25. static String blockList[]=new String[4096];
  26. static HashMap<String,Integer> blockMapping=new HashMap<String,Integer>();
  27.  
  28. static String itemList[]=new String[32000];
  29. static HashMap<String,Integer> itemMapping=new HashMap<String,Integer>();
  30.  
  31. static String enchantmentList[]=new String[256];
  32. static HashMap<String,Integer> enchantmentMapping=new HashMap<String,Integer>();
  33.  
  34. static boolean isBlockItem;
  35. public static String getModNamespace(){
  36. String res=null;
  37. isBlockItem=false;
  38.  
  39. try {
  40. throw new Exception();
  41. } catch (Exception ex) {
  42. StackTraceElement[] stack=ex.getStackTrace();
  43.  
  44. for(StackTraceElement e: ex.getStackTrace()){
  45. Class<?> cl=null;
  46.  
  47. try {
  48. cl = Class.forName(e.getClassName());
  49. } catch (ClassNotFoundException ee) {
  50. continue;
  51. }
  52.  
  53. if(ItemBlock.class.isAssignableFrom(cl)) isBlockItem=true;
  54.  
  55. if(cl.getName().startsWith("sun."))continue;
  56. if(cl.getName().startsWith("cpw.mods.fml."))continue;
  57. if(cl.getName().startsWith("java."))continue;
  58. if(Block.class.isAssignableFrom(cl)) continue;
  59. if(Item.class.isAssignableFrom(cl)) continue;
  60. if(Enchantment.class.isAssignableFrom(cl)) continue;
  61. if(Idfix.class.isAssignableFrom(cl)) continue;
  62.  
  63. if(res==null)
  64. res=e.getClassName();
  65.  
  66. if (BaseMod.class.isAssignableFrom(cl)){
  67. res=e.getClassName();
  68. break;
  69. }
  70. if (cl.isAnnotationPresent(Mod.class)) {
  71. res=e.getClassName();
  72. break;
  73. }
  74. }
  75. }
  76.  
  77. return res;
  78. }
  79.  
  80. static void loadConfig(){
  81. if(config!=null) return;
  82.  
  83. Configuration config = new Configuration(new
  84. Error >> File(event.getModConfigurationDirectory(), "idfix.txt"));
  85. config.load();
  86.  
  87. for(int i=0;i<blockList.length;i++){
  88. Property p=getBlockId(i,"");
  89. if(p==null) continue;
  90.  
  91. blockList[i]=p.getString();
  92. blockMapping.put(blockList[i],i);
  93. }
  94.  
  95. for(int i=0;i<itemList.length;i++){
  96. Property p=getItemId(i,"");
  97. if(p==null) continue;
  98.  
  99. itemList[i]=p.getString();
  100. itemMapping.put(itemList[i],i);
  101. }
  102.  
  103. for(int i=0;i<enchantmentList.length;i++){
  104. Property p=getEnchantmentId(i,"");
  105. if(p==null) continue;
  106.  
  107. enchantmentList[i]=p.getString();
  108. enchantmentMapping.put(enchantmentList[i],i);
  109. }
  110. }
  111.  
  112. static Property getBlockId(int id,String def){
  113. return getId("blocks","%04d", id, def);
  114. }
  115. static Property getItemId(int id,String def){
  116. return getId("items","%05d", id, def);
  117. }
  118. static Property getEnchantmentId(int id,String def){
  119. return getId("enchantments","%03d", id, def);
  120. }
  121. static Property getId(String category,String keyMask,int id,String def){
  122. String key=String.format(keyMask,id);
  123.  
  124. if(! config.hasKey(category, key) && def.isEmpty()) return null;
  125.  
  126. return config.get(category, key, def);
  127. }
  128.  
  129. public static int transformBlockId(int blockId) {
  130. if(Block.coalBlock==null) return blockId;
  131. loadConfig();
  132.  
  133. String key=getModNamespace()+"|"+blockId;
  134. if(blockMapping.containsKey(key))
  135. blockId=blockMapping.get(key);
  136.  
  137. int lower=0x100;
  138. int upper=Block.blocksList.length;
  139.  
  140. if(blockId<0 || blockId>=blockList.length)
  141. blockId=upper-1;
  142.  
  143. for(int possibleId=upper;possibleId>=lower;possibleId--){
  144. int id=possibleId==upper?blockId:possibleId;
  145.  
  146. if(Block.blocksList[id]!=null) continue;
  147. if(itemList[id]!=null) continue;
  148.  
  149. boolean empty=blockList[id]==null;
  150. boolean fits=!empty && blockList[id].equals(key);
  151. if(!empty && !fits) continue;
  152.  
  153. if(empty && config!=null){
  154. blockList[id]=key;
  155. blockMapping.put(key, id);
  156. getBlockId(id,key).set(key);
  157. config.save();
  158. }
  159.  
  160. if(id!=blockId){
  161. if(itemList[blockId]!=null)
  162. System.out.println("Idfix: changing block id for "+key+" because of conflict with item "+itemList[blockId]+"; from "+blockId+" to "+id);
  163. else
  164. System.out.println("Idfix: changing block id for "+key+" because of conflict with block "+blockList[blockId]+"; from "+blockId+" to "+id);
  165. }
  166.  
  167. return id;
  168. }
  169.  
  170. return blockId;
  171. }
  172.  
  173. public static int transformItemId(int itemId) {
  174. if(Item.recordWait==null) return itemId;
  175. loadConfig();
  176.  
  177. int shiftedItemId=itemId+0x100;
  178.  
  179. String namespace=getModNamespace();
  180. String key=namespace+"|"+itemId;
  181. String blockKey=namespace+"|"+shiftedItemId;
  182.  
  183. /* this is a normal block item */
  184. if(shiftedItemId>=0 && shiftedItemId<Block.blocksList.length && Block.blocksList[shiftedItemId]!=null && isBlockItem)
  185. return itemId;
  186.  
  187. /* this is a block item, but not inherited from ItemBlock */
  188. Integer itemBlockId=blockMapping.get(blockKey);
  189. if(itemBlockId!=null)
  190. return itemBlockId-0x100;
  191.  
  192. if(itemMapping.containsKey(key)){
  193. itemId=itemMapping.get(key);
  194. shiftedItemId=itemId+0x100;
  195. }
  196.  
  197. int lower=0x100;
  198. int upper=Item.itemsList.length-0x100;
  199.  
  200. if(itemId<0 || itemId+0x100>=itemList.length)
  201. itemId=upper-1;
  202.  
  203. for(int possibleId=upper;possibleId>=lower;possibleId--){
  204. int id=possibleId==upper?itemId:possibleId;
  205. int shiftedId=id+0x100;
  206.  
  207. if(Item.itemsList[shiftedId]!=null) continue;
  208. if(shiftedId>0 && shiftedId<blockList.length && blockList[shiftedId]!=null)
  209. continue;
  210.  
  211. boolean empty=itemList[id]==null;
  212. boolean fits=!empty && itemList[id].equals(key);
  213. if(!empty && !fits) continue;
  214.  
  215. if(empty && config!=null){
  216. itemList[id]=key;
  217. itemMapping.put(key, id);
  218. getItemId(id,key).set(key);
  219. config.save();
  220. }
  221.  
  222. if(id!=itemId){
  223. if(shiftedItemId>0 && shiftedItemId<blockList.length && blockList[shiftedItemId]!=null)
  224. System.out.println("Idfix: changing item id for "+key+" because of conflict with block "+blockList[shiftedItemId]+"; from "+itemId+" to "+id);
  225. else
  226. System.out.println("Idfix: changing item id for "+key+" because of conflict with item "+itemList[itemId]+"; from "+itemId+" to "+id);
  227. }
  228.  
  229. return id;
  230. }
  231.  
  232. return itemId;
  233. }
  234.  
  235. public static int transformEnchantmentId(int enchantmentId) {
  236. if(Enchantment.infinity==null) return enchantmentId;
  237. loadConfig();
  238.  
  239. String key=getModNamespace()+"|"+enchantmentId;
  240. if(enchantmentMapping.containsKey(key))
  241. enchantmentId=enchantmentMapping.get(key);
  242.  
  243. int lower=0;
  244. int upper=0x100;
  245.  
  246. if(enchantmentId<0 || enchantmentId>=enchantmentList.length)
  247. enchantmentId=upper-1;
  248.  
  249. for(int possibleId=upper;possibleId>=lower;possibleId--){
  250. int id=possibleId==upper?enchantmentId:possibleId;
  251.  
  252. if(Enchantment.enchantmentsList[id]!=null) continue;
  253.  
  254. boolean empty=enchantmentList[id]==null;
  255. boolean fits=!empty && enchantmentList[id].equals(key);
  256. if(!empty && !fits) continue;
  257.  
  258. if(empty && config!=null){
  259. enchantmentList[id]=key;
  260. enchantmentMapping.put(key, id);
  261. getEnchantmentId(id,key).set(key);
  262. config.save();
  263. }
  264.  
  265. if(id!=enchantmentId)
  266. System.out.println("Idfix: changing enchantment id for "+key+" because of conflict with "+enchantmentList[enchantmentId]+"; from "+enchantmentId+" to "+id);
  267.  
  268. return id;
  269. }
  270.  
  271. return enchantmentId;
  272. }
  273.  
  274. }
Advertisement
Add Comment
Please, Sign In to add comment