Loading...
Loading...
Compare original and translation side by side
CommandRegistryCommandManagerCommandRegistryCommandManagerAbstractCommand
├── CommandBase # Base for simple commands
├── AbstractAsyncCommand # For async execution
├── AbstractPlayerCommand # Requires player sender
├── AbstractWorldCommand # Requires world context
├── AbstractTargetPlayerCommand # Target another player
└── AbstractCommandCollection # Group of subcommandsAbstractCommand
├── CommandBase # 简单命令的基类
├── AbstractAsyncCommand # 用于异步执行
├── AbstractPlayerCommand # 要求发送者为玩家
├── AbstractWorldCommand # 要求世界上下文
├── AbstractTargetPlayerCommand # 以其他玩家为目标
└── AbstractCommandCollection # 子命令组Player Input -> CommandManager -> Parse Arguments -> Check Permissions -> Execute玩家输入 -> CommandManager -> 解析参数 -> 检查权限 -> 执行命令package com.example.myplugin.commands;
import com.hypixel.hytale.server.core.command.CommandBase;
import com.hypixel.hytale.server.core.command.CommandContext;
public class HelloCommand extends CommandBase {
public HelloCommand() {
super("hello", "Says hello to the world");
}
@Override
protected void execute(CommandContext ctx) {
ctx.sendSuccess("Hello, World!");
}
}package com.example.myplugin.commands;
import com.hypixel.hytale.server.core.command.CommandBase;
import com.hypixel.hytale.server.core.command.CommandContext;
public class HelloCommand extends CommandBase {
public HelloCommand() {
super("hello", "Says hello to the world");
}
@Override
protected void execute(CommandContext ctx) {
ctx.sendSuccess("Hello, World!");
}
}@Override
protected void setup() {
getCommandRegistry().registerCommand(new HelloCommand());
getCommandRegistry().registerCommand(new SpawnCommand());
getCommandRegistry().registerCommand(new TeleportCommand());
}@Override
protected void setup() {
getCommandRegistry().registerCommand(new HelloCommand());
getCommandRegistry().registerCommand(new SpawnCommand());
getCommandRegistry().registerCommand(new TeleportCommand());
}| ArgType | Description | Example Value |
|---|---|---|
| Text string | |
| Whole number | |
| Decimal number | |
| True/false | |
| Online player | |
| World name | |
| Item identifier | |
| Block identifier | |
| Entity type | |
| Block position | |
| Precise position | |
| Direction vector | |
| Time duration | |
| JSON object | |
| Rest of input | |
| 参数类型 | 描述 | 示例值 |
|---|---|---|
| 文本字符串 | |
| 整数 | |
| 小数 | |
| 布尔值 | |
| 在线玩家 | |
| 世界名称 | |
| 物品标识符 | |
| 方块标识符 | |
| 实体类型 | |
| 方块位置 | |
| 精确位置 | |
| 方向向量 | |
| 时长 | |
| JSON对象 | |
| 剩余全部输入 | |
// Required argument - must be provided
RequiredArg<String> nameArg = new RequiredArg<>("name", ArgType.STRING);
// Optional argument - can be omitted
OptionalArg<Integer> countArg = new OptionalArg<>("count", ArgType.INTEGER);
// Default argument - uses default if omitted
DefaultArg<Integer> amountArg = new DefaultArg<>("amount", ArgType.INTEGER, 1);
// Flag argument - boolean switch
FlagArg silentFlag = new FlagArg("silent", "s");// 必填参数 - 必须提供
RequiredArg<String> nameArg = new RequiredArg<>("name", ArgType.STRING);
// 可选参数 - 可省略
OptionalArg<Integer> countArg = new OptionalArg<>("count", ArgType.INTEGER);
// 默认参数 - 省略时使用默认值
DefaultArg<Integer> amountArg = new DefaultArg<>("amount", ArgType.INTEGER, 1);
// 标记参数 - 布尔开关
FlagArg silentFlag = new FlagArg("silent", "s");public class GiveCommand extends CommandBase {
private static final RequiredArg<PlayerRef> TARGET =
new RequiredArg<>("target", ArgType.PLAYER_REF);
private static final RequiredArg<ItemId> ITEM =
new RequiredArg<>("item", ArgType.ITEM_ID);
private static final DefaultArg<Integer> AMOUNT =
new DefaultArg<>("amount", ArgType.INTEGER, 1);
private static final FlagArg SILENT =
new FlagArg("silent", "s");
public GiveCommand() {
super("give", "Give items to a player");
addArg(TARGET);
addArg(ITEM);
addArg(AMOUNT);
addArg(SILENT);
}
@Override
protected void execute(CommandContext ctx) {
Player target = ctx.get(TARGET).resolve();
ItemId item = ctx.get(ITEM);
int amount = ctx.get(AMOUNT);
boolean silent = ctx.has(SILENT);
// Give the item
target.getInventory().addItem(item, amount);
if (!silent) {
ctx.sendSuccess("Gave " + amount + "x " + item + " to " + target.getName());
}
}
}public class GiveCommand extends CommandBase {
private static final RequiredArg<PlayerRef> TARGET =
new RequiredArg<>("target", ArgType.PLAYER_REF);
private static final RequiredArg<ItemId> ITEM =
new RequiredArg<>("item", ArgType.ITEM_ID);
private static final DefaultArg<Integer> AMOUNT =
new DefaultArg<>("amount", ArgType.INTEGER, 1);
private static final FlagArg SILENT =
new FlagArg("silent", "s");
public GiveCommand() {
super("give", "Give items to a player");
addArg(TARGET);
addArg(ITEM);
addArg(AMOUNT);
addArg(SILENT);
}
@Override
protected void execute(CommandContext ctx) {
Player target = ctx.get(TARGET).resolve();
ItemId item = ctx.get(ITEM);
int amount = ctx.get(AMOUNT);
boolean silent = ctx.has(SILENT);
// 给予物品
target.getInventory().addItem(item, amount);
if (!silent) {
ctx.sendSuccess("Gave " + amount + "x " + item + " to " + target.getName());
}
}
}executeimport com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import javax.annotation.Nonnull;
public class FlyCommand extends AbstractPlayerCommand {
public FlyCommand() {
super("fly", "Toggle flight mode");
}
@Override
protected void execute(
@Nonnull CommandContext context,
@Nonnull Store<EntityStore> store,
@Nonnull Ref<EntityStore> ref,
@Nonnull PlayerRef playerRef,
@Nonnull World world
) {
// Access player data through PlayerRef
String username = playerRef.getUsername();
// Execute on world thread for world modifications
world.execute(() -> {
context.sendSuccess("Flight toggled for " + username);
});
}
}executeimport com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import javax.annotation.Nonnull;
public class FlyCommand extends AbstractPlayerCommand {
public FlyCommand() {
super("fly", "Toggle flight mode");
}
@Override
protected void execute(
@Nonnull CommandContext context,
@Nonnull Store<EntityStore> store,
@Nonnull Ref<EntityStore> ref,
@Nonnull PlayerRef playerRef,
@Nonnull World world
) {
// 通过PlayerRef访问玩家数据
String username = playerRef.getUsername();
// 在世界线程执行以修改世界
world.execute(() -> {
context.sendSuccess("Flight toggled for " + username);
});
}
}public class TimeCommand extends AbstractWorldCommand {
private static final RequiredArg<Integer> TIME =
new RequiredArg<>("time", ArgType.INTEGER);
public TimeCommand() {
super("time", "Set world time");
addArg(TIME);
}
@Override
protected void execute(CommandContext ctx, World world) {
int time = ctx.get(TIME);
world.setTime(time);
ctx.sendSuccess("Set time to " + time + " in " + world.getName());
}
}public class TimeCommand extends AbstractWorldCommand {
private static final RequiredArg<Integer> TIME =
new RequiredArg<>("time", ArgType.INTEGER);
public TimeCommand() {
super("time", "Set world time");
addArg(TIME);
}
@Override
protected void execute(CommandContext ctx, World world) {
int time = ctx.get(TIME);
world.setTime(time);
ctx.sendSuccess("Set time to " + time + " in " + world.getName());
}
}public class HealCommand extends AbstractTargetPlayerCommand {
public HealCommand() {
super("heal", "Heal a player to full health");
}
@Override
protected void execute(CommandContext ctx, Player target) {
target.setHealth(target.getMaxHealth());
ctx.sendSuccess("Healed " + target.getName());
}
}public class HealCommand extends AbstractTargetPlayerCommand {
public HealCommand() {
super("heal", "Heal a player to full health");
}
@Override
protected void execute(CommandContext ctx, Player target) {
target.setHealth(target.getMaxHealth());
ctx.sendSuccess("Healed " + target.getName());
}
}public class BackupCommand extends AbstractAsyncCommand {
public BackupCommand() {
super("backup", "Create world backup");
}
@Override
protected CompletableFuture<Void> executeAsync(CommandContext ctx) {
return CompletableFuture.runAsync(() -> {
ctx.sendMessage("Starting backup...");
// Perform backup operation
performBackup();
ctx.sendSuccess("Backup complete!");
});
}
}public class BackupCommand extends AbstractAsyncCommand {
public BackupCommand() {
super("backup", "Create world backup");
}
@Override
protected CompletableFuture<Void> executeAsync(CommandContext ctx) {
return CompletableFuture.runAsync(() -> {
ctx.sendMessage("Starting backup...");
// 执行备份操作
performBackup();
ctx.sendSuccess("Backup complete!");
});
}
}public class AdminCommands extends AbstractCommandCollection {
public AdminCommands() {
super("admin", "Admin commands");
// Register subcommands
addSubCommand(new BanSubCommand());
addSubCommand(new KickSubCommand());
addSubCommand(new MuteSubCommand());
}
// Subcommand implementation
private class BanSubCommand extends CommandBase {
private static final RequiredArg<PlayerRef> TARGET =
new RequiredArg<>("target", ArgType.PLAYER_REF);
private static final OptionalArg<String> REASON =
new OptionalArg<>("reason", ArgType.GREEDY_STRING);
public BanSubCommand() {
super("ban", "Ban a player");
addArg(TARGET);
addArg(REASON);
}
@Override
protected void execute(CommandContext ctx) {
Player target = ctx.get(TARGET).resolve();
String reason = ctx.getOrDefault(REASON, "No reason provided");
// Ban logic
ctx.sendSuccess("Banned " + target.getName() + ": " + reason);
}
}
}/admin ban PlayerName Being naughtypublic class AdminCommands extends AbstractCommandCollection {
public AdminCommands() {
super("admin", "Admin commands");
// 注册子命令
addSubCommand(new BanSubCommand());
addSubCommand(new KickSubCommand());
addSubCommand(new MuteSubCommand());
}
// 子命令实现
private class BanSubCommand extends CommandBase {
private static final RequiredArg<PlayerRef> TARGET =
new RequiredArg<>("target", ArgType.PLAYER_REF);
private static final OptionalArg<String> REASON =
new OptionalArg<>("reason", ArgType.GREEDY_STRING);
public BanSubCommand() {
super("ban", "Ban a player");
addArg(TARGET);
addArg(REASON);
}
@Override
protected void execute(CommandContext ctx) {
Player target = ctx.get(TARGET).resolve();
String reason = ctx.getOrDefault(REASON, "No reason provided");
// 封禁逻辑
ctx.sendSuccess("Banned " + target.getName() + ": " + reason);
}
}
}/admin ban PlayerName Being naughty{plugin.group}.{plugin.name}.command.{commandName}com.example.myplugin.command.give{plugin.group}.{plugin.name}.command.{commandName}com.example.myplugin.command.givepublic class SecretCommand extends CommandBase {
public SecretCommand() {
super("secret", "A secret command");
// Override default permission
setPermission("admin.secret.access");
}
@Override
protected void execute(CommandContext ctx) {
ctx.sendSuccess("You found the secret!");
}
}public class SecretCommand extends CommandBase {
public SecretCommand() {
super("secret", "A secret command");
// 覆盖默认权限
setPermission("admin.secret.access");
}
@Override
protected void execute(CommandContext ctx) {
ctx.sendSuccess("You found the secret!");
}
}@Override
protected void execute(CommandContext ctx) {
if (!ctx.hasPermission("special.feature")) {
ctx.sendError("You don't have permission for this feature!");
return;
}
// Execute feature
}@Override
protected void execute(CommandContext ctx) {
if (!ctx.hasPermission("special.feature")) {
ctx.sendError("You don't have permission for this feature!");
return;
}
// 执行功能
}CommandContext@Override
protected void execute(CommandContext ctx) {
// Get sender info
CommandSender sender = ctx.getSender();
boolean isPlayer = ctx.isPlayer();
boolean isConsole = ctx.isConsole();
// Get player if sender is player
Optional<Player> player = ctx.getPlayerSender();
// Get world context
Optional<World> world = ctx.getWorld();
// Send messages
ctx.sendMessage("Plain message");
ctx.sendSuccess("Success message"); // Green
ctx.sendError("Error message"); // Red
ctx.sendWarning("Warning message"); // Yellow
// Get argument values
String name = ctx.get(NAME_ARG);
int count = ctx.getOrDefault(COUNT_ARG, 10);
boolean hasFlag = ctx.has(SOME_FLAG);
// Check permissions
boolean canUse = ctx.hasPermission("some.permission");
}CommandContext@Override
protected void execute(CommandContext ctx) {
// 获取发送者信息
CommandSender sender = ctx.getSender();
boolean isPlayer = ctx.isPlayer();
boolean isConsole = ctx.isConsole();
// 如果发送者是玩家,获取玩家对象
Optional<Player> player = ctx.getPlayerSender();
// 获取世界上下文
Optional<World> world = ctx.getWorld();
// 发送消息
ctx.sendMessage("Plain message");
ctx.sendSuccess("Success message"); // 绿色
ctx.sendError("Error message"); // 红色
ctx.sendWarning("Warning message"); // 黄色
// 获取参数值
String name = ctx.get(NAME_ARG);
int count = ctx.getOrDefault(COUNT_ARG, 10);
boolean hasFlag = ctx.has(SOME_FLAG);
// 检查权限
boolean canUse = ctx.hasPermission("some.permission");
}public class CustomArg extends RequiredArg<String> {
public CustomArg() {
super("mode", ArgType.STRING);
}
@Override
public List<String> getSuggestions(CommandContext ctx, String partial) {
return List.of("easy", "medium", "hard")
.stream()
.filter(s -> s.startsWith(partial.toLowerCase()))
.toList();
}
}public class CustomArg extends RequiredArg<String> {
public CustomArg() {
super("mode", ArgType.STRING);
}
@Override
public List<String> getSuggestions(CommandContext ctx, String partial) {
return List.of("easy", "medium", "hard")
.stream()
.filter(s -> s.startsWith(partial.toLowerCase()))
.toList();
}
}package com.example.admintools;
import com.hypixel.hytale.server.core.plugin.JavaPlugin;
import com.hypixel.hytale.server.core.plugin.JavaPluginInit;
import javax.annotation.Nonnull;
public class AdminToolsPlugin extends JavaPlugin {
public AdminToolsPlugin(@Nonnull JavaPluginInit init) {
super(init);
}
@Override
protected void setup() {
// Register individual commands
getCommandRegistry().registerCommand(new HealCommand());
getCommandRegistry().registerCommand(new FlyCommand());
getCommandRegistry().registerCommand(new TeleportCommand());
// Register command collection
getCommandRegistry().registerCommand(new AdminCommands());
getLogger().atInfo().log("AdminTools commands registered!");
}
}package com.example.admintools;
import com.hypixel.hytale.server.core.plugin.JavaPlugin;
import com.hypixel.hytale.server.core.plugin.JavaPluginInit;
import javax.annotation.Nonnull;
public class AdminToolsPlugin extends JavaPlugin {
public AdminToolsPlugin(@Nonnull JavaPluginInit init) {
super(init);
}
@Override
protected void setup() {
// 注册单个命令
getCommandRegistry().registerCommand(new HealCommand());
getCommandRegistry().registerCommand(new FlyCommand());
getCommandRegistry().registerCommand(new TeleportCommand());
// 注册命令集合
getCommandRegistry().registerCommand(new AdminCommands());
getLogger().atInfo().log("AdminTools commands registered!");
}
}@Override
protected void execute(CommandContext ctx) {
int amount = ctx.get(AMOUNT);
// Validate ranges
if (amount < 1 || amount > 64) {
ctx.sendError("Amount must be between 1 and 64");
return;
}
// Continue execution
}@Override
protected void execute(CommandContext ctx) {
int amount = ctx.get(AMOUNT);
// 验证范围
if (amount < 1 || amount > 64) {
ctx.sendError("Amount must be between 1 and 64");
return;
}
// 继续执行
}@Override
protected void execute(CommandContext ctx) {
try {
Player target = ctx.get(TARGET).resolve();
if (target == null) {
ctx.sendError("Player not found!");
return;
}
// Execute command
} catch (Exception e) {
ctx.sendError("An error occurred: " + e.getMessage());
getLogger().atSevere().withCause(e).log("Command error");
}
}@Override
protected void execute(CommandContext ctx) {
try {
Player target = ctx.get(TARGET).resolve();
if (target == null) {
ctx.sendError("Player not found!");
return;
}
// 执行命令
} catch (Exception e) {
ctx.sendError("An error occurred: " + e.getMessage());
getLogger().atSevere().withCause(e).log("Command error");
}
}@Override
protected void execute(CommandContext ctx) {
// Always provide feedback
ctx.sendMessage("Processing...");
// Do work
// Report result
if (success) {
ctx.sendSuccess("Operation completed!");
} else {
ctx.sendError("Operation failed: " + reason);
}
}@Override
protected void execute(CommandContext ctx) {
// 始终提供反馈
ctx.sendMessage("Processing...");
// 执行操作
// 报告结果
if (success) {
ctx.sendSuccess("Operation completed!");
} else {
ctx.sendError("Operation failed: " + reason);
}
}setup()setup()references/argument-types.mdreferences/command-patterns.mdreferences/argument-types.mdreferences/command-patterns.md