Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Introduce command trees; improve Commandessentials#5384

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
mdcfe wants to merge10 commits into2.x
base:2.x
Choose a base branch
Loading
fromfeature/improve-ess-command
Draft
Changes from1 commit
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
PrevPrevious commit
NextNext commit
More sensible types
  • Loading branch information
@mdcfe
mdcfe committedJun 6, 2023
commitb8e33d995ece8bcb95e6de73b136de1bd9899d1e
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,17 +6,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

public abstract class EssentialsCommandNode<T extends CommandSource> {
private ArrayList<EssentialsCommandNode<T>> childNodes = new ArrayList<>();
public abstract class EssentialsCommandNode<T> {
privatefinalArrayList<EssentialsCommandNode<T>> childNodes = new ArrayList<>();

protected EssentialsCommandNode(final Initializer<T> initializer) {
initializer.init(new BuildContext<>(this));
}

protected void run(Context<T> context) throws Exception {
protected void run(WalkContext<T> context) throws Exception {
for (EssentialsCommandNode<T> node : childNodes) {
if (node.matches(context)) {
node.run(context);
Expand All@@ -28,7 +26,7 @@ protected void run(Context<T> context) throws Exception {
throw new NoChargeException();
}

protected List<String> tabComplete(Context<T> context) throws Exception {
protected List<String> tabComplete(WalkContext<T> context) throws Exception {
for (EssentialsCommandNode<T> node : childNodes) {
if (node.matches(context)) {
return node.tabComplete(context);
Expand All@@ -39,17 +37,17 @@ protected List<String> tabComplete(Context<T> context) throws Exception {
throw new NoChargeException();
}

public abstract boolean matches(finalContext<T> context);
public abstract boolean matches(finalWalkContext<T> context);

public static Root<CommandSource> root(final Initializer<CommandSource> initializer) {
return new Root<>(initializer);
}

public interface Initializer<T extends CommandSource> {
public interface Initializer<T> {
void init(BuildContext<T> node);
}

public static class BuildContext<T extends CommandSource> {
public static class BuildContext<T> {
private final EssentialsCommandNode<T> node;

protected BuildContext(EssentialsCommandNode<T> node) {
Expand All@@ -60,129 +58,134 @@ public void literal(final String name, final Initializer<T> initializer) {
node.childNodes.add(new Literal<>(name, initializer));
}

public void execute(finalConsumer<Context<T>> runHandler) {
public void execute(finalRunHandler<T> runHandler) {
this.execute(runHandler, ctx -> new ArrayList<>());
}

public void execute(finalConsumer<Context<T>> runHandler, final List<String> tabValues) {
public void execute(finalRunHandler<T> runHandler, final List<String> tabValues) {
this.execute(runHandler, ctx -> tabValues);
}

public void execute(finalConsumer<Context<T>> runHandler, finalFunction<Context<T>, List<String>> tabHandler) {
public void execute(finalRunHandler<T> runHandler, finalTabHandler<T> tabHandler) {
node.childNodes.add(new Execute<>(runHandler, tabHandler));
}
}

public static class Root<T extends CommandSource> extends EssentialsCommandNode<T> {
public static class WalkContext<T> {
private final Server server;
private final T sender;
private final String label;
private final String[] args;

protected WalkContext(Server server, T sender, String label, String[] args) {
this.server = server;
this.sender = sender;
this.label = label;
this.args = args;
}

protected WalkContext<T> next() {
String[] nextArgs = {};
if (this.args.length > 1) {
nextArgs = Arrays.copyOfRange(this.args, 1, this.args.length);
}
return new WalkContext<>(this.server, this.sender, this.label, nextArgs);
}

public Server server() {
return server;
}

public T sender() {
return sender;
}

public String label() {
return label;
}

public String[] args() {
return args;
}
}

public static class Root<T> extends EssentialsCommandNode<T> {
protected Root(Initializer<T> initializer) {
super(initializer);
}

@Override
public boolean matches(Context<T> context) {
public boolean matches(WalkContext<T> context) {
throw new IllegalStateException("Root commands should not be placed in the tree");
}

public void run(Server server, T sender, String commandLabel, String[] args) throws Exception {
run(newContext<>(server, sender, commandLabel, args));
run(newWalkContext<>(server, sender, commandLabel, args));
}

public List<String> tabComplete(Server server, T sender, String commandLabel, String[] args) throws Exception {
return tabComplete(newContext<>(server, sender, commandLabel, args));
return tabComplete(newWalkContext<>(server, sender, commandLabel, args));
}

// run( ... args ...)
// tabComplete( ... args ...)
}

public static class Literal<T extends CommandSource> extends EssentialsCommandNode<T> {
public static class Literal<T> extends EssentialsCommandNode<T> {
private final String name;

protected Literal(String name, Initializer<T> initializer) {
super(initializer);
this.name = name;
}

public boolean matches(Context<T> context) {
public boolean matches(WalkContext<T> context) {
return context.args.length > 0 && context.args[0].equalsIgnoreCase(name);
}

@Override
protected void run(Context<T> context) throws Exception {
protected void run(WalkContext<T> context) throws Exception {
// consume argument
context = context.next();
super.run(context);
}

@Override
protected List<String> tabComplete(Context<T> context) throws Exception {
protected List<String> tabComplete(WalkContext<T> context) throws Exception {
// consume argument
context = context.next();
return super.tabComplete(context);
}
}

public static class Execute<T extends CommandSource> extends EssentialsCommandNode<T> {
private finalConsumer<Context<T>> runHandler;
private finalFunction<Context<T>, List<String>> tabHandler;
public static class Execute<T> extends EssentialsCommandNode<T> {
private finalRunHandler<T> runHandler;
private finalTabHandler<T> tabHandler;

protected Execute(Consumer<Context<T>> runHandler,Function<Context<T>, List<String>> tabHandler) {
protected Execute(RunHandler<T> runHandler,TabHandler<T> tabHandler) {
super(ctx -> {});
this.runHandler = runHandler;
this.tabHandler = tabHandler;
}

@Override
public boolean matches(Context<T> context) {
public boolean matches(WalkContext<T> context) {
return true;
}

@Override
protected void run(Context<T> context) throws Exception {
runHandler.accept(context);
protected void run(WalkContext<T> context) throws Exception {
runHandler.handle(context);
}

@Override
protected List<String> tabComplete(Context<T> context) throws Exception {
return tabHandler.apply(context);
protected List<String> tabComplete(WalkContext<T> context) throws Exception {
return tabHandler.handle(context);
}
}

public static class Context<T extends CommandSource> {
private final Server server;
private final T sender;
private final String label;
private final String[] args;

protected Context(Server server, T sender, String label, String[] args) {
this.server = server;
this.sender = sender;
this.label = label;
this.args = args;
}

protected Context<T> next() {
String[] nextArgs = {};
if (this.args.length > 1) {
nextArgs = Arrays.copyOfRange(this.args, 1, this.args.length);
}
return new Context<>(this.server, this.sender, this.label, nextArgs);
}

public Server server() {
return server;
}

public T sender() {
return sender;
}

public String label() {
return label;
}
public interface RunHandler<T> {
void handle(WalkContext<T> ctx) throws Exception;
}

public String[] args() {
return args;
}
public interface TabHandler<T> {
List<String> handle(WalkContext<T> ctx) throws Exception;
}
}

[8]ページ先頭

©2009-2025 Movatter.jp