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 interface
â”—đź“„OfflinePlayerCache.java interface
The contents of CacheableValue.java
are available here. The contents of OfflinePlayerCache.java
are shown below:
Modifiers and Type | Method/Field and Description |
---|---|
public static final String |
MODID The mod id. |
public static CacheableValue<V> |
register(CacheableValue<V> key) Registers a cacheable value to the server: these are keys that instruct the server to cache some data from the players when they disconnect. |
public static T |
getOfflinePlayerCache(MinecraftServer server, T fallback, Function<OfflinePlayerCache, T> function) Gains access to the offline player cache object. This should only be used on the logical server. |
V |
get(UUID uuid, CacheableValue<V> 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. |
V |
get(String name, CacheableValue<V> 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. |
Collection<UUID> |
playerIds() Returns all offline/cached AND online players’ UUIDs. |
Collection<String> |
playerNames() Returns all offline/cached AND online players’ names. |
boolean |
isPlayerCached(UUID uuid) Tests if the player with the input UUID exists in the cache. |
boolean |
isPlayerCached(String name) Tests if the player with the input name exists in the cache. |
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.