提供: Minecraft Modding Wiki
この編集を取り消せます。
下記の差分を確認して、本当に取り消していいか検証してください。よろしければ変更を保存して取り消しを完了してください。
最新版 | 編集中の文章 | ||
13行目: | 13行目: | ||
package com.example.examplemod; | package com.example.examplemod; | ||
− | import | + | import cpw.mods.fml.common.FMLCommonHandler; |
− | import | + | import cpw.mods.fml.common.Mod; |
− | import | + | import cpw.mods.fml.common.Mod.EventHandler; |
− | import | + | import cpw.mods.fml.common.event.FMLInitializationEvent; |
− | import | + | import cpw.mods.fml.common.event.FMLPreInitializationEvent; |
− | import | + | import cpw.mods.fml.common.eventhandler.SubscribeEvent; |
− | import | + | import cpw.mods.fml.common.gameevent.PlayerEvent; |
import net.minecraft.entity.player.EntityPlayer; | import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.entity.player.EntityPlayerMP; | import net.minecraft.entity.player.EntityPlayerMP; | ||
32行目: | 32行目: | ||
//1.8のforgeを指定 | //1.8のforgeを指定 | ||
− | @Mod(modid = "SampleMod", | + | @Mod(modid = "SampleMod", name = "SampleMod", version = "1.0", dependencies = "required-after:Forge@[11.14.0.1296,)", useMetadata = true) |
− | |||
− | |||
− | |||
− | |||
− | |||
public class SampleMod { | public class SampleMod { | ||
@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(FMLPreInitializationEvent event) { | public void preInit(FMLPreInitializationEvent event) { | ||
49行目: | 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); | ||
} | } | ||
} | } | ||
85行目: | 74行目: | ||
/*ワールドに入った時に呼ばれるイベント。ここでIExtendedEntityPropertiesを読み込む処理を呼び出す*/ | /*ワールドに入った時に呼ばれるイベント。ここでIExtendedEntityPropertiesを読み込む処理を呼び出す*/ | ||
public void onEntityJoinWorld(EntityJoinWorldEvent event) { | public void onEntityJoinWorld(EntityJoinWorldEvent event) { | ||
− | if (event. | + | if (event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer) { |
EntityPlayer player = (EntityPlayer)event.entity; | EntityPlayer player = (EntityPlayer)event.entity; | ||
PacketHandler.INSTANCE.sendToServer(new MessagePlayerJoinInAnnouncement(player)); | PacketHandler.INSTANCE.sendToServer(new MessagePlayerJoinInAnnouncement(player)); | ||
92行目: | 81行目: | ||
@SubscribeEvent | @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); | ||
} | } | ||
} | } | ||
</source> | </source> | ||
+ | |||
===ExtendedPlayerPropertiesクラス=== | ===ExtendedPlayerPropertiesクラス=== | ||
<source lang = "java"> | <source lang = "java"> | ||
145行目: | 135行目: | ||
private ItemStack[] sampleItemStacks = new ItemStack[10]; | private ItemStack[] sampleItemStacks = new ItemStack[10]; | ||
private NBTTagCompound sampleNBTTagCompound = new NBTTagCompound(); | private NBTTagCompound sampleNBTTagCompound = new NBTTagCompound(); | ||
+ | |||
+ | /*EntityPlayerインスタンスから外部保存時の固有文字列を返す | ||
+ | *playernameは変更可能になったので、UUIDに変更 */ | ||
+ | private static String getSaveKey(EntityPlayer player) { | ||
+ | return player.getGameProfile().getId().toString() + ":" + EXT_PROP_NAME; | ||
+ | } | ||
/*EntityPlayerにIExtendedEntityPropertiesを登録。登録文字列はMOD固有のものを割り当てること*/ | /*EntityPlayerにIExtendedEntityPropertiesを登録。登録文字列はMOD固有のものを割り当てること*/ | ||
213行目: | 209行目: | ||
/*初期化メソッド。今のところ使う必要はない。*/ | /*初期化メソッド。今のところ使う必要はない。*/ | ||
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。 | ||
279行目: | 283行目: | ||
===CommonProxyクラス=== | ===CommonProxyクラス=== | ||
− | <source lang = "java"> | + | <source lang = "java">= |
package com.example.examplemod; | package com.example.examplemod; | ||
295行目: | 299行目: | ||
===ClientProxyクラス=== | ===ClientProxyクラス=== | ||
− | <source lang = "java"> | + | <source lang = "java">= |
package com.example.examplemod; | package com.example.examplemod; | ||
317行目: | 321行目: | ||
</source> | </source> | ||
===PacketHandlerクラス=== | ===PacketHandlerクラス=== | ||
− | <source lang = "java"> | + | <source lang = "java">== |
package com.example.examplemod; | package com.example.examplemod; | ||
399行目: | 403行目: | ||
</source> | </source> | ||
− | ===MessagePlayerJoinInAnnouncementクラス | + | ===MessagePlayerJoinInAnnouncementクラス= |
<source lang = "java"> | <source lang = "java"> | ||
package com.example.examplemod; | package com.example.examplemod; | ||
415行目: | 419行目: | ||
public MessagePlayerJoinInAnnouncement(EntityPlayer player) { | public MessagePlayerJoinInAnnouncement(EntityPlayer player) { | ||
− | |||
this.uuid = player.getGameProfile().getId().toString(); | this.uuid = player.getGameProfile().getId().toString(); | ||
} | } | ||
434行目: | 437行目: | ||
} | } | ||
</source> | </source> | ||
− | ===MessagePlayerJoinInAnnouncementHandlerクラス | + | ===MessagePlayerJoinInAnnouncementHandlerクラス= |
<source lang = "java"> | <source lang = "java"> | ||
− | + | ackage com.example.examplemod; | |
import net.minecraft.entity.player.EntityPlayer; | import net.minecraft.entity.player.EntityPlayer; | ||
448行目: | 451行目: | ||
//UUIDの文字列を受け取る | //UUIDの文字列を受け取る | ||
String uuidString = message.getUuid(); | String uuidString = message.getUuid(); | ||
+ | //UUIDからカスタムデータを取得 | ||
+ | NBTTagCompound playerData = SampleMod.getEntityData(uuidString); | ||
EntityPlayer player = ctx.getServerHandler().playerEntity; | EntityPlayer player = ctx.getServerHandler().playerEntity; | ||
− | // | + | if (playerData != null) { |
− | + | //プレイヤーのNBTに書き込む | |
− | + | (player.getExtendedProperties(ExtendedPlayerProperties.EXT_PROP_NAME)).loadNBTData(playerData); | |
− | |||
} | } | ||
− | // | + | //クライアント側にデータを送る |
− | return | + | return new MessagePlayerProperties(player); |
} | } | ||
} | } | ||
466行目: | 470行目: | ||
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のビルドによっては修正されているかもしれないので、適宜確認の上、追加削除を行うこと。 |