iextendedentityproperties和存储NBT播放器数据

时间:2016-05-19 02:12:38

标签: java minecraft forge

每个人,用伪造学习修改,我完全不了解IExtendedEntityProperties。我想要的是为将存储在某处的玩家(NBT)制作一些自定义变量。而且,我用google搜索的方式告诉我使用IExtendedEntityProperties功能。这是代码。我做错了什么?谢谢!

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!

1 个答案:

答案 0 :(得分:0)

你定义了一个常量String,所以使用它,它会避免大小写问题;)

public final static String compoundName = "playerProps";

请参阅方法onEntityConstructing

event.entity.registerExtendedProperties("PlayerProps", new PlayerProps());

顺便说一下,为你的财产定义一个常数。