Minecraft Forge 1.12.2自定义箭矢拾取异常:无法回收CustomArrow
在Minecraft 1.12.2中实现拾取CustomArrow而非普通箭矢的方案
完全可以实现你的需求!核心就是让你的ECustomArrow实体在死亡掉落时生成CustomArrow的ItemStack,而不是默认的普通箭矢。下面是具体的实现步骤和代码示例:
1. 重写箭矢实体的掉落物品逻辑
你的ECustomArrow继承自EntityArrow,只需要重写它的getArrowStack()方法,让它返回你的自定义箭矢物品栈即可。这个方法决定了箭矢实体消失时(比如落地后一段时间、命中目标后)会掉落什么物品:
public class ECustomArrow extends EntityArrow { // 你的其他构造方法和逻辑 @Override protected ItemStack getArrowStack() { // 返回CustomArrow的ItemStack,这里替换成你实际注册的自定义箭矢实例 return new ItemStack(YourMod.MOD_CUSTOM_ARROW); } }
2. 确保自定义箭矢物品正确注册
确认你的CustomArrow物品已经在模组主类中完成注册,这样游戏才能识别它:
public class YourMod { // 注册自定义箭矢物品 public static final Item MOD_CUSTOM_ARROW = new CustomArrow() .setRegistryName("custom_arrow") .setUnlocalizedName("custom_arrow") .setCreativeTab(CreativeTabs.COMBAT); // 可选,设置物品所在的创造模式标签页 @EventHandler public void preInit(FMLPreInitializationEvent event) { // 注册物品 GameRegistry.register(MOD_CUSTOM_ARROW); } }
3. 确认弓发射的是ECustomArrow实体
如果你的自定义箭矢是通过弓发射的,需要确保弓的发射逻辑生成的是ECustomArrow而不是默认的EntityArrow。比如在弓的onPlayerStoppedUsing方法中修改实体生成代码:
public class CustomBow extends ItemBow { @Override public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft) { if (entityLiving instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) entityLiving; boolean hasAmmo = player.capabilities.isCreativeMode || player.inventory.hasItemStack(new ItemStack(YourMod.MOD_CUSTOM_ARROW)); if (hasAmmo) { if (!worldIn.isRemote) { // 生成ECustomArrow实体 ECustomArrow arrow = new ECustomArrow(worldIn, player); arrow.setAim(player, player.rotationPitch, player.rotationYaw, 0.0F, this.getArrowVelocity(timeLeft), 1.0F); // 如果不是创造模式,消耗背包里的自定义箭矢 if (!player.capabilities.isCreativeMode) { player.inventory.clearMatchingItems(YourMod.MOD_CUSTOM_ARROW, -1, 1, null); } worldIn.spawnEntity(arrow); } // 播放发射音效等逻辑 worldIn.playSound(null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.NEUTRAL, 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + this.getArrowVelocity(timeLeft) * 0.5F); } } } }
关键注意点
- 确保
ECustomArrow的getArrowStack()方法没有被其他逻辑覆盖,比如某些事件监听或者模组冲突导致掉落被替换。 - 如果你的自定义箭矢有特殊属性(比如自定义伤害、效果),记得在
ECustomArrow中实现对应的逻辑,避免和普通箭矢混淆。
这样处理后,当你的ECustomArrow实体掉落物品时,玩家拾取到的就会是CustomArrow而不是普通箭矢了。
内容的提问来源于stack exchange,提问作者user9165298




