Class CustomPlayerSave

java.lang.Object
com.faux.customentitydata.api.playersaves.CustomPlayerSave
All Implemented Interfaces:
IPlayerLoadListener, IPlayerSaveListener

public abstract class CustomPlayerSave extends Object implements IPlayerSaveListener, IPlayerLoadListener
This class provides a custom player data solution that stores player data in a separate auxiliary file in the player directory. While this is not the definitive way to store custom player data it is often the cleanest approach.

Some benefits of this approach include:

  • No risk of corrupting vanilla player data.
  • Custom data survives player respawn.
  • Custom data survives mod being uninstalled temporarily.

This implementation will invoke savePlayer(Player) when the vanilla player data is written to disk. You can use this method to pull data from your own in-memory sources and store it in the resulting NBT tag. Once this tag is generated it will be saved to the disk as a compressed NBT file. A backup save file will also be generated and maintained to help players rollback their data in the event of corruption or other adverse circumstances. Loading your custom data is done in loadPlayer(Player, CompoundTag) which is invoked after vanilla has loaded their player data. You can use this method to restore your in-memory player data.

Files will be saved within subdirectories of the vanilla player folder. These subdirectories are based on the handlerId provided when constructing your implementation.

  • Field Summary

    Fields inherited from interface com.faux.customentitydata.api.playersaves.IPlayerLoadListener

    EVENT

    Fields inherited from interface com.faux.customentitydata.api.playersaves.IPlayerSaveListener

    EVENT
  • Constructor Summary

    Constructors
    Constructor
    Description
    CustomPlayerSave(net.minecraft.resources.Identifier handlerId)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    loadPlayer(net.minecraft.world.entity.player.Player player, net.minecraft.nbt.CompoundTag saveData)
    Loads custom data for a player from NBT.
    void
    loadPlayerData(net.minecraft.world.entity.player.Player player, Path saveDir)
    Called when a player's data is loaded directly from the disk.
    abstract net.minecraft.nbt.CompoundTag
    savePlayer(net.minecraft.world.entity.player.Player player)
    Saves custom data for a player as an NBT.
    void
    savePlayerData(net.minecraft.world.entity.player.Player player, Path saveDir)
    Called when a player's data is saved directly to the disk.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CustomPlayerSave

      public CustomPlayerSave(net.minecraft.resources.Identifier handlerId)
  • Method Details

    • savePlayer

      public abstract net.minecraft.nbt.CompoundTag savePlayer(net.minecraft.world.entity.player.Player player)
      Saves custom data for a player as an NBT. This is invoked after the vanilla player data has been saved.
      Parameters:
      player - The player being saved.
      Returns:
      The data to save for the player.
    • loadPlayer

      public abstract void loadPlayer(net.minecraft.world.entity.player.Player player, net.minecraft.nbt.CompoundTag saveData)
      Loads custom data for a player from NBT. This is invoked after the vanilla player data has been loaded.
      Parameters:
      player - The player being loaded.
      saveData - The data for the player. If the player has no existing data an empty tag will be provided.
    • loadPlayerData

      public void loadPlayerData(net.minecraft.world.entity.player.Player player, Path saveDir)
      Description copied from interface: IPlayerLoadListener
      Called when a player's data is loaded directly from the disk.
      Specified by:
      loadPlayerData in interface IPlayerLoadListener
      Parameters:
      player - The player that is being loaded.
      saveDir - The save folder.
    • savePlayerData

      public void savePlayerData(net.minecraft.world.entity.player.Player player, Path saveDir)
      Description copied from interface: IPlayerSaveListener
      Called when a player's data is saved directly to the disk.
      Specified by:
      savePlayerData in interface IPlayerSaveListener
      Parameters:
      player - The player that is being saved.
      saveDir - The save folder.