package net.minestom.server.command; import net.minestom.server.entity.Player; import net.minestom.server.permission.Permission; import java.util.Collection; /** * Represent something which can send commands to the server *
* Main implementations are {@link Player} and {@link ConsoleSender}
*/
public interface CommandSender {
/**
* Send a raw string message
*
* @param message the message to send
*/
void sendMessage(String message);
/**
* Send multiple raw string messages
*
* @param messages the messages to send
*/
default void sendMessage(String[] messages) {
for (String message : messages) {
sendMessage(message);
}
}
/**
* Return all permissions associated to this command sender.
* The returned collection should be modified only by subclasses
*
* @return the permissions of this command sender.
*/
CollectiongetAllPermissions().contains(permission) && permission.isValidFor(this)
for readability.
*
* @param p permission to check against
* @return true if the sender has the permission and validate {@link Permission#isValidFor(CommandSender)}
*/
default boolean hasPermission(Permission p) {
return getAllPermissions().contains(p) && p.isValidFor(this);
}
/**
* Checks if the given {@link Permission} is possessed by this command sender.
* Will call {@link Permission#isValidFor(CommandSender)} on all permissions that are an instance of {@code permissionClass}.
* If no matching permission is found, this result returns false.
*
* @param permissionClass the permission class to check
* @return true if the sender has the permission and validate {@link Permission#isValidFor(CommandSender)}
* @see #getAllPermissions()
*/
default boolean hasPermission(Class extends Permission> permissionClass) {
boolean result = true;
boolean foundPerm = false;
for (Permission p : getAllPermissions()) {
if (permissionClass.isInstance(p)) {
foundPerm = true;
result &= p.isValidFor(this);
}
}
if (!foundPerm)
return false;
return result;
}
/**
* Get if the sender is a player
*
* @return true if 'this' is a player, false otherwise
*/
default boolean isPlayer() {
return this instanceof Player;
}
/**
* Get if the sender is the console
*
* @return true if 'this' is the console, false otherwise
*/
default boolean isConsole() {
return this instanceof ConsoleSender;
}
/**
* Casts this object to a {@link Player}
* No checks are performed, {@link ClassCastException} can very much happen
*
* @see #isPlayer()
*/
default Player asPlayer() {
return (Player) this;
}
/**
* Casts this object to a {@link ConsoleSender}
* No checks are performed, {@link ClassCastException} can very much happen
*
* @see #isConsole()
*/
default ConsoleSender asConsole() {
return (ConsoleSender) this;
}
}