PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/newcode/src/com/prupe/mcpatcher/ctm/CTMUtils.java

https://bitbucket.org/Freso/mcpatcher
Java | 325 lines | 284 code | 41 blank | 0 comment | 74 complexity | c10f5eb4d6aff8ee2df96b0d85a2dcce MD5 | raw file
  1. package com.prupe.mcpatcher.ctm;
  2. import com.prupe.mcpatcher.*;
  3. import net.minecraft.src.*;
  4. import java.util.*;
  5. public class CTMUtils {
  6. private static final MCLogger logger = MCLogger.getLogger(MCPatcherUtils.CONNECTED_TEXTURES, "CTM");
  7. private static final boolean enableStandard = Config.getBoolean(MCPatcherUtils.CONNECTED_TEXTURES, "standard", true);
  8. private static final boolean enableNonStandard = Config.getBoolean(MCPatcherUtils.CONNECTED_TEXTURES, "nonStandard", true);
  9. private static final boolean enableGrass = Config.getBoolean(MCPatcherUtils.CONNECTED_TEXTURES, "grass", false);
  10. private static final int maxRecursion = Config.getInt(MCPatcherUtils.CONNECTED_TEXTURES, "maxRecursion", 4);
  11. static final int BLOCK_ID_LOG = 17;
  12. static final int BLOCK_ID_QUARTZ = 155;
  13. static final int BLOCK_ID_GLASS = 20;
  14. static final int BLOCK_ID_GLASS_PANE = 102;
  15. static final int BLOCK_ID_BOOKSHELF = 47;
  16. static final int BLOCK_ID_GRASS = 2;
  17. static final int BLOCK_ID_MYCELIUM = 110;
  18. static final int BLOCK_ID_SNOW = 78;
  19. static final int BLOCK_ID_CRAFTED_SNOW = 80;
  20. private static final List<ITileOverride> allOverrides = new ArrayList<ITileOverride>();
  21. private static final ITileOverride blockOverrides[][] = new ITileOverride[Block.blocksList.length][];
  22. private static final Map<String, ITileOverride[]> tileOverrides = new HashMap<String, ITileOverride[]>();
  23. private static TileLoader tileLoader;
  24. private static TileOverrideImpl.BetterGrass betterGrass;
  25. static boolean active;
  26. static ITileOverride lastOverride;
  27. static {
  28. try {
  29. Class.forName(MCPatcherUtils.RENDER_PASS_CLASS).getMethod("finish").invoke(null);
  30. } catch (Throwable e) {
  31. }
  32. TexturePackChangeHandler.register(new TexturePackChangeHandler(MCPatcherUtils.CONNECTED_TEXTURES, 3) {
  33. @Override
  34. public void initialize() {
  35. }
  36. @Override
  37. public void beforeChange() {
  38. RenderPassAPI.instance.clear();
  39. allOverrides.clear();
  40. Arrays.fill(blockOverrides, null);
  41. tileOverrides.clear();
  42. tileLoader = new TileLoader("textures/blocks/", true, logger);
  43. betterGrass = null;
  44. if (enableStandard || enableNonStandard) {
  45. for (ResourceLocation resource : TexturePackAPI.listResources(TexturePackAPI.MCPATCHER_SUBDIR + "ctm", ".properties", true, false, true)) {
  46. registerOverride(TileOverride.create(resource, tileLoader));
  47. }
  48. }
  49. }
  50. @Override
  51. public void afterChange() {
  52. if (enableGrass) {
  53. registerOverride(betterGrass = new TileOverrideImpl.BetterGrass(tileLoader, BLOCK_ID_GRASS, "grass"));
  54. registerOverride(new TileOverrideImpl.BetterGrass(tileLoader, BLOCK_ID_MYCELIUM, "mycel"));
  55. }
  56. for (ITileOverride override : allOverrides) {
  57. override.registerIcons();
  58. }
  59. for (int i = 0; i < blockOverrides.length; i++) {
  60. if (blockOverrides[i] != null && Block.blocksList[i] != null) {
  61. for (ITileOverride override : blockOverrides[i]) {
  62. if (override != null && !override.isDisabled() && override.getRenderPass() >= 0) {
  63. RenderPassAPI.instance.setRenderPassForBlock(Block.blocksList[i], override.getRenderPass());
  64. }
  65. }
  66. }
  67. }
  68. for (ITileOverride[] overrides : blockOverrides) {
  69. sortOverrides(overrides);
  70. }
  71. for (ITileOverride[] overrides : tileOverrides.values()) {
  72. sortOverrides(overrides);
  73. }
  74. }
  75. private void sortOverrides(ITileOverride[] overrides) {
  76. if (overrides != null) {
  77. Arrays.sort(overrides);
  78. }
  79. }
  80. });
  81. }
  82. public static void start() {
  83. lastOverride = null;
  84. active = tileLoader.setDefaultTextureMap(Tessellator.instance);
  85. }
  86. public static Icon getTile(RenderBlocks renderBlocks, Block block, int i, int j, int k, Icon origIcon, Tessellator tessellator) {
  87. return getTile(renderBlocks, block, i, j, k, -1, origIcon, tessellator);
  88. }
  89. public static Icon getTile(RenderBlocks renderBlocks, Block block, final int i, final int j, final int k, final int face, Icon icon, Tessellator tessellator) {
  90. lastOverride = null;
  91. if (checkFace(face) && checkBlock(renderBlocks, block)) {
  92. final IBlockAccess blockAccess = renderBlocks.blockAccess;
  93. TileOverrideIterator iterator = new TileOverrideIterator(block, icon) {
  94. @Override
  95. Icon getTile(ITileOverride override, Block block, Icon currentIcon) {
  96. return override.getTile(blockAccess, block, currentIcon, i, j, k, face);
  97. }
  98. };
  99. lastOverride = iterator.go();
  100. if (lastOverride != null) {
  101. icon = iterator.getIcon();
  102. }
  103. }
  104. return lastOverride == null && skipDefaultRendering(block) ? null : icon;
  105. }
  106. public static Icon getTile(RenderBlocks renderBlocks, Block block, int face, int metadata, Tessellator tessellator) {
  107. return getTile(renderBlocks, block, face, metadata, renderBlocks.getIconBySideAndMetadata(block, face, metadata), tessellator);
  108. }
  109. public static Icon getTile(RenderBlocks renderBlocks, Block block, int face, Tessellator tessellator) {
  110. return getTile(renderBlocks, block, face, 0, renderBlocks.getIconBySide(block, face), tessellator);
  111. }
  112. private static Icon getTile(RenderBlocks renderBlocks, Block block, final int face, final int metadata, Icon icon, Tessellator tessellator) {
  113. lastOverride = null;
  114. if (checkFace(face) && checkRenderType(block)) {
  115. TileOverrideIterator iterator = new TileOverrideIterator(block, icon) {
  116. @Override
  117. Icon getTile(ITileOverride override, Block block, Icon currentIcon) {
  118. return override.getTile(block, currentIcon, face, metadata);
  119. }
  120. };
  121. lastOverride = iterator.go();
  122. if (lastOverride != null) {
  123. icon = iterator.getIcon();
  124. }
  125. }
  126. return icon;
  127. }
  128. public static void reset() {
  129. }
  130. public static void finish() {
  131. reset();
  132. RenderPassAPI.instance.finish();
  133. TessellatorUtils.clearDefaultTextureMap(Tessellator.instance);
  134. lastOverride = null;
  135. active = false;
  136. }
  137. public static boolean isBetterGrass(IBlockAccess blockAccess, Block block, int i, int j, int k, int face) {
  138. return betterGrass != null && betterGrass.isBetterGrass(blockAccess, block, i, j, k, face);
  139. }
  140. private static boolean checkBlock(RenderBlocks renderBlocks, Block block) {
  141. return active && renderBlocks.blockAccess != null;
  142. }
  143. private static boolean checkFace(int face) {
  144. return active && (face < 0 ? enableNonStandard : enableStandard);
  145. }
  146. private static boolean checkRenderType(Block block) {
  147. switch (block.getRenderType()) {
  148. case 11: // fence
  149. case 21: // fence gate
  150. return false;
  151. default:
  152. return true;
  153. }
  154. }
  155. private static boolean skipDefaultRendering(Block block) {
  156. return RenderPassAPI.instance.skipDefaultRendering(block);
  157. }
  158. private static void registerOverride(ITileOverride override) {
  159. if (override != null && !override.isDisabled()) {
  160. boolean registered = false;
  161. if (override.getMatchingBlocks() != null) {
  162. for (int index : override.getMatchingBlocks()) {
  163. String blockName = "";
  164. if (index >= 0 && index < Block.blocksList.length && Block.blocksList[index] != null) {
  165. blockName = Block.blocksList[index].getShortName();
  166. if (blockName == null) {
  167. blockName = "";
  168. } else {
  169. blockName = " (" + blockName.replaceFirst("^tile\\.", "") + ")";
  170. }
  171. }
  172. blockOverrides[index] = registerOverride(blockOverrides[index], override, "block " + index + blockName);
  173. registered = true;
  174. }
  175. }
  176. if (override.getMatchingTiles() != null) {
  177. for (String name : override.getMatchingTiles()) {
  178. tileOverrides.put(name, registerOverride(tileOverrides.get(name), override, "tile " + name));
  179. registered = true;
  180. }
  181. }
  182. if (registered) {
  183. allOverrides.add(override);
  184. }
  185. }
  186. }
  187. private static ITileOverride[] registerOverride(ITileOverride[] overrides, ITileOverride override, String description) {
  188. logger.fine("using %s for %s", override, description);
  189. if (overrides == null) {
  190. return new ITileOverride[]{override};
  191. } else {
  192. ITileOverride[] newList = new ITileOverride[overrides.length + 1];
  193. System.arraycopy(overrides, 0, newList, 0, overrides.length);
  194. newList[overrides.length] = override;
  195. return newList;
  196. }
  197. }
  198. abstract private static class TileOverrideIterator implements Iterator<ITileOverride> {
  199. private final Block block;
  200. private Icon currentIcon;
  201. private ITileOverride[] blockOverrides;
  202. private ITileOverride[] iconOverrides;
  203. private final Set<ITileOverride> skipOverrides = new HashSet<ITileOverride>();
  204. private int blockPos;
  205. private int iconPos;
  206. private boolean foundNext;
  207. private ITileOverride nextOverride;
  208. private ITileOverride lastMatchedOverride;
  209. TileOverrideIterator(Block block, Icon icon) {
  210. this.block = block;
  211. currentIcon = icon;
  212. blockOverrides = CTMUtils.blockOverrides[block.blockID];
  213. iconOverrides = CTMUtils.tileOverrides.get(this.currentIcon.getIconName());
  214. }
  215. private void resetForNextPass() {
  216. blockOverrides = null;
  217. iconOverrides = CTMUtils.tileOverrides.get(currentIcon.getIconName());
  218. blockPos = 0;
  219. iconPos = 0;
  220. foundNext = false;
  221. }
  222. public boolean hasNext() {
  223. if (foundNext) {
  224. return true;
  225. }
  226. if (iconOverrides != null) {
  227. while (iconPos < iconOverrides.length) {
  228. if (checkOverride(iconOverrides[iconPos++])) {
  229. return true;
  230. }
  231. }
  232. }
  233. if (blockOverrides != null) {
  234. while (blockPos < blockOverrides.length) {
  235. if (checkOverride(blockOverrides[blockPos++])) {
  236. return true;
  237. }
  238. }
  239. }
  240. return false;
  241. }
  242. public ITileOverride next() {
  243. if (!foundNext) {
  244. throw new IllegalStateException("next called before hasNext() == true");
  245. }
  246. foundNext = false;
  247. return nextOverride;
  248. }
  249. public void remove() {
  250. throw new UnsupportedOperationException("remove not supported");
  251. }
  252. private boolean checkOverride(ITileOverride override) {
  253. if (override != null && !override.isDisabled() && !skipOverrides.contains(override)) {
  254. foundNext = true;
  255. nextOverride = override;
  256. return true;
  257. } else {
  258. return false;
  259. }
  260. }
  261. ITileOverride go() {
  262. pass:
  263. for (int pass = 0; pass < maxRecursion; pass++) {
  264. while (hasNext()) {
  265. ITileOverride override = next();
  266. Icon newIcon = getTile(override, block, currentIcon);
  267. if (newIcon != null) {
  268. lastMatchedOverride = override;
  269. skipOverrides.add(override);
  270. currentIcon = newIcon;
  271. resetForNextPass();
  272. continue pass;
  273. }
  274. }
  275. break;
  276. }
  277. return lastMatchedOverride;
  278. }
  279. Icon getIcon() {
  280. return currentIcon;
  281. }
  282. abstract Icon getTile(ITileOverride override, Block block, Icon currentIcon);
  283. }
  284. }