Skip to content

Commit

Permalink
Updated Javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Despical committed Jul 19, 2024
1 parent e23de63 commit 86ae20c
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 28 deletions.
36 changes: 32 additions & 4 deletions src/main/java/me/despical/commandframework/CommandArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ public void sendMessage(String message, Object... params) {
commandSender.sendMessage(Message.applyColorFormatter(MessageFormat.format(message, params)));
}

/**
* Sends the specified {@link Message} to command sender associated with this object.
*
* @param message the {@link Message} object to be sent.
* @see #getSender()
*/
public boolean sendMessage(Message message) {
return message.sendMessage(command, this);
}
Expand Down Expand Up @@ -310,7 +316,7 @@ public Optional<Player> getPlayer(int index) {
* @return all arguments as a single String object.
* @since 1.3.8
*/
public String concatenateArguments() {
public String concatArguments() {
return String.join(" ", arguments);
}

Expand All @@ -327,7 +333,7 @@ public String concatenateArguments() {
* or {@code from} is greater than {@code to}.
* @since 1.3.8
*/
public String concatenateRangeOf(int from, int to) {
public String concatRangeOf(int from, int to) {
return String.join(" ", Arrays.copyOfRange(arguments, from, to));
}

Expand Down Expand Up @@ -381,7 +387,7 @@ public boolean isInteger(String string) {
try {
Integer.parseInt(string);
return true;
} catch (NumberFormatException | NullPointerException exception) {
} catch (Exception exception) {
return false;
}
}
Expand Down Expand Up @@ -411,11 +417,33 @@ public boolean isFloatingDecimal(String string) {
try {
Double.parseDouble(string);
return true;
} catch (NumberFormatException | NullPointerException exception) {
} catch (Exception exception) {
return false;
}
}

/**
* This method checks if the current command sender has a cooldown on the command that
* is associated with current {@link CommandArguments} object.
*
* <blockquote>For example,
* <pre>{@code
* @Command(name = "test")
* @Cooldown(cooldown = 5)
* public void testCommand(CommandArguments args) {
* if (args.getLength() != 1) {
* // if sender has cooldown, execution will be end here
* args.checkCooldown();
* }
*
* args.sendMessage("Test command successfully executed.");
* }
* }</pre></blockquote>
*
* Note that execution will be stopped if this method returns {@code true}.
*
* @return {@code true} if the sender has a cooldown on this command
*/
public boolean checkCooldown() {
return CommandFramework.instance.getCooldownManager().hasCooldown(this);
}
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/me/despical/commandframework/CommandFramework.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,45 +117,100 @@ public final void unregisterCommands() {
this.registry.unregisterCommands();
}

/**
* Adds a custom parameter to the parameter handler.
*
* <p>This method allows the addition of a custom parameter to the parameter handler by specifying
* a value and a function that converts {@link CommandArguments} to an instance of a type that
* extends {@link A}.
*
* @param <A> the type of the parent class that the custom parameter's type extends
* @param <B> the type of the custom parameter, which extends {@link A}
* @param value the value to call custom parameter using {@linkplain me.despical.commandframework.annotations.Param @Param}, must not be null,
* can be a class name
* @param function a function that takes {@link CommandArguments} and returns an instance of {@link B},
* must not be null
*
* @throws NullPointerException if {@code value} is already added as a custom parameter
*/
public final <A, B extends A> void addCustomParameter(@NotNull String value, @NotNull Function<CommandArguments, B> function) {
this.parameterHandler.addCustomParameter(value, function);
}

/**
* Returns the logger instance of Command Framework. By default, logger is {@code plugin}'s logger.
*
* @return the current logger instance.
* @since 1.4.8
*/
@NotNull
public final Logger getLogger() {
return logger;
}

/**
* Changes default logger
*
* @param logger the non-null new logger instance
* @since 1.4.8
*/
public final void setLogger(@NotNull Logger logger) {
this.logger = logger;
}

/**
* Enables the specified option.
*
* @param option the {@link Option} to be enabled. Must not be {@code null}.
* @throws IllegalArgumentException if the {@code option} is {@code null}.
* @since 1.4.8
*/
public final void enableOption(Option option) {
this.optionManager.enableOption(option);
}

/**
* Enables the specified options.
*
* @param option the {@link Option} to be enabled. Must not be {@code null}.
* @param options the array of {@link Option} to be enabled. Must not be {@code null}.
* @throws IllegalArgumentException if the {@code option} or {@code options} are {@code null}.
* @since 1.4.8
*/
public final void enableOptions(Option option, Option... options) {
this.optionManager.enableOptions(option, options);
}

/**
* Checks whether the specified {@link Option} is enabled.
*
* @param option the {@link Option} to check.
* @return {@code true} if the option is enabled; {@code false} otherwise.
* @throws IllegalArgumentException if {@code option} is {@code null}.
* @since 1.4.8
*/
public final boolean isOptionEnabled(Option option) {
return this.optionManager.isEnabled(option);
}

@ApiStatus.Internal
final CooldownManager getCooldownManager() {
if (this.cooldownManager == null)
this.cooldownManager = new CooldownManager(this);
return cooldownManager;
}

@ApiStatus.Internal
final CommandRegistry getRegistry() {
return registry;
}

@ApiStatus.Internal
final ParameterHandler getParameterHandler() {
return parameterHandler;
}

@ApiStatus.Internal
final boolean checkConfirmation(CommandSender sender, final Command command, final Method method) {
if (!isOptionEnabled(Option.CONFIRMATIONS)) return false;

Expand Down
15 changes: 12 additions & 3 deletions src/main/java/me/despical/commandframework/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Method;
Expand All @@ -18,16 +19,24 @@
import java.util.Map;

/**
* This class handles the command executions and tab completes.
*
* <p>This is an internal class and should not be instantiated or extended by
* any subclasses.
*
* @author Despical
* @since 1.4.8
* <p>
* Created at 18.07.2024
* Created on 18.07.2024
*/
@ApiStatus.Internal
@ApiStatus.NonExtendable
public abstract class CommandHandler implements CommandExecutor, TabCompleter {

private CommandRegistry registry;
private CommandFramework commandFramework;

public void setRegistry(CommandFramework commandFramework) {
void setRegistry(CommandFramework commandFramework) {
this.commandFramework = commandFramework;
this.registry = commandFramework.getRegistry();
}
Expand All @@ -49,7 +58,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
return true;
}

if ((!permission.isEmpty() && !sender.hasPermission(permission))) {
if (!permission.isEmpty() && !sender.hasPermission(permission)) {
arguments.sendMessage(Message.NO_PERMISSION);
return true;
}
Expand Down
48 changes: 35 additions & 13 deletions src/main/java/me/despical/commandframework/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@
import java.util.stream.Stream;

/**
* This class manages the registry of commands, sub-commands and tab completers
* associated with those commands. It also provides helper methods for matching
* commands and their corresponding tab completers.
*
* <p>This is an internal class and should not be instantiated or extended by
* any subclasses.
*
* @author Despical
* @since 1.4.8
* <p>
* Created at 18.07.2024
* Created on 18.07.2024
*/
@ApiStatus.Internal
public class CommandRegistry {
Expand Down Expand Up @@ -60,21 +68,31 @@ public class CommandRegistry {
final Field field = SimplePluginManager.class.getDeclaredField("commandMap");
field.setAccessible(true);

commandMap = (CommandMap) field.get(manager);
this.commandMap = (CommandMap) field.get(manager);
} catch (ReflectiveOperationException exception) {
exception.printStackTrace();
}
}
}

public void setCommandMap(@Nullable CommandMap commandMap) {
/**
* Sets the {@link CommandMap} for this instance.
*
* @param commandMap the {@link CommandMap} to be set. Must be non-null.
*/
public void setCommandMap(@NotNull CommandMap commandMap) {
this.commandMap = commandMap;
}

/**
* Registers commands in given object's class.
* Registers commands from the specified instance's class.
* <p>
* This method scans the class of the provided instance and registers all commands
* defined within that class. The class should contain methods annotated to be recognized
* as commands.
* </p>
*
* @param instance the class instance of given object.
* @param instance the instance of the class from which commands will be registered. Must not be {@code null}.
*/
protected void registerCommands(@NotNull Object instance) {
for (final Method method : instance.getClass().getMethods()) {
Expand Down Expand Up @@ -114,12 +132,12 @@ protected void registerCommands(@NotNull Object instance) {
}

/**
* Registers the command with given parameters if there are any.
* This method registers a command along with its associated method and instance.
* When the command is executed, the specified method will be invoked.
*
* @param command the command object of registered command method.
* @param method the command method which will invoked run when the
* command is executed.
* @param instance the class instance of the command method.
* @param command the {@link Command} object representing the command to be registered.
* @param method the {@link Method} object representing the method to be invoked when the command is executed.
* @param instance the instance of the class that contains the command method.
*/
protected void registerCommand(Command command, Method method, Object instance) {
final String cmdName = command.name();
Expand Down Expand Up @@ -148,9 +166,10 @@ protected void registerCommand(Command command, Method method, Object instance)
}

/**
* Unregisters command and tab completer if there is with the given name.
* Unregisters a command and its associated tab completer if they are registered with the specified name.
*
* @param commandName name of the command that's going to be removed
* @param commandName the name of the command to be unregistered. Must not be {@code null} or empty.
* @throws IllegalArgumentException if {@code commandName} is {@code null} or an empty string.
*/
protected void unregisterCommand(@NotNull String commandName) {
if (commandName.contains(".")) commandName = commandName.split("\\.")[0];
Expand Down Expand Up @@ -189,7 +208,7 @@ protected void unregisterCommand(@NotNull String commandName) {
}

/**
* Unregisters all of registered commands and tab completers created using that instance.
* Unregisters all commands and tab completers that were registered using the instance of this object.
*/
protected void unregisterCommands() {
Iterator<String> names = commands.keySet().stream().map(Command::name).iterator();
Expand All @@ -214,6 +233,9 @@ protected CommandMatcher getCommandMatcher() {
return this.commandMatcher;
}

/**
* A helper class that contains methods for matching commands and their corresponding tab completers.
*/
protected final class CommandMatcher {

@Nullable
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/me/despical/commandframework/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.despical.commandframework.annotations.Command;
import org.bukkit.ChatColor;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.function.BiFunction;
Expand Down Expand Up @@ -52,15 +53,22 @@ public static void setColorFormatter(@NotNull Function<String, String> colorForm
Message.colorFormatter = colorFormatter;
}

public static String applyColorFormatter(final @NotNull String string) {
return colorFormatter.apply(string);
}

/**
* Set a custom error message.
*
* @param message the custom error message.
*/
public void setMessage(final BiFunction<Command, CommandArguments, Boolean> message) {
this.message = message;
}

public boolean sendMessage(final Command command, final CommandArguments arguments) {
@ApiStatus.Internal
static String applyColorFormatter(final @NotNull String string) {
return colorFormatter.apply(string);
}

@ApiStatus.Internal
boolean sendMessage(final Command command, final CommandArguments arguments) {
return this.message.apply(command, arguments);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* The default value for the parameter annotated with
* {@link Param}, if the value of parameter is null.
*
* @return default value for the possible null parameter
* @return the default value for the possible null parameter
*/
String value();
}
10 changes: 10 additions & 0 deletions src/main/java/me/despical/commandframework/annotations/Param.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Param {

/**
* The unique id of the parameter.
* <p>
* If a {@link Class} name is used as an id then no need for
* this annotation to be used. The parameters can directly be
* used from method's parameter list.
* </p>
*
* @return the unique id to call annotated parameter
*/
String value();
}
Loading

0 comments on commit 86ae20c

Please sign in to comment.