You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Forge 1.12.2中猪摔死掉落物品的LootCondition实现求助

解决Forge 1.12.2猪摔落死亡掉落特定物品的LootCondition实现

1.12.2版本的Forge没有内置判断“摔落伤害致死”的LootCondition,需要自定义实现,步骤如下:

1. 自定义摔落伤害致死的LootCondition类

实现ILootCondition接口,在testCondition方法中检查实体的死亡原因是否为摔落伤害:

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.conditions.ILootCondition;
import com.google.gson.JsonObject;
import net.minecraft.world.storage.loot.conditions.LootCondition;

public class KilledByFallDamage implements ILootCondition {
    public KilledByFallDamage() {}

    @Override
    public boolean testCondition(LootContext context) {
        Entity entity = context.getLootedEntity();
        if (entity instanceof EntityLivingBase) {
            EntityLivingBase livingEntity = (EntityLivingBase) entity;
            DamageSource damageSource = livingEntity.getLastDamageSource();
            // 判断伤害来源是否为摔落伤害
            return damageSource != null && damageSource.isFallDamage();
        }
        return false;
    }

    // 序列化器,必须实现以让战利品系统识别这个条件
    public static class Serializer extends LootCondition.Serializer<KilledByFallDamage> {
        public Serializer() {
            super(new ResourceLocation("yeetstick", "killed_by_fall_damage"), KilledByFallDamage.class);
        }

        @Override
        public void serialize(JsonObject json, KilledByFallDamage value, com.google.gson.JsonSerializationContext context) {
            // 该条件无需额外参数,空实现即可
        }

        @Override
        public KilledByFallDamage deserialize(JsonObject json, com.google.gson.JsonDeserializationContext context) {
            return new KilledByFallDamage();
        }
    }
}

2. 注册LootCondition的序列化器

在你的模组主类或专用注册类中,通过LootConditionEvent.Register事件注册自定义条件的序列化器:

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.event.LootConditionEvent;

@Mod.EventBusSubscriber(modid = "yeetstick")
public class YeetstickModRegistry {
    @SubscribeEvent
    public static void registerLootConditions(LootConditionEvent.Register event) {
        event.register(new KilledByFallDamage.Serializer());
    }
}

3. 在战利品表加载事件中使用自定义条件

修改你现有的onLootTableLoad方法,将自定义条件填入对应位置:

@SubscribeEvent
public void onLootTableLoad(LootTableLoadEvent event){
    if (event.getName().equals(LootTableList.ENTITIES_PIG)) {
        final LootPool main = event.getTable().getPool("main");

        if (main != null) {
            main.addEntry(new LootEntryItem(
                new ItemYeetiumNugget("yeetium_nugget"),
                1,
                0,
                new LootFunction[0],
                new LootCondition[]{new KilledByFallDamage()}, // 这里填入自定义条件
                "yeetstick:yeetium_nugget_drop"
            ));
        }
    }
}

注意事项

  • 替换代码中的yeetstick为你的模组ID,保持前后一致。
  • 确保ItemYeetiumNugget类已正确定义并注册。

内容的提问来源于stack exchange,提问作者Warriorjacq9

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 22:25:24