PageRenderTime 4077ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/moCreatures/server/debug/sources - Copie/net/minecraft/src/spawnlist.java

https://code.google.com/p/minecraft-smp-mocreatures/
Java | 389 lines | 343 code | 43 blank | 3 comment | 34 complexity | 7dd173060f37fff3ccbaccbe1fbd2d93 MD5 | raw file
  1. package net.minecraft.src;
  2. // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
  3. // Jad home page: http://www.kpdus.com/jad.html
  4. // Decompiler options: packimports(3) braces deadcode
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.Modifier;
  7. import java.util.*;
  8. import java.util.logging.Logger;
  9. public final class spawnlist
  10. {
  11. private static Map<String, List<MobSpawnerBase>> biomes = new HashMap<String, List<MobSpawnerBase>>();
  12. private static Map<String, Class<? extends RegionFileCache>> mobNames;
  13. private static List<MobSpawnerBase> hellBiomes = new ArrayList<MobSpawnerBase>();
  14. private static List<MobSpawnerBase> surfaceBiomes = new ArrayList<MobSpawnerBase>();
  15. private static List<MobSpawnerBase> baseBiomes = new ArrayList<MobSpawnerBase>();
  16. private static List<MobSpawnerBase> swampBiomes = new ArrayList<MobSpawnerBase>();
  17. private static List<MobSpawnerBase> desertBiomes = new ArrayList<MobSpawnerBase>();
  18. public static final int MONSTER;
  19. public static final int CREATURE;
  20. public static final int WATERCREATURE;
  21. private static final int[] mobTypes;
  22. private static final Field[] fields;
  23. private static Field modifierField;
  24. private static final String[][] fieldNames = {
  25. {
  26. "r", "biomeMonsters", "field_4239_r"
  27. }, {
  28. "s", "biomeCreatures", "field_4238_s"
  29. }, {
  30. "t", "biomeWaterCreatures", "field_21108_t"
  31. }
  32. };
  33. private static final String[] fieldMapping = {
  34. "a", "stringToClassMapping", "field_1611_a"
  35. };
  36. private static final String[] fieldLimit = {
  37. "e", "maxNumberOfEntityType", "maxNumberOfCreature", "field_4277_d"
  38. };
  39. private static boolean loaded;
  40. private static Logger logger;
  41. private spawnlist()
  42. {
  43. }
  44. private static void load()
  45. {
  46. if(loaded)
  47. {
  48. return;
  49. }
  50. logger = Logger.getLogger("spawnlist");
  51. logger.setParent(ModLoader.getLogger());
  52. biomes.put("Hell biomes", hellBiomes);
  53. biomes.put("Surface biomes", surfaceBiomes);
  54. biomes.put("Desert biomes", desertBiomes);
  55. biomes.put("Base biomes", baseBiomes);
  56. biomes.put("Swamp biomes", swampBiomes);
  57. stealThemFields();
  58. loaded = true;
  59. }
  60. private static Field fetchField(Class c, String[] names)
  61. {
  62. for (String name : names)
  63. try {
  64. Field f = c.getDeclaredField(name);
  65. f.setAccessible(true);
  66. int j = modifierField.getInt(f);
  67. if ((j & 0x10) != 0) {
  68. modifierField.setInt(f, j & 0xFFFFFFEF);
  69. }
  70. return f;
  71. }
  72. catch (Throwable e)
  73. {
  74. }
  75. throwException("fetchField", "couldn't find a field: " + getFieldDescription(c, names));
  76. return null;
  77. }
  78. private static String joinString(String glue, String[] strings)
  79. {
  80. StringBuilder result = new StringBuilder();
  81. boolean first = true;
  82. for (String string : strings)
  83. {
  84. if (first)
  85. first = false;
  86. else
  87. result.append(glue);
  88. result.append(string);
  89. }
  90. return result.toString();
  91. }
  92. private static String getFieldDescription(Class c, String[] names)
  93. {
  94. return new StringBuilder(c.getName()).append(".{").append(joinString(",", names)).append("}").toString();
  95. }
  96. private static Object fetchValue(Class c, Object inst, String[] names)
  97. {
  98. Field f = fetchField(c, names);
  99. try
  100. {
  101. return f.get(inst);
  102. }
  103. catch(Throwable throwable)
  104. {
  105. throwException("fetchValue", "couldn't access a field: " + getFieldDescription(c, names));
  106. }
  107. return null;
  108. }
  109. private static void throwException(String method, String message)
  110. {
  111. throwException(method, new RuntimeException(message));
  112. }
  113. private static void throwException(String method, String message, Throwable cause)
  114. {
  115. throwException(method, new RuntimeException(message, cause));
  116. }
  117. private static void throwException(String method, Throwable e)
  118. {
  119. logger.throwing("spawnlist", method, e);
  120. ModLoader.throwException("", e);
  121. }
  122. private static void setValue(Class c, Object inst, Object value, String[] names)
  123. {
  124. Field field = fetchField(c, names);
  125. try
  126. {
  127. field.set(inst, value);
  128. }
  129. catch(Throwable throwable)
  130. {
  131. throwException("setValue", "couldn't access a field: " + getFieldDescription(c, names), throwable);
  132. }
  133. }
  134. private static List processArgs(Object[] mobs)
  135. {
  136. ArrayList<Class<? extends EntityLiving>> list = new ArrayList<Class<? extends EntityLiving>>();
  137. for (Object o : mobs)
  138. {
  139. if (o == null)
  140. continue;
  141. Class<?> c = null;
  142. if (o instanceof String)
  143. {
  144. c = mobNames.get((String)o);
  145. if (c == null)
  146. {
  147. throwException("processArgs", "unknown mob name: " + (String)o);
  148. return null;
  149. }
  150. } else if (o instanceof Class)
  151. {
  152. c = (Class<?>)o;
  153. } else
  154. {
  155. throwException("processArgs", "unknown parameter type: " + o.getClass().getName());
  156. return null;
  157. }
  158. if(!EntityLiving.class.isAssignableFrom(c))
  159. {
  160. throwException("processArgs", "can't add non living entities to spawnlists: " + c.getName());
  161. return null;
  162. }
  163. list.add(c.asSubclass(EntityLiving.class));
  164. }
  165. return list;
  166. }
  167. private static Field getField(int type)
  168. {
  169. checkMobType("getField", type);
  170. return fields[type];
  171. }
  172. private static void checkMobType(String method, int mobType)
  173. {
  174. if(mobType < 0 || mobType > fieldNames.length)
  175. throwException(method, "unknown mob type: " + Integer.toString(mobType));
  176. }
  177. public static void setLimit(int mobType, int limit)
  178. {
  179. checkMobType("setLimit", mobType);
  180. logger.fine((new StringBuilder()).append("set limit of ").append(fieldNames[mobType][1]).append(" to ").append(limit).toString());
  181. EnumCreatureType enumcreaturetype = EnumCreatureType.values()[mobType];
  182. setValue(EnumCreatureType.class, enumcreaturetype, Integer.valueOf(limit), fieldLimit);
  183. }
  184. public static int getLimit(int mobType)
  185. {
  186. checkMobType("getLimit", mobType);
  187. EnumCreatureType enumcreaturetype = EnumCreatureType.values()[mobType];
  188. return ((Integer)fetchValue(EnumCreatureType.class, enumcreaturetype, fieldLimit)).intValue();
  189. }
  190. public static void add(String biomeName, int mobType, Object[] mobs)
  191. {
  192. add(new String[] { biomeName }, mobType, mobs);
  193. }
  194. public static void add(String[] biomeNames, int type, Object[] mobs)
  195. {
  196. load();
  197. add(biomeNames, getField(type), processArgs(mobs));
  198. }
  199. private static List getBiomesByName(String name)
  200. {
  201. List<MobSpawnerBase> list = biomes.get(name);
  202. if(list == null)
  203. throwException("getBiomesByName", "Unknown biome: " + name);
  204. return list;
  205. }
  206. private static void add(String[] biomeNames, Field field, List mobs)
  207. {
  208. StringBuilder msg = new StringBuilder("add({").append(joinString(", ", biomeNames)).append("}, ").append(field.getName()).append(", {");
  209. boolean first = true;
  210. for (Class<? extends EntityLiving> mob : (List<Class<? extends EntityLiving>>)mobs)
  211. {
  212. if (first)
  213. first = false;
  214. else
  215. msg.append(", ");
  216. msg.append(mob.getName());
  217. }
  218. msg.append("})");
  219. logger.fine(msg.toString());
  220. for (String name : biomeNames)
  221. {
  222. List<MobSpawnerBase> biomes = getBiomesByName(name);
  223. for (MobSpawnerBase biome : biomes)
  224. {
  225. Set<Class<? extends EntityLiving>> existing = getMobs(biome, field);
  226. existing.addAll(mobs);
  227. setMobs(biome, field, existing);
  228. }
  229. }
  230. }
  231. public static void remove(String biomeName, int type, Object[] mobs)
  232. {
  233. remove(new String[] { biomeName }, type, mobs);
  234. }
  235. public static void remove(String[] biomeNames, int type, Object[] mobs)
  236. {
  237. load();
  238. remove(biomeNames, getField(type), processArgs(mobs));
  239. }
  240. private static void remove(String[] biomeNames, Field field, List mobs)
  241. {
  242. StringBuilder msg = new StringBuilder("remove({").append(joinString(", ", biomeNames)).append("}, ").append(field.getName()).append(", {");
  243. boolean first = true;
  244. for (Class<? extends EntityLiving> mob : (List<Class<? extends EntityLiving>>)mobs)
  245. {
  246. if (first)
  247. first = false;
  248. else
  249. msg.append(", ");
  250. msg.append(mob.getName());
  251. }
  252. msg.append("})");
  253. logger.fine(msg.toString());
  254. for (String name : biomeNames)
  255. {
  256. List<MobSpawnerBase> biomes = getBiomesByName(name);
  257. for (MobSpawnerBase biome : biomes)
  258. {
  259. Set<Class<? extends EntityLiving>> existing = getMobs(biome, field);
  260. existing.removeAll(mobs);
  261. setMobs(biome, field, existing);
  262. }
  263. }
  264. }
  265. private static void stealThemFields()
  266. {
  267. try
  268. {
  269. modifierField = (java.lang.reflect.Field.class).getDeclaredField("modifiers");
  270. modifierField.setAccessible(true);
  271. }
  272. catch(Throwable throwable)
  273. {
  274. throwException("stealThemFields", "couldn't get the modifier field of Field.class", throwable);
  275. }
  276. Object obj = fetchValue(EntityList.class, null, fieldMapping);
  277. if(!(obj instanceof Map))
  278. throwException("stealThemFields", "couldn't get EntityList.stringToClassMapping: unexpected type");
  279. mobNames = (Map<String, Class<? extends RegionFileCache>>)obj;
  280. try
  281. {
  282. for (Field field : MobSpawnerBase.class.getDeclaredFields())
  283. {
  284. field.setAccessible(true);
  285. if(Modifier.isStatic(field.getModifiers()))
  286. {
  287. Object value = field.get(null);
  288. if(value instanceof MobSpawnerBase)
  289. {
  290. MobSpawnerBase biome = (MobSpawnerBase)value;
  291. ArrayList<MobSpawnerBase> arraylist = new ArrayList<MobSpawnerBase>();
  292. arraylist.add(biome);
  293. biomes.put(biome.biomeName, arraylist);
  294. if(value instanceof MobSpawnerHell)
  295. hellBiomes.add(biome);
  296. else
  297. {
  298. surfaceBiomes.add(biome);
  299. ((biome instanceof MobSpawnerDesert) ? desertBiomes : (biome instanceof MobSpawnerSwamp) ? swampBiomes : baseBiomes).add(biome);
  300. }
  301. }
  302. }
  303. }
  304. }
  305. catch(Throwable throwable1)
  306. {
  307. throwException("stealThemFields", "couldn't steal biome fields", throwable1);
  308. return;
  309. }
  310. for(int i = 0; i < mobTypes.length; i++)
  311. fields[i] = fetchField(MobSpawnerBase.class, fieldNames[i]);
  312. }
  313. private static Set getMobs(MobSpawnerBase biome, Field field)
  314. {
  315. try
  316. {
  317. Class<? extends EntityLiving>[] result = (Class<? extends EntityLiving>[])field.get(biome);
  318. Set<Class<? extends EntityLiving>> set = new LinkedHashSet<Class<? extends EntityLiving>>(Arrays.asList(result));
  319. return set;
  320. }
  321. catch(Throwable throwable)
  322. {
  323. throwException("getMobs", "couldn't get biome mob field", throwable);
  324. }
  325. return null;
  326. }
  327. private static void setMobs(MobSpawnerBase mobspawnerbase, Field field, Set set)
  328. {
  329. try
  330. {
  331. Class<? extends EntityLiving> aclass[] = new Class[set.size()];
  332. set.toArray(aclass);
  333. field.set(mobspawnerbase, aclass);
  334. }
  335. catch(Throwable throwable)
  336. {
  337. throwException("setMobs", "couldn't set biome mob field", throwable);
  338. return;
  339. }
  340. }
  341. static
  342. {
  343. mobTypes = (new int[] { MONSTER = 0, CREATURE = 1, WATERCREATURE = 2 });
  344. fields = new Field[mobTypes.length];
  345. }
  346. }