提供: Minecraft Modding Wiki
この編集を取り消せます。
下記の差分を確認して、本当に取り消していいか検証してください。よろしければ変更を保存して取り消しを完了してください。
最新版 | 編集中の文章 | ||
1行目: | 1行目: | ||
{{前提MOD|reqmod="Minecraft Forge Universal 10.12.1.1090~"}} | {{前提MOD|reqmod="Minecraft Forge Universal 10.12.1.1090~"}} | ||
{{ チュートリアル難易度 | difficulty=1|clear=none}} | {{ チュートリアル難易度 | difficulty=1|clear=none}} | ||
− | {{ チュートリアルカテゴリー |difficulty= | + | {{ チュートリアルカテゴリー |difficulty=0|type=Player}} |
{{stb}} | {{stb}} | ||
==プレイヤーへのカスタムデータの追加== | ==プレイヤーへのカスタムデータの追加== | ||
17行目: | 17行目: | ||
import cpw.mods.fml.common.Mod.EventHandler; | import cpw.mods.fml.common.Mod.EventHandler; | ||
import cpw.mods.fml.common.event.FMLInitializationEvent; | import cpw.mods.fml.common.event.FMLInitializationEvent; | ||
− | import cpw.mods.fml.common.event. | + | import cpw.mods.fml.common.event.FMLPostInitializationEvent; |
import cpw.mods.fml.common.eventhandler.SubscribeEvent; | import cpw.mods.fml.common.eventhandler.SubscribeEvent; | ||
import cpw.mods.fml.common.gameevent.PlayerEvent; | import cpw.mods.fml.common.gameevent.PlayerEvent; | ||
36行目: | 36行目: | ||
@SidedProxy(clientSide = "sampleMod.ClientProxy", serverSide = "sampleMod.CommonProxy") | @SidedProxy(clientSide = "sampleMod.ClientProxy", serverSide = "sampleMod.CommonProxy") | ||
public static CommonProxy proxy; | public static CommonProxy proxy; | ||
+ | //Player毎のIExtendedEntityPropertiesを保存するMap。ダイヤモンド演算子はJava7以降なので注意。 | ||
+ | private static final Map<String, NBTTagCompound> extendedEntityData = new HashMap<>(); | ||
+ | |||
@EventHandler | @EventHandler | ||
− | public void preInit( | + | public void preInit(FMLPostInitializationEvent event) { |
//Messageの登録呼び出し | //Messageの登録呼び出し | ||
PacketHandler.init(); | PacketHandler.init(); | ||
44行目: | 47行目: | ||
@EventHandler | @EventHandler | ||
public void init(FMLInitializationEvent event) { | public void init(FMLInitializationEvent event) { | ||
− | |||
− | |||
//Forge Eventの登録。EntityEvent.EntityConstructingとLivingDeathEventとEntityJoinWorldEvent | //Forge Eventの登録。EntityEvent.EntityConstructingとLivingDeathEventとEntityJoinWorldEvent | ||
− | MinecraftForge.EVENT_BUS.register( | + | MinecraftForge.EVENT_BUS.register(this); |
− | //FML | + | //FML Eventの登録。PlayerChangedDimensionEventとPlayerRespawnEvent |
− | FMLCommonHandler.instance().bus().register( | + | FMLCommonHandler.instance().bus().register(this); |
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | @SubscribeEvent | ||
/*IExtendedEntityPropertiesを登録する処理を呼び出す*/ | /*IExtendedEntityPropertiesを登録する処理を呼び出す*/ | ||
− | |||
public void onEntityConstructing(EntityEvent.EntityConstructing event) { | public void onEntityConstructing(EntityEvent.EntityConstructing event) { | ||
if (event.entity instanceof EntityPlayer) { | if (event.entity instanceof EntityPlayer) { | ||
ExtendedPlayerProperties.register((EntityPlayer)event.entity); | ExtendedPlayerProperties.register((EntityPlayer)event.entity); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | @SubscribeEvent | ||
+ | /*死亡時に呼ばれるイベント。ここで、IExtendedEntityPropertiesを保存する処理を呼び出す*/ | ||
+ | public void onLivingDeathEvent(LivingDeathEvent event) { | ||
+ | if (event.entityLiving instanceof EntityPlayer && !event.entity.worldObj.isRemote) { | ||
+ | NBTTagCompound playerData = new NBTTagCompound(); | ||
+ | (event.entity.getExtendedProperties(ExtendedPlayerProperties.EXT_PROP_NAME)).saveNBTData(playerData); | ||
+ | storeEntityData(event.entity.getCommandSenderName(), playerData); | ||
} | } | ||
} | } | ||
80行目: | 74行目: | ||
/*ワールドに入った時に呼ばれるイベント。ここでIExtendedEntityPropertiesを読み込む処理を呼び出す*/ | /*ワールドに入った時に呼ばれるイベント。ここでIExtendedEntityPropertiesを読み込む処理を呼び出す*/ | ||
public void onEntityJoinWorld(EntityJoinWorldEvent event) { | public void onEntityJoinWorld(EntityJoinWorldEvent event) { | ||
− | if (event. | + | if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer) { |
− | + | NBTTagCompound playerData = getEntityData(event.entity.getCommandSenderName()); | |
− | + | if (playerData != null) { | |
+ | (event.entity.getExtendedProperties(ExtendedPlayerProperties.EXT_PROP_NAME)).loadNBTData(playerData); | ||
+ | } | ||
+ | ((ExtendedPlayerProperties)(event.entity.getExtendedProperties(ExtendedPlayerProperties.EXT_PROP_NAME))).loadProxyData((EntityPlayer)event.entity); | ||
} | } | ||
} | } | ||
− | + | @SubscribeEvent | |
− | // | + | /*リスポーン時に呼ばれるイベント。Serverとの同期を取る*/ |
− | public void | + | public void respawnEvent(PlayerEvent.PlayerRespawnEvent event) { |
− | + | if (!event.player.worldObj.isRemote) { | |
− | if (event. | + | PacketHandler.INSTANCE.sendTo(new MessagePlayerProperties(event.player), (EntityPlayerMP)event.player); |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
@SubscribeEvent | @SubscribeEvent | ||
− | /* | + | /*Dimensionを移動した時に呼ばれるイベント。Serverとの同期を取る*/ |
− | public void | + | public void changedDimension(PlayerEvent.PlayerChangedDimensionEvent event) { |
if (!event.player.worldObj.isRemote) { | if (!event.player.worldObj.isRemote) { | ||
PacketHandler.INSTANCE.sendTo(new MessagePlayerProperties(event.player), (EntityPlayerMP)event.player); | PacketHandler.INSTANCE.sendTo(new MessagePlayerProperties(event.player), (EntityPlayerMP)event.player); | ||
} | } | ||
+ | } | ||
+ | |||
+ | /*PlayerのIExtendedEntityPropertiesをMapに保存*/ | ||
+ | public static void storeEntityData(String name, NBTTagCompound compound) { | ||
+ | extendedEntityData.put(name, compound); | ||
+ | } | ||
+ | |||
+ | /*PlayerのIExtendedEntityPropertiesをMapから読み込み*/ | ||
+ | public static NBTTagCompound getEntityData(String name) { | ||
+ | return extendedEntityData.remove(name); | ||
} | } | ||
} | } | ||
140行目: | 137行目: | ||
private ItemStack[] sampleItemStacks = new ItemStack[10]; | private ItemStack[] sampleItemStacks = new ItemStack[10]; | ||
private NBTTagCompound sampleNBTTagCompound = new NBTTagCompound(); | private NBTTagCompound sampleNBTTagCompound = new NBTTagCompound(); | ||
+ | |||
+ | /*EntityPlayerインスタンスから外部保存時の固有文字列を返す | ||
+ | *1.7ではusername変数が使えないので、コマンド送信時の名前で代用 */ | ||
+ | private static String getSaveKey(EntityPlayer player) { | ||
+ | return player.getCommandSenderName() + ":" + EXT_PROP_NAME; | ||
+ | } | ||
/*EntityPlayerにIExtendedEntityPropertiesを登録。登録文字列はMOD固有のものを割り当てること*/ | /*EntityPlayerにIExtendedEntityPropertiesを登録。登録文字列はMOD固有のものを割り当てること*/ | ||
208行目: | 211行目: | ||
/*初期化メソッド。今のところ使う必要はない。*/ | /*初期化メソッド。今のところ使う必要はない。*/ | ||
public void init(Entity entity, World world) {} | public void init(Entity entity, World world) {} | ||
+ | |||
+ | /*ServerのIExtendedEntityPropertiesを読み込んで、Clientに送信するメソッド*/ | ||
+ | public void loadProxyData(EntityPlayer player) { | ||
+ | ExtendedPlayerProperties playerData = ExtendedPlayerProperties.get(player); | ||
+ | NBTTagCompound savedData = SampleMod.getEntityData(getSaveKey(player)); | ||
+ | if (savedData != null) { playerData.loadNBTData(savedData); } | ||
+ | PacketHandler.INSTANCE.sendTo(new MessagePlayerProperties(player), (EntityPlayerMP)player); | ||
+ | } | ||
/*以降、各変数のGetterおよびSetter。 | /*以降、各変数のGetterおよびSetter。 | ||
331行目: | 342行目: | ||
* 第四引数:ClientとServerのどちらに送るか。送り先*/ | * 第四引数:ClientとServerのどちらに送るか。送り先*/ | ||
INSTANCE.registerMessage(MessagePlayerPropertiesHandler.class, MessagePlayerProperties.class, 0, Side.CLIENT); | INSTANCE.registerMessage(MessagePlayerPropertiesHandler.class, MessagePlayerProperties.class, 0, Side.CLIENT); | ||
− | |||
} | } | ||
} | } | ||
394行目: | 404行目: | ||
</source> | </source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==解説== | ==解説== | ||
細かい説明はソースコード内に付記したので、そちらを参照のこと。 | 細かい説明はソースコード内に付記したので、そちらを参照のこと。 | ||
465行目: | 411行目: | ||
ExtendedPlayerProperties.get(playerインスタンス).getSampleInt()という形でデータを呼び出す。<br> | ExtendedPlayerProperties.get(playerインスタンス).getSampleInt()という形でデータを呼び出す。<br> | ||
ExtendedPlayerProperties.get(playerインスタンス).setSampleInt(sample)という形でデータを書き込む。<br> | ExtendedPlayerProperties.get(playerインスタンス).setSampleInt(sample)という形でデータを書き込む。<br> | ||
− | + | 死亡時等でクライアント側のNBTデータが初期化されるので、LivingDeathEventでデータを保存し、EntityJoinWorldEventで、保存したデータを復帰させる。<br> | |
+ | PlayerRespawnEventやPlayerChangedDimensionEventでも同期を取っているが、Forgeのビルドによっては修正されているかもしれないので、適宜確認の上、追加削除を行うこと。 |