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