Commands
Dokumentace k příkazovému systému v Hytale.
Soubory
- COMMAND_TYPES.md - Typy příkazů a kdy který použít
- ARGUMENTS.md - Argumenty příkazů a ArgTypes
Rychlý Přehled
Který Typ Použít?
| Potřeba | Třída |
|---------|-------|
| Jednoduchý příkaz | CommandBase |
| Přístup ke komponentům | AbstractPlayerCommand |
| Async operace (DB, soubory) | AbstractAsyncCommand |
| Sub-příkazy | AbstractCommandCollection |
Minimální Příklad
// Jednoduchý příkaz
public class HelloCommand extends CommandBase {
public HelloCommand() {
super("hello", "myplugin.commands.hello.desc");
} @Override
protected void executeSync(@Nonnull CommandContext context) {
context.sendMessage(Message.raw("Hello!"));
}
}
// Příkaz s komponenty
public class StatsCommand extends AbstractPlayerCommand {
public StatsCommand() {
super("stats", "myplugin.commands.stats.desc");
}
@Override
protected void execute(
@Nonnull CommandContext context,
@Nonnull Store store,
@Nonnull Ref ref,
@Nonnull PlayerRef playerRef,
@Nonnull World world
) {
Player player = store.getComponent(ref, Player.getComponentType());
// Bezpečný přístup ke komponentům
}
}
// Registrace
@Override
protected void setup() {
getCommandRegistry().registerCommand(new HelloCommand());
getCommandRegistry().registerCommand(new StatsCommand());
}
Argumenty
// Povinný
RequiredArg nameArg = withRequiredArg("name", "desc", ArgTypes.STRING);// Volitelný
OptionalArg countArg = withOptionalArg("count", "desc", ArgTypes.INTEGER);
// S výchozí hodnotou
DefaultArg timesArg = withDefaultArg("times", "desc", ArgTypes.INTEGER, 1, "1");
// Použití
String name = nameArg.get(context);
if (countArg.provided(context)) {
int count = countArg.get(context);
}
int times = timesArg.get(context); // Nikdy null
Časté Chyby
| Problém | Řešení |
|---------|--------|
| "Assert not in thread!" | Použij AbstractPlayerCommand |
| Argument je null | Použij provided() pro OptionalArg |
| Příkaz se nezobrazuje | Registruj v setup() |