Class PlayerDataKey<T>

java.lang.Object
fr.robie.machineengine.api.player.PlayerDataKey<T>
All Implemented Interfaces:
net.kyori.adventure.key.Keyed

public final class PlayerDataKey<T> extends Object implements net.kyori.adventure.key.Keyed
A value MachineEngine keeps for each player (D37), declared by an addon in its own namespace and registered with RegistrationContext#playerData(). Read and written through MachinePlayer.get(PlayerDataKey)/MachinePlayer.set(PlayerDataKey, T). Values must be immutable.
public static final PlayerDataKey<Double> DOSE = PlayerDataKey.persistent(Key.key("mymod", "dose"),
        VersionedCodec.builder(Key.key("mymod", "dose"), Codecs.DOUBLE).build(), 0.0).resetOnDeath();
public static final PlayerDataKey<Long> LAST_TELEPORT = PlayerDataKey.session(Key.key("mymod", "last_teleport"), 0L);

Keys are immutable: resetOnDeath, migratePdc(String, PersistentDataType) and display return a new key.

  • Method Details

    • persistent

      @NotNull public static <T> @NotNull PlayerDataKey<T> persistent(@NotNull @NotNull net.kyori.adventure.key.Key key, @NotNull @NotNull VersionedCodec<T> codec, @NotNull T defaultValue)
      A value saved with the player's data and loaded again when they join.
    • session

      @NotNull public static <T> @NotNull PlayerDataKey<T> session(@NotNull @NotNull net.kyori.adventure.key.Key key, @NotNull T defaultValue)
      A value kept while the player is online and forgotten when they leave; never saved.
    • resetOnDeath

      @NotNull public @NotNull PlayerDataKey<T> resetOnDeath()
      The value goes back to its default when the player dies.
    • migratePdc

      @NotNull public @NotNull PlayerDataKey<T> migratePdc(@NotNull @NotNull String oldKey, @NotNull @NotNull org.bukkit.persistence.PersistentDataType<?,T> type)
      Where an older version kept the value: a namespace:key entry of the player's PersistentDataContainer. When the player joins, a value found there replaces this key's, and the old entry is removed. Only for persistent(Key, VersionedCodec, T) keys.
    • display

      @NotNull public @NotNull PlayerDataKey<T> display(@NotNull @NotNull Function<? super T, String> display)
      How the value is shown in placeholders (%zmenu_machineengine_player_<namespace>_<id>%) and admin commands; String.valueOf by default.
    • key

      @NotNull public @NotNull net.kyori.adventure.key.Key key()
      Specified by:
      key in interface net.kyori.adventure.key.Keyed
    • codec

      @Nullable public @Nullable VersionedCodec<T> codec()
      How the value is saved; null for a session key.
    • isPersistent

      public boolean isPersistent()
    • defaultValue

      @NotNull public T defaultValue()
    • isResetOnDeath

      public boolean isResetOnDeath()
    • format

      @NotNull public @NotNull String format(@NotNull T value)
      value as display shows it.
    • pdcMigration

      @Nullable public @Nullable PlayerDataKey.PdcMigration<T> pdcMigration()
    • toString

      public String toString()
      Overrides:
      toString in class Object