명령어 만들기
첫 번째 단계는 새로운 CommandSpec 빌더를 가져오는 것입니다. 빌더는 명령어의 도움말, 인자, 그리고 로직을 수정하는 메소드를 제공합니다. 이 메소드들은 연결할 수 있습니다.
마지막으로 명령어를 생성하려면, 당신은 CommandSpec.Builder#build() 메소드를 호출해야 합니다.
그 후, 명령어를 등록해야 합니다.
예시: 간단한 명령어 생성
import org.spongepowered.api.Sponge;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.command.spec.CommandSpec;
PluginContainer plugin = ...;
CommandSpec myCommandSpec = CommandSpec.builder()
.description(Text.of("Hello World Command"))
.permission("myplugin.command.helloworld")
.executor(new HelloWorldCommand())
.build();
Sponge.getCommandManager().register(plugin, myCommandSpec, "helloworld", "hello", "test");
Overview of the CommandSpec builder methods
메소드 |
설명 |
---|---|
|
명령어의 로직을 정의합니다 (Writing a Command Executor 를 참고하세요). 만약 자식 명령어가 설정되어 있지 않다면, Executor를 꼭 설정해야 합니다. |
|
Sets the argument specification for this command (See 인자 파싱). |
|
이 명령어를 사용하기 전에 확인할 펄미션을 설정합니다. |
|
도움말 시스템에 표시될 명령어 용도의 짧은 한 줄의 설명입니다. |
|
Sets an extended description to use in longer help listings. Will be appended to the short description. |
|
Adds a child command to this command with its aliases (See 자식 명령어). |
|
Sets the child commands of this command with their aliases (See 자식 명령어). |
|
Defines how the arguments will be parsed. By default, the parser splits the command input by spaces. Quotations count as a single argument. 예시: |
|
Builds the command. After that, you have to register the command. |
Command Executor 작성하기
The only required component to build a simple command is the command executor class, which contains the logic of the command.
The class has to implement the CommandExecutor interface, which defines the CommandExecutor#execute(CommandSource, CommandContext) method. The method is called on command execution and has two arguments:
The source of the command call (e.g. the console, a command block or a player)
The command context object, which contains the parsed arguments (See 인자 파싱)
예시: 간단한 Command Executor
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
public class HelloWorldCommand implements CommandExecutor {
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
src.sendMessage(Text.of("Hello World!"));
return CommandResult.success();
}
}
팁
You can use anonymous classes to define the command executor in the command build process (See the example in the 인자 파싱 page).
Player-Only Commands
가끔 오직 플레이어만 명령어를 실행할 수 있어야 하는 경우가 있습니다 (e.g. /suicide
명령어).
Perform an instanceof
check to determine the type of the CommandSource:
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.command.source.CommandBlockSource;
import org.spongepowered.api.command.source.ConsoleSource;
if (src instanceof Player) {
Player player = (Player) src;
player.sendMessage(Text.of("Hello " + player.getName() + "!"));
}
else if(src instanceof ConsoleSource) {
src.sendMessage(Text.of("Hello GLaDOS!"));
// The Cake Is a Lie
}
else if(src instanceof CommandBlockSource) {
src.sendMessage(Text.of("Hello Companion Cube!"));
// <3
}
참고
We recommend you to add an optional [player]
argument to make the command console-friendly (e.g. /suicide
[player]
).
The easiest solution for this is to append a playerOrSource
command element (See 인자 파싱).
명령어 결과
CommandExecutor#execute()
메소드는 항상 무조건 CommandResult 를 반환합니다. 대부분의 경우 성공하면 CommandResult#success() 를 반환하거나, 그렇지 않으면 :javadoc:`CommandResult#empty()`를 반환하면 충분합니다. 더 많은 정보를 전달해야 하는 경우는, :javadoc:`CommandResult#builder()`를 사용해야 합니다. 빌더는 정수를 받아들이고 동일한 이름의 속성을 설정할 수 있는 여러 메소드를 제공합니다. 빌더가 설정하지 않은 속성들은 비어있게 됩니다.
Command blocks can use those values to modify scoreboard stats, which then can be used for elaborate constructions consisting of multiple command blocks. A tutorial how the data is accessed can be found here.
예시: CommandResult 생성
CommandResult result = CommandResult.builder()
.affectedEntities(42)
.successCount(1)
.build();
This example uses a builder to create a CommandResult
for a command which affected 42 entities and was successful.
Error Handling
The execute()
method may also throw a CommandException, signaling that an error occurred while trying to
execute the command. If such an Exception is thrown, its message will be displayed to the command source, formatted as
an error. Also, the commands usage message will be displayed. An ArgumentParseException, a subtype of
CommandException
is automatically thrown if the commands arguments could not be parsed.