Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Asintotoo authored Jul 19, 2024
1 parent 26291d3 commit 6dab401
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.asintoto</groupId>
<artifactId>Basic</artifactId>
<version>1.2.5</version>
<version>1.2.6</version>
<packaging>jar</packaging>

<name>Basic</name>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/asintoto/basic/Basic.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.asintoto.basic.listeners.InventoryClickListener;
import com.asintoto.basic.listeners.InventoryCloseListener;
import com.asintoto.basic.menu.MenuManager;
import com.asintoto.basic.reflection.ReflectionUtils;
import com.asintoto.basic.regions.RegionManager;
import com.asintoto.basic.utils.Debug;
import com.asintoto.basic.utils.Options;
Expand Down Expand Up @@ -51,6 +52,9 @@ public static <T extends JavaPlugin> void init(T plugin) {
MenuManager.init();
Debug.log("Menu Manager successfully initialized!");

ReflectionUtils.registerAll();
Debug.log("Listeners and Basic Commands registered!");

File folder = new File(getPlugin().getDataFolder().getAbsolutePath());
if (!folder.exists()) {
folder.mkdir();
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/asintoto/basic/interfaces/AutoRegister.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.asintoto.basic.interfaces;


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

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoRegister {
}
136 changes: 136 additions & 0 deletions src/main/java/com/asintoto/basic/reflection/ReflectionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.asintoto.basic.reflection;

import com.asintoto.basic.Basic;
import com.asintoto.basic.BasicPlugin;
import com.asintoto.basic.commands.BasicCommand;
import com.asintoto.basic.interfaces.AutoRegister;
import com.asintoto.basic.utils.Common;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.event.Listener;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

public class ReflectionUtils {
public static String getPackageName(Class<?> clazz) {
return clazz.getPackage().getName();
}

public static Set<Class<?>> getValidPluginClasses() {

String packageName = getPackageName(Basic.getPlugin().getClass());

final Set<Class<?>> classes = new HashSet<>();

final Pattern anonymousClassPattern = Pattern.compile("\\w+\\$[0-9]$");

Method getFile = null;
try {

if (Basic.getPlugin() instanceof BasicPlugin) {
getFile = Basic.getPlugin().getClass().getSuperclass().getSuperclass().getDeclaredMethod("getFile");
} else {
getFile = Basic.getPlugin().getClass().getSuperclass().getDeclaredMethod("getFile");
}

} catch (NoSuchMethodException e) {
Common.error("Method not found: Make sure that you main plugin class extends either BasicPlugin or JavaPlugin");
}
getFile.setAccessible(true);
File filepl = null;
try {
filepl = (File) getFile.invoke(Basic.getPlugin());
} catch (IllegalAccessException e) {
Common.error("Can't access to the given method");
} catch (InvocationTargetException e) {
Common.error("Invocation Target Exception");
} finally {
getFile.setAccessible(false);
}

try (final JarFile file = new JarFile(filepl)) {
for (final Enumeration<JarEntry> entry = file.entries(); entry.hasMoreElements(); ) {
final JarEntry jar = entry.nextElement();
final String name = jar.getName().replace("/", ".");

if (!name.endsWith(".class"))
continue;

final String className = name.substring(0, name.length() - 6);
Class<?> clazz = null;

try {
clazz = Basic.class.getClassLoader().loadClass(className);

} catch (final ClassFormatError | VerifyError | NoClassDefFoundError | ClassNotFoundException |
IncompatibleClassChangeError error) {
continue;
}

if (!Modifier.isAbstract(clazz.getModifiers()) && !anonymousClassPattern.matcher(className).find())
if (clazz.getPackage().getName().startsWith(packageName)) {
classes.add(clazz);
}
}

} catch (final Throwable t) {
Common.warning(t.getMessage());
}

return classes;
}

public static void registerAll() {
for (Class<?> clazz : getValidPluginClasses()) {

if(clazz.isAnnotationPresent(AutoRegister.class)) {

// Register Listeners
if (Listener.class.isAssignableFrom(clazz)) {
try {
Constructor constructor = clazz.getConstructor();
Object object = constructor.newInstance();
Basic.registerListener((Listener) object);
} catch (NoSuchMethodException e) {
Common.error("Method not found");
} catch (InvocationTargetException e) {
Common.error("Invocation target Exception");
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
Common.error("Can't access this method");
}

}

//Register Basic Commands
if (clazz.getSuperclass() == BasicCommand.class) {
try {
Constructor constructor = clazz.getConstructor();
Object object = constructor.newInstance();
Basic.registerCommand((BasicCommand) object);
} catch (NoSuchMethodException e) {
Common.error("Method not found");
} catch (InvocationTargetException e) {
Common.error("Invocation target Exception");
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
Common.error("Can't access this method");
}
}

}
}
}
}

0 comments on commit 6dab401

Please sign in to comment.