Package me.totalfreedom.api
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.
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 TypeMethodDescriptiondefault @Nullable org.bukkit.event.block.ActionasAction()default @Nullable BooleanGets the context as a boolean.default @Nullable org.bukkit.command.CommandSenderdefault @Nullable net.kyori.adventure.text.Componentdefault <U> UGets the context as a custom class.default @Nullable DoubleasDouble()default @Nullable FloatasFloat()default @Nullable IntegerasInt()default @Nullable org.bukkit.entity.LivingEntitydefault @Nullable org.bukkit.Locationdefault @Nullable LongasLong()default @Nullable org.bukkit.entity.PlayerasPlayer()default @Nullable org.bukkit.entity.Projectiledefault @Nullable StringasString()Gets the context as a string.default @Nullable org.bukkit.WorldasWorld()get()Gets the context.default @NotNull Stringliteral()This is the same as callingget()and then callingObject.toString()on the result.default <S> Context<S>Maps the context to another context.
-
Method Details
-
map
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
Gets the context as a string.- Returns:
- The context as a string.
-
asBoolean
Gets the context as a boolean.- Returns:
- The context as a boolean.
-
asDouble
- Returns:
- The context as a
Double.
-
asInt
- Returns:
- The context as a
Integer.
-
asLong
- Returns:
- The context as a
Byte.
-
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
This is the same as callingget()and then callingObject.toString()on the result.- Returns:
- The context as a
Stringliteral.
-
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
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.classon 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:
-