Skip to content

Commit e5ce64d

Browse files
committed
v1.1.0 - Brigadier, etc.
1 parent c911b90 commit e5ce64d

File tree

88 files changed

+1567
-3131
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1567
-3131
lines changed

build.gradle

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'java'
22
apply plugin: 'maven-publish'
33

44
group = 'dev.manere'
5-
version = '1.0.6'
5+
version = '1.1.0'
66

77
repositories {
88
mavenCentral()
@@ -30,15 +30,6 @@ tasks.withType(JavaCompile).configureEach {
3030
}
3131
}
3232

33-
processResources {
34-
def props = [version: version]
35-
inputs.properties props
36-
filteringCharset 'UTF-8'
37-
filesMatching('plugin.yml') {
38-
expand props
39-
}
40-
}
41-
4233
publishing {
4334
publications {
4435
maven(MavenPublication) {
Lines changed: 17 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,39 @@
11
package dev.manere.commands;
22

3-
import com.google.common.collect.ImmutableList;
4-
import dev.manere.commands.api.APIHolder;
5-
import dev.manere.commands.api.CommandManager;
6-
import dev.manere.commands.api.CommandsAPI;
73
import dev.manere.commands.argument.CommandArgument;
8-
import dev.manere.commands.ctx.CommandContext;
9-
import dev.manere.commands.handler.CommandRequirement;
10-
import dev.manere.commands.handler.ExecutionHandler;
11-
import dev.manere.commands.handler.RequirementResult;
12-
import dev.manere.commands.info.CommandInfo;
13-
import org.bukkit.plugin.java.JavaPlugin;
4+
import org.bukkit.command.CommandSender;
145
import org.jetbrains.annotations.NotNull;
15-
import org.jetbrains.annotations.Nullable;
166
import org.jetbrains.annotations.Unmodifiable;
177

18-
import java.lang.reflect.InvocationTargetException;
8+
import java.util.Collection;
199
import java.util.List;
10+
import java.util.Optional;
2011

21-
/**
22-
* Represents a command node in a command tree structure.
23-
* A basic command node may contain information about the command, its children,
24-
* and the command's literal and arguments.
25-
* This interface also extends {@link ExecutionHandler}, allowing for command execution handling.
26-
*
27-
* @see CommandManager
28-
* @see CommandsAPI
29-
* @see CommandNode
30-
*/
31-
public interface BasicCommandNode extends ExecutionHandler, CommandRequirement {
32-
/**
33-
* Registers a command node with the provided {@link CommandsAPI}.
34-
*
35-
* @param api The CommandsAPI instance used to manage command nodes.
36-
* @param root The root command node to register.
37-
*/
38-
static void register(final @NotNull CommandsAPI api, final @NotNull BasicCommandNode root) {
39-
api.manager().register(root);
40-
}
41-
42-
/**
43-
* Registers a command node by its class type with the provided {@link CommandsAPI}.
44-
* <p>
45-
* If the root class does not have an empty constructor, this method attempts to find
46-
* a constructor that accepts a {@link JavaPlugin} instance and registers using it.
47-
* </p>
48-
*
49-
* @param api The CommandsAPI instance used to manage command nodes.
50-
* @param root The class type of the root command node to register.
51-
* @throws RuntimeException If no suitable constructor is found.
52-
*/
53-
static void register(final @NotNull CommandsAPI api, final @NotNull Class<? extends BasicCommandNode> root) {
54-
try {
55-
register(api, root.getDeclaredConstructor().newInstance());
56-
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
57-
api.plugin().getLogger().severe("Attempted to register a basic command node. Couldn't find a empty constructor, attempting to provide a plugin inside the parameters.");
58-
59-
try {
60-
register(api, root.getDeclaredConstructor(api.plugin().getClass()).newInstance(api.plugin()));
61-
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
62-
api.plugin().getLogger().severe("Attempted to register a basic command node. Couldn't find a empty constructor, attempting to provide a plugin inside the parameters.");
63-
throw new RuntimeException("Attempted to register a basic command node, couldn't find empty constructor and constructor of parameters JavaPlugin/Plugin/" + api.plugin().getClass().getSimpleName());
64-
}
65-
66-
throw new RuntimeException(e);
67-
}
68-
}
69-
70-
/**
71-
* Registers a command node using the default {@link CommandsAPI} instance.
72-
*
73-
* @param root The root command node to register.
74-
*/
75-
static void register(final @NotNull BasicCommandNode root) {
76-
register(APIHolder.api(), root);
77-
}
78-
79-
/**
80-
* Registers a command node by its class type using the default {@link CommandsAPI} instance.
81-
*
82-
* @param root The class type of the root command node to register.
83-
*/
84-
static void register(final @NotNull Class<? extends BasicCommandNode> root) {
85-
register(APIHolder.api(), root);
86-
}
87-
88-
/**
89-
* Registers this command node using the default {@link CommandsAPI} instance.
90-
*/
91-
default void register() {
92-
register(this);
93-
}
12+
public interface BasicCommandNode {
13+
@NotNull
14+
String getLiteral();
9415

95-
/**
96-
* Registers this command node with the provided {@link CommandsAPI} instance.
97-
*
98-
* @param api The CommandsAPI instance used to manage command nodes.
99-
*/
100-
default void register(final @NotNull CommandsAPI api) {
101-
register(api, this);
16+
@NotNull
17+
default Optional<String> getPermission() {
18+
return Optional.empty();
10219
}
10320

104-
/**
105-
* Gets the command information associated with this command node.
106-
*
107-
* @return the {@link CommandInfo} object containing the command's metadata,
108-
* or {@code null} if no information is associated with this command node.
109-
*/
110-
@Nullable
111-
CommandInfo info();
112-
113-
/**
114-
* Gets the literal string representing this command node.
115-
* The literal is typically the name or identifier of the command.
116-
*
117-
* @return a non-null string representing the literal of this command node.
118-
*/
11921
@NotNull
120-
String literal();
22+
default Collection<String> getAliases() {
23+
return List.of();
24+
}
12125

122-
/**
123-
* Gets an unmodifiable list of arguments that this command node accepts.
124-
* This method returns an empty list by default, which can be overridden by implementing classes.
125-
*
126-
* @return a non-null unmodifiable list of {@link CommandArgument} objects.
127-
*/
12826
@NotNull
12927
@Unmodifiable
130-
default List<CommandArgument<?>> arguments() {
131-
return ImmutableList.of();
28+
default Collection<? extends CommandArgument> getArguments() {
29+
return List.of();
13230
}
13331

134-
/**
135-
* Gets an unmodifiable list of child command nodes for this command node.
136-
* Child nodes represent sub-commands or additional layers in the command hierarchy.
137-
* This method returns an empty list by default, which can be overridden by implementing classes.
138-
*
139-
* @return a non-null unmodifiable list of {@link BasicCommandNode} objects representing the child nodes.
140-
*/
14132
@NotNull
14233
@Unmodifiable
143-
default List<BasicCommandNode> children() {
144-
return ImmutableList.of();
34+
default Collection<?> getChildren() {
35+
return List.of();
14536
}
14637

147-
@NotNull
148-
@Override
149-
default RequirementResult require(final @NotNull CommandContext context) {
150-
return RequirementResult.passed();
151-
}
38+
void execute(final @NotNull CommandContext<? extends CommandSender> context);
15239
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package dev.manere.commands;
2+
3+
import com.mojang.brigadier.context.ParsedArgument;
4+
import dev.manere.commands.argument.Argument;
5+
import dev.manere.commands.argument.CommandArgument;
6+
import dev.manere.commands.argument.SingleCommandArgument;
7+
import io.papermc.paper.command.brigadier.CommandSourceStack;
8+
import org.bukkit.command.CommandSender;
9+
import org.jetbrains.annotations.ApiStatus;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
12+
13+
import java.lang.reflect.Field;
14+
import java.util.Map;
15+
import java.util.Optional;
16+
17+
@SuppressWarnings("UnstableApiUsage")
18+
public class CommandContext<S extends CommandSender> {
19+
private final S source;
20+
private final CommandNode node;
21+
22+
private final String input;
23+
24+
private final com.mojang.brigadier.context.CommandContext<CommandSourceStack> stack;
25+
26+
public CommandContext(final @NotNull S source, final @NotNull CommandNode node, final @NotNull com.mojang.brigadier.context.CommandContext<CommandSourceStack> stack, final @NotNull String input) {
27+
this.source = source;
28+
this.node = node;
29+
this.input = input;
30+
this.stack = stack;
31+
}
32+
33+
@NotNull
34+
public S getSource() {
35+
return source;
36+
}
37+
38+
@NotNull
39+
public <T> Optional<T> getArgument(final @NotNull Class<T> type, final @NotNull String key) {
40+
try {
41+
final Field field = stack.getClass().getDeclaredField("arguments");
42+
field.setAccessible(true);
43+
final Map<String, ParsedArgument<S, ?>> object = (Map<String, ParsedArgument<S, ?>>) field.get(stack);
44+
45+
final var argument = (SingleCommandArgument<? extends Argument<Object, Object>>) getArgument(key);
46+
if (argument == null) return Optional.empty();
47+
48+
final ParsedArgument<S, ?> parsed = object.get(key);
49+
if (parsed == null) return Optional.empty();
50+
51+
final Object result = parsed.getResult();
52+
53+
final Argument<Object, Object> converter = argument.getArgument().get();
54+
return Optional.ofNullable(type.cast(converter.convert(stack.getSource(), result)));
55+
} catch (final Exception e) {
56+
return Optional.empty();
57+
}
58+
}
59+
60+
@Nullable
61+
@ApiStatus.Internal
62+
private SingleCommandArgument<? extends Argument<?, ?>> getArgument(final @NotNull String key) {
63+
for (final CommandArgument argument : node.arguments()) {
64+
if (argument instanceof SingleCommandArgument<?> single) {
65+
if (single.getKey().equals(key)) return single;
66+
}
67+
}
68+
69+
return null;
70+
}
71+
72+
@NotNull
73+
public CommandNode getNode() {
74+
return node;
75+
}
76+
77+
@NotNull
78+
public String getInput() {
79+
return input;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return "CommandContext[source = " + source + ", node = " + node + ", input = " + input + "]";
85+
}
86+
}

0 commit comments

Comments
 (0)