From f7736e9a0ae41e11abc5c83dd58602780a9f40fb Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 21 Jun 2014 17:38:21 -0400 Subject: [PATCH] Fire an event when a player loads or saves from disk. Mods that want to load an additional player related file from the players dir can now do so in that event. --- .../world/storage/SaveHandler.java.patch | 18 ++++ .../event/ForgeEventFactory.java | 11 +++ .../event/entity/player/PlayerEvent.java | 83 ++++++++++++++++++- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 patches/minecraft/net/minecraft/world/storage/SaveHandler.java.patch diff --git a/patches/minecraft/net/minecraft/world/storage/SaveHandler.java.patch b/patches/minecraft/net/minecraft/world/storage/SaveHandler.java.patch new file mode 100644 index 000000000..eaef04a59 --- /dev/null +++ b/patches/minecraft/net/minecraft/world/storage/SaveHandler.java.patch @@ -0,0 +1,18 @@ +--- ../src-base/minecraft/net/minecraft/world/storage/SaveHandler.java ++++ ../src-work/minecraft/net/minecraft/world/storage/SaveHandler.java +@@ -254,6 +254,7 @@ + } + + file1.renameTo(file2); ++ net.minecraftforge.event.ForgeEventFactory.firePlayerSavingEvent(p_75753_1_, this.field_75771_c, p_75753_1_.func_110124_au().toString()); + } + catch (Exception exception) + { +@@ -270,6 +271,7 @@ + p_75752_1_.func_70020_e(nbttagcompound); + } + ++ net.minecraftforge.event.ForgeEventFactory.firePlayerLoadingEvent(p_75752_1_, field_75771_c, p_75752_1_.func_110124_au().toString()); + return nbttagcompound; + } + diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index c19f4a92c..2b88ff0ae 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -1,5 +1,6 @@ package net.minecraftforge.event; +import java.io.File; import java.util.ArrayList; import java.util.List; import cpw.mods.fml.common.eventhandler.Event.Result; @@ -170,4 +171,14 @@ public class ForgeEventFactory { MinecraftForge.EVENT_BUS.post(new PlayerEvent.StopTracking(player, entity)); } + + public static void firePlayerLoadingEvent(EntityPlayer player, File playerDirectory, String uuidString) + { + MinecraftForge.EVENT_BUS.post(new PlayerEvent.SaveToFile(player, playerDirectory, uuidString)); + } + + public static void firePlayerSavingEvent(EntityPlayer player, File playerDirectory, String uuidString) + { + MinecraftForge.EVENT_BUS.post(new PlayerEvent.SaveToFile(player, playerDirectory, uuidString)); + } } diff --git a/src/main/java/net/minecraftforge/event/entity/player/PlayerEvent.java b/src/main/java/net/minecraftforge/event/entity/player/PlayerEvent.java index 68c60a649..fbbc8556a 100644 --- a/src/main/java/net/minecraftforge/event/entity/player/PlayerEvent.java +++ b/src/main/java/net/minecraftforge/event/entity/player/PlayerEvent.java @@ -1,5 +1,6 @@ package net.minecraftforge.event.entity.player; +import java.io.File; import cpw.mods.fml.common.eventhandler.Cancelable; import net.minecraft.block.Block; import net.minecraft.entity.Entity; @@ -129,6 +130,86 @@ public class PlayerEvent extends LivingEvent super(player); this.target = target; } - + + } + + /** + * The player is being loaded from the world save. Note that the + * player won't have been added to the world yet. Intended to + * allow mods to load an additional file from the players directory + * containing additional mod related player data. + */ + public static class LoadFromFile extends PlayerEvent { + /** + * The directory where player data is being stored. Use this + * to locate your mod additional file. + */ + public final File playerDirectory; + /** + * The UUID is the standard for player related file storage. + * It is broken out here for convenience for quick file generation. + */ + public final String playerUUID; + + public LoadFromFile(EntityPlayer player, File originDirectory, String playerUUID) + { + super(player); + this.playerDirectory = originDirectory; + this.playerUUID = playerUUID; + } + + /** + * Construct and return a recommended file for the supplied suffix + * @param suffix The suffix to use. + * @return + */ + public File getPlayerFile(String suffix) + { + if ("dat".equals(suffix)) throw new IllegalArgumentException("The suffix 'dat' is reserved"); + return new File(this.playerDirectory, this.playerUUID+"."+suffix); + } + } + /** + * The player is being saved to the world store. Note that the + * player may be in the process of logging out or otherwise departing + * from the world. Don't assume it's association with the world. + * This allows mods to load an additional file from the players directory + * containing additional mod related player data. + *
+ * Use this event to save the additional mod related player data to the world. + * + *
+ * WARNING: Do not overwrite the player's .dat file here. You will + * corrupt the world state. + */ + public static class SaveToFile extends PlayerEvent { + /** + * The directory where player data is being stored. Use this + * to locate your mod additional file. + */ + public final File playerDirectory; + /** + * The UUID is the standard for player related file storage. + * It is broken out here for convenience for quick file generation. + */ + public final String playerUUID; + + public SaveToFile(EntityPlayer player, File originDirectory, String playerUUID) + { + super(player); + this.playerDirectory = originDirectory; + this.playerUUID = playerUUID; + } + + /** + * Construct and return a recommended file for the supplied suffix + * @param suffix The suffix to use. + * @return + */ + public File getPlayerFile(String suffix) + { + if ("dat".equals(suffix)) throw new IllegalArgumentException("The suffix 'dat' is reserved"); + return new File(this.playerDirectory, this.playerUUID+"."+suffix); + } } }