Interface Context<T>

Type Parameters:
T - The type of the context.
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Context<T>
Represents an object context. This class is a simple generic type wrapper that can be used to ensure data types. This class is also used to provide a simple way to map data types.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default @Nullable org.bukkit.event.block.Action
     
    default @Nullable Boolean
    Gets the context as a boolean.
    default @Nullable org.bukkit.command.CommandSender
     
    default @Nullable net.kyori.adventure.text.Component
     
    default <U> U
    asCustom(Class<U> clazz)
    Gets the context as a custom class.
    default @Nullable Double
     
    default @Nullable Float
     
    default @Nullable Integer
     
    default @Nullable org.bukkit.entity.LivingEntity
     
    default @Nullable org.bukkit.Location
     
    default @Nullable Long
     
    default @Nullable org.bukkit.entity.Player
     
    default @Nullable org.bukkit.entity.Projectile
     
    default @Nullable String
    Gets the context as a string.
    default @Nullable org.bukkit.World
     
    get()
    Gets the context.
    default @NotNull String
    This is the same as calling get() and then calling Object.toString() on the result.
    default <S> Context<S>
    map(@NotNull Function<T,S> mapper)
    Maps the context to another context.
  • Method Details

    • map

      default <S> Context<S> map(@NotNull @NotNull Function<T,S> mapper)
      Maps the context to another context.
      Type Parameters:
      S - The type of the mapped context.
      Parameters:
      mapper - The mapper function.
      Returns:
      The mapped context.
    • get

      T get()
      Gets the context.
      Returns:
      The context.
    • asString

      @Nullable default @Nullable String asString()
      Gets the context as a string.
      Returns:
      The context as a string.
    • asBoolean

      @Nullable default @Nullable Boolean asBoolean()
      Gets the context as a boolean.
      Returns:
      The context as a boolean.
    • asDouble

      @Nullable default @Nullable Double asDouble()
      Returns:
      The context as a Double.
    • asInt

      @Nullable default @Nullable Integer asInt()
      Returns:
      The context as a Integer.
    • asLong

      @Nullable default @Nullable Long asLong()
      Returns:
      The context as a Byte.
    • asFloat

      @Nullable default @Nullable Float asFloat()
      Returns:
      The context as a Float.
    • asPlayer

      @Nullable default @Nullable org.bukkit.entity.Player asPlayer()
      Returns:
      The context as a Player.
    • asCommandSender

      @Nullable default @Nullable org.bukkit.command.CommandSender asCommandSender()
      Returns:
      The context as a CommandSender.
    • literal

      @NotNull default @NotNull String literal()
      This is the same as calling get() and then calling Object.toString() on the result.
      Returns:
      The context as a String literal.
    • asWorld

      @Nullable default @Nullable org.bukkit.World asWorld()
      Returns:
      The context as a World.
    • asLocation

      @Nullable default @Nullable org.bukkit.Location asLocation()
      Returns:
      The context as a Location.
    • asLivingEntity

      @Nullable default @Nullable org.bukkit.entity.LivingEntity asLivingEntity()
      Returns:
      The context as a LivingEntity.
    • asComponent

      @Nullable default @Nullable net.kyori.adventure.text.Component asComponent()
      Returns:
      The context as a Component.
    • asProjectile

      @Nullable default @Nullable org.bukkit.entity.Projectile asProjectile()
      Returns:
      The context as a Projectile.
    • asAction

      @Nullable default @Nullable org.bukkit.event.block.Action asAction()
      Returns:
      The context as an Action.
    • asCustom

      @Nullable default <U> U asCustom(Class<U> clazz)
      Gets the context as a custom class. This will cast the object to the class if it is an instance of it.
      Typically, Context objects are useful when used to collect unknown data and then cast it to a known type.
      In the case where we know what the data should be but the compiler or the runtime does not, the object is wrapped in a Context which then exposes multiple methods to get the data as one of the known types.

      For example, if we have a Context<Object> and we already know that the wrapped object should be of type X, we can use X.class on this method to retrieve the actual object. That is, to say, if there is not already a predefined method to get the object as the type we want.

      Type Parameters:
      U -
      Parameters:
      clazz -
      Returns: