Base implementation for custom player data.
This is a library and doesn’t add any functionality itself.
Supports ticking since version 1.5
Loading custom data now get’s priority
Updated to Java 17 in v1.10 (also changed some names around like CustomPlayers -> CustomPlayersHandler)
Docs available here[jubiman.github.io].
Example mod using this library:
https://github.com/jubiman/Human-Meat
Make sure to modify your build.gradle file:
Using the library
First you want to create a CustomPlayer class, which has a constructor with a long as argument.
All code fragments can be found in the example[github.com]
public class MyPlayer extends CustomPlayer { public MyPlayer(long auth) { super(auth); } @Override public void addSaveData(SaveData save) { SaveData player = generatePlayerSave(); // save stuff save.addSaveData(player); } @Override public void loadEnter(LoadData data) { // load stuff before the rest of the player is loaded } @Override public void loadExit(LoadData data) { // load stuff after the rest of the player is loaded } }
Source[github.com]
Then create a CustomPlayersHandler class, which does the logic for all players in the game.
public class MyPlayersHandler extends CustomPlayersHandler<MyPlayer> { // It’s recommended to store the name in a static constant, so you can easily access it public static final String name = "myplayers"; public MyPlayersHandler() { super(MyPlayer.class, name); // call this class’ method init(); } /** * Optional method */ public void init() { // initialize stuff in this class } /** * These methods are not necessary, but they are recommended. * This replaces
CustomPlayerRegistry.get(MyPlayers.name).get(auth) * with MyPlayers.getPlayer(auth). * It’s just less code to write 🙂 */ public static MyPlayersHandler getInstance() { return (MyPlayersHandler) CustomPlayerRegistry.get(name); } /** * A null safe way to get a player from the map, adds player if they don’t exist yet * @param auth the authentication of the player’s ServerClient * @return the MyPlayer object belonging to the player */ public static MyPlayer getPlayer(long auth) { return getInstance().get(auth); } }
Source[github.com]
Then at last we need to register the classes by adding the following line to your mod’s (annotated with @ModEntry) init() function:
Creating a tickable CustomPlayer
All you need to do to create a tickable CustomPlayer is to extend from CustomPlayerTickable and CustomPlayersTickable instead of the non-tickable ones (don’t forget to implement the mandatory methods):
public class MyPlayerTickable extends CustomPlayerTickable { public MyPlayerTickable(long auth) { super(auth); } @Override public void serverTick(Server server) { // perform server tick } @Override public void clientTick(Client client) { // perform client tick } @Override public void addSaveData(SaveData save) { // save the player } @Override public void loadEnter(LoadData data) { // load the custom player before the rest of the player is loaded } @Override public void loadExit(LoadData data) { // load the custom player after the rest of the player is loaded } }
Source[github.com]
And MyPlayersTickable:
public class MyPlayersHandlerTickable extends CustomPlayersHandlerTickable<MyPlayerTickable> { public static final String name = "myplayerstickable"; public MyPlayersHandlerTickable() { super(MyPlayerTickable.class, name); } /** * These methods are not necessary, but they are recommended. * This replaces
CustomPlayerRegistry.get(MyPlayersHandlerTickable.name).get(auth) with MyPlayersHandlerTickable.getPlayer(auth). * It’s just less code to write 🙂 */ public static MyPlayersHandlerTickable getInstance() { return (MyPlayersHandlerTickable ) CustomPlayerRegistry.get(name); } /** * A null safe way to get a player from the map, adds player if they don’t exist yet * @param auth the authentication of the player’s ServerClient * @return the MyPlayer object belonging to the player */ public static MyPlayer getPlayer(long auth) { return getInstance().get(auth); } }
Source[github.com]
That’s it! Please let me know if you are missing functionality!