001package com.ericlam.mc.bungee.hnmc.commands.caxerx;
002
003import com.ericlam.mc.bungee.hnmc.commands.caxerx.exception.NotExecutableException;
004import com.ericlam.mc.bungee.hnmc.commands.caxerx.functional.CmdExecutor;
005import com.ericlam.mc.bungee.hnmc.commands.caxerx.functional.TabCompleter;
006import net.md_5.bungee.api.CommandSender;
007
008import java.util.List;
009
010/**
011 * 指令節點建造器
012 *
013 * @author Eric Lam
014 * @see CommandNode
015 */
016public class CommandNodeBuilder {
017
018    private String command;
019    private String permission;
020    private String description;
021    private String placeholder;
022    private CommandNode parent;
023    private TabCompleter tabCompleter;
024    private CmdExecutor cmdExecutor;
025    private String[] alias = new String[0];
026
027    /**
028     * @param command 指令
029     */
030    public CommandNodeBuilder(String command) {
031        this.command = command;
032    }
033
034    /**
035     * @param alias 縮寫
036     * @return this
037     */
038    public CommandNodeBuilder alias(String... alias) {
039        this.alias = alias;
040        return this;
041    }
042
043    /**
044     * @param permission 權限
045     * @return this
046     */
047    public CommandNodeBuilder permission(String permission) {
048        this.permission = permission;
049        return this;
050    }
051
052    /**
053     * @param description 介紹
054     * @return this
055     */
056    public CommandNodeBuilder description(String description) {
057        this.description = description;
058        return this;
059    }
060
061    /**
062     * @param placeholder 用法
063     * @return this
064     */
065    public CommandNodeBuilder placeholder(String placeholder) {
066        this.placeholder = placeholder;
067        return this;
068    }
069
070    /**
071     * @param parent 父類指令
072     * @return this
073     */
074    public CommandNodeBuilder parent(CommandNode parent) {
075        this.parent = parent;
076        return this;
077    }
078
079    /**
080     * @param executor tab 執行
081     * @return this
082     */
083    public CommandNodeBuilder tabComplete(TabCompleter executor) {
084        this.tabCompleter = executor;
085        return this;
086    }
087
088    /**
089     * @param cmdExecutor 指令執行
090     * @return this
091     */
092    public CommandNodeBuilder execute(CmdExecutor cmdExecutor) {
093        this.cmdExecutor = cmdExecutor;
094        return this;
095    }
096
097    /**
098     * @return 指令節點
099     * @throws NotExecutableException     指令無法執行
100     */
101    public CommandNode build() {
102        if (cmdExecutor == null) throw new NotExecutableException(command);
103        return new CommandNode(parent, command, permission, description, placeholder, alias) {
104            @Override
105            public void executeCommand(CommandSender sender, List<String> args) {
106                cmdExecutor.executeCommand(sender, args);
107            }
108
109            @Override
110            public List<String> executeTabCompletion(CommandSender sender, List<String> args) {
111                return tabCompleter == null ? null : tabCompleter.executeTabCompletion(sender, args);
112            }
113        };
114    }
115
116}
117//如何使用
118/*
119class CommandNodeUse{
120    CommandNode getNode(){
121        return new CommandNodeBuilder("hello")
122                .description("向一個人 say hello")
123                .parent(null)
124                .placeholder("<player>")
125                .tabComplete((sender, args) -> null)
126                .permission("say.hello").execute((sender, args) -> {
127                    String name = args.get(0);
128                    ProxiedPlayer target = ProxyServer.getInstance().getPlayer(name);
129                    if (target == null){
130                        MessageBuilder.sendMessage(sender,"對方不在線!");
131                        return;
132                    }
133                    MessageBuilder.sendMessage(sender,"已向 "+name+" say hello !");
134                    MessageBuilder.sendMessage(target,sender.getName()+" 向你 say hello 了!");
135                }).build();
136    }
137}
138
139 */
140
141
142