    001/**
    002 * This software is provided under the terms of the Minecraft Forge Public
    003 * License v1.0.
    004 */
    005
    006package net.minecraftforge.client;
    007
    008import org.lwjgl.opengl.Display;
    009
    010import net.minecraft.block.Block;
    011import net.minecraft.entity.Entity;
    012import net.minecraft.item.Item;
    013import net.minecraft.item.ItemStack;
    014import net.minecraft.client.renderer.RenderBlocks;
    015import net.minecraft.world.World;
    016import net.minecraftforge.client.IItemRenderer.ItemRenderType;
    017import net.minecraftforge.common.MinecraftForge;
    018
    019public class MinecraftForgeClient
    020{
    021    /** Register a new render context handler.  A render context is a block
    022     * of rendering performed with similar OpenGL modes, for example,
    023     * texture name.
    024     * @param texture The name of the texture for this render context.
    025     * @param subid The subid of this render context.  0 is the default pass
    026     * for normal rendering, higher subids render later.  All subids of 0
    027     * will render before all subids of 1, etc.
    028     * @param handler The handler to register.
    029     */
    030    public static void registerRenderContextHandler(String texture, int subid, IRenderContextHandler handler)
    031    {
    032        ForgeHooksClient.registerRenderContextHandler(texture, subid, handler);
    033    }
    034
    035    /**
    036     * Preload a texture.  Textures must be preloaded before the first
    037     * use, or they will cause visual anomalies.
    038     */
    039    public static void preloadTexture(String texture)
    040    {
    041        ForgeHooksClient.engine().getTexture(texture);
    042    }
    043
    044    /** Render a block.  Render a block which may have a custom texture.
    045     */
    046    public static void renderBlock(RenderBlocks render, Block block, int x, int y, int z)
    047    {
    048        ForgeHooksClient.beforeBlockRender(block, render);
    049        render.renderBlockByRenderType(block, x, y, z);
    050        ForgeHooksClient.afterBlockRender(block, render);
    051    }
    052
    053    /**
    054     * Get the current render pass.
    055     */
    056    public static int getRenderPass()
    057    {
    058        return ForgeHooksClient.renderPass;
    059    }
    060
    061    private static IItemRenderer[] customItemRenderers = new IItemRenderer[Item.itemsList.length];
    062
    063    /**
    064     * Register a custom renderer for a specific item. This can be used to
    065     * render the item in-world as an EntityItem, when the item is equipped, or
    066     * when the item is in an inventory slot.
    067     * @param itemID The item ID (shifted index) to handle rendering.
    068     * @param renderer The IItemRenderer interface that handles rendering for
    069     * this item.
    070     */
    071    public static void registerItemRenderer(int itemID, IItemRenderer renderer)
    072    {
    073        customItemRenderers[itemID] = renderer;
    074    }
    075
    076    public static IItemRenderer getItemRenderer(ItemStack item, ItemRenderType type)
    077    {
    078        IItemRenderer renderer = customItemRenderers[item.itemID];
    079        if (renderer != null && renderer.handleRenderType(item, type))
    080        {
    081            return customItemRenderers[item.itemID];
    082        }
    083        return null;
    084    }
    085}