-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegister.java
More file actions
116 lines (105 loc) · 3.91 KB
/
Register.java
File metadata and controls
116 lines (105 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.github.pinont.plugin.register;
import com.github.pinont.singularitylib.api.annotation.AutoRegister;
import com.github.pinont.singularitylib.api.command.SimpleCommand;
import com.github.pinont.singularitylib.api.items.CustomItem;
import com.github.pinont.singularitylib.api.manager.CommandManager;
import com.github.pinont.singularitylib.api.manager.CustomItemManager;
import com.github.pinont.singularitylib.api.utils.Console;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.reflections.Reflections;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Class for automatically registering annotated components.
* Scans packages for classes annotated with @AutoRegister and registers them appropriately.
*/
public class Register {
private final Set<Listener> listeners = new HashSet<>();
private final List<SimpleCommand> commands = new ArrayList<>();
private final List<CustomItem> customItems = new ArrayList<>();
/**
* Default constructor for Register.
*/
public Register() {
}
/**
* Scans the specified package for annotated classes and collects them for registration.
*
* @param packageName the package name to scan
*/
public void scanAndCollect(String packageName) {
Reflections reflections = new Reflections(packageName);
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(AutoRegister.class);
for (Class<?> clazz : annotated) {
try {
Object instance = clazz.getDeclaredConstructor().newInstance();
if (Listener.class.isAssignableFrom(clazz)) {
listeners.add((Listener) instance);
}
if (SimpleCommand.class.isAssignableFrom(clazz)) {
commands.add((SimpleCommand) instance);
}
if (CustomItem.class.isAssignableFrom(clazz)) {
customItems.add((CustomItem) instance);
}
} catch (NoSuchMethodException e) {
Console.logError(ChatColor.RED + "No default constructor found for class: " + clazz.getName());
} catch (InstantiationException e) {
Console.logError(ChatColor.RED + "Failed to instantiate class: " + clazz.getName());
} catch (IllegalAccessException e) {
Console.logError(ChatColor.RED + "Illegal access while instantiating class: " + clazz.getName());
} catch (Exception e) {
Console.logError(ChatColor.RED + "Unexpected error while processing class: " + clazz.getName());
Console.logError(ChatColor.RED + e.getMessage());
}
}
}
/**
* Registers all collected components with the specified plugin.
*
* @param plugin the plugin to register components with
*/
public void registerAll(Plugin plugin) {
// Register listeners
for (Listener listener : listeners) {
Bukkit.getPluginManager().registerEvents(listener, plugin);
}
new CommandManager().register(plugin, commands);
// Register custom items
for (CustomItem item : customItems) {
if (item.getItem() != null) {
item.register();
}
}
new CustomItemManager().register(customItems);
}
/**
* Gets the collected listeners.
*
* @return set of listeners
*/
public Set<Listener> getListeners() {
return listeners;
}
/**
* Gets the collected commands.
*
* @return list of commands
*/
public List<SimpleCommand> getCommands() {
return commands;
}
/**
* Gets the collected custom items.
*
* @return list of custom items
*/
public List<CustomItem> getCustomItems() {
return customItems;
}
}