001package net.minecraftforge.client; 002 003import java.util.HashMap; 004import java.util.Random; 005import java.util.TreeSet; 006 007import org.lwjgl.opengl.GL11; 008import org.lwjgl.opengl.GL12; 009 010import cpw.mods.fml.client.FMLClientHandler; 011 012import net.minecraft.client.Minecraft; 013import net.minecraft.block.Block; 014import net.minecraft.entity.item.EntityItem; 015import net.minecraft.entity.EntityLiving; 016import net.minecraft.entity.player.EntityPlayer; 017import net.minecraft.client.texturepacks.ITexturePack; 018import net.minecraft.item.Item; 019import net.minecraft.item.ItemBlock; 020import net.minecraft.item.ItemStack; 021import net.minecraft.util.MathHelper; 022import net.minecraft.util.MovingObjectPosition; 023import net.minecraft.client.renderer.RenderBlocks; 024import net.minecraft.client.renderer.RenderEngine; 025import net.minecraft.client.renderer.RenderGlobal; 026import net.minecraft.client.renderer.Tessellator; 027import net.minecraft.client.renderer.entity.RenderItem; 028import net.minecraftforge.client.event.DrawBlockHighlightEvent; 029import net.minecraftforge.client.event.RenderWorldLastEvent; 030import net.minecraftforge.client.event.TextureLoadEvent; 031import net.minecraftforge.common.IArmorTextureProvider; 032import net.minecraftforge.common.MinecraftForge; 033import static net.minecraftforge.client.IItemRenderer.ItemRenderType.*; 034import static net.minecraftforge.client.IItemRenderer.ItemRendererHelper.*; 035 036public class ForgeHooksClient 037{ 038 static RenderEngine engine() 039 { 040 return FMLClientHandler.instance().getClient().renderEngine; 041 } 042 043 public static String getArmorTexture(ItemStack armor, String _default) 044 { 045 if (armor.getItem() instanceof IArmorTextureProvider) 046 { 047 return ((IArmorTextureProvider)armor.getItem()).getArmorTextureFile(armor); 048 } 049 return _default; 050 } 051 052 public static boolean renderEntityItem(EntityItem entity, ItemStack item, float bobing, float rotation, Random random, RenderEngine engine, RenderBlocks renderBlocks) 053 { 054 IItemRenderer customRenderer = MinecraftForgeClient.getItemRenderer(item, ENTITY); 055 if (customRenderer == null) 056 { 057 return false; 058 } 059 060 if (customRenderer.shouldUseRenderHelper(ENTITY, item, ENTITY_ROTATION)) 061 { 062 GL11.glRotatef(rotation, 0.0F, 1.0F, 0.0F); 063 } 064 if (!customRenderer.shouldUseRenderHelper(ENTITY, item, ENTITY_BOBBING)) 065 { 066 GL11.glTranslatef(0.0F, -bobing, 0.0F); 067 } 068 boolean is3D = customRenderer.shouldUseRenderHelper(ENTITY, item, BLOCK_3D); 069 070 if (item.getItem() instanceof ItemBlock && (is3D || RenderBlocks.renderItemIn3d(Block.blocksList[item.itemID].getRenderType()))) 071 { 072 engine.func_98187_b("/terrain.png"); 073 int renderType = Block.blocksList[item.itemID].getRenderType(); 074 float scale = (renderType == 1 || renderType == 19 || renderType == 12 || renderType == 2 ? 0.5F : 0.25F); 075 076 if (RenderItem.field_82407_g) 077 { 078 GL11.glScalef(1.25F, 1.25F, 1.25F); 079 GL11.glTranslatef(0.0F, 0.05F, 0.0F); 080 GL11.glRotatef(-90.0F, 0.0F, 1.0F, 0.0F); 081 } 082 083 GL11.glScalef(scale, scale, scale); 084 085 int size = item.stackSize; 086 int count = (size > 20 ? 4 : (size > 5 ? 3 : (size > 1 ? 2 : 1))); 087 088 for(int j = 0; j < count; j++) 089 { 090 GL11.glPushMatrix(); 091 if (j > 0) 092 { 093 GL11.glTranslatef( 094 ((random.nextFloat() * 2.0F - 1.0F) * 0.2F) / 0.5F, 095 ((random.nextFloat() * 2.0F - 1.0F) * 0.2F) / 0.5F, 096 ((random.nextFloat() * 2.0F - 1.0F) * 0.2F) / 0.5F); 097 } 098 customRenderer.renderItem(ENTITY, item, renderBlocks, entity); 099 GL11.glPopMatrix(); 100 } 101 } 102 else 103 { 104 engine.func_98187_b("/gui/items.png"); 105 GL11.glScalef(0.5F, 0.5F, 0.5F); 106 customRenderer.renderItem(ENTITY, item, renderBlocks, entity); 107 } 108 return true; 109 } 110 111 public static boolean renderInventoryItem(RenderBlocks renderBlocks, RenderEngine engine, ItemStack item, boolean inColor, float zLevel, float x, float y) 112 { 113 IItemRenderer customRenderer = MinecraftForgeClient.getItemRenderer(item, INVENTORY); 114 if (customRenderer == null) 115 { 116 return false; 117 } 118 119 engine.func_98187_b("/gui/items.png"); 120 if (customRenderer.shouldUseRenderHelper(INVENTORY, item, INVENTORY_BLOCK)) 121 { 122 GL11.glPushMatrix(); 123 GL11.glTranslatef(x - 2, y + 3, -3.0F + zLevel); 124 GL11.glScalef(10F, 10F, 10F); 125 GL11.glTranslatef(1.0F, 0.5F, 1.0F); 126 GL11.glScalef(1.0F, 1.0F, -1F); 127 GL11.glRotatef(210F, 1.0F, 0.0F, 0.0F); 128 GL11.glRotatef(45F, 0.0F, 1.0F, 0.0F); 129 130 if(inColor) 131 { 132 int color = Item.itemsList[item.itemID].getColorFromItemStack(item, 0); 133 float r = (float)(color >> 16 & 0xff) / 255F; 134 float g = (float)(color >> 8 & 0xff) / 255F; 135 float b = (float)(color & 0xff) / 255F; 136 GL11.glColor4f(r, g, b, 1.0F); 137 } 138 139 GL11.glRotatef(-90F, 0.0F, 1.0F, 0.0F); 140 renderBlocks.useInventoryTint = inColor; 141 customRenderer.renderItem(INVENTORY, item, renderBlocks); 142 renderBlocks.useInventoryTint = true; 143 GL11.glPopMatrix(); 144 } 145 else 146 { 147 GL11.glDisable(GL11.GL_LIGHTING); 148 GL11.glPushMatrix(); 149 GL11.glTranslatef(x, y, -3.0F + zLevel); 150 151 if (inColor) 152 { 153 int color = Item.itemsList[item.itemID].getColorFromItemStack(item, 0); 154 float r = (float)(color >> 16 & 255) / 255.0F; 155 float g = (float)(color >> 8 & 255) / 255.0F; 156 float b = (float)(color & 255) / 255.0F; 157 GL11.glColor4f(r, g, b, 1.0F); 158 } 159 160 customRenderer.renderItem(INVENTORY, item, renderBlocks); 161 GL11.glPopMatrix(); 162 GL11.glEnable(GL11.GL_LIGHTING); 163 } 164 return true; 165 } 166 167 public static void renderEquippedItem(IItemRenderer customRenderer, RenderBlocks renderBlocks, EntityLiving entity, ItemStack item) 168 { 169 if (customRenderer.shouldUseRenderHelper(EQUIPPED, item, EQUIPPED_BLOCK)) 170 { 171 GL11.glPushMatrix(); 172 GL11.glTranslatef(-0.5F, -0.5F, -0.5F); 173 customRenderer.renderItem(EQUIPPED, item, renderBlocks, entity); 174 GL11.glPopMatrix(); 175 } 176 else 177 { 178 GL11.glPushMatrix(); 179 GL11.glEnable(GL12.GL_RESCALE_NORMAL); 180 GL11.glTranslatef(0.0F, -0.3F, 0.0F); 181 GL11.glScalef(1.5F, 1.5F, 1.5F); 182 GL11.glRotatef(50.0F, 0.0F, 1.0F, 0.0F); 183 GL11.glRotatef(335.0F, 0.0F, 0.0F, 1.0F); 184 GL11.glTranslatef(-0.9375F, -0.0625F, 0.0F); 185 customRenderer.renderItem(EQUIPPED, item, renderBlocks, entity); 186 GL11.glDisable(GL12.GL_RESCALE_NORMAL); 187 GL11.glPopMatrix(); 188 } 189 } 190 191 //Optifine Helper Functions u.u, these are here specifically for Optifine 192 //Note: When using Optfine, these methods are invoked using reflection, which 193 //incurs a major performance penalty. 194 public static void orientBedCamera(Minecraft mc, EntityLiving entity) 195 { 196 int x = MathHelper.floor_double(entity.posX); 197 int y = MathHelper.floor_double(entity.posY); 198 int z = MathHelper.floor_double(entity.posZ); 199 Block block = Block.blocksList[mc.theWorld.getBlockId(x, y, z)]; 200 201 if (block != null && block.isBed(mc.theWorld, x, y, z, entity)) 202 { 203 int var12 = block.getBedDirection(mc.theWorld, x, y, z); 204 GL11.glRotatef((float)(var12 * 90), 0.0F, 1.0F, 0.0F); 205 } 206 } 207 208 public static boolean onDrawBlockHighlight(RenderGlobal context, EntityPlayer player, MovingObjectPosition target, int subID, ItemStack currentItem, float partialTicks) 209 { 210 return MinecraftForge.EVENT_BUS.post(new DrawBlockHighlightEvent(context, player, target, subID, currentItem, partialTicks)); 211 } 212 213 public static void dispatchRenderLast(RenderGlobal context, float partialTicks) 214 { 215 MinecraftForge.EVENT_BUS.post(new RenderWorldLastEvent(context, partialTicks)); 216 } 217 218 public static void onTextureLoad(String texture, ITexturePack pack) 219 { 220 MinecraftForge.EVENT_BUS.post(new TextureLoadEvent(texture, pack)); 221 } 222 223 /** 224 * This is added for Optifine's convenience. And to explode if a ModMaker is developing. 225 * @param texture 226 */ 227 public static void onTextureLoadPre(String texture) 228 { 229 if (Tessellator.renderingWorldRenderer) 230 { 231 String msg = String.format("Warning: Texture %s not preloaded, will cause render glitches!", texture); 232 System.out.println(msg); 233 if (Tessellator.class.getPackage() != null) 234 { 235 if (Tessellator.class.getPackage().getName().startsWith("net.minecraft.")) 236 { 237 Minecraft mc = FMLClientHandler.instance().getClient(); 238 if (mc.ingameGUI != null) 239 { 240 mc.ingameGUI.getChatGUI().printChatMessage(msg); 241 } 242 } 243 } 244 } 245 } 246 247 static int renderPass = -1; 248 public static void setRenderPass(int pass) 249 { 250 renderPass = pass; 251 } 252}