    001    package cpw.mods.fml.client.registry;
    002
    003    import java.util.List;
    004    import java.util.Map;
    005
    006    import net.minecraft.src.Block;
    007    import net.minecraft.src.Entity;
    008    import net.minecraft.src.IBlockAccess;
    009    import net.minecraft.src.Render;
    010    import net.minecraft.src.RenderBlocks;
    011    import net.minecraft.src.RenderManager;
    012    import net.minecraft.src.RenderPlayer;
    013
    014    import com.google.common.collect.Lists;
    015    import com.google.common.collect.Maps;
    016    import com.google.common.collect.ObjectArrays;
    017
    018    import cpw.mods.fml.client.SpriteHelper;
    019    import cpw.mods.fml.client.TextureFXManager;
    020
    021    /**
    022     * @author cpw
    023     *
    024     */
    025    public class RenderingRegistry
    026    {
    027        private static final RenderingRegistry INSTANCE = new RenderingRegistry();
    028
    029        private int nextRenderId = 32;
    030
    031        private Map<Integer, ISimpleBlockRenderingHandler> blockRenderers = Maps.newHashMap();
    032
    033        private List<EntityRendererInfo> entityRenderers = Lists.newArrayList();
    034
    035        /**
    036         * Add a new armour prefix to the RenderPlayer
    037         *
    038         * @param armor
    039         */
    040        public static int addNewArmourRendererPrefix(String armor)
    041        {
    042            RenderPlayer.armorFilenamePrefix = ObjectArrays.concat(RenderPlayer.armorFilenamePrefix, armor);
    043            return RenderPlayer.armorFilenamePrefix.length - 1;
    044        }
    045
    046        /**
    047         * Register an entity rendering handler. This will, after mod initialization, be inserted into the main
    048         * render map for entities
    049         *
    050         * @param entityClass
    051         * @param renderer
    052         */
    053        public static void registerEntityRenderingHandler(Class<? extends Entity> entityClass, Render renderer)
    054        {
    055            instance().entityRenderers.add(new EntityRendererInfo(entityClass, renderer));
    056        }
    057
    058        /**
    059         * Register a simple block rendering handler
    060         *
    061         * @param handler
    062         */
    063        public static void registerBlockHandler(ISimpleBlockRenderingHandler handler)
    064        {
    065            instance().blockRenderers.put(handler.getRenderId(), handler);
    066        }
    067
    068        /**
    069         * Register the simple block rendering handler
    070         * This version will not call getRenderId on the passed in handler, instead using the supplied ID, so you
    071         * can easily re-use the same rendering handler for multiple IDs
    072         *
    073         * @param renderId
    074         * @param handler
    075         */
    076        public static void registerBlockHandler(int renderId, ISimpleBlockRenderingHandler handler)
    077        {
    078            instance().blockRenderers.put(renderId, handler);
    079        }
    080        /**
    081         * Get the next available renderId from the block render ID list
    082         */
    083        public static int getNextAvailableRenderId()
    084        {
    085            return instance().nextRenderId++;
    086        }
    087
    088        /**
    089         * Add a texture override for the given path and return the used index
    090         *
    091         * @param fileToOverride
    092         * @param fileToAdd
    093         */
    094        public static int addTextureOverride(String fileToOverride, String fileToAdd)
    095        {
    096            int idx = SpriteHelper.getUniqueSpriteIndex(fileToOverride);
    097            addTextureOverride(fileToOverride, fileToAdd, idx);
    098            return idx;
    099        }
    100
    101        /**
    102         * Add a texture override for the given path and index
    103         *
    104         * @param path
    105         * @param overlayPath
    106         * @param index
    107         */
    108        public static void addTextureOverride(String path, String overlayPath, int index)
    109        {
    110            TextureFXManager.instance().addNewTextureOverride(path, overlayPath, index);
    111        }
    112
    113        /**
    114         * Get and reserve a unique texture index for the supplied path
    115         *
    116         * @param path
    117         */
    118        public static int getUniqueTextureIndex(String path)
    119        {
    120            return SpriteHelper.getUniqueSpriteIndex(path);
    121        }
    122
    123        @Deprecated public static RenderingRegistry instance()
    124        {
    125            return INSTANCE;
    126        }
    127
    128        private static class EntityRendererInfo
    129        {
    130            public EntityRendererInfo(Class<? extends Entity> target, Render renderer)
    131            {
    132                this.target = target;
    133                this.renderer = renderer;
    134            }
    135            private Class<? extends Entity> target;
    136            private Render renderer;
    137        }
    138
    139        public boolean renderWorldBlock(RenderBlocks renderer, IBlockAccess world, int x, int y, int z, Block block, int modelId)
    140        {
    141            if (!blockRenderers.containsKey(modelId)) { return false; }
    142            ISimpleBlockRenderingHandler bri = blockRenderers.get(modelId);
    143            return bri.renderWorldBlock(world, x, y, z, block, modelId, renderer);
    144        }
    145
    146        public void renderInventoryBlock(RenderBlocks renderer, Block block, int metadata, int modelID)
    147        {
    148            if (!blockRenderers.containsKey(modelID)) { return; }
    149            ISimpleBlockRenderingHandler bri = blockRenderers.get(modelID);
    150            bri.renderInventoryBlock(block, metadata, modelID, renderer);
    151        }
    152
    153        public boolean renderItemAsFull3DBlock(int modelId)
    154        {
    155            ISimpleBlockRenderingHandler bri = blockRenderers.get(modelId);
    156            return bri != null && bri.shouldRender3DInInventory();
    157        }
    158
    159        public void loadEntityRenderers(Map<Class<? extends Entity>, Render> rendererMap)
    160        {
    161            for (EntityRendererInfo info : entityRenderers)
    162            {
    163                rendererMap.put(info.target, info.renderer);
    164                info.renderer.setRenderManager(RenderManager.instance);
    165            }
    166        }
    167    }