如何比较两个项目未本地化的名称
How to compare two Items unlocalized names
乍一听标题是个愚蠢的问题,但这让我很困惑,我想不出一个简洁的方式来表达它。
无论如何,这是我使用的代码:
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;}
}
}
当我 运行 这段代码输出行 "this is my override" 但它永远不会打印出行 "this is my sword"
我在游戏中测试的剑是正确的剑,我已经在创造模式中测试过它所以剑不会受到任何伤害,而在生存模式下剑会受到伤害。
如果有人可以向我解释我应该做什么来检查一个项目或者我在实施中可能遗漏了什么,我将不胜感激。
比较 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());
乍一听标题是个愚蠢的问题,但这让我很困惑,我想不出一个简洁的方式来表达它。
无论如何,这是我使用的代码:
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;}
}
}
当我 运行 这段代码输出行 "this is my override" 但它永远不会打印出行 "this is my sword"
我在游戏中测试的剑是正确的剑,我已经在创造模式中测试过它所以剑不会受到任何伤害,而在生存模式下剑会受到伤害。 如果有人可以向我解释我应该做什么来检查一个项目或者我在实施中可能遗漏了什么,我将不胜感激。
比较 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());