Player Cache
This page describes the player cache API as of v7.0.0. See the 7.0.0 release notes for an overview.
@PlayerCache removes a player’s entries from managed collections on quit. It does not load data or guarantee persistence.
Basic Usage
Declare a mutable Map<UUID, ?> field in a managed singleton and annotate it with @PlayerCache:
package com.ultikits.docs.cache;
import com.ultikits.ultitools.abstracts.UltiToolsPlugin;
import com.ultikits.ultitools.annotations.*;
import com.ultikits.ultitools.aop.ExceptionHandler;
import com.ultikits.ultitools.interfaces.DataOperator;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class CooldownService {
@PlayerCache
private final Map<UUID, Long> cooldowns = new ConcurrentHashMap<>();
public boolean isOnCooldown(UUID playerId) {
Long expiry = cooldowns.get(playerId);
return expiry != null && System.currentTimeMillis() < expiry;
}
public void setCooldown(UUID playerId, long durationMs) {
cooldowns.put(playerId, System.currentTimeMillis() + durationMs);
}
}On PlayerQuitEvent, the framework calls cooldowns.remove(playerUuid). It does not populate the map on join or load it from storage; the bean decides when to add or load entries.
Caching Stored Data
A @PlayerCache map works best as a read cache for data that is stored elsewhere, such as a database table. Read through the cache, write each change when it is made, and let the framework drop the entry when the player quits:
package com.ultikits.docs.cache;
import com.ultikits.ultitools.abstracts.UltiToolsPlugin;
import com.ultikits.ultitools.annotations.Autowired;
import com.ultikits.ultitools.annotations.PlayerCache;
import com.ultikits.ultitools.annotations.Service;
import com.ultikits.ultitools.entities.WhereCondition;
import com.ultikits.ultitools.interfaces.DataOperator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class PlayerSettingsService {
@Autowired
private UltiToolsPlugin plugin;
// A read cache only. Nothing is ever written from it: the framework drops the entry when the
// player quits, and a change is written at the moment it is made.
@PlayerCache
private final Map<UUID, PlayerSettings> settingsCache = new ConcurrentHashMap<>();
public PlayerSettings getSettings(UUID playerId) {
return settingsCache.computeIfAbsent(playerId, id -> {
PlayerSettingsEntity stored = readStored(id);
return stored != null ? PlayerSettings.fromEntity(stored) : new PlayerSettings();
});
}
public void setShowScoreboard(UUID playerId, boolean show) {
DataOperator<PlayerSettingsEntity> operator = plugin.getDataOperator(PlayerSettingsEntity.class);
// Read the stored row now, not the cache: another server may have created or changed it.
PlayerSettingsEntity stored = readStored(playerId);
if (stored == null) {
operator.insert(PlayerSettingsEntity.builder()
.playerId(playerId.toString())
.showScoreboard(show)
.build());
} else {
// Write only the column that changed, so nothing else in the row is touched.
operator.update("show_scoreboard", show, stored.getId());
}
// Drop the cached copy; the next read loads what is stored.
settingsCache.remove(playerId);
}
private PlayerSettingsEntity readStored(UUID playerId) {
List<PlayerSettingsEntity> rows = plugin.getDataOperator(PlayerSettingsEntity.class)
.getAll(WhereCondition.builder().column("player_id").value(playerId.toString()).build());
return rows.isEmpty() ? null : rows.get(0);
}
}setShowScoreboard reads the stored row, updates just the changed column (or inserts a row if none exists), and invalidates the cache so the next read loads from storage. Quit does not write the cached copy back. See Caching on a Shared MySQL Database.
Package the @Table entity in the owning module’s JAR. This also applies to PlaySessionEntity and TeleportPrefsEntity below. See Entity Registration for entity ownership and the standalone Paper plugin API.
Save Before Remove
Set saveBeforeRemove = true and implement PlayerCacheSaver to run savePlayerData(playerUuid) before removal. Use it for state that exists only in memory and needs to be written on quit, not for a cached copy of a stored row.
The time a player joined this server is such state: no stored row holds it. The service below appends one new row for the finished session when the player quits, and reads or overwrites no stored row:
package com.ultikits.docs.cache;
import com.ultikits.ultitools.abstracts.UltiToolsPlugin;
import com.ultikits.ultitools.annotations.Autowired;
import com.ultikits.ultitools.annotations.PlayerCache;
import com.ultikits.ultitools.annotations.PlayerCacheSaver;
import com.ultikits.ultitools.annotations.PreDestroy;
import com.ultikits.ultitools.annotations.Service;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class PlaySessionService implements PlayerCacheSaver {
@Autowired
private UltiToolsPlugin plugin;
// State that exists only in this server's memory: when each player joined here.
// No stored row holds it, so there is nothing a copy could go stale against.
@PlayerCache(saveBeforeRemove = true)
private final Map<UUID, Long> joinedAt = new ConcurrentHashMap<>();
// Called from a join listener.
public void sessionStarted(UUID playerId) {
joinedAt.put(playerId, System.currentTimeMillis());
}
// Called by the framework when the player quits, just before the entry is removed.
@Override
public void savePlayerData(UUID playerId) {
Long start = joinedAt.get(playerId);
if (start == null) {
return;
}
// Append one new row for the finished session. No stored row is read or overwritten.
plugin.getDataOperator(PlaySessionEntity.class).insert(PlaySessionEntity.builder()
.playerId(playerId.toString())
.durationMillis(System.currentTimeMillis() - start)
.build());
}
// The framework does not call savePlayerData when the server stops (players are disconnected
// after plugins are disabled), so write the sessions that are still open.
@PreDestroy
public void saveOpenSessions() {
for (UUID playerId : joinedAt.keySet()) {
savePlayerData(playerId);
}
joinedAt.clear();
}
}At module unload or server shutdown, the framework unregisters tracked beans before closing their container. Unregistering neither empties the collections nor invokes savePlayerData. The example uses a separate @PreDestroy method to write sessions still in memory at container close. Do not rely on a quit event to save them.
For each registered field, the quit handler:
- Calls
savePlayerData(playerUuid)ifsaveBeforeRemove = true, even when the field isnullor has no entry for that player. - Reads the collection and tries to remove the player’s entries if the collection is not
null.
A save failure does not prevent the removal attempt. A removal failure may leave the entry in the collection. Each failed operation logs one WARNING with its exception or Error; the other fields and beans are still processed. There is no automatic retry. The messages below are logged directly in English on both locales. Angle brackets indicate runtime values.
Failed to save @PlayerCache field '<field>' on <bean class> for player <UUID> on quit; continuing with the remaining entries
Failed to remove the entry of @PlayerCache field '<field>' on <bean class> for player <UUID> on quit; continuing with the remaining entriesCaching on a Shared MySQL Database
Several servers can share one MySQL database, for example behind a proxy. A copy of a row that one server holds in memory can then be older than the stored row. A player leaves server A for server B and changes a setting there. If A still holds the row from before the change and writes it when the player quits, A's write replaces B's change with the old value. Writing cached copies at shutdown, or whole copies on every later change, has the same effect.
For data that is stored in a database, follow these rules:
- Scope the cache to the player’s time on this server. Load it in your join handler or on first read and let
@PlayerCachedrop the entry on quit. It does not invalidate entries when another server writes. - Never write a cached copy back: not when a value changes, not on quit (
saveBeforeRemove), and not at shutdown. - Use a single-column write by ID for an independent change, or read the current stored row before computing a change that depends on the old value.
- For a change that depends on the old value, use
updateIfwith the values you read as expectations. If it returnsfalse, no row matched the ID and all expectations. Read again and decide whether to retry; cap the number of attempts. - Write an increment as a compare-and-set loop of this kind, never as the cached value plus the amount.
DataOperator#updateIf tests the stored values and writes in one operation. It writes every mapped field, so condition the write on the prior value of every field being written if you need to prevent a concurrent change from being overwritten. See Conditional Update. This example assumes PlayerStats uses the player UUID string as its ID and maps only coins and kills besides the ID:
private static final int MAX_ATTEMPTS = 3;
public boolean addCoins(UUID playerId, int amount) {
DataOperator<PlayerStats> stats = plugin.getDataOperator(PlayerStats.class);
for (int attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
PlayerStats row = stats.getById(playerId.toString()); // the stored row, never the cache
if (row == null) {
return false;
}
int coins = row.getCoins();
int kills = row.getKills();
row.setCoins(coins + amount); // apply this one change
boolean written = stats.updateIf(row,
WhereCondition.builder().column("coins").value(coins).build(),
WhereCondition.builder().column("kills").value(kills).build());
if (written) {
return true;
}
// The row is gone or no longer matches; read again.
}
return false; // contended: tell the player to try again instead of writing
}If a change does not depend on the old value, use update(column, value, id) to write just that column of an existing row, as PlayerSettingsService does. This also works on SQLite and JSON.
The settings examples use a read followed by an insert for a missing row, not an atomic upsert. In a shared MySQL deployment, enforce uniqueness on player_id and handle a duplicate-key failure by re-reading the row and applying the change to it. The examples do not implement that handling.
Annotation Reference
| Attribute | Type | Default | Description |
|---|---|---|---|
saveBeforeRemove | boolean | false | If true, calls savePlayerData(UUID) once for this field on quit, before attempting removal. The bean must implement PlayerCacheSaver. |
PlayerCacheSaver Interface
public interface PlayerCacheSaver {
void savePlayerData(UUID playerId);
}The interface is in com.ultikits.ultitools.annotations. It is required only for beans with a field that sets saveBeforeRemove = true.
Cache registration refusal
A missing saver throws PluginModuleException (ULTI-1001) and fails module loading or external plugin connection. Implement PlayerCacheSaver on the bean or leave saveBeforeRemove at false.
The exception message is @PlayerCache field '<field>' declared on <declaring class> requests saveBeforeRemove = true, but bean <bean class> does not implement PlayerCacheSaver. For a module, the load failure is also logged as [UltiTools-API] <module> load failed!. For an external plugin, connect(...) throws IllegalStateException with the message Failed to connect plugin <plugin> to UltiTools and keeps the original exception as its cause.
Multiple Maps
A bean with several save-marked fields receives one callback per field, even after an earlier callback fails. Later callbacks can see earlier fields already cleaned. If one callback saves the entire bean’s state, mark only one field or make the save safe to repeat. Separate callbacks are not retries.
In this example only openInventories triggers a callback. Treat it as a placeholder for the bean’s save logic; do not assume the other maps are still populated when it runs. Every field is cleaned independently.
package com.ultikits.docs.cache;
import com.ultikits.ultitools.abstracts.UltiToolsPlugin;
import com.ultikits.ultitools.annotations.*;
import com.ultikits.ultitools.aop.ExceptionHandler;
import com.ultikits.ultitools.interfaces.DataOperator;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class GameService implements PlayerCacheSaver {
@PlayerCache
private final Map<UUID, Integer> scores = new ConcurrentHashMap<>();
@PlayerCache(saveBeforeRemove = true)
private final Map<UUID, Inventory> openInventories = new ConcurrentHashMap<>();
@PlayerCache
private final Map<UUID, Long> lastActivity = new ConcurrentHashMap<>();
@Override
public void savePlayerData(UUID playerId) {
// Called only for the saveBeforeRemove=true field,
// but you can save all data here
}
}Requirements
- Declare a mutable
Map<UUID, ?>,Map<?, UUID>, orSet<UUID>with explicitUUIDon the relevant side. Raw collections and UUID wildcards are not accepted. - UUID-keyed maps remove the player’s key, including
Map<UUID, UUID>. Otherwise a UUID-valued map removes all entries whose value equals the player’s UUID. A set removes the UUID itself. - Unsupported field types are rejected with
PluginModuleException(ULTI-1001):@PlayerCache field '<field>' on <declaring class> has an unsupported shape; PlayerCacheManager can only sweep Map<UUID, ?> (key-side), Map<?, UUID> (value-side), and Set<UUID>. - The framework scans the existing container singletons after
registerSelf()succeeds for a module, or during a connected Paper plugin’s registration. A managed@Serviceor an object added withregisterSingleton(...)before the scan can be tracked.registerType(...)alone does not make an object a tracked singleton. External disconnect also unregisters tracked beans before closing the container. - Saver validation checks the actual bean instance, not the class that declares the field. A subclass implementing
PlayerCacheSavercan therefore inherit a save-marked field. - If async code accesses a map, use a thread-safe collection such as
ConcurrentHashMap. This does not make a sequence of cache and database operations atomic.
Complete Example
package com.ultikits.docs.cache;
import com.ultikits.ultitools.abstracts.UltiToolsPlugin;
import com.ultikits.ultitools.annotations.Autowired;
import com.ultikits.ultitools.annotations.PlayerCache;
import com.ultikits.ultitools.annotations.Service;
import com.ultikits.ultitools.entities.WhereCondition;
import com.ultikits.ultitools.interfaces.DataOperator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class TeleportRequestService {
@Autowired
private UltiToolsPlugin plugin;
// Pending teleport requests: requester -> target. Held in memory only.
@PlayerCache
private final Map<UUID, UUID> pendingRequests = new ConcurrentHashMap<>();
// Player's teleport settings: a read cache of the stored row, dropped on quit and never written back.
@PlayerCache
private final Map<UUID, TeleportPrefs> preferences = new ConcurrentHashMap<>();
public void sendRequest(UUID from, UUID to) {
pendingRequests.put(from, to);
}
public UUID getRequest(UUID from) {
return pendingRequests.get(from);
}
public void acceptRequest(UUID from) {
pendingRequests.remove(from);
}
public TeleportPrefs getPreferences(UUID playerId) {
return preferences.computeIfAbsent(playerId, id -> {
TeleportPrefsEntity stored = readStored(id);
return stored != null ? TeleportPrefs.fromEntity(stored) : new TeleportPrefs(false);
});
}
public void setAutoAccept(UUID playerId, boolean autoAccept) {
DataOperator<TeleportPrefsEntity> operator = plugin.getDataOperator(TeleportPrefsEntity.class);
// The change is written when it is made, to the row as stored now.
TeleportPrefsEntity stored = readStored(playerId);
if (stored == null) {
operator.insert(TeleportPrefsEntity.builder()
.playerId(playerId.toString())
.autoAccept(autoAccept)
.build());
} else {
operator.update("auto_accept", autoAccept, stored.getId());
}
preferences.remove(playerId);
}
private TeleportPrefsEntity readStored(UUID playerId) {
List<TeleportPrefsEntity> rows = plugin.getDataOperator(TeleportPrefsEntity.class)
.getAll(WhereCondition.builder().column("player_id").value(playerId.toString()).build());
return rows.isEmpty() ? null : rows.get(0);
}
}Quit cleanup
A UUID-keyed map evicts entries by key. In the teleport example a departing requester’s entry is removed, but requests that name the departing player only as a target are not removed by the annotation.