mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-04 16:48:16 +08:00
feat(command): 新增快捷子指令制作的相关API
新增指令相关API,便于快速制作指令与子指令。
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.1</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>easyplugin-command</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,136 @@
|
||||
|
||||
package cc.carm.lib.easyplugin.command;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
|
||||
protected final @NotNull JavaPlugin plugin;
|
||||
protected final @NotNull String cmd;
|
||||
|
||||
protected final @NotNull Map<String, SubCommand> registeredCommands = new HashMap<>();
|
||||
protected final @NotNull Map<String, CommandHandler> registeredHandlers = new HashMap<>();
|
||||
|
||||
public CommandHandler(@NotNull JavaPlugin plugin) {
|
||||
this(plugin, plugin.getName());
|
||||
}
|
||||
|
||||
public CommandHandler(@NotNull JavaPlugin plugin, @NotNull String cmd) {
|
||||
this.plugin = plugin;
|
||||
this.cmd = cmd;
|
||||
}
|
||||
|
||||
public abstract void noArgs(CommandSender sender);
|
||||
|
||||
public abstract void unknownCommand(CommandSender sender, String[] args);
|
||||
|
||||
public abstract void noPermission(CommandSender sender);
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.cmd;
|
||||
}
|
||||
|
||||
public void registerSubCommand(SubCommand command) {
|
||||
for (String alias : command.getAliases()) {
|
||||
if (this.registeredCommands.containsKey(alias)) {
|
||||
this.plugin.getLogger().warning("Conflicting command aliases '" + alias + "' for '" + command.getName() + "', overwriting.");
|
||||
}
|
||||
this.registeredCommands.put(alias, command);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerHandler(CommandHandler handler) {
|
||||
for (String alias : handler.getAliases()) {
|
||||
if (this.registeredCommands.containsKey(alias)) {
|
||||
this.plugin.getLogger().warning("Conflicting command aliases '" + alias + "' for '" + handler.getName() + "', overwriting.");
|
||||
}
|
||||
this.registeredHandlers.put(alias, handler);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||
if (args.length == 0) {
|
||||
this.noArgs(sender);
|
||||
} else {
|
||||
String sub = args[0].toLowerCase();
|
||||
CommandHandler handler = this.registeredHandlers.get(sub);
|
||||
if (handler != null) {
|
||||
if (!handler.hasPermission(sender)) {
|
||||
this.noPermission(sender);
|
||||
} else {
|
||||
handler.onCommand(sender, command, label, this.shortenArgs(args));
|
||||
}
|
||||
} else {
|
||||
SubCommand subCommand = this.registeredCommands.get(sub);
|
||||
if (subCommand == null) {
|
||||
this.unknownCommand(sender, args);
|
||||
} else if (!subCommand.hasPermission(sender)) {
|
||||
this.noPermission(sender);
|
||||
} else {
|
||||
try {
|
||||
subCommand.execute(this.plugin, sender, this.shortenArgs(args));
|
||||
} catch (ArrayIndexOutOfBoundsException var9) {
|
||||
this.unknownCommand(sender, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||
if (args.length == 0) return Collections.emptyList();
|
||||
|
||||
String input = args[0].toLowerCase();
|
||||
if (args.length == 1) {
|
||||
return getExecutors().stream()
|
||||
.filter(e -> e.hasPermission(sender))
|
||||
.map(NamedExecutor::getName)
|
||||
.filter(s -> StringUtil.startsWithIgnoreCase(s, input))
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
|
||||
CommandHandler handler = this.registeredHandlers.get(input);
|
||||
if (handler != null && handler.hasPermission(sender)) {
|
||||
return handler.onTabComplete(sender, command, alias, this.shortenArgs(args));
|
||||
}
|
||||
|
||||
SubCommand subCommand = this.registeredCommands.get(input);
|
||||
if (subCommand != null && subCommand.hasPermission(sender)) {
|
||||
return subCommand.tabComplete(this.plugin, sender, this.shortenArgs(args));
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public List<NamedExecutor> getExecutors() {
|
||||
Set<NamedExecutor> executors = new HashSet<>();
|
||||
executors.addAll(this.registeredHandlers.values());
|
||||
executors.addAll(this.registeredCommands.values());
|
||||
List<NamedExecutor> sortedExecutors = new ArrayList<>(executors);
|
||||
sortedExecutors.sort(Comparator.comparing(NamedExecutor::getName));
|
||||
return sortedExecutors;
|
||||
}
|
||||
|
||||
protected String[] shortenArgs(String[] args) {
|
||||
if (args.length == 0) {
|
||||
return args;
|
||||
} else {
|
||||
List<String> argList = new ArrayList<>(Arrays.asList(args).subList(1, args.length));
|
||||
return argList.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cc.carm.lib.easyplugin.command;
|
||||
|
||||
import org.bukkit.permissions.Permissible;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public interface NamedExecutor {
|
||||
|
||||
String getName();
|
||||
|
||||
default List<String> getAliases() {
|
||||
return Collections.singletonList(getName());
|
||||
}
|
||||
|
||||
default boolean hasPermission(Permissible permissible) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package cc.carm.lib.easyplugin.command;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SimpleCompleter {
|
||||
|
||||
public static @NotNull List<String> objects(@NotNull String input, Object... objects) {
|
||||
return objects(input, objects.length, objects);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> objects(@NotNull String input, int limit, Object... objects) {
|
||||
return objects(input, limit, Arrays.asList(objects));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> objects(@NotNull String input, List<String> objects) {
|
||||
return objects(input, objects.size(), objects);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> objects(@NotNull String input, int limit, List<Object> objects) {
|
||||
return objects.stream().filter(Objects::nonNull).map(Object::toString)
|
||||
.filter(s -> StringUtil.startsWithIgnoreCase(s, input))
|
||||
.limit(Math.min(0, limit)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static @NotNull List<String> text(@NotNull String input, String... texts) {
|
||||
return text(input, texts.length, texts);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> text(@NotNull String input, int limit, String... texts) {
|
||||
return text(input, limit, Arrays.asList(texts));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> text(@NotNull String input, List<String> texts) {
|
||||
return text(input, texts.size(), texts);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> text(@NotNull String input, int limit, List<String> texts) {
|
||||
return objects(input, limit, texts);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> onlinePlayers(@NotNull String input) {
|
||||
return onlinePlayers(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> onlinePlayers(@NotNull String input, int limit) {
|
||||
return text(input, limit, Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> allPlayers(@NotNull String input) {
|
||||
return allPlayers(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> allPlayers(@NotNull String input, int limit) {
|
||||
return text(input, limit, Arrays.stream(Bukkit.getOfflinePlayers()).map(OfflinePlayer::getName).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> worlds(@NotNull String input) {
|
||||
return worlds(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> worlds(@NotNull String input, int limit) {
|
||||
return text(input, limit, Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> materials(@NotNull String input) {
|
||||
return materials(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> materials(@NotNull String input, int limit) {
|
||||
return text(input, limit, Arrays.stream(Material.values()).map(Enum::name).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> effects(@NotNull String input) {
|
||||
return effects(input, 10);
|
||||
}
|
||||
|
||||
public static @NotNull List<String> effects(@NotNull String input, int limit) {
|
||||
return text(input, limit, Arrays.stream(PotionEffectType.values()).map(PotionEffectType::getName).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cc.carm.lib.easyplugin.command;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public abstract class SubCommand implements NamedExecutor {
|
||||
private final String name;
|
||||
|
||||
public SubCommand(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public abstract Void execute(JavaPlugin plugin, CommandSender sender, String[] args);
|
||||
|
||||
public List<String> tabComplete(JavaPlugin plugin, CommandSender sender, String[] args) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.0</version>
|
||||
<version>1.4.1</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.0</version>
|
||||
<version>1.4.1</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
Reference in New Issue
Block a user