Structure and Overview
Offline Player Cache offers a small API package, located at com.github.clevernucleus.opc.api
and contains the following:
đź“‚api
┣📄CacheableValue.java abstract class
â”—đź“„PlayerCacheAPI.java class
The contents of CacheableValue.java
are available here. The contents of PlayerCacheAPI.java
are shown below:
Modifiers and Type | Method/Field and Description |
---|---|
public static final String |
MODID The mod id. |
public static CacheableValue<T> |
registerCacheableValue(CacheableValue<T> key) Gains access to the offline player cache object. This should only be used on the logical server. |
public static T |
get(MinecraftServer server, UUID uuid, CacheableValue<T> key) If the player is offline and exists in the cache, retrieves the last cached value. If the player is online, retrieves the player’s current value. |
public static T |
get(MinecraftServer server, String name, CacheableValue<T> key) If the player is offline and exists in the cache, retrieves the last cached value. If the player is online, retrieves the player’s current value. |
public static void |
uncacheValue(String name, CacheableValue<T> key) Removes the input value cached to the input player, if it exists. |
public static void |
uncachePlayer(UUID uuid) Removes the player who’s uuid this belongs to from the cache, if they exist. |
public static void |
uncachePlayer(String name) Removes the player who’s name this belongs to from the cache, if they exist. |
Collection<String> |
getPlayerNames(MinecraftServer server) Returns all offline/cached AND online players’ names. |
Collection<UUID> |
getPlayerIds(MinecraftServer server) Returns all offline/cached AND online players’ UUIDs. |
Using Offline Player Cache
This section will use the cacheable value implementation shown here to further provide an example of how OPC could be used in a project:
1. Registering a Cacheable Value
That is all that is needed. Players will disconnect and their current health will be cached on the server, ready to be accessed at any time. An potential usage is with commands - in the next example, we create a new command: /example health <name>
.
2. Creating a command with OPC
Using examples 1. and 2., we have added the command /example health <name>
. The output of which is <name>'s health is: <health>
. You can now effectively get the current health of all online and offline players on your server. This is obviously a very simplistic example: usually you would also add a command node to take a UUID argument in addition to the player’s name; and you would have a suggestion provider to give you all player names/uuids.