Skip to content

Commit

Permalink
Fixed issues with subcommand executors registration, added JetBrain's…
Browse files Browse the repository at this point in the history
… annotations
  • Loading branch information
SoKnight committed Nov 30, 2021
1 parent 1547229 commit a44d23f
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 130 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Add SKLibrary dependency:
<dependency>
<groupId>com.github.SoKnight</groupId>
<artifactId>SKLibrary</artifactId>
<version>1.14.3</version>
<version>1.14.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>ru.soknight</groupId>
<artifactId>sklibrary</artifactId>
<version>1.14.3</version>
<version>1.14.4</version>

<name>SKLibrary</name>
<description>The library for SoKnight's plugins</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import ru.soknight.lib.argument.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.soknight.lib.argument.BaseParameterRegistry;
import ru.soknight.lib.argument.CommandArguments;
import ru.soknight.lib.argument.ParameterRegistry;
import ru.soknight.lib.argument.suggestion.SuggestionResolver;
import ru.soknight.lib.command.response.CommandResponseType;
import ru.soknight.lib.configuration.Messages;
import ru.soknight.lib.tool.Validate;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* The enhanced configurable command executor
Expand All @@ -28,7 +36,8 @@ public abstract class EnhancedExecutor extends ExecutionHelper {
* The new enhanced executor instance
* @param messages The messages configuration for some default command response messages
*/
public EnhancedExecutor(Messages messages) {
public EnhancedExecutor(@NotNull Messages messages) {
Validate.notNull(messages, "messages");
this.messages = messages;
this.parameterRegistry = new BaseParameterRegistry();
this.responses = new HashMap<>();
Expand All @@ -38,7 +47,7 @@ public EnhancedExecutor(Messages messages) {
* Get the registry of parameters for this command executor
* @return The parameters registry for this executor
*/
protected ParameterRegistry parameterRegistry() {
protected @NotNull ParameterRegistry parameterRegistry() {
return parameterRegistry;
}

Expand All @@ -48,7 +57,9 @@ protected ParameterRegistry parameterRegistry() {
* @param sender command sender who used this command
* @param parameter wrong used parameter
*/
protected void onParameterValueUnspecified(CommandSender sender, String parameter) {
protected void onParameterValueUnspecified(@NotNull CommandSender sender, @NotNull String parameter) {
Validate.notNull(sender, "sender");
Validate.notEmpty(parameter, "parameter");
messages.sendFormatted(sender, "error.parameter-value-required", "%parameter%", parameter);
}

Expand All @@ -60,7 +71,10 @@ protected void onParameterValueUnspecified(CommandSender sender, String paramete
* @param sender The command sender
* @param args The specified command arguments
*/
public void validateAndExecute(CommandSender sender, CommandArguments args) {
public void validateAndExecute(@NotNull CommandSender sender, @NotNull CommandArguments args) {
Validate.notNull(sender, "sender");
Validate.notNull(args, "args");

// Checks for player-only flag
if(playerOnly && !isPlayer(sender)) {
sendResponseMessage(sender, CommandResponseType.ONLY_FOR_PLAYERS);
Expand Down Expand Up @@ -91,7 +105,10 @@ public void validateAndExecute(CommandSender sender, CommandArguments args) {
* @param args The specified command arguments
* @return The list of tab-completions as a {@link List} of strings
*/
public List<String> validateAndTabComplete(CommandSender sender, CommandArguments args) {
public @Nullable List<String> validateAndTabComplete(@NotNull CommandSender sender, @NotNull CommandArguments args) {
Validate.notNull(sender, "sender");
Validate.notNull(args, "args");

// check for player-only flag
if(playerOnly && !isPlayer(sender))
return null;
Expand All @@ -118,8 +135,9 @@ public List<String> validateAndTabComplete(CommandSender sender, CommandArgument
* @param sender The command sender (receiver for message)
* @param type The type of command response message
*/
public void sendResponseMessage(CommandSender sender, CommandResponseType type) {
if(type == null) return;
public void sendResponseMessage(@NotNull CommandSender sender, @NotNull CommandResponseType type) {
Validate.notNull(sender, "sender");
Validate.notNull(type, "type");

String message = responses.get(type);
String response = message != null ? message : ChatColor.RED + type.toString().toLowerCase();
Expand All @@ -134,7 +152,7 @@ public void sendResponseMessage(CommandSender sender, CommandResponseType type)
* @param sender The command sender
* @param args The specified command arguments
*/
protected abstract void executeCommand(CommandSender sender, CommandArguments args);
protected abstract void executeCommand(@NotNull CommandSender sender, @NotNull CommandArguments args);

/**
* Executes the tab-completion for command sender
Expand All @@ -144,7 +162,7 @@ public void sendResponseMessage(CommandSender sender, CommandResponseType type)
* @param args The specified command arguments
* @return The list of tab-completions as a {@link List} of strings
*/
protected List<String> executeTabCompletion(CommandSender sender, CommandArguments args) {
protected @Nullable List<String> executeTabCompletion(@NotNull CommandSender sender, @NotNull CommandArguments args) {
return null;
}

Expand All @@ -155,7 +173,7 @@ protected List<String> executeTabCompletion(CommandSender sender, CommandArgumen
*
* @see EnhancedExecutor#setResponseMessage(CommandResponseType, String)
*/
public String getResponseMessage(CommandResponseType type) {
public @Nullable String getResponseMessage(@NotNull CommandResponseType type) {
return responses.get(type);
}

Expand All @@ -166,7 +184,8 @@ public String getResponseMessage(CommandResponseType type) {
*
* @see EnhancedExecutor#getResponseMessage(CommandResponseType)
*/
public void setResponseMessage(CommandResponseType type, String message) {
public void setResponseMessage(@NotNull CommandResponseType type, @Nullable String message) {
Validate.notNull(type, "type");
responses.put(type, message);
}

Expand All @@ -179,7 +198,9 @@ public void setResponseMessage(CommandResponseType type, String message) {
*
* @see EnhancedExecutor#getResponseMessage(CommandResponseType)
*/
public void setResponseMessageByKey(CommandResponseType type, String messageKey) {
public void setResponseMessageByKey(@NotNull CommandResponseType type, @NotNull String messageKey) {
Validate.notNull(type, "type");
Validate.notEmpty(messageKey, "messageKey");
responses.put(type, messages != null ? messages.get(messageKey) : null);
}

Expand All @@ -194,15 +215,17 @@ public void setResponseMessageByKey(CommandResponseType type, String messageKey)
* @see EnhancedExecutor#getResponseMessage(CommandResponseType)
* @since 1.12.0
*/
public void setResponseMessageByKey(CommandResponseType type, String messageKey, Object... replacements) {
public void setResponseMessageByKey(@NotNull CommandResponseType type, @NotNull String messageKey, Object... replacements) {
Validate.notNull(type, "type");
Validate.notEmpty(messageKey, "messageKey");
responses.put(type, messages != null ? messages.getFormatted(messageKey, replacements) : null);
}

/**
* Gets the used {@link Messages} configuration for some command response messages
* @return The used messages configuration (required value, but may be null)
*/
public Messages getMessages() {
public @NotNull Messages getMessages() {
return messages;
}

Expand All @@ -212,7 +235,7 @@ public Messages getMessages() {
*
* @see EnhancedExecutor#setPermission(String)
*/
public String getPermission() {
public @Nullable String getPermission() {
return permission;
}

Expand Down Expand Up @@ -251,7 +274,7 @@ public boolean doSuggestParameters() {
*
* @see EnhancedExecutor#getPermission()
*/
public void setPermission(String permission) {
public void setPermission(@Nullable String permission) {
this.permission = permission;
}

Expand Down
38 changes: 28 additions & 10 deletions src/main/java/ru/soknight/lib/command/enhanced/ExecutionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.soknight.lib.argument.CommandArguments;
import ru.soknight.lib.tool.Validate;

/**
* The collection of useful methods for a command executors
Expand All @@ -20,7 +23,7 @@ public abstract class ExecutionHelper {
* @param args The command arguments object
* @return The last argument if it's possible (may be null)
*/
public String getLastArgument(CommandArguments args) {
public @Nullable String getLastArgument(@NotNull CommandArguments args) {
return getLastArgument(args, false);
}

Expand All @@ -32,8 +35,11 @@ public String getLastArgument(CommandArguments args) {
* @param lowerCase Should the argument to be in the lower-case or not
* @return The last argument if it's possible (may be null)
*/
public String getLastArgument(CommandArguments args, boolean lowerCase) {
if(args.isEmpty()) return null;
public @Nullable String getLastArgument(@NotNull CommandArguments args, boolean lowerCase) {
Validate.notNull(args, "args");

if(args.isEmpty())
return null;

String arg = args.get(args.size() - 1);
if(lowerCase)
Expand All @@ -47,7 +53,7 @@ public String getLastArgument(CommandArguments args, boolean lowerCase) {
* @param sender The target command sender
* @return The 'true' value if sender is player or 'false' if not
*/
public boolean isPlayer(CommandSender sender) {
public boolean isPlayer(@Nullable CommandSender sender) {
return sender instanceof Player;
}

Expand All @@ -57,7 +63,7 @@ public boolean isPlayer(CommandSender sender) {
* @return The 'true' value if offline player is exists or 'false' if not
*/
@SuppressWarnings("deprecation")
public boolean isPlayerExists(String nickname) {
public boolean isPlayerExists(@NotNull String nickname) {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(nickname);
return offlinePlayer.isOnline() || offlinePlayer.hasPlayedBefore();
}
Expand All @@ -67,7 +73,7 @@ public boolean isPlayerExists(String nickname) {
* @param nickname The nickname of target player
* @return The 'true' value if player is online or 'false' if not
*/
public boolean isPlayerOnline(String nickname) {
public boolean isPlayerOnline(@NotNull String nickname) {
return Bukkit.getPlayer(nickname) != null;
}

Expand All @@ -76,7 +82,10 @@ public boolean isPlayerOnline(String nickname) {
* @param source The string to check
* @return The 'true' value if this string is integer of 'false' if not
*/
public boolean isInteger(String source) {
public boolean isInteger(@Nullable String source) {
if(source == null || source.isEmpty())
return false;

try {
Integer.parseInt(source);
return true;
Expand All @@ -90,7 +99,10 @@ public boolean isInteger(String source) {
* @param source The string to check
* @return The 'true' value if this string is float of 'false' if not
*/
public boolean isFloat(String source) {
public boolean isFloat(@Nullable String source) {
if(source == null || source.isEmpty())
return false;

try {
Float.parseFloat(source);
return true;
Expand All @@ -104,7 +116,10 @@ public boolean isFloat(String source) {
* @param source The string to check
* @return The 'true' value if this string is double of 'false' if not
*/
public boolean isDouble(String source) {
public boolean isDouble(@Nullable String source) {
if(source == null || source.isEmpty())
return false;

try {
Double.parseDouble(source);
return true;
Expand All @@ -118,7 +133,10 @@ public boolean isDouble(String source) {
* @param source The string to check
* @return The 'true' value if this string is boolean of 'false' if not
*/
public boolean isBoolean(String source) {
public boolean isBoolean(@NotNull String source) {
if(source == null || source.isEmpty())
return false;

return source.equalsIgnoreCase("true") || source.equalsIgnoreCase("false");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.soknight.lib.argument.BaseCommandArguments;
import ru.soknight.lib.argument.CommandArguments;
import ru.soknight.lib.command.MergedExecutor;
import ru.soknight.lib.configuration.Messages;
import ru.soknight.lib.exception.ParameterValueRequiredException;
import ru.soknight.lib.tool.Validate;

import java.util.List;

Expand All @@ -28,9 +31,10 @@ public abstract class StandaloneExecutor extends EnhancedExecutor implements Mer
* @param command The command name
* @param messages The messages configuration for some default command response messages
*/
public StandaloneExecutor(String command, Messages messages) {
public StandaloneExecutor(@NotNull String command, @NotNull Messages messages) {
super(messages);


Validate.notEmpty(command, "command");
this.command = command;
}

Expand All @@ -45,7 +49,7 @@ public StandaloneExecutor(String command, Messages messages) {
* <u>plugin hasn't this command</u>, registration of this class will be aborted
* @param plugin The plugin which is owner of command
*/
public void register(JavaPlugin plugin) {
public void register(@NotNull JavaPlugin plugin) {
register(plugin, false);
}

Expand All @@ -57,22 +61,24 @@ public void register(JavaPlugin plugin) {
* @param plugin The plugin which is owner of command
* @param registerTabCompleter Should this class will be registered as tab-completer or not
*/
public void register(JavaPlugin plugin, boolean registerTabCompleter) {
if(command == null || command.isEmpty()) return;
public void register(@NotNull JavaPlugin plugin, boolean registerTabCompleter) {
Validate.notNull(plugin, "plugin");

PluginCommand command = plugin.getCommand(this.command);
if(command == null) return;

command.setExecutor(this);
if(registerTabCompleter)
command.setTabCompleter(this);
if(command != null) {
command.setExecutor(this);

if(registerTabCompleter) {
command.setTabCompleter(this);
}
}
}

@Override
protected abstract void executeCommand(CommandSender sender, CommandArguments args);
protected abstract void executeCommand(@NotNull CommandSender sender, @NotNull CommandArguments args);

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
try {
CommandArguments arguments = new BaseCommandArguments(sender, args, parameterRegistry());
arguments.getDispatchPath().appendCommand(getCommand());
Expand All @@ -84,7 +90,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
try {
CommandArguments arguments = new BaseCommandArguments(sender, args, parameterRegistry());
arguments.getDispatchPath().appendCommand(getCommand());
Expand All @@ -101,7 +107,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
* Gets the command name which was be specified by the constructor
* @return The command name (required value, but may be null)
*/
public String getCommand() {
public @NotNull String getCommand() {
return command;
}

Expand Down
Loading

0 comments on commit a44d23f

Please sign in to comment.