1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2024-09-19 19:25:45 +00:00

feat(command): 提供指令别名映射模块,支持将插件内复杂的子指令简化为一个单独的指令,方便玩家使用。

This commit is contained in:
Carm Jos 2023-03-17 22:24:16 +08:00
parent fa4a97d60d
commit ce1538003b
18 changed files with 216 additions and 15 deletions

View File

@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<artifactId>easyplugin-command-alias</artifactId>
<packaging>jar</packaging>
<name>EasyPlugin-Command-Alias</name>
<description>轻松插件指令别名映射模块,支持将插件内复杂的子指令简化为一个单独的指令,方便玩家使用。</description>
<url>https://github.com/CarmJos/EasyPlugin</url>
<developers>
<developer>
<id>CarmJos</id>
<name>Carm Jos</name>
<email>carm@carm.cc</email>
<url>https://www.carm.cc</url>
</developer>
</developers>
<licenses>
<license>
<name>The MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
</license>
</licenses>
<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/CarmJos/EasyPlugin/issues</url>
</issueManagement>
<ciManagement>
<system>GitHub Actions</system>
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
</ciManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,51 @@
package cc.carm.lib.easyplugin.command.alias;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class AliasCommand extends Command {
protected final AliasCommandManager aliasCommandManager;
protected final String targetCommand;
public AliasCommand(String name, AliasCommandManager aliasCommandManager, String targetCommand) {
super(name);
this.aliasCommandManager = aliasCommandManager;
this.targetCommand = targetCommand;
}
protected SimpleCommandMap getCommandMap() {
return this.aliasCommandManager.getCommandMap();
}
protected String buildCommand(String[] args) {
return this.targetCommand + " " + String.join(" ", args);
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
return getCommandMap().dispatch(sender, buildCommand(args));
}
@NotNull
@Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias,
@NotNull String[] args, @Nullable Location location) throws IllegalArgumentException {
return Optional.ofNullable(getCommandMap().tabComplete(sender, buildCommand(args))).orElse(Collections.emptyList());
}
@NotNull
@Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException {
return tabComplete(sender, alias, args, null);
}
}

View File

@ -0,0 +1,88 @@
package cc.carm.lib.easyplugin.command.alias;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.SimplePluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* 指令简化别名(简化映射)管理器
* <br>支持将插件内复杂的子指令简化为一个单独的指令方便玩家使用
*/
public class AliasCommandManager {
protected final @NotNull JavaPlugin plugin;
protected final @NotNull String prefix;
protected final @NotNull SimpleCommandMap commandMap;
protected final @NotNull Field knownCommandsFiled;
protected final @NotNull Map<String, AliasCommand> registeredCommands = new HashMap<>();
public AliasCommandManager(@NotNull JavaPlugin plugin) throws Exception {
this(plugin, plugin.getName());
}
public AliasCommandManager(@NotNull JavaPlugin plugin, @NotNull String prefix) throws Exception {
this.plugin = plugin;
this.prefix = prefix.trim();
SimplePluginManager manager = (SimplePluginManager) Bukkit.getPluginManager();
Field commandMapField = SimplePluginManager.class.getDeclaredField("commandMap");
commandMapField.setAccessible(true);
this.commandMap = (SimpleCommandMap) commandMapField.get(manager);
this.knownCommandsFiled = SimpleCommandMap.class.getDeclaredField("knownCommands");
this.knownCommandsFiled.setAccessible(true);
}
@SuppressWarnings("unchecked")
protected Map<String, Command> getKnownCommands() {
try {
return (Map<String, Command>) knownCommandsFiled.get(commandMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return new HashMap<>();
}
protected @NotNull SimpleCommandMap getCommandMap() {
return commandMap;
}
public String getCommandPrefix() {
return this.prefix;
}
public void register(@NotNull String alias, @NotNull String subCommand) {
AliasCommand current = this.registeredCommands.get(alias);
if (current != null) current.unregister(getCommandMap());
AliasCommand cmd = new AliasCommand(alias, this, getCommandPrefix() + " " + subCommand);
this.registeredCommands.put(alias, cmd);
getCommandMap().register(plugin.getName(), cmd);
}
public void unregister(@NotNull String alias) {
AliasCommand current = this.registeredCommands.remove(alias);
if (current != null) {
getKnownCommands().remove(alias);
current.unregister(getCommandMap());
}
}
public void unregisterAll() {
registeredCommands.forEach((k, v) -> {
getKnownCommands().remove(k);
v.unregister(getCommandMap());
});
registeredCommands.clear();
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.4</version>
<version>1.5.5</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging>
<version>1.5.4</version>
<version>1.5.5</version>
<modules>
<module>base/color</module>
<module>base/utils</module>
@ -24,6 +24,7 @@
<module>base/messages</module>
<module>base/gui</module>
<module>base/command</module>
<module>base/command-alias</module>
<module>base/storage</module>
<module>base/user</module>