Changes

3,859 bytes added ,  11:38, 3 September 2022
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..."
{{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 rendering logic in <code>Item#initializeClient</code>.

== Defining custom client extension ==
First, create a implementation of <code>IClientItemExtensions</code>. It can be either anonymous or named.

<syntaxhighlight lang="java">
IClientItemExtensions extension = new IClientItemExtensions() {
// .....
}
</syntaxhighlight>

An instance of it should be consumed in <code>Item#initializeClient</code> like this:

<syntaxhighlight lang="java">
@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer) {
consumer.accept(extension);
}
</syntaxhighlight>

Or you can define and consume implementation at same time:

<syntaxhighlight lang="java">
@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer) {
consumer.accept(new IClientItemExtensions() {
// .....
});
}
</syntaxhighlight>

== <tt>Implementing first person Item Animation</tt> ==
In order to implmement first person Item Animation in IClientItemExtensions, override <code>#applyForgeHandTransform</code>
<syntaxhighlight lang="java">
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.
}
}
</syntaxhighlight>
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>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>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>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 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.

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;
}
});
}

[[Category:Items]]
17

edits