标题听起来像是一个愚蠢的问题,但这让我感到困惑,我想不出一个简洁的方法来表达它。
无论如何,这是我正在使用的代码:
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;}
}
}
当我运行此代码时,它会输出“这是我的覆盖”行,但它从未打印出“这是我的剑”这一行
我在游戏中测试的剑是正确的剑,我在创造性中测试了这一点,因此剑不会受到任何伤害,并且在剑的生存中受到伤害。 如果有人可以向我解释我应该做些什么来检查项目或我在实施中可能缺少的内容,我将不胜感激。
答案 0 :(得分:1)
在比较Java中的两个字符串时,始终使用public boolean equals(Object obj)
。
if(sword.getUnlocalizedName().equals(SWORDTYPES.COAL.getSword().getUnlocalizedName()))
另外,如果它们不相同,那么一个很好的技巧就是尝试打印它们以查看是什么使它们不相等,例如;
System.out.println("sword: " + sword.getUnlocalizedName());
System.out.println("SWORDTYPES: " + SWORDTYPES.COAL.getSword().getUnlocalizedName());