Difference between revisions of "Custom Item Animations"

From Forge Community Wiki
(Created page with "{{Under construction}} Custom Item animation allows items to have both 3rd person and 1st person animation. This is done by consuming custom client extension with custom rende...")
(No difference)

Revision as of 11:38, 3 September 2022

This page is under construction.

This page is incomplete, and needs more work. Feel free to edit and improve this page!

Custom Item animation allows items to have both 3rd person and 1st person animation. This is done by consuming custom client extension with custom rendering logic in Item#initializeClient.

Defining custom client extension

First, create a implementation of IClientItemExtensions. It can be either anonymous or named.

IClientItemExtensions extension = new IClientItemExtensions() {
// .....
}

An instance of it should be consumed in Item#initializeClient like this:

@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer) {
    consumer.accept(extension);
}

Or you can define and consume implementation at same time:

@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer) {
    consumer.accept(new IClientItemExtensions() {
    // .....
    });
}

Implementing first person Item Animation

In order to implmement first person Item Animation in IClientItemExtensions, override #applyForgeHandTransform

IClientItemExtensions extension = new IClientItemExtensions() {
    @Override
    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.
    }
}

The applyForgeHandTransform method's parameters are for:

  • PoseStack poseStack: For stacking matrix poses. You can translate, rotate, scale item with it.
  • LocalPlayer player: Main client player holding the item.
  • HumanoidArm arm: For distinguishing which arm is holding the item. It's either #LEFT or #RIGHT
  • ItemStack itemInHand: ItemStack that player is holding. Its Item is always the custom Item that you're defining currently.
  • float partialTick: 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.
  • float equipProcess: 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.
    Error creating thumbnail: Unable to save thumbnail to destination
  • float swingProcess: 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.

Here's example implementation: @Override public void initializeClient(Consumer<IClientItemExtensions> consumer) {

   consumer.accept(new IClientItemExtensions() {
       @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);
           applyItemArmTransform(poseStack, arm);
           if (player.getUseItem() != itemInHand) {
               return true;
           }
            if (player.isUsingItem()) {
               poseStack.translate(0.0, -0.05, 0.0);
           }
           return true;
       }
   });

}