0

标题起初听起来像是一个愚蠢的问题,但这让我感到困惑,我想不出一个简洁的方式来表达它。

无论如何,这是我正在使用的代码:

public class OreSword extends ItemSword{

public OreSword(SWORDTYPES sword)
{
    super(sword.getMaterial());
    setUnlocalizedName(RefStrings.MODID + "_" + sword.getName());
    setTextureName(RefStrings.MODID + ":" + sword.getName());
    //setCreativeTab(CreativeTabs.tabCombat);
    sword.setSword(this);
}

public boolean hitEntity(ItemStack sword, EntityLivingBase target, EntityLivingBase self)
{
    System.out.println("this is my override");
    sword.damageItem(1, self);
    if(sword.getUnlocalizedName() == SWORDTYPES.COAL.getSword().getUnlocalizedName())
    {
        System.out.println("this is my sword");
        target.setFire(100);
    }
    return true;
}

public static enum SWORDTYPES
{
    COAL("CoalSword", 1, 131, 4.0F, 1.0F, 5),
    DIAMOND("DiamondSword", 3, 1200, 8.0F, 3.0F, 30),
    EMERALD("EmeraldSword", 3, 2300, 8.0F, 4.0F, 10),
    GOLD("GoldSword", 0, 25, 10.0F, 1.0F, 12),
    IRON("IronSword", 2, 131, 6.0F, 2.0F, 14),
    LAPIS("LapisSword", 1, 131, 4.0F, 1.0F, 44),
    QUARTZ("QuartzSword", 3, 131, 8.0F, 3.0F, 10),
    REDSTONE("RedstoneSword", 2, 131, 6.0F, 2.0F, 14);

    private String name;
    /*private int hLevel;
    private int mUse;
    private float effic;
    private float damage;
    private int ench;*/
    private ToolMaterial mat;
    private OreSword sword;
    private SWORDTYPES(String name, int hLevel, int mUse, float effic, float damage, int ench)
    {
        this.name = name;
        this.mat = EnumHelper.addToolMaterial(name, hLevel, mUse, effic, damage, ench);
    }
    public String getName(){return name;}
    public ToolMaterial getMaterial(){return mat;}
    public void setSword(OreSword sword){this.sword = sword;}
    public OreSword getSword(){return sword;}
}
}

当我运行此代码时,它会输出“这是我的替代”这一行,但它从未打印出“这是我的剑”这一行

我在游戏中测试的剑是正确的剑,我在创意中测试过,所以剑不会受到任何伤害,在剑受到伤害的情况下生存。如果有人可以向我解释我应该做什么来检查一个项目或者我在我的实施中可能缺少什么,我将不胜感激。

4

1 回答 1

1

public boolean equals(Object obj)在 Java 中比较两个字符串时始终使用。

if(sword.getUnlocalizedName().equals(SWORDTYPES.COAL.getSword().getUnlocalizedName()))

此外,如果它们不相等,一个很好的技巧是尝试打印它们以查看是什么使它们不相等,例如;

System.out.println("sword: " + sword.getUnlocalizedName()); System.out.println("SWORDTYPES: " + SWORDTYPES.COAL.getSword().getUnlocalizedName());

于 2015-08-08T05:44:59.420 回答