mirror of
https://github.com/aikar/commands.git
synced 2026-06-01 23:02:18 +00:00
* misc improvements (as suggested by intellij) * first draft of the DI functionality (#85) * address review * reenabled disabled inspections * overload registerDependency method * inject fields of superclasses two and remove invalid sponge default dependency
This commit is contained in:
@@ -78,17 +78,27 @@ public final class ACFExample extends JavaPlugin {
|
||||
}
|
||||
});
|
||||
|
||||
// 6: Register your commands - This first command demonstrates adding an exception handler to that command
|
||||
// 6: (Optionally) Register dependencies - Dependencies can be injected into fields of command classes by
|
||||
// marking them with @Dependency. Some classes, like your Plugin, are already registered by default.
|
||||
SomeHandler someHandler = new SomeHandler();
|
||||
someHandler.setSomeField("Secret");
|
||||
commandManager.registerDependency(SomeHandler.class, someHandler);
|
||||
commandManager.registerDependency(String.class, "Test3");
|
||||
commandManager.registerDependency(String.class, "test", "Test");
|
||||
commandManager.registerDependency(String.class, "test2", "Test2");
|
||||
|
||||
// 7: Register your commands - This first command demonstrates adding an exception handler to that command
|
||||
commandManager.registerCommand(new SomeCommand().setExceptionHandler((command, registeredCommand, sender, args, t) -> {
|
||||
sender.sendMessage(MessageType.ERROR, MessageKeys.ERROR_GENERIC_LOGGED);
|
||||
return true; // mark as handeled, default message will not be send to sender
|
||||
}));
|
||||
// 7: Register an additional command. This one happens to share the same CommandAlias as the previous command
|
||||
|
||||
// 8: Register an additional command. This one happens to share the same CommandAlias as the previous command
|
||||
// This means it simply registers additional sub commands under the same command, but organized into separate
|
||||
// Classes (Maybe different permission sets)
|
||||
commandManager.registerCommand(new SomeCommand_ExtraSubs());
|
||||
|
||||
// 8: Register default exception handler for any command that doesn't supply its own
|
||||
// 9: Register default exception handler for any command that doesn't supply its own
|
||||
commandManager.setDefaultExceptionHandler((command, registeredCommand, sender, args, t) -> {
|
||||
getLogger().warning("Error occured while executing command " + command.getName());
|
||||
return false; // mark as unhandeled, sender will see default message
|
||||
|
||||
@@ -29,6 +29,7 @@ import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Conditions;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Dependency;
|
||||
import co.aikar.commands.annotation.Optional;
|
||||
import co.aikar.commands.annotation.Subcommand;
|
||||
import co.aikar.commands.annotation.Values;
|
||||
@@ -36,6 +37,7 @@ import co.aikar.commands.contexts.OnlinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@CommandAlias("acf|somecommand|sc|somcom")
|
||||
public class SomeCommand extends BaseCommand {
|
||||
@@ -46,6 +48,29 @@ public class SomeCommand extends BaseCommand {
|
||||
setContextFlags(SomeObject.class, "foo=bar");
|
||||
}
|
||||
|
||||
// marking fields with @Dependency allows you to define instances that should be easily accessible to commands
|
||||
@Dependency
|
||||
private SomeHandler someHandler;
|
||||
// some classes like your plugin are automatically registered for injection
|
||||
@Dependency
|
||||
private Plugin plugin;
|
||||
// you can even use named injections to have multiple instances of the same type
|
||||
@Dependency("test")
|
||||
private String testString;
|
||||
@Dependency("test2")
|
||||
private String testString2;
|
||||
@Dependency
|
||||
private String testString3;
|
||||
|
||||
@Subcommand("testDI")
|
||||
public void onTestDI(CommandSender sender){
|
||||
sender.sendMessage("The value for the injected SomeHandler is " + someHandler.getSomeField());
|
||||
sender.sendMessage("Plugin is null? " + (plugin == null));
|
||||
sender.sendMessage("Test String 1: " + testString);
|
||||
sender.sendMessage("Test String 2: " + testString2);
|
||||
sender.sendMessage("Test String 3: " + testString3);
|
||||
}
|
||||
|
||||
// %testcmd was defined in ACFExample plugin and defined as "test4|foobar|barbaz"
|
||||
// This means, /test4, /foobar and /barbaz all are aliased here.
|
||||
// functionally equivalent to @CommandAlias("test4|foobar|barbaz") but could be dynamic (Read from config)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package co.aikar.acfexample;
|
||||
|
||||
public class SomeHandler {
|
||||
|
||||
private String someField;
|
||||
|
||||
public String getSomeField() {
|
||||
return someField;
|
||||
}
|
||||
|
||||
public void setSomeField(String someField) {
|
||||
this.someField = someField;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user