|
@@ -24,12 +24,15 @@ package com.dmdirc.plugins.implementations;
|
24
|
24
|
import com.dmdirc.commandparser.CommandInfo;
|
25
|
25
|
import com.dmdirc.commandparser.commands.Command;
|
26
|
26
|
import com.dmdirc.interfaces.CommandController;
|
|
27
|
+import com.dmdirc.plugins.PluginInfo;
|
27
|
28
|
|
28
|
29
|
import java.util.HashMap;
|
29
|
30
|
import java.util.Map;
|
30
|
31
|
|
31
|
32
|
import lombok.AccessLevel;
|
32
|
|
-import lombok.Getter;
|
|
33
|
+import lombok.Setter;
|
|
34
|
+
|
|
35
|
+import dagger.ObjectGraph;
|
33
|
36
|
|
34
|
37
|
/**
|
35
|
38
|
* Implementation of {@link BasePlugin} that maintains commands.
|
|
@@ -39,28 +42,37 @@ public abstract class BaseCommandPlugin extends BasePlugin {
|
39
|
42
|
/**
|
40
|
43
|
* List of commands to load and unload.
|
41
|
44
|
*/
|
42
|
|
- private final Map<CommandInfo, Command> commands =
|
43
|
|
- new HashMap<CommandInfo, Command>();
|
|
45
|
+ private final Map<CommandInfo, Command> commands = new HashMap<>();
|
|
46
|
+
|
44
|
47
|
/**
|
45
|
48
|
* Command controller to register commands.
|
46
|
49
|
*/
|
47
|
|
- @Getter(AccessLevel.PROTECTED)
|
48
|
|
- private final CommandController commandController;
|
|
50
|
+ @Setter(AccessLevel.PROTECTED)
|
|
51
|
+ private CommandController commandController;
|
49
|
52
|
|
50
|
53
|
/**
|
51
|
54
|
* Creates a new instance of this plugin
|
52
|
55
|
*
|
53
|
|
- * @param commandController Command controller to register commands
|
|
56
|
+ * @param commandController Command controller to register commands.
|
|
57
|
+ * @deprecated No longer required, use empty constructor.
|
54
|
58
|
*/
|
|
59
|
+ @Deprecated
|
55
|
60
|
public BaseCommandPlugin(final CommandController commandController) {
|
56
|
61
|
this.commandController = commandController;
|
57
|
62
|
}
|
58
|
63
|
|
|
64
|
+ /**
|
|
65
|
+ * Creates a new instance of this plugin.
|
|
66
|
+ */
|
|
67
|
+ public BaseCommandPlugin() {
|
|
68
|
+ }
|
|
69
|
+
|
59
|
70
|
/**
|
60
|
71
|
* {@inheritDoc}
|
61
|
72
|
*/
|
62
|
73
|
@Override
|
63
|
74
|
public void onLoad() {
|
|
75
|
+ super.onLoad();
|
64
|
76
|
loadCommands();
|
65
|
77
|
}
|
66
|
78
|
|
|
@@ -69,20 +81,47 @@ public abstract class BaseCommandPlugin extends BasePlugin {
|
69
|
81
|
*/
|
70
|
82
|
@Override
|
71
|
83
|
public void onUnload() {
|
|
84
|
+ super.onUnload();
|
72
|
85
|
unloadCommands();
|
73
|
86
|
}
|
74
|
87
|
|
|
88
|
+ /** {@inheritDoc} */
|
|
89
|
+ @Override
|
|
90
|
+ public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
|
|
91
|
+ super.load(pluginInfo, graph);
|
|
92
|
+
|
|
93
|
+ commandController = graph.plus(new CommandHelper.CommandHelperModule())
|
|
94
|
+ .get(CommandHelper.class).getCommandController();
|
|
95
|
+ }
|
|
96
|
+
|
75
|
97
|
/**
|
76
|
98
|
* Registers a command from this plugin.
|
77
|
99
|
*
|
78
|
100
|
* @param command Command to register
|
79
|
101
|
* @param commandInfo Command info to register
|
80
|
102
|
*/
|
81
|
|
- protected void registerCommand(final Command command,
|
82
|
|
- final CommandInfo commandInfo) {
|
|
103
|
+ protected void registerCommand(final Command command, final CommandInfo commandInfo) {
|
83
|
104
|
commands.put(commandInfo, command);
|
84
|
105
|
}
|
85
|
106
|
|
|
107
|
+ /**
|
|
108
|
+ * Registers a command from this plugin.
|
|
109
|
+ *
|
|
110
|
+ * <p>This method will create a new instance of the specified command class using the
|
|
111
|
+ * dependency-injection framework. It must only be called after
|
|
112
|
+ * {@link #setObjectGraph(dagger.ObjectGraph)}, and any command must be injectable using
|
|
113
|
+ * that object graph.
|
|
114
|
+ *
|
|
115
|
+ * @param <T> The type of the command that will be registered.
|
|
116
|
+ * @param command The class of the command to register.
|
|
117
|
+ * @param commandInfo Command info to register.
|
|
118
|
+ */
|
|
119
|
+ protected <T extends Command> void registerCommand(
|
|
120
|
+ final Class<T> command,
|
|
121
|
+ final CommandInfo commandInfo) {
|
|
122
|
+ commands.put(commandInfo, getObjectGraph().get(command));
|
|
123
|
+ }
|
|
124
|
+
|
86
|
125
|
/**
|
87
|
126
|
* Unregisters a command from this plugin.
|
88
|
127
|
*
|
|
@@ -98,8 +137,7 @@ public abstract class BaseCommandPlugin extends BasePlugin {
|
98
|
137
|
*/
|
99
|
138
|
private void loadCommands() {
|
100
|
139
|
for (Map.Entry<CommandInfo, Command> command : commands.entrySet()) {
|
101
|
|
- commandController.registerCommand(
|
102
|
|
- command.getValue(), command.getKey());
|
|
140
|
+ commandController.registerCommand(command.getValue(), command.getKey());
|
103
|
141
|
}
|
104
|
142
|
}
|
105
|
143
|
|
|
@@ -111,4 +149,14 @@ public abstract class BaseCommandPlugin extends BasePlugin {
|
111
|
149
|
commandController.unregisterCommand(command);
|
112
|
150
|
}
|
113
|
151
|
}
|
|
152
|
+
|
|
153
|
+ /**
|
|
154
|
+ * Gets the command controller used.
|
|
155
|
+ *
|
|
156
|
+ * @return A command controller.
|
|
157
|
+ * @deprecated Plugins should inject their own controllers.
|
|
158
|
+ */
|
|
159
|
+ protected CommandController getCommandController() {
|
|
160
|
+ return commandController;
|
|
161
|
+ }
|
114
|
162
|
}
|