CMDoS because Brigadier
Dieser Commit ist enthalten in:
Ursprung
66ae2a837c
Commit
da5fca4078
@ -23,6 +23,7 @@ import com.mojang.brigadier.arguments.*;
|
|||||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||||
|
import net.minecraft.server.v1_15_R1.CommandListenerWrapper;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -67,16 +68,15 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unregister();
|
|
||||||
register();
|
|
||||||
|
|
||||||
SWCommandUtils.createList(swCommand, commandList, commandHelpList, localTypeMapper, this::createSubCommand);
|
SWCommandUtils.createList(swCommand, commandList, commandHelpList, localTypeMapper, this::createSubCommand);
|
||||||
Stream.of(commandList, commandHelpList).flatMap(List::stream).forEach(subCommand -> {
|
Stream.of(commandList, commandHelpList).flatMap(List::stream).forEach(subCommand -> {
|
||||||
register(command, subCommand, command, aliases);
|
register(this.command.getName(), subCommand);
|
||||||
for (String s : aliases) {
|
for (String s : this.command.getAliases()) {
|
||||||
register(s, subCommand, command, aliases);
|
register(s, subCommand);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// unregister();
|
||||||
|
// register();
|
||||||
}
|
}
|
||||||
|
|
||||||
private SubCommand createSubCommand(Method method, String[] strings) {
|
private SubCommand createSubCommand(Method method, String[] strings) {
|
||||||
@ -96,7 +96,6 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface {
|
|||||||
Parameter parameter = current.parameters[i + 1];
|
Parameter parameter = current.parameters[i + 1];
|
||||||
Class<?> parameterType = parameter.getType();
|
Class<?> parameterType = parameter.getType();
|
||||||
ArgumentType<?> argumentType = getArgumentType(parameter, parameterType, current);
|
ArgumentType<?> argumentType = getArgumentType(parameter, parameterType, current);
|
||||||
|
|
||||||
RequiredArgumentBuilder requiredArgumentBuilder = RequiredArgumentBuilder.argument(parameter.getName(), argumentType);
|
RequiredArgumentBuilder requiredArgumentBuilder = RequiredArgumentBuilder.argument(parameter.getName(), argumentType);
|
||||||
if (argumentBuilder != null) {
|
if (argumentBuilder != null) {
|
||||||
argumentBuilder.then(requiredArgumentBuilder);
|
argumentBuilder.then(requiredArgumentBuilder);
|
||||||
@ -104,9 +103,6 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface {
|
|||||||
current.argumentNode = requiredArgumentBuilder;
|
current.argumentNode = requiredArgumentBuilder;
|
||||||
}
|
}
|
||||||
argumentBuilder = requiredArgumentBuilder;
|
argumentBuilder = requiredArgumentBuilder;
|
||||||
if (i == current.arguments.length - 1) {
|
|
||||||
argumentBuilder.executes(commandContext -> 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (current.varArgType != null) {
|
if (current.varArgType != null) {
|
||||||
// TODO: UNSUPORTED
|
// TODO: UNSUPORTED
|
||||||
@ -129,14 +125,29 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface {
|
|||||||
CommandNode<?> commandNode = argumentBuilder.build();
|
CommandNode<?> commandNode = argumentBuilder.build();
|
||||||
argumentBuilder.redirect(commandNode);*/
|
argumentBuilder.redirect(commandNode);*/
|
||||||
}
|
}
|
||||||
|
current.argumentNodeEnd = argumentBuilder;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void register(String name, SubCommand subCommand, String command, String... aliases) {
|
private com.mojang.brigadier.Command<?> executes(SubCommand current) {
|
||||||
|
return commandContext -> {
|
||||||
|
CommandSender commandSender = ((CommandListenerWrapper) commandContext.getSource()).getBukkitSender();
|
||||||
|
List<String> stringList = Arrays.stream(commandContext.getInput().split(" ")).collect(Collectors.toList());
|
||||||
|
stringList.remove(0);
|
||||||
|
String[] args = stringList.toArray(new String[0]);
|
||||||
|
current.invoke(commandSender, args);
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private void register(String name, SubCommand subCommand) {
|
||||||
LiteralArgumentBuilder literalArgumentBuilder = LiteralArgumentBuilder.literal(name);
|
LiteralArgumentBuilder literalArgumentBuilder = LiteralArgumentBuilder.literal(name);
|
||||||
if (subCommand.argumentNode == null) {
|
if (subCommand.argumentNode == null) {
|
||||||
|
literalArgumentBuilder.executes(executes(subCommand));
|
||||||
|
SWCommand.dispatcher.register(literalArgumentBuilder);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
subCommand.argumentNodeEnd.executes(executes(subCommand));
|
||||||
literalArgumentBuilder.then(subCommand.argumentNode);
|
literalArgumentBuilder.then(subCommand.argumentNode);
|
||||||
SWCommand.dispatcher.register(literalArgumentBuilder);
|
SWCommand.dispatcher.register(literalArgumentBuilder);
|
||||||
}
|
}
|
||||||
@ -174,7 +185,6 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unregister() {
|
public void unregister() {
|
||||||
if (command == null) return;
|
|
||||||
SWCommandUtils.knownCommandMap.remove(command.getName());
|
SWCommandUtils.knownCommandMap.remove(command.getName());
|
||||||
command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove);
|
command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove);
|
||||||
command.unregister(SWCommandUtils.commandMap);
|
command.unregister(SWCommandUtils.commandMap);
|
||||||
@ -182,8 +192,13 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void register() {
|
public void register() {
|
||||||
if (command == null) return;
|
|
||||||
SWCommandUtils.commandMap.register("steamwar", this.command);
|
SWCommandUtils.commandMap.register("steamwar", this.command);
|
||||||
|
Stream.of(commandList, commandHelpList).flatMap(List::stream).forEach(subCommand -> {
|
||||||
|
register(command.getName(), subCommand);
|
||||||
|
for (String s : command.getAliases()) {
|
||||||
|
register(s, subCommand);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArgumentType<?> getArgumentType(Parameter parameter, Class<?> parameterType, SubCommand subCommand) {
|
private ArgumentType<?> getArgumentType(Parameter parameter, Class<?> parameterType, SubCommand subCommand) {
|
||||||
@ -208,7 +223,6 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface {
|
|||||||
else argumentType = DoubleArgumentType.doubleArg();
|
else argumentType = DoubleArgumentType.doubleArg();
|
||||||
} else {
|
} else {
|
||||||
argumentType = StringArgumentType.string();
|
argumentType = StringArgumentType.string();
|
||||||
subCommand.normalTabCompleteNeeded = true;
|
|
||||||
}
|
}
|
||||||
return argumentType;
|
return argumentType;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ import static de.steamwar.command.SWCommandUtils.*;
|
|||||||
class SubCommand {
|
class SubCommand {
|
||||||
|
|
||||||
ArgumentBuilder argumentNode = null;
|
ArgumentBuilder argumentNode = null;
|
||||||
boolean normalTabCompleteNeeded = false;
|
ArgumentBuilder argumentNodeEnd = null;
|
||||||
|
|
||||||
SWCommand swCommand;
|
SWCommand swCommand;
|
||||||
Parameter[] parameters;
|
Parameter[] parameters;
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren