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