Skip to content

Commit

Permalink
JavaDoc improvements and other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Despical committed Sep 20, 2024
1 parent 84fd87e commit c84823c
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 45 deletions.
51 changes: 28 additions & 23 deletions src/main/java/me/despical/commandframework/CommandFramework.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
import me.despical.commandframework.annotations.Command;
import me.despical.commandframework.confirmations.ConfirmationManager;
import me.despical.commandframework.cooldown.CooldownManager;
import me.despical.commandframework.debug.DebugLogger;
import me.despical.commandframework.options.Option;
import me.despical.commandframework.options.OptionManager;
import org.bukkit.command.*;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.*;
import java.util.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.logging.Logger;

Expand Down Expand Up @@ -60,14 +63,16 @@ public CommandFramework(@NotNull Plugin plugin) {
this.checkIsAlreadyInitialized();

this.plugin = plugin;
this.logger = plugin.getLogger();
this.optionManager = new OptionManager();
this.registry = new CommandRegistry(this);
this.registry = new CommandRegistry();
this.parameterHandler = new ParameterHandler();
this.initializeLogger();
super.setRegistry(this);
}

private void checkRelocation() {
if (this.isOptionEnabled(Option.DEBUG)) return;

String suppressRelocation = System.getProperty("commandframework.suppressrelocation");

if ("true".equals(suppressRelocation)) return;
Expand All @@ -91,6 +96,15 @@ private void checkIsAlreadyInitialized() {
} else instance = this;
}

private void initializeLogger() {
if (this.isOptionEnabled(Option.DEBUG)) {
this.logger = new DebugLogger();
return;
}

this.logger = plugin.getLogger();
}

/**
* Registers commands in given object's class.
*
Expand Down Expand Up @@ -137,7 +151,7 @@ public final <A, B extends A> void addCustomParameter(@NotNull String value, @No
}

/**
* Returns the logger instance of Command Framework. By default, logger is {@code plugin}'s logger.
* Returns the logger instance of Command Framework. By default, logger is {@link #plugin} 's logger.
*
* @return the current logger instance.
* @since 1.4.8
Expand All @@ -158,26 +172,13 @@ public final void setLogger(@NotNull Logger logger) {
}

/**
* Enables the specified option.
* Returns the option manager.
*
* @param option the {@link Option} to be enabled. Must not be {@code null}.
* @throws IllegalArgumentException if the {@code option} is {@code null}.
* @return the option manager.
* @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);
public final OptionManager options() {
return this.optionManager;
}

/**
Expand Down Expand Up @@ -254,4 +255,8 @@ public final List<Command> getAllCommands() {

return commands;
}

public static CommandFramework getInstance() {
return instance;
}
}
32 changes: 24 additions & 8 deletions src/main/java/me/despical/commandframework/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import me.despical.commandframework.annotations.Command;
import me.despical.commandframework.annotations.Completer;
import me.despical.commandframework.debug.Debug;
import me.despical.commandframework.options.Option;
import me.despical.commandframework.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
Expand All @@ -16,7 +19,15 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.stream.Stream;

Expand All @@ -39,9 +50,6 @@ public class CommandRegistry {
@Nullable
private CommandMap commandMap;

@NotNull
private final CommandFramework commandFramework;

@NotNull
private final CommandMatcher commandMatcher;

Expand All @@ -51,15 +59,14 @@ public class CommandRegistry {
@NotNull
private final Map<Completer, Map.Entry<Method, Object>> commandCompletions, subCommandCompletions;

CommandRegistry(CommandFramework commandFramework) {
this.commandFramework = commandFramework;
CommandRegistry() {
this.commandMatcher = new CommandMatcher();
this.commands = new HashMap<>();
this.commandCompletions = new HashMap<>();
this.subCommands = new TreeMap<>(Comparator.comparing(Command::name).reversed());
this.subCommandCompletions = new TreeMap<>(Comparator.comparing(Completer::name).reversed());

final PluginManager pluginManager = commandFramework.plugin.getServer().getPluginManager();
final PluginManager pluginManager = Bukkit.getServer().getPluginManager();

if (pluginManager instanceof SimplePluginManager) {
final SimplePluginManager manager = (SimplePluginManager) pluginManager;
Expand Down Expand Up @@ -95,7 +102,14 @@ public void setCommandMap(@NotNull CommandMap commandMap) {
* @param instance the instance of the class from which commands will be registered. Must not be {@code null}.
*/
protected void registerCommands(@NotNull Object instance) {
final CommandFramework commandFramework = CommandFramework.getInstance();
final boolean notDebug = !commandFramework.isOptionEnabled(Option.DEBUG);

for (final Method method : instance.getClass().getMethods()) {
if (notDebug && method.isAnnotationPresent(Debug.class)) {
continue;
}

final Command command = method.getAnnotation(Command.class);

if (command != null) {
Expand Down Expand Up @@ -140,6 +154,7 @@ protected void registerCommands(@NotNull Object instance) {
* @param instance the instance of the class that contains the command method.
*/
protected void registerCommand(Command command, Method method, Object instance) {
final CommandFramework commandFramework = CommandFramework.getInstance();
final String cmdName = command.name();

if (cmdName.contains(".")) {
Expand All @@ -155,7 +170,7 @@ protected void registerCommand(Command command, Method method, Object instance)
pluginCommand.setTabCompleter(commandFramework);
pluginCommand.setExecutor(commandFramework);
pluginCommand.setUsage(command.usage());
pluginCommand.setPermission(!command.permission().isEmpty() ? null : command.permission());
pluginCommand.setPermission(command.permission().isEmpty() ? null : command.permission());
pluginCommand.setDescription(command.desc());

commandMap.register(cmdName, pluginCommand);
Expand All @@ -175,6 +190,7 @@ protected void unregisterCommand(@NotNull String commandName) {
if (commandName.contains(".")) commandName = commandName.split("\\.")[0];

final Map.Entry<Command, Map.Entry<Method, Object>> entry = commandMatcher.getAssociatedCommand(commandName, new String[0]);
final CommandFramework commandFramework = CommandFramework.getInstance();

if (entry == null) {
commandFramework.plugin.getLogger().log(Level.WARNING, "Command removal is failed because there is no command named ''{0}''!", commandName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

package me.despical.commandframework.annotations;

import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Main class of the framework to create commands in easy way.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

package me.despical.commandframework.annotations;

import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* A utility class in framework to create argument completions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package me.despical.commandframework.annotations;

import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

package me.despical.commandframework.annotations;

import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package me.despical.commandframework.annotations;

import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Despical
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package me.despical.commandframework.annotations;

import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Despical
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.despical.commandframework.cooldown;

import me.despical.commandframework.*;
import me.despical.commandframework.CommandArguments;
import me.despical.commandframework.CommandFramework;
import me.despical.commandframework.Message;
import me.despical.commandframework.annotations.Command;
import me.despical.commandframework.annotations.Completer;
import me.despical.commandframework.annotations.Cooldown;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/me/despical/commandframework/debug/Debug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package me.despical.commandframework.debug;

import java.lang.annotation.*;

/**
* @author Despical
* <p>
* Created at 8.09.2024
*/
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Debug {
}
15 changes: 15 additions & 0 deletions src/main/java/me/despical/commandframework/debug/DebugLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.despical.commandframework.debug;

import java.util.logging.Logger;

/**
* @author Despical
* <p>
* Created at 9.09.2024
*/
public class DebugLogger extends Logger {

public DebugLogger() {
super("CF Debugger", null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.jetbrains.annotations.ApiStatus;

import java.util.*;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Set;

/**
* This class handles the options related Command Framework.
Expand Down Expand Up @@ -30,10 +32,8 @@ public void enableOption(Option option) {
this.options.add(option);
}

public void enableOptions(Option option, Option... options) {
this.options.add(option);

Collections.addAll(this.options, options);
public void enableOptions(Option... options) {
this.options.addAll(Arrays.asList(options));
}

public boolean isEnabled(Option option) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import be.seeseemelk.mockbukkit.MockPlugin;
import be.seeseemelk.mockbukkit.ServerMock;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import me.despical.commandframework.*;
import me.despical.commandframework.CommandArguments;
import me.despical.commandframework.CommandFramework;
import me.despical.commandframework.annotations.Command;
import me.despical.commandframework.annotations.Completer;
import me.despical.commandframework.annotations.Cooldown;
Expand Down Expand Up @@ -141,7 +142,7 @@ private CommandFramework createCommandFramework() {
CommandFramework commandFramework = new CommandFrameworkMock(plugin);
commandFramework.registerCommands(new ExampleCommand());
commandFramework.addCustomParameter("String", arguments -> arguments.getArgument(0));
commandFramework.enableOption(Option.CUSTOM_COOLDOWN_CHECKER);
commandFramework.options().enableOption(Option.CUSTOM_COOLDOWN_CHECKER);
return commandFramework;
}

Expand Down

0 comments on commit c84823c

Please sign in to comment.