Class BukkitCommandSource

java.lang.Object
studio.mevera.imperat.BukkitCommandSource
All Implemented Interfaces:
studio.mevera.imperat.adventure.AdventureCommandSource, studio.mevera.imperat.context.CommandSource

public class BukkitCommandSource extends Object implements studio.mevera.imperat.adventure.AdventureCommandSource
A Bukkit-specific implementation of CommandSource that wraps a Bukkit CommandSender. This class provides a bridge between Bukkit's command system and the Imperat framework, supporting both legacy Bukkit messaging and modern Adventure API components.

Features:

  • Support for both Player and Console command sources
  • Adventure API integration for rich text messaging
  • Legacy color code support for backward compatibility
  • Type-safe casting to Player or CommandSender
  • UUID-based identification for players

Usage Example:


 // In a command method
 @Command("teleport")
 public void teleport(BukkitCommandSource source, Player target) {
     if (source.isConsole()) {
         source.error("Only players can teleport!");
         return;
     }
     Player player = source.asPlayer();
     // ... teleport logic
 }
 
Since:
1.0
See Also:
  • CommandSource
  • CommandSender
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final studio.mevera.imperat.adventure.AdventureProvider<org.bukkit.command.CommandSender>
     
    protected final org.bukkit.command.CommandSender
     

    Fields inherited from interface studio.mevera.imperat.context.CommandSource

    CONSOLE_UUID
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    BukkitCommandSource(org.bukkit.command.CommandSender sender, studio.mevera.imperat.adventure.AdventureProvider<org.bukkit.command.CommandSender> provider)
    Creates a new BukkitCommandSource wrapping the specified CommandSender.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    as(Class<T> clazz)
    Casts the origin command sender to the specified class type.
    org.bukkit.entity.Player
    Casts this source to a Player, if possible.
    boolean
     
    void
    error(String message)
    Sends an error message to the command sender.
    studio.mevera.imperat.adventure.AdventureProvider<org.bukkit.command.CommandSender>
    Gets the Adventure provider used for rich text messaging.
    int
     
    boolean
    Checks if the command source is from the console.
    Gets the name of this command source.
    org.bukkit.command.CommandSender
    Gets the original command sender instance.
    void
    reply(String message)
    Replies to the command sender with a string message.
    void
    reply(net.kyori.adventure.text.ComponentLike component)
    Replies to the command sender with a component message.
    Gets the UUID of the command source.
    void
    warn(String message)
    Sends a warning message to the command sender.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • sender

      protected final org.bukkit.command.CommandSender sender
    • provider

      protected final studio.mevera.imperat.adventure.AdventureProvider<org.bukkit.command.CommandSender> provider
  • Constructor Details

    • BukkitCommandSource

      protected BukkitCommandSource(org.bukkit.command.CommandSender sender, studio.mevera.imperat.adventure.AdventureProvider<org.bukkit.command.CommandSender> provider)
      Creates a new BukkitCommandSource wrapping the specified CommandSender.
      Parameters:
      sender - the Bukkit CommandSender to wrap (player or console)
      provider - the Adventure provider for rich text support
  • Method Details

    • getProvider

      public studio.mevera.imperat.adventure.AdventureProvider<org.bukkit.command.CommandSender> getProvider()
      Gets the Adventure provider used for rich text messaging.
      Returns:
      the Adventure provider instance
    • name

      public String name()
      Gets the name of this command source.
      Specified by:
      name in interface studio.mevera.imperat.context.CommandSource
      Returns:
      the player name if this is a player, or "CONSOLE" if this is the console
    • origin

      public org.bukkit.command.CommandSender origin()
      Gets the original command sender instance. This can be used to access Bukkit's API directly if needed.
      Specified by:
      origin in interface studio.mevera.imperat.context.CommandSource
      Returns:
      the original CommandSender instance
    • asPlayer

      public org.bukkit.entity.Player asPlayer()
      Casts this source to a Player, if possible.
      Returns:
      the Player instance, or null if this source is not a player
    • reply

      public void reply(String message)
      Replies to the command sender with a string message. This method supports legacy color codes (&f) for backward compatibility.
      Specified by:
      reply in interface studio.mevera.imperat.context.CommandSource
      Parameters:
      message - the message to send
    • warn

      public void warn(String message)
      Sends a warning message to the command sender. Warning messages are prefixed with yellow color.
      Specified by:
      warn in interface studio.mevera.imperat.context.CommandSource
      Parameters:
      message - the warning message
    • error

      public void error(String message)
      Sends an error message to the command sender. Error messages are prefixed with red color.
      Specified by:
      error in interface studio.mevera.imperat.context.CommandSource
      Parameters:
      message - the error message
    • reply

      public void reply(net.kyori.adventure.text.ComponentLike component)
      Replies to the command sender with a component message. This method should be used for sending rich text messages using the Adventure API.
      Specified by:
      reply in interface studio.mevera.imperat.adventure.AdventureCommandSource
      Parameters:
      component - the message component
    • isConsole

      public boolean isConsole()
      Checks if the command source is from the console.
      Specified by:
      isConsole in interface studio.mevera.imperat.context.CommandSource
      Returns:
      true if this source is from the console, false if from a player
    • uuid

      public UUID uuid()
      Gets the UUID of the command source. For console sources, a constant UUID is returned.
      Specified by:
      uuid in interface studio.mevera.imperat.context.CommandSource
      Returns:
      the UUID of the source
    • as

      public <T> T as(Class<T> clazz)
      Casts the origin command sender to the specified class type. This method is type-safe and avoids the need for explicit casting.
      Specified by:
      as in interface studio.mevera.imperat.context.CommandSource
      Type Parameters:
      T - the type parameter
      Parameters:
      clazz - the class to cast to
      Returns:
      the casted instance
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object