2020-06-21 14:01:03 +02:00
|
|
|
package net.minestom.server.command;
|
|
|
|
|
2020-06-22 00:48:12 +02:00
|
|
|
import net.minestom.server.entity.Player;
|
2020-07-31 22:31:58 +02:00
|
|
|
import net.minestom.server.permission.Permission;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
2020-06-22 00:48:12 +02:00
|
|
|
|
2020-07-21 18:48:15 +02:00
|
|
|
/**
|
2020-10-17 13:24:18 +02:00
|
|
|
* Represents something which can send commands to the server.
|
2020-07-21 18:48:15 +02:00
|
|
|
* <p>
|
2020-10-17 13:24:18 +02:00
|
|
|
* Main implementations are {@link Player} and {@link ConsoleSender}.
|
2020-07-21 18:48:15 +02:00
|
|
|
*/
|
2020-06-21 14:01:03 +02:00
|
|
|
public interface CommandSender {
|
|
|
|
|
2020-07-21 18:48:15 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Sends a raw string message.
|
2020-07-21 18:48:15 +02:00
|
|
|
*
|
|
|
|
* @param message the message to send
|
|
|
|
*/
|
2020-06-21 14:01:03 +02:00
|
|
|
void sendMessage(String message);
|
|
|
|
|
2020-07-21 18:48:15 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Sends multiple raw string messages.
|
2020-07-21 18:48:15 +02:00
|
|
|
*
|
|
|
|
* @param messages the messages to send
|
|
|
|
*/
|
2020-06-21 14:01:03 +02:00
|
|
|
default void sendMessage(String[] messages) {
|
|
|
|
for (String message : messages) {
|
|
|
|
sendMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 22:31:58 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Returns all permissions associated to this command sender.
|
|
|
|
* The returned collection should be modified only by subclasses.
|
2020-10-11 18:35:32 +02:00
|
|
|
*
|
|
|
|
* @return the permissions of this command sender.
|
2020-07-31 22:31:58 +02:00
|
|
|
*/
|
|
|
|
Collection<Permission> getAllPermissions();
|
|
|
|
|
|
|
|
/**
|
2020-10-11 18:35:32 +02:00
|
|
|
* Adds a {@link Permission} to this commandSender
|
|
|
|
*
|
|
|
|
* @param permission the permission to add
|
2020-07-31 22:31:58 +02:00
|
|
|
*/
|
|
|
|
default void addPermission(Permission permission) {
|
|
|
|
getAllPermissions().add(permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-11 18:35:32 +02:00
|
|
|
* Removes a {@link Permission} from this commandSender
|
|
|
|
*
|
|
|
|
* @param permission the permission to remove
|
2020-07-31 22:31:58 +02:00
|
|
|
*/
|
|
|
|
default void removePermission(Permission permission) {
|
|
|
|
getAllPermissions().remove(permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-11 18:35:32 +02:00
|
|
|
* Checks if the given {@link Permission} is possessed by this command sender.
|
2020-09-20 15:04:07 +02:00
|
|
|
* Simple shortcut to <pre>getAllPermissions().contains(permission) && permission.isValidFor(this)</pre> for readability.
|
2020-10-11 18:35:32 +02:00
|
|
|
*
|
2020-07-31 22:31:58 +02:00
|
|
|
* @param p permission to check against
|
2020-10-11 18:35:32 +02:00
|
|
|
* @return true if the sender has the permission and validate {@link Permission#isValidFor(CommandSender)}
|
2020-07-31 22:31:58 +02:00
|
|
|
*/
|
|
|
|
default boolean hasPermission(Permission p) {
|
2020-07-31 22:37:33 +02:00
|
|
|
return getAllPermissions().contains(p) && p.isValidFor(this);
|
2020-07-31 22:31:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-11 18:35:32 +02:00
|
|
|
* 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}.
|
2020-07-31 22:31:58 +02:00
|
|
|
* If no matching permission is found, this result returns false.
|
|
|
|
*
|
2020-10-11 18:35:32 +02:00
|
|
|
* @param permissionClass the permission class to check
|
|
|
|
* @return true if the sender has the permission and validate {@link Permission#isValidFor(CommandSender)}
|
2020-07-31 22:31:58 +02:00
|
|
|
* @see #getAllPermissions()
|
|
|
|
*/
|
|
|
|
default boolean hasPermission(Class<? extends Permission> permissionClass) {
|
|
|
|
boolean result = true;
|
|
|
|
boolean foundPerm = false;
|
2020-10-11 18:35:32 +02:00
|
|
|
for (Permission p : getAllPermissions()) {
|
|
|
|
if (permissionClass.isInstance(p)) {
|
2020-07-31 22:31:58 +02:00
|
|
|
foundPerm = true;
|
|
|
|
result &= p.isValidFor(this);
|
|
|
|
}
|
|
|
|
}
|
2020-10-11 18:35:32 +02:00
|
|
|
if (!foundPerm)
|
2020-07-31 22:31:58 +02:00
|
|
|
return false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:48:15 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Gets if the sender is a {@link Player}.
|
2020-07-21 18:48:15 +02:00
|
|
|
*
|
|
|
|
* @return true if 'this' is a player, false otherwise
|
|
|
|
*/
|
2020-06-22 00:48:12 +02:00
|
|
|
default boolean isPlayer() {
|
|
|
|
return this instanceof Player;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:48:15 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Gets if the sender is a {@link ConsoleSender}.
|
2020-07-21 18:48:15 +02:00
|
|
|
*
|
|
|
|
* @return true if 'this' is the console, false otherwise
|
|
|
|
*/
|
2020-06-22 00:48:12 +02:00
|
|
|
default boolean isConsole() {
|
|
|
|
return this instanceof ConsoleSender;
|
|
|
|
}
|
|
|
|
|
2020-07-31 22:31:58 +02:00
|
|
|
/**
|
2020-10-12 02:56:30 +02:00
|
|
|
* Casts this object to a {@link Player}.
|
|
|
|
* No checks are performed, {@link ClassCastException} can very much happen.
|
2020-10-11 18:35:32 +02:00
|
|
|
*
|
2020-07-31 22:31:58 +02:00
|
|
|
* @see #isPlayer()
|
|
|
|
*/
|
|
|
|
default Player asPlayer() {
|
2020-10-11 18:35:32 +02:00
|
|
|
return (Player) this;
|
2020-07-31 22:31:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-12 02:56:30 +02:00
|
|
|
* Casts this object to a {@link ConsoleSender}.
|
|
|
|
* No checks are performed, {@link ClassCastException} can very much happen.
|
2020-10-11 18:35:32 +02:00
|
|
|
*
|
2020-07-31 22:31:58 +02:00
|
|
|
* @see #isConsole()
|
|
|
|
*/
|
|
|
|
default ConsoleSender asConsole() {
|
2020-10-11 18:35:32 +02:00
|
|
|
return (ConsoleSender) this;
|
2020-07-31 22:31:58 +02:00
|
|
|
}
|
2020-06-21 14:01:03 +02:00
|
|
|
}
|