Skip to content

Commit 544dcc1

Browse files
Add back several command entries
1 parent 70bac5d commit 544dcc1

5 files changed

Lines changed: 311 additions & 21 deletions

File tree

src/main/java/org/skriptlang/skript/bukkit/command/brigadier/CommandParsingData.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import ch.njol.skript.lang.parser.ParserInstance;
44
import ch.njol.skript.lang.parser.ParserInstance.Data;
5+
import org.jetbrains.annotations.Nullable;
56

67
import java.util.ArrayDeque;
78
import java.util.Deque;
89
import java.util.LinkedList;
910
import java.util.List;
11+
import java.util.function.Function;
1012

1113
/**
1214
* Parsing data holding command context.
@@ -16,10 +18,19 @@ public final class CommandParsingData extends Data {
1618
private final LinkedList<ArgumentData<?>> arguments = new LinkedList<>();
1719
private final Deque<Integer> indices = new ArrayDeque<>(4);
1820

21+
private final Deque<ExecutorData> executorDatas = new ArrayDeque<>(4);
22+
1923
public CommandParsingData(ParserInstance parserInstance) {
2024
super(parserInstance);
2125
}
2226

27+
/**
28+
* @return Whether this parsing data contains any data.
29+
*/
30+
public boolean isEmpty() {
31+
return indices.isEmpty();
32+
}
33+
2334
/**
2435
* @return Arguments currently stored on this data, in declaration order.
2536
*/
@@ -29,7 +40,7 @@ public List<ArgumentData<?>> getArguments() {
2940

3041
/**
3142
* Pushes arguments to this data.
32-
* @param arguments The arguments to record.
43+
* @param arguments The arguments to push.
3344
* @see #popArguments()
3445
*/
3546
public void pushArguments(List<ArgumentData<?>> arguments) {
@@ -45,4 +56,36 @@ public void popArguments() {
4556
arguments.subList(indices.pop(), arguments.size()).clear();
4657
}
4758

59+
/**
60+
* Pushes executor data to this data.
61+
* @param executorData The data to push.
62+
* @see #popExecutorData()
63+
*/
64+
public void pushExecutorData(ExecutorData executorData) {
65+
executorDatas.push(executorData);
66+
}
67+
68+
/**
69+
* Removes the last pushed executor data from this data.
70+
* @see #pushExecutorData(ExecutorData)
71+
*/
72+
public void popExecutorData() {
73+
executorDatas.pop();
74+
}
75+
76+
/**
77+
* Obtains the first non-null instance of a value from the pushed executors.
78+
* @param function A function to obtain the desired value from the data.
79+
* @return The value, or null if it was never set.
80+
*/
81+
public @Nullable <T> T getExecutorData(Function<ExecutorData, T> function) {
82+
for (ExecutorData executorData : executorDatas) {
83+
T result = function.apply(executorData);
84+
if (result != null) {
85+
return result;
86+
}
87+
}
88+
return null;
89+
}
90+
4891
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.skriptlang.skript.bukkit.command.brigadier;
2+
3+
import org.bukkit.command.CommandSender;
4+
import org.bukkit.command.ConsoleCommandSender;
5+
import org.bukkit.command.RemoteConsoleCommandSender;
6+
import org.bukkit.entity.Player;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.function.Predicate;
10+
11+
public record ExecutorData(
12+
@Nullable SkriptCommandExecutor executor,
13+
@Nullable ExecutableBy executableBy,
14+
@Nullable String permission
15+
) {
16+
17+
/**
18+
* Enum describing the types of {@link org.bukkit.command.CommandSender}s that can execute this command.
19+
*/
20+
public enum ExecutableBy {
21+
22+
/**
23+
* This command is only executable by players.
24+
*/
25+
PLAYERS(sender -> sender instanceof Player),
26+
27+
/**
28+
* This command is only executable by console.
29+
*/
30+
CONSOLE(sender -> sender instanceof ConsoleCommandSender || sender instanceof RemoteConsoleCommandSender),
31+
32+
/**
33+
* This command is executable by any {@link org.bukkit.command.CommandSender}.
34+
*/
35+
ALL(ignored -> true),
36+
37+
/**
38+
* This command is not executable.
39+
*/
40+
NONE(ignored -> false);
41+
42+
private final Predicate<CommandSender> predicate;
43+
44+
ExecutableBy(Predicate<CommandSender> predicate) {
45+
this.predicate = predicate;
46+
}
47+
48+
/**
49+
* @return A predicate to validate the behavior expected by this restriction.
50+
*/
51+
public Predicate<CommandSender> predicate() {
52+
return predicate;
53+
}
54+
55+
/**
56+
* Combines (ORs) this ExecutableBy with another.
57+
* For example, {@link #PLAYERS}.with({@link #CONSOLE}) is {@link #ALL}.
58+
* @param other Other to OR with.
59+
* @return The resulting ExecutableBy.
60+
*/
61+
public ExecutableBy with(ExecutableBy other) {
62+
if (this == NONE) {
63+
return other;
64+
} else if (other == NONE) {
65+
return this;
66+
}
67+
return this == other ? this : ALL;
68+
}
69+
70+
/**
71+
* @param other Subset ExecutableBy to consider.
72+
* @return Whether this ExecutableBy is the same as or covers a superset of {@code other}.
73+
*/
74+
public boolean includes(ExecutableBy other) {
75+
return this == ALL || this == other;
76+
}
77+
78+
@Override
79+
public String toString() {
80+
return switch (this) {
81+
case PLAYERS -> "players";
82+
case CONSOLE -> "the console";
83+
case ALL -> "the console and players";
84+
case NONE -> "none";
85+
};
86+
}
87+
88+
}
89+
90+
}

src/main/java/org/skriptlang/skript/bukkit/command/elements/structures/StructCommand.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.skriptlang.skript.registration.SyntaxInfo;
2222
import org.skriptlang.skript.registration.SyntaxRegistry;
2323

24+
import java.util.List;
2425
import java.util.Set;
2526
import java.util.concurrent.ConcurrentHashMap;
2627
import java.util.concurrent.atomic.AtomicBoolean;
@@ -38,14 +39,22 @@ public static void register(SkriptAddon addon, SyntaxRegistry syntaxRegistry) {
3839

3940
Skript.getInstance().getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS,commands -> {
4041
var registrar = commands.registrar();
41-
COMMANDS.forEach(registrar::register);
42+
for (var registration : COMMANDS) {
43+
registrar.register(registration.node, registration.description, registration.aliases);
44+
}
4245
});
4346
}
4447

4548
private static final SubCommandEntryData ROOT_ENTRY_DATA =
4649
new SubCommandEntryData("command", false, false);
4750

48-
private static final Set<LiteralCommandNode<CommandSourceStack>> COMMANDS = ConcurrentHashMap.newKeySet();
51+
private record CommandRegistration(
52+
LiteralCommandNode<CommandSourceStack> node,
53+
List<String> aliases,
54+
String description
55+
) { }
56+
57+
private static final Set<CommandRegistration> COMMANDS = ConcurrentHashMap.newKeySet();
4958
private static final AtomicBoolean SYNC_COMMANDS = new AtomicBoolean();
5059

5160
private static void performSync() {
@@ -55,7 +64,7 @@ private static void performSync() {
5564
}
5665

5766
private SectionNode rootNode;
58-
private LiteralCommandNode<CommandSourceStack> command;
67+
private CommandRegistration command;
5968

6069
@Override
6170
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult, EntryContainer entryContainer) {
@@ -65,15 +74,15 @@ public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResu
6574

6675
@Override
6776
public boolean load() {
68-
var command = ROOT_ENTRY_DATA.getValue(rootNode);
69-
if (command == null) {
77+
var result = ROOT_ENTRY_DATA.getValue(rootNode);
78+
if (result == null) { // parsing failed, entry will have emitted a specific error message
7079
return false;
7180
}
72-
if (command.size() != 1 || !(command.getFirst().build() instanceof LiteralCommandNode<CommandSourceStack> commandNode)) {
81+
if (result.arguments().size() != 1 || !(result.arguments().getFirst().build() instanceof LiteralCommandNode<CommandSourceStack> node)) {
7382
Skript.error("A command must have a name.");
7483
return false;
7584
}
76-
this.command = commandNode;
85+
this.command = new CommandRegistration(node, result.aliases(), result.description());
7786
// TODO validate whether command already exists
7887

7988
COMMANDS.add(this.command);

0 commit comments

Comments
 (0)