2020-06-21 14:01:03 +02:00
|
|
|
package net.minestom.server.command;
|
|
|
|
|
2020-12-09 16:09:29 +01:00
|
|
|
import net.minestom.server.chat.ColoredText;
|
|
|
|
import net.minestom.server.chat.JsonMessage;
|
|
|
|
import net.minestom.server.chat.RichMessage;
|
2020-06-22 00:48:12 +02:00
|
|
|
import net.minestom.server.entity.Player;
|
2020-11-14 07:06:46 +01:00
|
|
|
import net.minestom.server.permission.PermissionHandler;
|
2020-10-24 16:58:27 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
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-11-14 07:06:46 +01:00
|
|
|
public interface CommandSender extends PermissionHandler {
|
2020-06-21 14:01:03 +02:00
|
|
|
|
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-10-24 16:58:27 +02:00
|
|
|
void sendMessage(@NotNull String message);
|
2020-06-21 14:01:03 +02:00
|
|
|
|
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-10-24 16:58:27 +02:00
|
|
|
default void sendMessage(@NotNull String[] messages) {
|
2020-06-21 14:01:03 +02:00
|
|
|
for (String message : messages) {
|
|
|
|
sendMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 16:09:29 +01:00
|
|
|
/**
|
|
|
|
* Sends a {@link ColoredText} message.
|
|
|
|
*
|
|
|
|
* If this is not a {@link Player}, only the content of the message will be sent as a string.
|
|
|
|
*
|
|
|
|
* @param text The {@link ColoredText} to send.
|
|
|
|
* */
|
|
|
|
default void sendMessage(@NotNull ColoredText text) {
|
|
|
|
if (this instanceof Player) ((Player) this).sendMessage((JsonMessage) text);
|
|
|
|
else sendMessage(text.getMessage());
|
|
|
|
}
|
|
|
|
|
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-11-14 09:02:29 +01:00
|
|
|
* @throws ClassCastException if 'this' is not a player
|
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-11-14 09:02:29 +01:00
|
|
|
* @throws ClassCastException if 'this' is not a console sender
|
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
|
|
|
}
|