Skip to content

Commit

Permalink
RegisterCommand annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
ablax committed Jan 26, 2020
1 parent ffe0241 commit baf570f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 34 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.atteo.classindex</groupId>
<artifactId>classindex</artifactId>
<version>3.4</version>
</dependency>
</dependencies>


Expand Down
18 changes: 17 additions & 1 deletion src/main/java/me/ablax/decode/AdvancedPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

import org.bukkit.plugin.java.JavaPlugin;

public class AdvancedPlugin extends JavaPlugin {
public class AdvancedPlugin extends JavaPlugin {

private ApiManagerImpl apiManager;

@Override
public void onEnable() {
apiManager = ApiManagerImpl.getInstance();
}

public void registerPlugin(JavaPlugin javaPlugin) {
apiManager.register(javaPlugin);
}

public Object getComponent(Class<?> getObject) {
return apiManager.getComponent(getObject);
}


}
65 changes: 45 additions & 20 deletions src/main/java/me/ablax/decode/ApiManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.google.common.reflect.ClassPath;
import me.ablax.decode.annotation.AutoInject;
import me.ablax.decode.annotation.Component;
import me.ablax.decode.annotation.RegisterCommand;
import me.ablax.decode.annotation.RegisterListener;
import org.atteo.classindex.ClassIndex;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
Expand All @@ -32,10 +32,13 @@ public static ApiManagerImpl getInstance() {
return instance;
}

private void registerAllComponents(List<? extends Class<?>> claz) {
private void registerAllComponents(List<? extends Class<?>> allClasses) {
try {
for (Class<?> aClass : ClassIndex.getAnnotated(Component.class)) {
registerComponent(aClass);
for (Class<?> aClass : allClasses) {
if (aClass.isAnnotationPresent(Component.class)) {
registerComponent(aClass);
}

}
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -112,23 +115,50 @@ private void populateField(Object klass, Field field, Class<?> type) throws NoSu
field.set(klass, components.get(type.getCanonicalName()));
}

private void registerAllListeners(Object claz, List<? extends Class<?>> classesList) {
private void registerAllListeners(JavaPlugin javaPlugin, List<? extends Class<?>> classesList) {
try {
for (Class<?> aClass : classesList) {
if (aClass.isAnnotationPresent(RegisterListener.class)) {
registerListener(javaPlugin, aClass);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private void registerAllCommands(JavaPlugin javaPlugin, List<? extends Class<?>> classesList) {
try {
for (Class<?> aClass : ClassIndex.getAnnotated(RegisterListener.class)) {
registerListener(claz, aClass);
for (Class<?> aClass : classesList) {
if (aClass.isAnnotationPresent(RegisterCommand.class)) {
registerCommand(javaPlugin, aClass);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private void registerListener(Object claz, Class<?> aClass) {
private void registerListener(JavaPlugin claz, Class<?> aClass) {
if (components.containsKey(aClass.getCanonicalName())) {
Bukkit.getPluginManager().registerEvents((Listener) components.get(aClass.getCanonicalName()), claz);
} else {
registerComponent(aClass);
if (components.containsKey(aClass.getCanonicalName())) {
Bukkit.getPluginManager().registerEvents((Listener) components.get(aClass.getCanonicalName()), claz);
}
}
}

private void registerCommand(JavaPlugin plugin, Class<?> aClass) {
if (components.containsKey(aClass.getCanonicalName())) {
Bukkit.getPluginManager().registerEvents((Listener) components.get(aClass.getCanonicalName()), (Plugin) claz);
} else if (aClass.isAnnotationPresent(RegisterListener.class)) {
final RegisterCommand annotation = aClass.getAnnotation(RegisterCommand.class);
plugin.getCommand(annotation.commandName()).setExecutor((CommandExecutor) components.get(aClass.getCanonicalName()));
} else {
registerComponent(aClass);
if (components.containsKey(aClass.getCanonicalName())) {
Bukkit.getPluginManager().registerEvents((Listener) components.get(aClass.getCanonicalName()), (Plugin) claz);
final RegisterCommand annotation = aClass.getAnnotation(RegisterCommand.class);
plugin.getCommand(annotation.commandName()).setExecutor((CommandExecutor) components.get(aClass.getCanonicalName()));
}
}
}
Expand All @@ -152,7 +182,7 @@ private List<? extends Class<?>> getClassesList(Class<? extends JavaPlugin> java
return classesList;
}

public void register(JavaPlugin javaPlugin) {
void register(JavaPlugin javaPlugin) {
final Class<? extends JavaPlugin> javaPluginClass = javaPlugin.getClass();
components.put(javaPluginClass.getCanonicalName(), javaPlugin);
populateInjectors(javaPlugin);
Expand All @@ -161,16 +191,11 @@ public void register(JavaPlugin javaPlugin) {
if (classesList != null) {
registerAllComponents(classesList);
registerAllListeners(javaPlugin, classesList);
registerAllCommands(javaPlugin, classesList);
}

components.values().forEach(o -> {
if (o.getClass().isAnnotationPresent(RegisterListener.class)) {
registerListener(javaPlugin, o.getClass());
}
});
}

public Object getComponent(Class<?> getObject) {
Object getComponent(Class<?> getObject) {
if (!components.containsKey(getObject.getCanonicalName())
&& getObject.isAnnotationPresent(Component.class)) {
registerComponent(getObject);
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/me/ablax/decode/annotation/AutoInject.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package me.ablax.decode.annotation;


import org.atteo.classindex.IndexAnnotated;

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

@IndexAnnotated
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AutoInject {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/me/ablax/decode/annotation/Component.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package me.ablax.decode.annotation;

import org.atteo.classindex.IndexAnnotated;

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

@IndexAnnotated
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/me/ablax/decode/annotation/RegisterCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.ablax.decode.annotation;

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

@Component
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface RegisterCommand {

String commandName();

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package me.ablax.decode.annotation;

import org.atteo.classindex.IndexAnnotated;

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

@IndexAnnotated
@Component
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
Expand Down

0 comments on commit baf570f

Please sign in to comment.