引起:java.lang.ClassCastException: class net.minecraft.world.item.AirItem 不能轉換為 class net.minecraft.world.item.ArmorItem

E2Z1

我已經為 Minecraft Mods 編程了 3 週並有一個問題。我想要,可變距離是你穿上多少盔甲。這是我的代碼:

package com.E2Z1.pickaxemod.item.custom;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ArmorMaterial;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import com.E2Z1.pickaxemod.item.ModArmorMaterial;


public class ModArmorItem extends ArmorItem {
    public ModArmorItem(ArmorMaterial material, EquipmentSlot slot, Properties settings) {
        super(material, slot, settings);
    }

    @Override
    public void inventoryTick(ItemStack stack, Level world, Entity entity, int slot,
boolean selected) {
        if(!world.isClientSide()) {
            if(entity instanceof Player) {
                Player player = (Player)entity;

                GlobalVar.distance = howmanyarmor(ModArmorMaterial.PICKAXE_ARMOR, player);

            }
        }

        super.inventoryTick(stack, world, entity, slot, selected);
    }


    private int howmanyarmor(ArmorMaterial material, Player player) {
        ArmorItem boots = ((ArmorItem)player.getInventory().getArmor(0).getItem());
        ArmorItem leggings = ((ArmorItem)player.getInventory().getArmor(1).getItem());
        ArmorItem breastplate = ((ArmorItem)player.getInventory().getArmor(2).getItem());
        ArmorItem helmet = ((ArmorItem)player.getInventory().getArmor(3).getItem());
        ItemStack boots2 = player.getInventory().getArmor(0);
        ItemStack leggings2 = player.getInventory().getArmor(1);
        ItemStack breastplate2 = player.getInventory().getArmor(2);
        ItemStack helmet2 = player.getInventory().getArmor(3);
        int numberofarmor = 0;
        if (!helmet2.isEmpty()) {
            if (helmet.getMaterial() == material){
                numberofarmor =+ 1;
            }}
        if (!breastplate2.isEmpty()) {
            if (breastplate.getMaterial() == material){
                numberofarmor =+ 1;
            }}
        if (!leggings2.isEmpty()) {
            if (leggings.getMaterial() == material){
                numberofarmor =+ 1;
            }}
        if (!boots2.isEmpty()) {
            if (boots.getMaterial() == material){
                numberofarmor =+ 1;
            }}

        return numberofarmor;
    }
}

當我開始世界時,會出現此錯誤:

Caused by: java.lang.ClassCastException: class net.minecraft.world.item.AirItem cannot be cast to class net.minecraft.world.item.ArmorItem (net.minecraft.world.item.AirItem and net.minecraft.world.item.ArmorItem are in module [email protected] of loader 'TRANSFORMER' @2c8662ac)

Forge 版本:37.0.104 Minecraft 版本:1.17.1

(抱歉英語不好)

我希望你能幫忙

我並不是真的喜歡getArmorMinecraft 模組,但是看起來你正在使用-Getters 玩家角色的盔甲碎片進行檢索如果玩家在插槽中沒有穿任何盔甲,我想,你不會收到一個實例,ArmorItem而是收到一個AirItem(基本上是一個空插槽)。

因此,您可能需要實際檢查收到的實例類型。以同樣的方式,您檢查public void inventoryTickifentity是 type 的實例Player

結果實現可能如下所示:

private int howmanyarmor(ArmorMaterial material, Player player) {
        int numberofarmor = 0;
        Item bootsItem = player.getInventory().getArmor(0).getItem();
        if (bootsItem instanceof ArmorItem) {
            ArmorItem boots = (ArmorItem)bootsItem;
            ItemStack boots2 = player.getInventory().getArmor(0);
            if (!boots2.isEmpty()) {
                if (boots.getMaterial() == material){
                    numberofarmor =+ 1;
                }}
        }
        // [... re-do for remaining slots ...]
        return numberofarmor;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章