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.
This commit is contained in:
Christian 2014-06-21 17:38:21 -04:00
parent ea7c11baf4
commit f7736e9a0a
3 changed files with 111 additions and 1 deletions

View File

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

View File

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

View File

@ -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.
* <br>
* Use this event to save the additional mod related player data to the world.
*
* <br>
* <em>WARNING</em>: 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);
}
}
}