Minestom/src/main/java/net/minestom/server/command/CommandSender.java

125 lines
3.3 KiB
Java
Raw Normal View History

2020-06-21 14:01:03 +02:00
package net.minestom.server.command;
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-07-21 18:48:15 +02:00
/**
* Represent something which can send commands to the server
* <p>
* Main implementations are {@link Player} and {@link ConsoleSender}
*/
2020-06-21 14:01:03 +02:00
public interface CommandSender {
2020-07-21 18:48:15 +02:00
/**
* Send a raw string message
*
* @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
/**
* Send multiple raw string messages
*
* @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
/**
* Return all permissions associated to this command sender.
* The returned collection should be modified only by subclasses
* @return
*/
Collection<Permission> getAllPermissions();
/**
* Adds a permission to this commandSender
* @param permission
*/
default void addPermission(Permission permission) {
getAllPermissions().add(permission);
}
/**
* Removes a permission from this commandSender
* @param permission
*/
default void removePermission(Permission permission) {
getAllPermissions().remove(permission);
}
/**
* Checks if the given permission is possessed by this command sender.
* Simple shortcut to <pre>getAllPermissions().contains(permission) && permission.isValidFor(this)</pre> for readability.
2020-07-31 22:31:58 +02:00
* @param p permission to check against
* @return
*/
default boolean hasPermission(Permission p) {
return getAllPermissions().contains(p) && p.isValidFor(this);
2020-07-31 22:31:58 +02:00
}
/**
* Checks if the given permission is possessed by this command sender.
2020-07-31 22:38:03 +02:00
* Will call {@link Permission#isValidFor(CommandSender)} on all permissions that are an instance of permissionClass.
2020-07-31 22:31:58 +02:00
* If no matching permission is found, this result returns false.
*
* @param permissionClass
* @see #getAllPermissions()
* @return
*/
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;
}
2020-07-21 18:48:15 +02:00
/**
* Get if the sender is a player
*
* @return true if 'this' is a player, false otherwise
*/
default boolean isPlayer() {
return this instanceof Player;
}
2020-07-21 18:48:15 +02:00
/**
* Get if the sender is the console
*
* @return true if 'this' is the console, false otherwise
*/
default boolean isConsole() {
return this instanceof ConsoleSender;
}
2020-07-31 22:31:58 +02:00
/**
* Casts this object to a Player
* No checks are performed, {@link ClassCastException} can very much happen
* @see #isPlayer()
*/
default Player asPlayer() {
return (Player)this;
}
/**
* Casts this object to a ConsoleSender
* No checks are performed, {@link ClassCastException} can very much happen
* @see #isConsole()
*/
default ConsoleSender asConsole() {
return (ConsoleSender)this;
}
2020-06-21 14:01:03 +02:00
}