Skip to content
This repository was archived by the owner on Feb 19, 2019. It is now read-only.

Commit ba929e7

Browse files
committed
Additional Module and Command constructors
Parameters that would be passed through an annotation can now be passed through a constructor for both Modules and Commands
1 parent 18c83f4 commit ba929e7

File tree

3 files changed

+61
-19
lines changed

3 files changed

+61
-19
lines changed

src/main/java/me/zero/client/api/command/Command.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,37 @@
1717
package me.zero.client.api.command;
1818

1919
import me.zero.client.api.command.exception.CommandInitException;
20+
import me.zero.client.api.util.ClientUtils;
2021

2122
/**
2223
* @author Brady
2324
* @since 5/31/2017 8:55 AM
2425
*/
2526
public abstract class Command implements ICommand {
2627

27-
private final String[] headers;
28-
private final String description;
29-
private final String[] syntax;
28+
private String[] headers;
29+
private String description;
30+
private String[] syntax;
3031

3132
public Command() {
3233
if (!this.getClass().isAnnotationPresent(Cmd.class))
33-
throw new RuntimeException(new CommandInitException(this, "@Cmd annotation not found!"));
34+
throw new RuntimeException(new CommandInitException(this, "@Cmd annotation must be present if required parameters aren't passed through constructor"));
3435

35-
Cmd cmd = this.getClass().getAnnotation(Cmd.class);
36-
this.headers = cmd.headers();
37-
this.description = cmd.description();
38-
this.syntax = cmd.syntax();
36+
Cmd data = this.getClass().getAnnotation(Cmd.class);
37+
setup(data.headers(), data.description(), data.syntax());
38+
}
39+
40+
public Command(String[] headers, String description, String[] syntax) {
41+
setup(headers, description, syntax);
42+
}
43+
44+
private void setup(String[] headers, String description, String[] syntax) {
45+
this.headers = headers;
46+
this.description = description;
47+
this.syntax = syntax;
48+
49+
if (ClientUtils.containsNull(headers, description, syntax))
50+
throw new NullPointerException("One or more Command members were null!");
3951
}
4052

4153
@Override

src/main/java/me/zero/client/api/module/Module.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import me.zero.client.api.event.defaults.ModuleStateEvent;
2121
import me.zero.client.api.exception.ActionNotSupportedException;
2222
import me.zero.client.api.manage.Node;
23+
import me.zero.client.api.module.exception.ModuleInitException;
2324
import me.zero.client.api.util.ClientUtils;
2425
import me.zero.client.api.util.keybind.Keybind;
26+
import org.lwjgl.input.Keyboard;
2527

2628
import java.util.ArrayList;
2729
import java.util.Arrays;
@@ -42,10 +44,15 @@
4244
*/
4345
public abstract class Module extends Node implements IModule {
4446

47+
/**
48+
* Reference to self-class
49+
*/
50+
private final Class<? extends Module> self = this.getClass();
51+
4552
/**
4653
* The type/category of the module
4754
*/
48-
private final Class<?> type;
55+
private Class<?> type;
4956

5057
/**
5158
* The Keybind of this Module
@@ -68,19 +75,30 @@ public abstract class Module extends Node implements IModule {
6875
private ModuleMode mode;
6976

7077
public Module() {
71-
Class<? extends Module> c = this.getClass();
72-
if (c.isAnnotationPresent(Mod.class)) {
73-
Mod data = c.getAnnotation(Mod.class);
78+
if (!self.isAnnotationPresent(Mod.class))
79+
throw new ModuleInitException("@Mod annotation must be present if required parameters aren't passed through constructor");
7480

75-
this.name = data.name();
76-
this.description = data.description();
81+
Mod data = self.getAnnotation(Mod.class);
82+
setup(data.name(), data.description(), data.bind());
83+
}
7784

78-
this.bind = new Keybind(Keybind.Type.TOGGLE, data.bind(), type -> {
79-
if (type == CLICK) Module.this.toggle();
80-
});
81-
}
85+
public Module(String name, String description) {
86+
this(name, description, Keyboard.KEY_NONE);
87+
}
88+
89+
public Module(String name, String description, int bind) {
90+
setup(name, description, bind);
91+
}
92+
93+
private void setup(String name, String description, int bind) {
94+
this.name = name;
95+
this.description = description;
96+
97+
this.bind = new Keybind(Keybind.Type.TOGGLE, bind, type -> {
98+
if (type == CLICK) Module.this.toggle();
99+
});
82100

83-
this.type = Arrays.stream(c.getInterfaces())
101+
this.type = Arrays.stream(self.getInterfaces())
84102
.filter(clazz -> clazz.isAnnotationPresent(Category.class))
85103
.findFirst().orElse(Category.Default.class);
86104

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package me.zero.client.api.module.exception;
2+
3+
/**
4+
* @author Brady
5+
* @since 6/29/2017 3:05 PM
6+
*/
7+
public final class ModuleInitException extends RuntimeException {
8+
9+
public ModuleInitException(String message) {
10+
super(message);
11+
}
12+
}

0 commit comments

Comments
 (0)