mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-05 00:58:17 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c7190274d7 | |||
| be798a981a | |||
| bbdfe313d5 | |||
| 2acc81185a | |||
| 5886f162af |
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -15,17 +16,25 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
|
||||
protected final @NotNull JavaPlugin plugin;
|
||||
protected final @NotNull String cmd;
|
||||
protected final @NotNull List<String> aliases;
|
||||
|
||||
protected final @NotNull Map<String, SubCommand> registeredCommands = new HashMap<>();
|
||||
protected final @NotNull Map<String, CommandHandler> registeredHandlers = new HashMap<>();
|
||||
|
||||
protected final @NotNull Map<String, String> aliasesMap = new HashMap<>();
|
||||
|
||||
public CommandHandler(@NotNull JavaPlugin plugin) {
|
||||
this(plugin, plugin.getName());
|
||||
}
|
||||
|
||||
public CommandHandler(@NotNull JavaPlugin plugin, @NotNull String cmd) {
|
||||
this(plugin, cmd, new String[0]);
|
||||
}
|
||||
|
||||
public CommandHandler(@NotNull JavaPlugin plugin, @NotNull String cmd, @NotNull String... aliases) {
|
||||
this.plugin = plugin;
|
||||
this.cmd = cmd;
|
||||
this.aliases = Arrays.asList(aliases);
|
||||
}
|
||||
|
||||
public abstract void noArgs(CommandSender sender);
|
||||
@@ -34,27 +43,26 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
|
||||
public abstract void noPermission(CommandSender sender);
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
String name = command.getName().toLowerCase();
|
||||
this.registeredCommands.put(name, command);
|
||||
command.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
String name = handler.getName().toLowerCase();
|
||||
this.registeredHandlers.put(name, handler);
|
||||
handler.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -62,28 +70,30 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
if (args.length == 0) {
|
||||
this.noArgs(sender);
|
||||
} else {
|
||||
String sub = args[0].toLowerCase();
|
||||
CommandHandler handler = this.registeredHandlers.get(sub);
|
||||
String input = args[0].toLowerCase();
|
||||
|
||||
CommandHandler handler = getHandler(input);
|
||||
if (handler != null) {
|
||||
if (!handler.hasPermission(sender)) {
|
||||
this.noPermission(sender);
|
||||
} else {
|
||||
handler.onCommand(sender, command, label, this.shortenArgs(args));
|
||||
}
|
||||
}
|
||||
|
||||
SubCommand subCommand = getSubCommand(input);
|
||||
if (subCommand == null) {
|
||||
this.unknownCommand(sender, args);
|
||||
} else if (!subCommand.hasPermission(sender)) {
|
||||
this.noPermission(sender);
|
||||
} else {
|
||||
SubCommand subCommand = this.registeredCommands.get(sub);
|
||||
if (subCommand == null) {
|
||||
try {
|
||||
subCommand.execute(this.plugin, sender, this.shortenArgs(args));
|
||||
} catch (ArrayIndexOutOfBoundsException var9) {
|
||||
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;
|
||||
}
|
||||
@@ -101,12 +111,12 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
|
||||
CommandHandler handler = this.registeredHandlers.get(input);
|
||||
CommandHandler handler = getHandler(input);
|
||||
if (handler != null && handler.hasPermission(sender)) {
|
||||
return handler.onTabComplete(sender, command, alias, this.shortenArgs(args));
|
||||
}
|
||||
|
||||
SubCommand subCommand = this.registeredCommands.get(input);
|
||||
SubCommand subCommand = getSubCommand(input);
|
||||
if (subCommand != null && subCommand.hasPermission(sender)) {
|
||||
return subCommand.tabComplete(this.plugin, sender, this.shortenArgs(args));
|
||||
}
|
||||
@@ -124,6 +134,24 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||
return sortedExecutors;
|
||||
}
|
||||
|
||||
protected @Nullable CommandHandler getHandler(@NotNull String name) {
|
||||
CommandHandler fromName = this.registeredHandlers.get(name);
|
||||
if (fromName != null) return fromName;
|
||||
|
||||
String nameFromAlias = this.aliasesMap.get(name);
|
||||
if (nameFromAlias == null) return null;
|
||||
else return this.registeredHandlers.get(nameFromAlias);
|
||||
}
|
||||
|
||||
protected @Nullable SubCommand getSubCommand(@NotNull String name) {
|
||||
SubCommand fromName = this.registeredCommands.get(name);
|
||||
if (fromName != null) return fromName;
|
||||
|
||||
String nameFromAlias = this.aliasesMap.get(name);
|
||||
if (nameFromAlias == null) return null;
|
||||
else return this.registeredCommands.get(nameFromAlias);
|
||||
}
|
||||
|
||||
protected String[] shortenArgs(String[] args) {
|
||||
if (args.length == 0) {
|
||||
return args;
|
||||
|
||||
@@ -2,16 +2,13 @@ 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());
|
||||
}
|
||||
List<String> getAliases();
|
||||
|
||||
default boolean hasPermission(Permissible permissible) {
|
||||
return true;
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SimpleCompleter {
|
||||
|
||||
@@ -21,9 +22,13 @@ public class SimpleCompleter {
|
||||
}
|
||||
|
||||
public static @NotNull List<String> objects(@NotNull String input, int limit, List<?> objects) {
|
||||
return objects.stream().filter(Objects::nonNull).map(Object::toString)
|
||||
return objects(input, limit, objects.stream());
|
||||
}
|
||||
|
||||
public static @NotNull List<String> objects(@NotNull String input, int limit, Stream<?> stream) {
|
||||
return stream.filter(Objects::nonNull).map(Object::toString)
|
||||
.filter(s -> StringUtil.startsWithIgnoreCase(s, input))
|
||||
.limit(Math.min(0, limit)).collect(Collectors.toList());
|
||||
.limit(Math.max(0, limit)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static @NotNull List<String> text(@NotNull String input, String... texts) {
|
||||
@@ -47,7 +52,7 @@ public class SimpleCompleter {
|
||||
}
|
||||
|
||||
public static @NotNull List<String> onlinePlayers(@NotNull String input, int limit) {
|
||||
return text(input, limit, Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList()));
|
||||
return objects(input, limit, Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> allPlayers(@NotNull String input) {
|
||||
@@ -55,7 +60,7 @@ public class SimpleCompleter {
|
||||
}
|
||||
|
||||
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()));
|
||||
return objects(input, limit, Arrays.stream(Bukkit.getOfflinePlayers()).map(OfflinePlayer::getName));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> worlds(@NotNull String input) {
|
||||
@@ -63,7 +68,7 @@ public class SimpleCompleter {
|
||||
}
|
||||
|
||||
public static @NotNull List<String> worlds(@NotNull String input, int limit) {
|
||||
return text(input, limit, Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList()));
|
||||
return objects(input, limit, Bukkit.getWorlds().stream().map(World::getName));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> materials(@NotNull String input) {
|
||||
@@ -71,7 +76,7 @@ public class SimpleCompleter {
|
||||
}
|
||||
|
||||
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()));
|
||||
return objects(input, limit, Arrays.stream(Material.values()).map(Enum::name));
|
||||
}
|
||||
|
||||
public static @NotNull List<String> effects(@NotNull String input) {
|
||||
@@ -79,7 +84,7 @@ public class SimpleCompleter {
|
||||
}
|
||||
|
||||
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()));
|
||||
return objects(input, limit, Arrays.stream(PotionEffectType.values()).map(PotionEffectType::getName));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,15 +3,34 @@ package cc.carm.lib.easyplugin.command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public abstract class SubCommand implements NamedExecutor {
|
||||
|
||||
private final String name;
|
||||
private final List<String> aliases;
|
||||
|
||||
public SubCommand(String name) {
|
||||
this(name, new String[0]);
|
||||
}
|
||||
|
||||
public SubCommand(String name, String... aliases) {
|
||||
this.name = name;
|
||||
this.aliases = Arrays.asList(aliases);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAliases() {
|
||||
return this.aliases;
|
||||
}
|
||||
|
||||
public abstract Void execute(JavaPlugin plugin, CommandSender sender, String[] args);
|
||||
@@ -20,9 +39,5 @@ public abstract class SubCommand implements NamedExecutor {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@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.1</version>
|
||||
<version>1.4.3</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.1</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.3</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -15,10 +15,11 @@
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>easyplugin-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.4.1</version>
|
||||
<version>1.4.3</version>
|
||||
<modules>
|
||||
|
||||
<module>base/main</module>
|
||||
<module>base/command</module>
|
||||
<module>base/gui</module>
|
||||
|
||||
<module>extension/papi</module>
|
||||
@@ -27,7 +28,6 @@
|
||||
<module>collection/all</module>
|
||||
<module>collection/bom</module>
|
||||
<module>collection/common</module>
|
||||
<module>base/command</module>
|
||||
|
||||
</modules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user