[1.19.x] Custom usage animations for items (#8932)

This commit is contained in:
한현빈 authored and GitHub committed 2022-08-17 23:08:13 +02:00
1 parent 8998fa71b2
commit f0ecf16c6b
8 files changed
+270 -1

No files matched your search

@@ -0,0 +1,34 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.client;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
/**
* An ArmPose that can be defined by the user.
* Register one by creating a custom {@link net.minecraft.client.model.HumanoidModel.ArmPose}
* and returning it in {@link IClientItemExtensions#getArmPose(LivingEntity, InteractionHand, ItemStack)}.
*/
@FunctionalInterface
public interface IArmPoseTransformer
{
/**
* This method should be used to apply all wanted transformations to the player when the ArmPose is active.
* You can use {@link LivingEntity#getTicksUsingItem()} and {@link LivingEntity#getUseItemRemainingTicks()} for moving animations.
*
* @param model The humanoid model
* @param entity The humanoid entity
* @param arm Arm to pose
*/
void applyTransform(HumanoidModel<?> model, LivingEntity entity, HumanoidArm arm);
}
@@ -5,19 +5,24 @@
package net.minecraftforge.client.extensions.common;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.Model;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.IArmPoseTransformer;
import net.minecraftforge.fml.LogicalSide;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -57,6 +62,37 @@ public interface IClientItemExtensions
return null;
}
/**
* This method returns an ArmPose that can be defined using the {@link net.minecraft.client.model.HumanoidModel.ArmPose#create(String, boolean, IArmPoseTransformer)} method.
* This allows for creating custom item use animations.
*
* @param entityLiving The entity holding the item
* @param hand The hand the ArmPose will be applied to
* @param itemStack The stack being held
* @return A custom ArmPose that can be used to define movement of the arm
*/
@Nullable
default HumanoidModel.ArmPose getArmPose(LivingEntity entityLiving, InteractionHand hand, ItemStack itemStack)
{
return null;
}
/**
* Called right before when client applies transformations to item in hand and render it.
*
* @param poseStack The pose stack
* @param player The player holding the item, it's always main client player
* @param arm The arm holding the item
* @param itemInHand The held item
* @param partialTick Partial tick time, useful for interpolation
* @param equipProcess Equip process time, Ranging from 0.0 to 1.0. 0.0 when it's done equipping
* @param swingProcess Swing process time, Ranging from 0.0 to 1.0. 0.0 when it's done swinging
* @return true if it should skip applying other transforms and go straight to rendering
*/
default boolean applyForgeHandTransform(PoseStack poseStack, LocalPlayer player, HumanoidArm arm, ItemStack itemInHand, float partialTick, float equipProcess, float swingProcess) {
return false;
}
/**
* Queries the humanoid armor model for this item when it's equipped.
*
@@ -0,0 +1,109 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug.entity.player;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.UseAnim;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import java.util.function.Consumer;
/**
* Tests if item usage animation system works as intended. `item_use_animation_test:thing` is edible item with custom usage animation made with this system.
* In game, use `/give @s item_use_animation_test:thing 1` to obtain test item
* When you try to eat it, your arm in 3d person should start swinging really fast.
* And item in your hand will go down little.
*/
@Mod(ItemUseAnimationTest.MOD_ID)
public class ItemUseAnimationTest
{
public static final String MOD_ID = "item_use_animation_test";
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID);
private static final RegistryObject<Item> THING = ITEMS.register("thing", () -> new ThingItem(new Item.Properties().tab(CreativeModeTab.TAB_TOOLS).food(new FoodProperties.Builder().nutrition(4).alwaysEat().build())));
public ItemUseAnimationTest()
{
ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
}
private static class ThingItem extends Item
{
public ThingItem(Item.Properties props)
{
super(props);
}
@Override
public UseAnim getUseAnimation(ItemStack stack)
{
return UseAnim.CUSTOM;
}
@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer)
{
consumer.accept(new IClientItemExtensions()
{
private static final HumanoidModel.ArmPose SWING_POSE = HumanoidModel.ArmPose.create("SWING", 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 SWING_POSE;
}
}
return HumanoidModel.ArmPose.EMPTY;
}
@Override
public boolean applyForgeHandTransform(PoseStack poseStack, LocalPlayer player, HumanoidArm arm, ItemStack itemInHand, float partialTick, float equipProcess, float swingProcess) {
applyItemArmTransform(poseStack, arm);
if (player.isUsingItem()) {
poseStack.translate(0.0, -0.05, 0.0);
}
return true;
}
private void applyItemArmTransform(PoseStack poseStack, HumanoidArm arm) {
int i = arm == HumanoidArm.RIGHT ? 1 : -1;
poseStack.translate(i * 0.56F, -0.52F, -0.72F);
}
});
}
}
}
+2
View File
@@ -245,6 +245,8 @@ modId="emissive_elements_test"
[[mods]]
modId="anvil_update_event_fix"
[[mods]]
modId="item_use_animation_test"
[[mods]]
modId="custom_color_resolver_test"