Changes

3,354 bytes added ,  10:45, 4 September 2022
no edit summary
Line 32: Line 32:     
== <tt>Implementing first person Item Animation</tt> ==
 
== <tt>Implementing first person Item Animation</tt> ==
In order to implmement first person Item Animation in IClientItemExtensions, override <code>#applyForgeHandTransform</code>
+
In order to implmement first person Item Animation in <code>IClientItemExtensions</code>, override <code>#applyForgeHandTransform</code>
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
IClientItemExtensions extension = new IClientItemExtensions() {
 
IClientItemExtensions extension = new IClientItemExtensions() {
 
     @Override
 
     @Override
 
     public boolean applyForgeHandTransform(PoseStack poseStack, LocalPlayer player, HumanoidArm arm, ItemStack itemInHand, float partialTick, float equipProcess, float swingProcess) {
 
     public boolean applyForgeHandTransform(PoseStack poseStack, LocalPlayer player, HumanoidArm arm, ItemStack itemInHand, float partialTick, float equipProcess, float swingProcess) {
     // Returning true in this method will prevent this item stack from being handled by vanilla rendering system.
+
     // Returning true in this method is recommended as it will prevent this item stack from being handled by vanilla rendering system, preventing additional unwanted transformations from being applied.
 
     }
 
     }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
The <code>applyForgeHandTransform</code> method's parameters are for:
 
The <code>applyForgeHandTransform</code> method's parameters are for:
* <code><nowiki>PoseStack poseStack</nowiki></code>: For stacking matrix poses. You can translate, rotate, scale item with it.
+
* <code><nowiki>PoseStack poseStack</nowiki></code>: For stacking matrix poses. You can translate, rotate, scale item model with it.
 
* <code><nowiki>LocalPlayer player</nowiki></code>: Main client player holding the item.
 
* <code><nowiki>LocalPlayer player</nowiki></code>: Main client player holding the item.
 
* <code><nowiki>HumanoidArm arm</nowiki></code>: For distinguishing which arm is holding the item. It's either <code>#LEFT</code> or <code>#RIGHT</code>
 
* <code><nowiki>HumanoidArm arm</nowiki></code>: For distinguishing which arm is holding the item. It's either <code>#LEFT</code> or <code>#RIGHT</code>
* <code><nowiki>ItemStack itemInHand</nowiki></code>: <code>ItemStack</code> that player is holding. Its <code>Item</code> is always the custom <code>Item</code> that you're defining currently.
+
* <code><nowiki>ItemStack itemInHand</nowiki></code>: <code>ItemStack</code> that player is holding. Its <code>Item</code> is always the custom <code>Item</code> that you're implementing animation for.
 
* <code><nowiki>float partialTick</nowiki></code>: Value ranging from 0.0 to 1.0 for telling how much time has passed since last tick. If it's close to 0.0, it means current tick just started. If it's close to 1.0, it means current tick is almost over.
 
* <code><nowiki>float partialTick</nowiki></code>: Value ranging from 0.0 to 1.0 for telling how much time has passed since last tick. If it's close to 0.0, it means current tick just started. If it's close to 1.0, it means current tick is almost over.
* <code><nowiki>float equipProcess</nowiki></code>: Value ranging from 0.0 to 1.0. If it's close to 0.0, It's close to 0.0 when the sword gauge bar is full. It's close to 1.0, when sword gauge bar is low. [[File:That_short_gage_thing_under_cross.png]]
+
* <code><nowiki>float equipProcess</nowiki></code>: Value ranging from 0.0 to 1.0. It's close to 0.0 when the sword gauge bar is full. It's close to 1.0 when sword gauge bar is low. [[File:That_short_gage_thing_under_cross.png]]
 
* <code><nowiki>float swingProcess</nowiki></code>: Value ranging from 0.0 to 1.0 for telling animation progress. If it's exactly at 0.0, the animation is not playing. If it's just close to 0.0, animation has just started playing. If it's close to 1.0, the animation is almost over.
 
* <code><nowiki>float swingProcess</nowiki></code>: Value ranging from 0.0 to 1.0 for telling animation progress. If it's exactly at 0.0, the animation is not playing. If it's just close to 0.0, animation has just started playing. If it's close to 1.0, the animation is almost over.
   Line 59: Line 59:  
             int i = arm == HumanoidArm.RIGHT ? 1 : -1;
 
             int i = arm == HumanoidArm.RIGHT ? 1 : -1;
 
             poseStack.translate(i * 0.56F, -0.52F, -0.72F);
 
             poseStack.translate(i * 0.56F, -0.52F, -0.72F);
            applyItemArmTransform(poseStack, arm);
+
            if (player.getUseItem() == itemInHand && player.isUsingItem()) {
            if (player.getUseItem() != itemInHand) {
+
                 poseStack.translate(0.0, -0.05, 0.0);
                 return true;
   
             }
 
             }
            if (player.isUsingItem()) {
+
            return true;
 +
        }
 +
    });
 +
}
 +
</syntaxhighlight>
 +
 
 +
== <tt>Implementing third person Item Animation</tt> ==
 +
Implementing third person Item Animation is done via defining custom <code>HumanoidModel$ArmPose</code> and returning it in <code>IClientItemExtensions#getArmPose</code>.
 +
 
 +
=== Defining custom ArmPose ===
 +
Forge makes <code>HumanoidModel$ArmPose</code> enum extensible, allowing you to create custom entries using <code>HumanoidModel$ArmPose#create</code>.
 +
<syntaxhighlight lang="java">
 +
private static final HumanoidModel.ArmPose EXAMPLE_POSE = HumanoidModel.ArmPose.create("EXAMPLE", false, (model, entity, arm) -> {
 +
    if (arm == HumanoidArm.RIGHT) {
 +
        model.rightArm.xRot = (float) (Math.random() * Math.PI * 2);
 +
    } else {
 +
        model.leftArm.xRot = (float) (Math.random() * Math.PI * 2);
 +
    }
 +
});
 +
</syntaxhighlight>
 +
<code>create</code> takes in 3 arguments:
 +
* <code><nowiki>String name</nowiki></code>: Name of the custom pose.
 +
* <code><nowiki>boolean twoHanded</nowiki></code>: Whether or not the animation should be two handed or single handed.
 +
* <code><nowiki>IArmPoseTransformer forgeArmPose</nowiki></code>: Functional interface for custom arm rendering logic.
 +
 
 +
after that, override <code>IClientItemExtensions#getArmPose</code> so that it returns newly created enum entry.
 +
<syntaxhighlight lang="java">
 +
@Override
 +
public HumanoidModel.ArmPose getArmPose(LivingEntity entityLiving, InteractionHand hand, ItemStack itemStack) {
 +
    if (!itemStack.isEmpty()) {
 +
        if (entityLiving.getUsedItemHand() == hand && entityLiving.getUseItemRemainingTicks() > 0) {
 +
            return EXAMPLE_POSE;
 +
        }
 +
    }
 +
    return HumanoidModel.ArmPose.EMPTY;
 +
}
 +
</syntaxhighlight>
 +
It has additional check to ensure only play custom arm rendering logic when player is actually using this item.
 +
 
 +
== Tying it all up ==
 +
Combining both examples into single <code>IClientItemExtensions</code> would look like this:
 +
 
 +
<syntaxhighlight lang="java">
 +
@Override
 +
public void initializeClient(Consumer<IClientItemExtensions> consumer)
 +
{
 +
    consumer.accept(new IClientItemExtensions() {
 +
 
 +
        private static final HumanoidModel.ArmPose EXAMPLE_POSE = HumanoidModel.ArmPose.create("EXAMPLE", false, (model, entity, arm) -> {
 +
            if (arm == HumanoidArm.RIGHT) {
 +
                model.rightArm.xRot = (float) (Math.random() * Math.PI * 2);
 +
            } else {
 +
                model.leftArm.xRot = (float) (Math.random() * Math.PI * 2);
 +
            }
 +
        });
 +
 
 +
        @Override
 +
        public HumanoidModel.ArmPose getArmPose(LivingEntity entityLiving, InteractionHand hand, ItemStack itemStack) {
 +
            if (!itemStack.isEmpty()) {
 +
                if (entityLiving.getUsedItemHand() == hand && entityLiving.getUseItemRemainingTicks() > 0) {
 +
                    return EXAMPLE_POSE;
 +
                }
 +
            }
 +
            return HumanoidModel.ArmPose.EMPTY;
 +
        }
 +
 
 +
        @Override
 +
        public boolean applyForgeHandTransform(PoseStack poseStack, LocalPlayer player, HumanoidArm arm, ItemStack itemInHand, float partialTick, float equipProcess, float swingProcess) {
 +
            int i = arm == HumanoidArm.RIGHT ? 1 : -1;
 +
            poseStack.translate(i * 0.56F, -0.52F, -0.72F);
 +
            if (player.getUseItem() == itemInHand && player.isUsingItem()) {
 
                 poseStack.translate(0.0, -0.05, 0.0);
 
                 poseStack.translate(0.0, -0.05, 0.0);
 
             }
 
             }
17

edits