Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommandHelp #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions core/src/main/java/cn/lunadeer/dominion/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,43 @@

public class Commands implements TabExecutor {

public static List<String> boolOptions() {
return Arrays.asList("true", "false");
}

public static List<String> playerNames() {
List<PlayerDTO> players = PlayerController.allPlayers();
List<String> names = new ArrayList<>();
for (PlayerDTO player : players) {
names.add(player.getLastKnownName());
}
return names;
}

/**
* Executes the given command, returning its success.
* 执行给定的命令,返回是否执行成功。
* <br>
* If false is returned, then the "usage" plugin.yml entry for this command
* (if defined) will be sent to the player.
* 如果返回 false,则此命令在plugin.yml中的 “usage”(用法)
* (如果已定义) 将发送给玩家。
*
* @param sender Source of the command
* @param command Command which was executed
* @param label Alias of the command which was used
* @param args Passed command arguments
* @return true if a valid command, otherwise false
* @param sender 命令的来源
* @param command 执行的命令
* @param label 使用的命令的别名
* @param args 传递的命令参数
* @return 如果命令执行成功则为 true,否则为 false
*/
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
//当args长度为0时执行,即使用命令“/dom”或者“/dominion”时
if (args.length == 0) {
//显示主菜单
Menu.show(sender, args);
return true;
}
//当args不为0时,取出0索引的值用来确定使用了什么子命令,如“/dom menu”则args[0]为“menu”
switch (args[0]) {
case "menu":
//显示主菜单
Menu.show(sender, args);
break;
case "list":
Expand Down Expand Up @@ -196,17 +213,17 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

/**
* Requests a list of possible completions for a command argument.
* 请求 command 参数的可能列表,使用Tab补全。
*
* @param sender Source of the command. For players tab-completing a
* command inside a command block, this will be the player, not
* the command block.
* @param command Command which was executed
* @param label Alias of the command which was used
* @param args The arguments passed to the command, including final
* partial argument to be completed
* @return A List of possible completions for the final argument, or null
* to default to the command executor
* @param sender 命令的来源。 对于玩家 Tab 键补全
* 命令,这将是玩家,而不是
* 命令方块。
* @param command 执行的命令
* @param label 使用的命令的别名
* @param args 传递给命令的参数,包括 final
* 部分参数待完成
* @return 最后一个参数的可能完成列表,或 null
* 默认为命令 executor
*/
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
Expand Down Expand Up @@ -303,17 +320,4 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}
return null;
}

public static List<String> boolOptions() {
return Arrays.asList("true", "false");
}

public static List<String> playerNames() {
List<PlayerDTO> players = PlayerController.allPlayers();
List<String> names = new ArrayList<>();
for (PlayerDTO player : players) {
names.add(player.getLastKnownName());
}
return names;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,31 @@ public class Translation extends Localization {

@i18nField(defaultValue = "你不是领地 %s 的拥有者或管理员,无权访问此页面")
public static i18n TUI_NotDominionOwnerOrAdminForPage;
@i18nField(defaultValue = "领地插件命令帮助")
@i18nField(defaultValue = "Dominion 命令帮助")
public static i18n TUI_CommandHelp_Title;
@i18nField(defaultValue = "<>表示必填参数 []表示可选参数")
public static i18n TUI_CommandHelp_SubTitle;
@i18nField(defaultValue = "领地管理相关指令帮助")
public static i18n TUI_CommandHelp_DominionManageDescription;
@i18nField(defaultValue = "成员管理相关指令帮助")
public static i18n TUI_CommandHelp_MemberManageDescription;
@i18nField(defaultValue = "权限组管理相关指令帮助")
public static i18n TUI_CommandHelp_GroupManageDescription;
@i18nField(defaultValue = "杂项相关指令帮助")
public static i18n TUI_CommandHelp_MiscDescription;
@i18nField(defaultValue = "管理员指令帮助")
public static i18n TUI_CommandHelp_OpCommandDescription;

@i18nField(defaultValue = "领地管理")
public static i18n TUI_CommandHelp_DominionManageButton;
@i18nField(defaultValue = "成员管理")
public static i18n TUI_CommandHelp_MemberManageButton;
@i18nField(defaultValue = "权限组管理")
public static i18n TUI_CommandHelp_GroupManageButton;
@i18nField(defaultValue = "杂项")
public static i18n TUI_CommandHelp_MiscButton;
@i18nField(defaultValue = "管理员指令")
public static i18n TUI_CommandHelp_OpCommandButton;

@i18nField(defaultValue = "主菜单")
public static i18n TUI_Navigation_Menu;
Expand Down Expand Up @@ -546,6 +567,8 @@ public class Translation extends Localization {
public static i18n TUI_Navigation_MigrateList;
@i18nField(defaultValue = "权限组称号列表")
public static i18n TUI_Navigation_TitleList;
@i18nField(defaultValue = "帮助列表总览")
public static i18n TUI_Navigation_CommandHelpList;

@i18nField(defaultValue = "管理")
public static i18n TUI_ManageButton;
Expand Down
41 changes: 38 additions & 3 deletions core/src/main/java/cn/lunadeer/dominion/uis/tuis/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,34 @@

import static cn.lunadeer.dominion.utils.CommandUtils.playerOnly;

/**
* 显示聊天框主菜单GUI
*/
public class Menu {

/**
* 在聊天栏显示主菜单,包含各指令的快捷按钮和简单介绍
*
* @param sender 命令的来源
* @param args 传递的命令参数
*/
public static void show(CommandSender sender, String[] args) {
//调用playerOnly方法验证sender对象是否为Player并存储在player变量中
Player player = playerOnly(sender);
//如果player为null直接return
if (player == null) return;

//初始化页码为1
int page = 1;
if (args.length == 2) {
try {
//确定要显示的页码
page = Integer.parseInt(args[1]);
} catch (Exception ignored) {
}
}

//链式创建菜单中的标题以及各命令的按钮和指令简介组件
Line create = Line.create()
.append(Button.create(Translation.TUI_Menu_CreateDominionButton).setExecuteCommand("/dominion cui_create").build())
.append(Translation.TUI_Menu_CreateDominionDescription);
Expand All @@ -38,9 +53,7 @@ public static void show(CommandSender sender, String[] args) {
.append(Button.create(Translation.TUI_Menu_TemplateListButton).setExecuteCommand("/dominion template list").build())
.append(Translation.TUI_Menu_TemplateListDescription);
Line help = Line.create()
.append(Button.create(Translation.TUI_Menu_CommandHelpButton).setOpenURL(
String.format("https://dominion.lunadeer.cn/%s/command-list.html", Dominion.config.getLanguage())
).build())
.append(Button.create(Translation.TUI_Menu_CommandHelpButton).setExecuteCommand("/dominion help").build())
.append(Translation.TUI_Menu_CommandHelpDescription);
Line link = Line.create()
.append(Button.create(Translation.TUI_Menu_DocumentButton).setOpenURL(
Expand All @@ -59,25 +72,47 @@ public static void show(CommandSender sender, String[] args) {
Line reload_config = Line.create()
.append(Button.create(Translation.TUI_Menu_ReloadConfigButton).setExecuteCommand("/dominion reload_config").build())
.append(Translation.TUI_Menu_ReloadConfigDescription);

//以下用于将上方的组件添加到菜单中

//设置主菜单十行为一页以及触发指令
ListView view = ListView.create(10, "/dominion menu");
//设置主菜单标题
view.title(Translation.TUI_Menu_Title);
//设置当前所在菜单名称
view.navigator(Line.create().append(Translation.TUI_Navigation_Menu));
//添加第一行指令为创建领地指令
view.add(create);
//添加第二行指令为领地列表指令
view.add(list);
//从配置文件获取GroupTitle是否开启,如果开启添加第三行指令为称号列表,默认关闭
if (Dominion.config.getGroupTitleEnable()) view.add(title);
//添加第四行(如果GroupTitle开启)指令为成员权限模版列表
view.add(template);
//添加第五行(如果GroupTitle开启)指令为跳转帮助列表网页,其中%s会被替换为对应语言,如zh-cn
view.add(help);
//添加第六行(如果GroupTitle开启)指令为跳转使用帮助网页,其中%s会被替换为对应语言,如zh-cn
view.add(link);
//获取配置文件ResidenceMigration的boolean值
if (Dominion.config.getResidenceMigration()) {
//如果开启添加第七行指令为迁移数据,用于帮助从Residence迁移数据至Dominion
view.add(migrate);
}
//判断player是否拥有op权限
if (player.isOp()) {
//添加一行空白作为间隔
view.add(Line.create().append(""));
//添加一行提示“--- 以下选项仅OP可见 ---”
view.add(Line.create().append(Component.text(Translation.TUI_Menu_OpOnlySection.trans(), ViewStyles.main_color)));
//添加指令“所有领地”用以查看所有的领地
view.add(all);
//添加“重载缓存”,用以重新加载领地缓存数据
view.add(reload_cache);
//添加“重载配置”,用以重新加载配置文件
view.add(reload_config);
}
//将请求显示菜单GUI的player对象和需要显示的页码传给showOn以显示GUI
//由showOn检查page是否存在,若不存在则会在菜单第一行添加“页码错误”
view.showOn(player, page);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
import static cn.lunadeer.dominion.utils.TuiUtils.getPage;

public class DominionList {
/**
* 在聊天栏显示个人领地列表菜单,包含快捷操作按钮和领地名称
*
* @param sender 命令的来源
* @param args 传递的命令参数
*/
public static void show(CommandSender sender, String[] args) {
Player player = playerOnly(sender);
if (player == null) return;
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/cn/lunadeer/dominion/utils/CommandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@
import java.util.HashMap;
import java.util.Map;

/**
* 指令工具类
*/
public class CommandUtils {
/**
* 验证指令发送者是否为Player
* @param sender 指令发送者
* @return Player对象或者null
*/
public static Player playerOnly(CommandSender sender) {
if (!(sender instanceof Player)) {
//如果发送者不是Player返回null
Notification.error(sender, Translation.Messages_CommandPlayerOnly);
return null;
}
//将sender对象强转为Player对象返回
return (Player) sender;
}

Expand Down
37 changes: 33 additions & 4 deletions core/src/main/java/cn/lunadeer/dominion/utils/TuiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.lunadeer.dominion.managers.Translation;
import cn.lunadeer.minecraftpluginutils.Notification;
import cn.lunadeer.minecraftpluginutils.stui.ListView;
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
import cn.lunadeer.minecraftpluginutils.stui.components.Line;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -66,11 +67,39 @@ public static void printHelp(CommandSender sender, String[] args) {
Player player = playerOnly(sender);
if (player == null) return;
int page = getPage(args, 1);

//TODO 帮助菜单的子菜单
Line dominionManage = Line.create()
.append(Button.create(Translation.TUI_CommandHelp_DominionManageButton).setExecuteCommand("").build())
.append(Translation.TUI_CommandHelp_DominionManageDescription);

Line memberManage = Line.create()
.append(Button.create(Translation.TUI_CommandHelp_MemberManageButton).setExecuteCommand("").build())
.append(Translation.TUI_CommandHelp_MemberManageDescription);

Line groupManage = Line.create()
.append(Button.create(Translation.TUI_CommandHelp_GroupManageButton).setExecuteCommand("").build())
.append(Translation.TUI_CommandHelp_GroupManageDescription);

Line misc = Line.create()
.append(Button.create(Translation.TUI_CommandHelp_MiscButton).setExecuteCommand("").build())
.append(Translation.TUI_CommandHelp_MiscDescription);

Line opCommand = Line.create()
.append(Button.create(Translation.TUI_CommandHelp_OpCommandButton).setExecuteCommand("").build())
.append(Translation.TUI_CommandHelp_OpCommandDescription);

ListView view = ListView.create(10, "/dominion help");
view.title(Translation.TUI_CommandHelp_Title.trans())
.subtitle(Line.create().append(Translation.TUI_CommandHelp_SubTitle.trans()))
// todo ...
.showOn(player, page);
view.title(Translation.TUI_CommandHelp_Title);
view.navigator(Line.create()
.append(Button.create(Translation.TUI_Navigation_Menu).setExecuteCommand("/dominion menu").build())
.append(Translation.TUI_Navigation_CommandHelpList));
view.add(dominionManage);
view.add(memberManage);
view.add(groupManage);
view.add(misc);
view.add(opCommand);
view.showOn(player, page);
}

}
Loading
Loading