2016-05-19 1 views
0

誰もが鍛造でモデリングを学び、IExtendedEntityPropertiesを完全に理解できません。私が望むのは、どこかに格納されるプレイヤーのカスタム変数を作ることです(NBT)。そして、私はこれがIExtendedEntityProperties機能を使用するように言わgoogled方法。ここにコードがあります。私は間違っているの?ありがとう!iextendedentitypropertiesとNBTプレーヤーのデータを保存する

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()); 
    } 


} 

} 

私は2個のテストブロックを使用しています - 1で-rightclickingスニークしながら、それが文字列変数を読み取る必要があり、他に - チャット

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; 
} 

にそれを送信そして、それは常にnullと言います。どうも!ところで

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

、またのための定数を定義します。方法onEntityConstructing中)

public final static String compoundName = "playerProps"; 

を参照してください。

+0

メソッドを実装するときにコメントを削除します。// TODO自動生成メソッドスタブ –

答えて

0

文字列定数を定義し、それはケースの問題を回避する、それを使用しますあなたの財産。

+0

NBTExplorerを使用してプレーヤーの保存をチェックしましたが、カスタムエントリが見つかりませんでした。私はまだ私が推測するentitydataについての基本的なことを完全には理解していません=( – HungryCat