找到项目的build.gradle文件,在repositories和dependencies中添加依赖。
repositories {
//添加这个
maven { url 'https://dl.cloudsmith.io/public/geckolib3/geckolib/maven/' }
}
dependencies {
minecraft 'net.minecraftforge:forge:1.18.2-40.1.0'
//添加这个
implementation fg.deobf('software.bernie.geckolib:geckolib-1.18-forge:3.0.18')
}


Main.java
public Main() {
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
ItemInit.ITEMS.register(bus);
//添加GeckoLib初始化函数
GeckoLib.initialize();
MinecraftForge.EVENT_BUS.register(this);
}
GeckoLib Animation Utils,并下载这个插件
Convert Project,之后选择Geckolib Animated Model
Animate栏,点击进去:


HeisensuitArmorItemHeisensuitArmorItem.java
package com.joy187.re8joymod.items.armor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.joy187.re8joymod.Main;
import com.joy187.re8joymod.init.EffectInit;
import com.joy187.re8joymod.init.ItemInit;
import com.joy187.re8joymod.util.CustomArmorMaterial;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ArmorMaterial;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import software.bernie.geckolib3.core.IAnimatable;
import software.bernie.geckolib3.core.PlayState;
import software.bernie.geckolib3.core.builder.AnimationBuilder;
import software.bernie.geckolib3.core.controller.AnimationController;
import software.bernie.geckolib3.core.event.predicate.AnimationEvent;
import software.bernie.geckolib3.core.manager.AnimationData;
import software.bernie.geckolib3.core.manager.AnimationFactory;
import software.bernie.geckolib3.item.GeoArmorItem;
public class HeisensuitArmorItem extends GeoArmorItem implements IAnimatable{
private AnimationFactory factory = new AnimationFactory(this);
public HeisensuitArmorItem(ArmorMaterial materialIn, EquipmentSlot slot, Properties builder) {
super(materialIn, slot, builder.tab(Main.TUTORIAL_TAB));
}
//动画状态机,判断每个时刻我们的盔甲的动画状态
@SuppressWarnings("unused")
private PlayState predicate(AnimationEvent
event) {
// This is all the extradata this event carries. The livingentity is the entity
// that's wearing the armor. The itemstack and equipmentslottype are self
// explanatory.
List slotData = event.getExtraDataOfType(EquipmentSlot.class);
List stackData = event.getExtraDataOfType(ItemStack.class);
LivingEntity livingEntity = event.getExtraDataOfType(LivingEntity.class).get(0);
// Always loop the animation but later on in this method we'll decide whether or
// not to actually play it
event.getController().setAnimation(new AnimationBuilder().addAnimation("animation.heisensuit.idle", true));
// If the living entity is an armorstand just play the animation nonstop
if (livingEntity instanceof ArmorStand) {
return PlayState.CONTINUE;
}
// The entity is a player, so we want to only play if the player is wearing the
// full set of armor
else if (livingEntity instanceof Player) {
Player player = (Player) livingEntity;
// Get all the equipment, aka the armor, currently held item, and offhand item
List- equipmentList = new ArrayList<>();
player.getAllSlots().forEach((x) -> equipmentList.add(x.getItem()));
// 包含四个盔甲位置
List
- armorList = equipmentList.subList(2, 6);
//如果玩家穿上了所有的套装就会播放相应的动画
// Make sure the player is wearing all the armor. If they are, continue playing
// the animation, otherwise stop
//ItemInit.HEISEN_BOOTS.get(), ItemInit.HEISEN_LEGGINGS.get(),ItemInit.HEISEN_CHEST.get(), ItemInit.HEISEN_HEAD.get()
boolean isWearingAll = armorList
.containsAll(Arrays.asList(ItemInit.HEISEN_BOOTS.get(), ItemInit.HEISEN_LEGG.get()
,ItemInit.HEISEN_SUIT.get(), ItemInit.HEISEN_HEAD.get()));
return isWearingAll ? PlayState.CONTINUE : PlayState.STOP;
}
return PlayState.STOP;
}
//将我们的待机动画进行注册
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void registerControllers(AnimationData data) {
data.addAnimationController(new AnimationController(this, "controller", 20, this::predicate));
}
@Override
public AnimationFactory getFactory() {
return this.factory;
}
private static final Map
MATERIAL_TO_EFFECT_MAP =
(new ImmutableMap.Builder())
.put(CustomArmorMaterial.ARMOR_MATERIAL_HEISENSUIT,
new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 100, 1)).build();
//穿上后给予效果
@Override
public void onArmorTick(ItemStack stack, Level world, Player player) {
if(!world.isClientSide()) {
//穿上头盔就给予一个药水效果
if(!player.getInventory().getArmor(3).isEmpty()) {
MobEffectInstance mapStatusEffect1 = new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 100, 2);
ArmorItem helmet = ((ArmorItem)player.getInventory().getArmor(3).getItem());
if(helmet.getMaterial() == CustomArmorMaterial.ARMOR_MATERIAL_HEISENSUIT) {
boolean hasPlayerEffect = player.hasEffect(mapStatusEffect1.getEffect());
if(!hasPlayerEffect) {
player.addEffect(new MobEffectInstance(mapStatusEffect1.getEffect(),mapStatusEffect1.getDuration(), mapStatusEffect1.getAmplifier()));
}
}
}
//穿上胸甲就给予一个药水效果
if(!player.getInventory().getArmor(2).isEmpty()) {
MobEffectInstance mapStatusEffect2 = new MobEffectInstance(MobEffects.FIRE_RESISTANCE, 100, 1);
ArmorItem breastplate = ((ArmorItem)player.getInventory().getArmor(2).getItem());
if(breastplate.getMaterial() == CustomArmorMaterial.ARMOR_MATERIAL_HEISENSUIT) {
boolean hasPlayerEffect = player.hasEffect(mapStatusEffect2.getEffect());
if(!hasPlayerEffect) {
player.addEffect(new MobEffectInstance(mapStatusEffect2.getEffect(),mapStatusEffect2.getDuration(), mapStatusEffect2.getAmplifier()));
}
}
}
//穿上护腿就给予一个药水效果
if(!player.getInventory().getArmor(1).isEmpty()) {
MobEffectInstance mapStatusEffect3 = new MobEffectInstance(MobEffects.SLOW_FALLING, 100, 1);
ArmorItem leggings = ((ArmorItem)player.getInventory().getArmor(1).getItem());
if(leggings.getMaterial() == CustomArmorMaterial.ARMOR_MATERIAL_HEISENSUIT) {
boolean hasPlayerEffect = player.hasEffect(mapStatusEffect3.getEffect());
if(!hasPlayerEffect) {
player.addEffect(new MobEffectInstance(mapStatusEffect3.getEffect(),mapStatusEffect3.getDuration(), mapStatusEffect3.getAmplifier()));
}
}
}
//穿上靴子就给予一个药水效果
if(!player.getInventory().getArmor(0).isEmpty()) {
MobEffectInstance mapStatusEffect4 = new MobEffectInstance(MobEffects.MOVEMENT_SPEED, 100, 0);
ArmorItem boots = ((ArmorItem)player.getInventory().getArmor(0).getItem());
if(boots.getMaterial() == CustomArmorMaterial.ARMOR_MATERIAL_HEISENSUIT) {
boolean hasPlayerEffect = player.hasEffect(mapStatusEffect4.getEffect());
if(!hasPlayerEffect) {
player.addEffect(new MobEffectInstance(mapStatusEffect4.getEffect(),mapStatusEffect4.getDuration(), mapStatusEffect4.getAmplifier()));
}
}
}
}
}
}
ModelHeisensuit:ModelHeisensuit.java
package com.joy187.re8joymod.items.armor.model;
import com.joy187.re8joymod.Main;
import com.joy187.re8joymod.items.armor.HeisensuitArmorItem;
import net.minecraft.resources.ResourceLocation;
import software.bernie.geckolib3.model.AnimatedGeoModel;
public class ModelHeisensuit extends AnimatedGeoModel {
//盔甲模型文件地址
@Override
public ResourceLocation getModelLocation(HeisensuitArmorItem object) {
return new ResourceLocation(Main.MOD_ID, "geo/heisensuit.geo.json");
}
//盔甲材质文件地址
@Override
public ResourceLocation getTextureLocation(HeisensuitArmorItem object) {
return new ResourceLocation(Main.MOD_ID, "textures/models/armor/heisensuit_layer_1.png");
}
//盔甲动画文件地址
@Override
public ResourceLocation getAnimationFileLocation(HeisensuitArmorItem animatable) {
return new ResourceLocation(Main.MOD_ID, "animations/heisensuit.animation.json");
}
}
ModelHeisensuit:``
package com.joy187.re8joymod.items.armor.render;
import com.joy187.re8joymod.items.armor.HeisensuitArmorItem;
import com.joy187.re8joymod.items.armor.model.ModelHeisensuit;
import software.bernie.geckolib3.renderers.geo.GeoArmorRenderer;
public class RenderHeisensuit extends GeoArmorRenderer {
//渲染盔甲穿在身上的每一个部位的效果
public RenderHeisensuit() {
super(new ModelHeisensuit());
//这里要和第二步你blockbench中建模的名称一一对应
this.headBone = "Head";
this.bodyBone = "chestplate";
this.rightArmBone = "rightArm";
this.leftArmBone = "leftArm";
this.rightLegBone = "rightLeg";
this.leftLegBone = "leftLeg";
this.rightBootBone = "rightBoot";
this.leftBootBone = "leftBoot";
}
}
ClientModEventSubscriber类中将我们的盔甲渲染类进行注册:ClientModEventSubscriber.java
@Mod.EventBusSubscriber(modid = Main.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ClientModEventSubscriber extends ModEventSubscriber{
@OnlyIn(Dist.CLIENT)
@SubscribeEvent
public static void registerRenderers(final EntityRenderersEvent.AddLayers event) {
//渲染类进行注册
GeoArmorRenderer.registerArmorRenderer(HeisensuitArmorItem.class, new RenderHeisensuit());
}
}
ItemInit.java
//头盔
public static final RegistryObject HEISEN_HEAD = ITEMS.register("heisenhead",
() -> new HeisensuitArmorItem(CustomArmorMaterial.ARMOR_MATERIAL_HEISENSUIT, EquipmentSlot.HEAD, new Item.Properties()));
//胸甲
public static final RegistryObject HEISEN_SUIT = ITEMS.register("heisensuit",
() -> new HeisensuitArmorItem(CustomArmorMaterial.ARMOR_MATERIAL_HEISENSUIT, EquipmentSlot.CHEST, new Item.Properties()));
//护腿
public static final RegistryObject HEISEN_LEGG = ITEMS.register("heisenlegg",
() -> new HeisensuitArmorItem(CustomArmorMaterial.ARMOR_MATERIAL_HEISENSUIT, EquipmentSlot.LEGS, new Item.Properties()));
//靴子
public static final RegistryObject HEISEN_BOOTS = ITEMS.register("heisenboots",
() -> new HeisensuitArmorItem(CustomArmorMaterial.ARMOR_MATERIAL_HEISENSUIT, EquipmentSlot.FEET, new Item.Properties()));
在resources\assets\你的modid中的lang包中的en_us.json添加盔甲的英文名称:
"item.re8joymod.heisenhead":"H",
"item.re8joymod.heisensuit":"H",
"item.re8joymod.heisenlegg":"Hei",
"item.re8joymod.heisenboots":"Heis",
models\item包中添加所有盔甲的模型文件:heisenhead.json
{
"parent": "item/generated",
"textures": {
"layer0": "re8joymod:item/heisenhead"
}
}
heisensuit.json
{
"parent": "item/generated",
"textures": {
"layer0": "re8joymod:item/heisensuit"
}
}
heisenlegg.json
{
"parent": "item/generated",
"textures": {
"layer0": "re8joymod:item/heisenlegg"
}
}
heisenboots.json
{
"parent": "item/generated",
"textures": {
"layer0": "re8joymod:item/heisenboots"
}
}
textures\item中添加盔甲的手持贴图:
textures\models\armor中添加盔甲的穿戴后贴图:
assets\minecraft\textures\models\armor中同样添加我们的穿戴后贴图:

