- 62
- 1
- 9
RenderHandler:
package com.example.examplemod.client.handler;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.capabilities.ICustomArmorInventory;
import com.example.examplemod.capabilities.ModCapabilities;
import com.example.examplemod.client.model.ModelCustomArmor;
import com.example.examplemod.item.armor.BaseArmor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.ModelPlayer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import static org.lwjgl.opengl.GL11.GL_LEQUAL;
@SideOnly(Side.CLIENT)
@Mod.EventBusSubscriber(value = Side.CLIENT, modid = ExampleMod.MODID)
public class RenderHandler {
private static final ModelCustomArmor ARMOR_MODEL = new ModelCustomArmor(0.0f);
@SubscribeEvent
public static void onRenderPlayerPost(RenderPlayerEvent.Post event) {
EntityPlayer player = event.getEntityPlayer();
float partialTicks = event.getPartialRenderTick();
ICustomArmorInventory invCap = player.getCapability(ModCapabilities.CUSTOM_ARMOR_INVENTORY, null);
if (invCap == null) return;
ModelPlayer playerModel = event.getRenderer().getMainModel();
GlStateManager.pushMatrix();
float eyeHeight = player.getEyeHeight();
GlStateManager.translate(0.0F, eyeHeight - 0.17F, 0.0F);
float renderYawOffset = interpolateRotation(
player.prevRenderYawOffset,
player.renderYawOffset,
partialTicks
);
GlStateManager.rotate(180.0F - renderYawOffset, 0.0F, 1.0F, 0.0F);
GlStateManager.scale(-1.0F, -1.0F, 1.0F);
float limbSwing = player.limbSwing - player.limbSwingAmount * (1.0F - partialTicks);
float limbSwingAmount = player.prevLimbSwingAmount + (player.limbSwingAmount - player.prevLimbSwingAmount) * partialTicks;
float rotationYawHead = interpolateRotation(
player.prevRotationYawHead,
player.rotationYawHead,
partialTicks
);
float headYaw = rotationYawHead - renderYawOffset;
headYaw = MathHelper.wrapDegrees(headYaw);
float headPitch = player.prevRotationPitch + (player.rotationPitch - player.prevRotationPitch) * partialTicks;
float ageInTicks = player.ticksExisted + partialTicks;
if (player.isPlayerSleeping()) {
headYaw = 0.0F;
headPitch = 0.0F;
}
GlStateManager.enablePolygonOffset();
GlStateManager.doPolygonOffset(-6.5F, -7.0F);
GlStateManager.enableBlend();
GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
GlStateManager.depthMask(true);
GlStateManager.enableDepth();
GlStateManager.depthFunc(515);
for (int i = 0; i < invCap.getInventory().getSizeInventory(); i++) {
ItemStack stack = invCap.getInventory().getStackInSlot(i);
if (!stack.isEmpty() && stack.getItem() instanceof BaseArmor) {
BaseArmor armor = (BaseArmor) stack.getItem();
if (armor.getArmorSlot().ordinal() != i) continue;
Minecraft.getMinecraft().getTextureManager().bindTexture(
new ResourceLocation(armor.getTexturePath())
);
ARMOR_MODEL.setModelAttributes(playerModel);
syncWithPlayerModel(playerModel, player, partialTicks);
ARMOR_MODEL.render(
player,
limbSwing,
limbSwingAmount,
ageInTicks,
headYaw,
headPitch,
0.0625F
);
}
}
GlStateManager.depthFunc(515);
GlStateManager.disableBlend();
GlStateManager.disablePolygonOffset();
GlStateManager.popMatrix();
}
private static void syncWithPlayerModel(ModelPlayer playerModel, EntityPlayer player, float partialTicks) {
playerModel.swingProgress = player.getSwingProgress(partialTicks);
playerModel.isSneak = player.isSneaking();
playerModel.isRiding = player.isRiding();
}
private static float interpolateRotation(float prevYaw, float yaw, float partialTicks) {
float difference = yaw - prevYaw;
while (difference < -180.0F) {
difference += 360.0F;
}
while (difference >= 180.0F) {
difference -= 360.0F;
}
return prevYaw + difference * partialTicks;
}
}
ModelCustomArmor:
package com.example.examplemod.client.model;
import net.minecraft.client.model.ModelBiped;
import net.minecraft.client.model.ModelPlayer;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.util.EnumHandSide;
public class ModelCustomArmor extends ModelBiped {
private EnumHandSide swingingHand;
public ModelCustomArmor(float modelSize) {
super(modelSize, 0.0F, 64, 32);
}
public ModelCustomArmor(float modelSize, float yOffset, int textureWidth, int textureHeight) {
super(modelSize, yOffset, textureWidth, textureHeight);
}
@Override
public void setRotationAngles(float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch, float scaleFactor, Entity entityIn) {
super.setRotationAngles(limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch, scaleFactor, entityIn);
}
public void setModelAttributes(ModelBiped sourceModel) {
super.setModelAttributes(sourceModel);
this.leftArmPose = sourceModel.leftArmPose;
this.rightArmPose = sourceModel.rightArmPose;
if (sourceModel instanceof ModelPlayer) {
try {
java.lang.reflect.Field swingingHandField = ModelPlayer.class.getDeclaredField("swingingHand");
swingingHandField.setAccessible(true);
EnumHandSide handSide = (EnumHandSide) swingingHandField.get(sourceModel);
this.swingingHand = handSide;
} catch (Exception e) {
this.swingingHand = EnumHandSide.RIGHT;
}
}
copyModelPositions(sourceModel.bipedHead, this.bipedHead);
copyModelPositions(sourceModel.bipedBody, this.bipedBody);
copyModelPositions(sourceModel.bipedRightArm, this.bipedRightArm);
copyModelPositions(sourceModel.bipedLeftArm, this.bipedLeftArm);
copyModelPositions(sourceModel.bipedRightLeg, this.bipedRightLeg);
copyModelPositions(sourceModel.bipedLeftLeg, this.bipedLeftLeg);
copyModelPositions(sourceModel.bipedHeadwear, this.bipedHeadwear);
}
private void copyModelPositions(ModelRenderer source, ModelRenderer dest) {
dest.rotateAngleX = source.rotateAngleX;
dest.rotateAngleY = source.rotateAngleY;
dest.rotateAngleZ = source.rotateAngleZ;
dest.rotationPointX = source.rotationPointX;
dest.rotationPointY = source.rotationPointY;
dest.rotationPointZ = source.rotationPointZ;
dest.offsetX = source.offsetX;
dest.offsetY = source.offsetY;
dest.offsetZ = source.offsetZ;
}
}