PlayerProps.class
public class PlayerProps implements IExtendedEntityProperties {
public final static String compoundName = "playerProps";
protected EntityPlayer propsPlayer;
protected World parWorld;
protected String testString;
@Override
public void saveNBTData(NBTTagCompound parCompound) {
NBTTagCompound compound = new NBTTagCompound();
parCompound.setTag(compoundName, compound);
compound.setString("testPar", testString);
}
@Override
public void loadNBTData(NBTTagCompound parCompound) {
NBTTagCompound compound = new NBTTagCompound();
compound.getTag(compoundName);
testString = compound.getString("testPar");
}
@Override
public void init(Entity entity, World world) {
propsPlayer = (EntityPlayer)entity;
parWorld = world;
}
public String getTestString() {
return testString;
}
public void setTestString(String string) {
testString = string;
}
@SubscribeEvent
public void onEntityConstructing(EntityConstructing event) {
if (event.entity instanceof EntityPlayer)
{
event.entity.registerExtendedProperties("PlayerProps", new PlayerProps());
}
}
}
对于测试我使用两个测试块 - 一方面偷偷右键,一方面必须读取字符串变量,另一方面 - 在聊天中发送
public boolean onBlockActivated(World world, int par2, int par3, int par4,
EntityPlayer player, int par6, float par7, float par8, float par9) {
PlayerProps props = new PlayerProps();
if (player.isSneaking()) {
player.addChatMessage(new ChatComponentText(props.getTestString()));
return true;
}
return false;
}
public boolean onBlockActivated(World world, int par2, int par3, int par4,
EntityPlayer player, int par6, float par7, float par8, float par9) {
PlayerProps props = new PlayerProps();
if (player.isSneaking()) {
props.setTestString("It Works!");
return true;
}
return false;
}
它总是说空。 THX!
答案 0 :(得分:0)
你定义了一个常量String,所以使用它,它会避免大小写问题;)
public final static String compoundName = "playerProps";
请参阅方法onEntityConstructing
:
event.entity.registerExtendedProperties("PlayerProps", new PlayerProps());
顺便说一下,为你的财产定义一个常数。