Skip to content

Commit

Permalink
初步完成litecommands的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmeng-qwq committed Feb 12, 2024
1 parent a44ca17 commit f1bd953
Show file tree
Hide file tree
Showing 12 changed files with 587 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@
<version>${mixin.version}</version>
</dependency>

<!--Command - LiteCommands-->
<!--Command - litecommands-->
<dependency>
<groupId>dev.rollczi</groupId>
<artifactId>litecommands-framework</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package dev.rollczi.litecommands.annotations;

import dev.rollczi.litecommands.annotations.argument.ArgArgumentProcessor;
import dev.rollczi.litecommands.annotations.argument.KeyAnnotationResolver;
import dev.rollczi.litecommands.annotations.argument.collector.ArgCollectionArgumentProcessor;
import dev.rollczi.litecommands.annotations.async.AsyncAnnotationResolver;
import dev.rollczi.litecommands.annotations.bind.BindRequirementProcessor;
import dev.rollczi.litecommands.annotations.command.CommandAnnotationProcessor;
import dev.rollczi.litecommands.annotations.command.RootCommandAnnotationProcessor;
import dev.rollczi.litecommands.annotations.context.ContextRequirementProcessor;
import dev.rollczi.litecommands.annotations.description.DescriptionAnnotationResolver;
import dev.rollczi.litecommands.annotations.execute.ExecuteAnnotationResolver;
import dev.rollczi.litecommands.annotations.flag.FlagArgumentProcessor;
import dev.rollczi.litecommands.annotations.join.JoinArgumentProcessor;
import dev.rollczi.litecommands.annotations.meta.MarkMetaAnnotationResolver;
import dev.rollczi.litecommands.annotations.optional.OptionalArgArgumentProcessor;
import dev.rollczi.litecommands.annotations.permission.PermissionAnnotationResolver;
import dev.rollczi.litecommands.annotations.permission.PermissionsAnnotationResolver;
import dev.rollczi.litecommands.annotations.quoted.QuotedAnnotationProcessor;
import dev.rollczi.litecommands.annotations.shortcut.ShortcutCommandAnnotationProcessor;
import dev.rollczi.litecommands.annotations.validator.ValidateAnnotationResolver;
import dev.rollczi.litecommands.command.builder.CommandBuilder;
import snw.kookbc.impl.command.litecommands.annotations.prefix.PrefixAnnotationResolver;

import java.util.ArrayList;
import java.util.List;

public class AnnotationProcessorService<SENDER> {

private final List<AnnotationProcessor<SENDER>> annotationProcessors = new ArrayList<>();

public <A extends AnnotationProcessor<SENDER>> AnnotationProcessorService<SENDER> register(A processor) {
annotationProcessors.add(processor);
return this;
}

public CommandBuilder<SENDER> process(AnnotationInvoker<SENDER> invoker) {
for (AnnotationProcessor<SENDER> processor : annotationProcessors) {
invoker = processor.process(invoker);
}

return invoker.getResult();
}

public static <SENDER> AnnotationProcessorService<SENDER> defaultService() {
return new AnnotationProcessorService<SENDER>()
// class processors
.register(new CommandAnnotationProcessor<>())
.register(new RootCommandAnnotationProcessor<>())
// method processors
.register(new ExecuteAnnotationResolver<>())
.register(new ShortcutCommandAnnotationProcessor<>())
// meta holder processors
.register(new MarkMetaAnnotationResolver<>())
.register(new DescriptionAnnotationResolver<>())
.register(new AsyncAnnotationResolver<>())
.register(new PermissionAnnotationResolver<>())
.register(new PermissionsAnnotationResolver<>())
.register(new ValidateAnnotationResolver<>())
// argument meta processors
.register(new KeyAnnotationResolver<>())
.register(new QuotedAnnotationProcessor<>())
// argument processors
.register(new FlagArgumentProcessor<>())
.register(new ArgCollectionArgumentProcessor<>())
.register(new ArgArgumentProcessor<>())
.register(new OptionalArgArgumentProcessor<>())
.register(new JoinArgumentProcessor<>())
// other requirements processors
.register(new ContextRequirementProcessor<>())
.register(new BindRequirementProcessor<>())
.register(new PrefixAnnotationResolver<>())
;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* KookBC -- The Kook Bot Client & JKook API standard implementation for Java.
* Copyright (C) 2022 - 2023 KookBC contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package snw.kookbc.impl.command.litecommands;

import dev.rollczi.litecommands.command.CommandRoute;
import dev.rollczi.litecommands.meta.Meta;
import dev.rollczi.litecommands.meta.MetaKey;
import dev.rollczi.litecommands.meta.MetaType;
import dev.rollczi.litecommands.platform.AbstractPlatform;
import dev.rollczi.litecommands.platform.PlatformInvocationListener;
import dev.rollczi.litecommands.platform.PlatformSuggestionListener;
import org.jetbrains.annotations.NotNull;
import snw.jkook.command.CommandExecutor;
import snw.jkook.command.CommandSender;
import snw.jkook.command.JKookCommand;
import snw.jkook.plugin.Plugin;
import snw.kookbc.impl.command.CommandMap;
import snw.kookbc.impl.command.WrappedCommand;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* 2024/2/12<br>
* KookBC<br>
*
* @author huanmeng_qwq
*/
public class KookLitePlatform extends AbstractPlatform<CommandSender, LiteKookSettings> {
public static final MetaKey<Set<String>> PREFIX = MetaKey.of("kookbc_prefix", MetaType.set(), new HashSet<>(Collections.singletonList("/")));

private final Plugin plugin;
private final CommandMap commandMap;

protected KookLitePlatform(@NotNull LiteKookSettings settings, Plugin plugin, CommandMap commandMap) {
super(settings);
this.plugin = plugin;
this.commandMap = commandMap;
}

@Override
protected void hook(CommandRoute<CommandSender> commandRoute, PlatformInvocationListener<CommandSender> platformInvocationListener, PlatformSuggestionListener<CommandSender> platformSuggestionListener) {
for (String label : commandRoute.names()) {
List<String> desc = commandRoute.meta().get(Meta.DESCRIPTION);
Set<String> prefixes = commandRoute.meta().get(PREFIX);
// List<String> perms = commandRoute.meta().get(Meta.PERMISSIONS);
JKookCommand command = new JKookCommand(label, prefixes)
.setDescription(String.join("\n", desc))
.setExecutor(new LiteKookCommandExecutor(plugin.getCore(), settings, commandRoute, label, platformInvocationListener, platformSuggestionListener));
commandMap.register(plugin, command);
}
}

@Override
protected void unhook(CommandRoute<CommandSender> commandRoute) {
for (WrappedCommand wrappedCommand : commandMap.getView(false).values()) {
CommandExecutor executor = wrappedCommand.getCommand().getExecutor();
if (executor instanceof LiteKookCommandExecutor) {
if (commandRoute.isNameOrAlias(wrappedCommand.getCommand().getRootName())) {
commandMap.unregister(wrappedCommand.getCommand());
}
}
}
}
}
57 changes: 57 additions & 0 deletions src/main/java/snw/kookbc/impl/command/litecommands/KookSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* KookBC -- The Kook Bot Client & JKook API standard implementation for Java.
* Copyright (C) 2022 - 2023 KookBC contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package snw.kookbc.impl.command.litecommands;

import dev.rollczi.litecommands.identifier.Identifier;
import dev.rollczi.litecommands.platform.AbstractPlatformSender;
import snw.jkook.command.CommandSender;
import snw.jkook.entity.User;

class KookSender extends AbstractPlatformSender {

private final CommandSender handle;

public KookSender(CommandSender handle) {
this.handle = handle;
}

@Override
public String getName() {
if (this.handle instanceof User) {
return ((User) this.handle).getName();
}

return handle.getClass().getSimpleName();
}

@Override
public Identifier getIdentifier() {
if (this.handle instanceof User) {
return Identifier.of(((User) this.handle).getId());
}

return Identifier.CONSOLE;
}

@Override
public boolean hasPermission(String permission) {
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* KookBC -- The Kook Bot Client & JKook API standard implementation for Java.
* Copyright (C) 2022 - 2023 KookBC contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package snw.kookbc.impl.command.litecommands;

import dev.rollczi.litecommands.argument.parser.input.ParseableInput;
import dev.rollczi.litecommands.command.CommandRoute;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.invocation.InvocationContext;
import dev.rollczi.litecommands.platform.PlatformInvocationListener;
import dev.rollczi.litecommands.platform.PlatformSuggestionListener;
import org.jetbrains.annotations.Nullable;
import snw.jkook.Core;
import snw.jkook.command.CommandExecutor;
import snw.jkook.command.CommandSender;
import snw.jkook.message.Message;

import java.util.Arrays;

/**
* 2024/2/12<br>
* KookBC<br>
*
* @author huanmeng_qwq
*/
public class LiteKookCommandExecutor implements CommandExecutor {
private final Core core;
private final LiteKookSettings settings;
private final CommandRoute<CommandSender> commandSection;
private final String label;
private final PlatformInvocationListener<CommandSender> executeListener;
private final PlatformSuggestionListener<CommandSender> suggestionListener;

public LiteKookCommandExecutor(Core core, LiteKookSettings settings, CommandRoute<CommandSender> commandSection, String label, PlatformInvocationListener<CommandSender> executeListener, PlatformSuggestionListener<CommandSender> suggestionListener) {
this.core = core;
this.settings = settings;
this.commandSection = commandSection;
this.label = label;
this.executeListener = executeListener;
this.suggestionListener = suggestionListener;
}

@Override
public void onCommand(CommandSender commandSender, Object[] objects, @Nullable Message message) {
ParseableInput<?> input = ParseableInput.raw(Arrays.stream(objects).map(Object::toString).toArray(String[]::new));
KookSender platformSender = new KookSender(commandSender);
InvocationContext invocationContext = InvocationContext.builder()
.put(Message.class, message)
.put(Core.class, core)
.build();
Invocation<CommandSender> invocation = new Invocation<>(commandSender, platformSender, this.commandSection.getName(), this.label, input, invocationContext);

this.executeListener.execute(invocation, input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* KookBC -- The Kook Bot Client & JKook API standard implementation for Java.
* Copyright (C) 2022 - 2023 KookBC contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package snw.kookbc.impl.command.litecommands;

import dev.rollczi.litecommands.LiteCommandsBuilder;
import dev.rollczi.litecommands.LiteCommandsFactory;
import snw.jkook.Core;
import snw.jkook.command.CommandSender;
import snw.jkook.command.ConsoleCommandSender;
import snw.jkook.entity.User;
import snw.jkook.plugin.Plugin;
import snw.kookbc.impl.command.CommandManagerImpl;
import snw.kookbc.impl.command.litecommands.tools.KookOnlyConsoleContextual;
import snw.kookbc.impl.command.litecommands.tools.KookOnlyUserContextual;

/**
* 2024/2/12<br>
* KookBC<br>
*
* @author huanmeng_qwq
*/
public class LiteKookFactory {
private LiteKookFactory() {
}

public static <B extends LiteCommandsBuilder<CommandSender, LiteKookSettings, B>> B builder(Plugin plugin) {
return builder(plugin, new LiteKookSettings());
}

@SuppressWarnings("unchecked")
public static <B extends LiteCommandsBuilder<CommandSender, LiteKookSettings, B>> B builder(Plugin plugin, LiteKookSettings liteBungeeSettings) {
return (B) LiteCommandsFactory.builder(CommandSender.class, new KookLitePlatform(liteBungeeSettings, plugin, ((CommandManagerImpl) plugin.getCore().getCommandManager()).getCommandMap()))
.bind(Core.class, plugin::getCore)
.context(User.class, new KookOnlyUserContextual<>("只有用户才能执行该命令"))
.context(ConsoleCommandSender.class, new KookOnlyConsoleContextual<>("只有后台才能执行该命令"))

.result(String.class, new StringHandler());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* KookBC -- The Kook Bot Client & JKook API standard implementation for Java.
* Copyright (C) 2022 - 2023 KookBC contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package snw.kookbc.impl.command.litecommands;

import dev.rollczi.litecommands.platform.PlatformSettings;

/**
* 2024/2/12<br>
* KookBC<br>
*
* @author huanmeng_qwq
*/
public class LiteKookSettings implements PlatformSettings {
private boolean nativePermissions = false;

public LiteKookSettings nativePermissions(boolean nativePermissions) {
this.nativePermissions = nativePermissions;
return this;
}

boolean isNativePermissions() {
return this.nativePermissions;
}
}
Loading

0 comments on commit f1bd953

Please sign in to comment.