PageRenderTime 132ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/prupe/mcpatcher
Java | 226 lines | 198 code | 26 blank | 2 comment | 37 complexity | f064ea4707d54394b53b3becc750a9d7 MD5 | raw file
  1. package com.prupe.mcpatcher.ctm;
  2. import com.prupe.mcpatcher.Config;
  3. import com.prupe.mcpatcher.MCLogger;
  4. import com.prupe.mcpatcher.MCPatcherUtils;
  5. import com.prupe.mcpatcher.mal.block.BlockAPI;
  6. import com.prupe.mcpatcher.mal.block.BlockStateMatcher;
  7. import com.prupe.mcpatcher.mal.block.RenderBlocksUtils;
  8. import com.prupe.mcpatcher.mal.block.RenderPassAPI;
  9. import com.prupe.mcpatcher.mal.resource.BlendMethod;
  10. import com.prupe.mcpatcher.mal.resource.ResourceList;
  11. import com.prupe.mcpatcher.mal.resource.TexturePackAPI;
  12. import com.prupe.mcpatcher.mal.resource.TexturePackChangeHandler;
  13. import com.prupe.mcpatcher.mal.tile.TileLoader;
  14. import net.minecraft.src.*;
  15. import java.util.*;
  16. public class CTMUtils {
  17. private static final MCLogger logger = MCLogger.getLogger(MCPatcherUtils.CONNECTED_TEXTURES, "CTM");
  18. private static final boolean enableStandard = Config.getBoolean(MCPatcherUtils.CONNECTED_TEXTURES, "standard", true);
  19. private static final boolean enableNonStandard = Config.getBoolean(MCPatcherUtils.CONNECTED_TEXTURES, "nonStandard", true);
  20. private static final List<ITileOverride> allOverrides = new ArrayList<ITileOverride>();
  21. private static final Map<Block, List<BlockStateMatcher>> blockOverrides = new IdentityHashMap<Block, List<BlockStateMatcher>>();
  22. private static final Map<String, List<ITileOverride>> tileOverrides = new HashMap<String, List<ITileOverride>>();
  23. private static TileLoader tileLoader;
  24. private static ITileOverride lastOverride;
  25. private static final TileOverrideIterator.IJK ijkIterator = newIJKIterator();
  26. private static final TileOverrideIterator.Metadata metadataIterator = newMetadataIterator();
  27. private static boolean haveBlockFace;
  28. private static final BlockOrientation renderBlockState = new BlockOrientation();
  29. static {
  30. try {
  31. Class.forName(MCPatcherUtils.RENDER_PASS_CLASS).getMethod("finish").invoke(null);
  32. } catch (Throwable e) {
  33. }
  34. TexturePackChangeHandler.register(new TexturePackChangeHandler(MCPatcherUtils.CONNECTED_TEXTURES, 3) {
  35. @Override
  36. public void initialize() {
  37. }
  38. @Override
  39. public void beforeChange() {
  40. RenderPassAPI.instance.clear();
  41. try {
  42. GlassPaneRenderer.clear();
  43. } catch (NoClassDefFoundError e) {
  44. // nothing
  45. } catch (Throwable e) {
  46. e.printStackTrace();
  47. }
  48. try {
  49. CTMUtils18.reset();
  50. } catch (NoClassDefFoundError e) {
  51. // nothing
  52. } catch (Throwable e) {
  53. e.printStackTrace();
  54. }
  55. renderBlockState.clear();
  56. ijkIterator.clear();
  57. metadataIterator.clear();
  58. allOverrides.clear();
  59. blockOverrides.clear();
  60. tileOverrides.clear();
  61. lastOverride = null;
  62. RenderBlocksUtils.blankIcon = null;
  63. tileLoader = new TileLoader("textures/blocks", logger);
  64. RenderPassAPI.instance.refreshBlendingOptions();
  65. if (enableStandard || enableNonStandard) {
  66. for (ResourceLocation resource : ResourceList.getInstance().listResources(TexturePackAPI.MCPATCHER_SUBDIR + "ctm", ".properties", true)) {
  67. registerOverride(TileOverride.create(resource, tileLoader));
  68. }
  69. }
  70. for (ResourceLocation resource : BlendMethod.getAllBlankResources()) {
  71. tileLoader.preloadTile(resource, false);
  72. }
  73. }
  74. @Override
  75. public void afterChange() {
  76. for (ITileOverride override : allOverrides) {
  77. override.registerIcons();
  78. }
  79. for (Map.Entry<Block, List<BlockStateMatcher>> entry : blockOverrides.entrySet()) {
  80. for (BlockStateMatcher matcher : entry.getValue()) {
  81. ITileOverride override = (ITileOverride) matcher.getData();
  82. if (override.getRenderPass() >= 0) {
  83. RenderPassAPI.instance.setRenderPassForBlock(entry.getKey(), override.getRenderPass());
  84. }
  85. }
  86. }
  87. for (List<BlockStateMatcher> overrides : blockOverrides.values()) {
  88. Collections.sort(overrides, new Comparator<BlockStateMatcher>() {
  89. @Override
  90. public int compare(BlockStateMatcher m1, BlockStateMatcher m2) {
  91. ITileOverride o1 = (ITileOverride) m1.getData();
  92. ITileOverride o2 = (ITileOverride) m2.getData();
  93. return o1.compareTo(o2);
  94. }
  95. });
  96. }
  97. for (List<ITileOverride> overrides : tileOverrides.values()) {
  98. Collections.sort(overrides);
  99. }
  100. setBlankResource();
  101. }
  102. });
  103. }
  104. private static void clearBlockFace() {
  105. haveBlockFace = false;
  106. }
  107. public static Icon getBlockIcon(Icon icon, RenderBlocks renderBlocks, Block block, IBlockAccess blockAccess, int i, int j, int k, int face) {
  108. lastOverride = null;
  109. if (blockAccess != null && checkFace(face)) {
  110. if (!haveBlockFace) {
  111. renderBlockState.setBlock(block, blockAccess, i, j, k);
  112. renderBlockState.setFace(face);
  113. }
  114. lastOverride = ijkIterator.go(renderBlockState, icon);
  115. if (lastOverride != null) {
  116. icon = ijkIterator.getIcon();
  117. }
  118. }
  119. clearBlockFace();
  120. return lastOverride == null && skipDefaultRendering(block) ? RenderBlocksUtils.blankIcon : icon;
  121. }
  122. public static Icon getBlockIcon(Icon icon, RenderBlocks renderBlocks, Block block, int face, int metadata) {
  123. lastOverride = null;
  124. if (checkFace(face) && checkRenderType(block)) {
  125. renderBlockState.setBlockMetadata(block, metadata, face);
  126. lastOverride = metadataIterator.go(renderBlockState, icon);
  127. if (lastOverride != null) {
  128. icon = metadataIterator.getIcon();
  129. }
  130. }
  131. return icon;
  132. }
  133. public static Icon getBlockIcon(Icon icon, RenderBlocks renderBlocks, Block block, int face) {
  134. return getBlockIcon(icon, renderBlocks, block, face, 0);
  135. }
  136. public static void reset() {
  137. }
  138. private static boolean checkFace(int face) {
  139. return face < 0 ? enableNonStandard : enableStandard;
  140. }
  141. private static boolean checkRenderType(Block block) {
  142. switch (block.getRenderType()) {
  143. case 11: // fence
  144. case 21: // fence gate
  145. return false;
  146. default:
  147. return true;
  148. }
  149. }
  150. private static boolean skipDefaultRendering(Block block) {
  151. return RenderPassAPI.instance.skipDefaultRendering(block);
  152. }
  153. private static void registerOverride(ITileOverride override) {
  154. if (override != null && !override.isDisabled()) {
  155. boolean registered = false;
  156. List<BlockStateMatcher> matchingBlocks = override.getMatchingBlocks();
  157. if (!MCPatcherUtils.isNullOrEmpty(matchingBlocks)) {
  158. for (BlockStateMatcher matcher : matchingBlocks) {
  159. if (matcher == null) {
  160. continue;
  161. }
  162. Block block = matcher.getBlock();
  163. List<BlockStateMatcher> list = blockOverrides.get(block);
  164. if (list == null) {
  165. list = new ArrayList<BlockStateMatcher>();
  166. blockOverrides.put(block, list);
  167. }
  168. list.add(matcher);
  169. logger.fine("using %s for block %s", override, BlockAPI.getBlockName(block));
  170. registered = true;
  171. }
  172. }
  173. Set<String> matchingTiles = override.getMatchingTiles();
  174. if (!MCPatcherUtils.isNullOrEmpty(matchingTiles)) {
  175. for (String name : matchingTiles) {
  176. List<ITileOverride> list = tileOverrides.get(name);
  177. if (list == null) {
  178. list = new ArrayList<ITileOverride>();
  179. tileOverrides.put(name, list);
  180. }
  181. list.add(override);
  182. logger.fine("using %s for tile %s", override, name);
  183. registered = true;
  184. }
  185. }
  186. if (registered) {
  187. allOverrides.add(override);
  188. }
  189. }
  190. }
  191. public static void setBlankResource() {
  192. RenderBlocksUtils.blankIcon = tileLoader.getIcon(RenderPassAPI.instance.getBlankResource());
  193. }
  194. public static TileOverrideIterator.IJK newIJKIterator() {
  195. return new TileOverrideIterator.IJK(blockOverrides, tileOverrides);
  196. }
  197. public static TileOverrideIterator.Metadata newMetadataIterator() {
  198. return new TileOverrideIterator.Metadata(blockOverrides, tileOverrides);
  199. }
  200. }