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

87 lines
2.4 KiB
Java
Raw Normal View History

2020-06-21 14:01:03 +02:00
package net.minestom.server.command;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.chat.RichMessage;
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-07-21 18:48:15 +02:00
/**
* Represents something which can send commands to the server.
2020-07-21 18:48:15 +02:00
* <p>
* 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);
}
}
/**
* 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
*/
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
*/
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
*
* @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
*
* @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
}