DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamfireJavaAPI.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamJava;
25
26import org.bukkit.plugin.java.JavaPlugin;
27
28import java.io.File;
29import java.io.FileInputStream;
30import java.io.IOException;
31import java.net.URISyntaxException;
32import java.util.ArrayList;
33import java.util.List;
34import java.util.zip.ZipEntry;
35import java.util.zip.ZipInputStream;
36
49public class DreamfireJavaAPI {
50
57 public static List<Class<?>> getAutoRegisterClasses(JavaPlugin javaPlugin) {
58 try {
59 return getAutoRegisterClassesRaw(javaPlugin);
60 } catch (URISyntaxException | IOException | ClassNotFoundException e) {
61 throw new RuntimeException("Error while retrieving auto-register classes", e);
62 }
63 }
64
73 public static List<Class<?>> getAutoRegisterClassesRaw(JavaPlugin javaPlugin)
74 throws URISyntaxException, IOException, ClassNotFoundException {
75 List<Class<?>> annotatedClasses = new ArrayList<>();
76 for (Class<?> clazz : getAllClassesFromPlugin(javaPlugin)) {
77 if (clazz.isAnnotationPresent(PulseAutoRegister.class)) {
78 annotatedClasses.add(clazz);
79 }
80 }
81 return annotatedClasses;
82 }
83
92 public static List<Class<?>> getAllClassesFromPlugin(JavaPlugin javaPlugin)
93 throws URISyntaxException, IOException, ClassNotFoundException {
94
95 List<Class<?>> classes = new ArrayList<>();
96 for (String className : getAllClassNamesFromPlugin(javaPlugin)) {
97 classes.add(Class.forName(className));
98 }
99 return classes;
100 }
101
112 private static List<String> getAllClassNamesFromPlugin(JavaPlugin javaPlugin)
113 throws URISyntaxException, IOException {
114 File pluginFile = new File(javaPlugin.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
115 List<String> classNames = new ArrayList<>();
116 try (ZipInputStream zipStream = new ZipInputStream(new FileInputStream(pluginFile))) {
117 ZipEntry entry;
118 while ((entry = zipStream.getNextEntry()) != null) {
119 if (isValidClassEntry(entry, javaPlugin)) {
120 String className = entry.getName().replace('/', '.').replace(".class", "");
121 classNames.add(className);
122 }
123 }
124 }
125 return classNames;
126 }
127
134 private static boolean isValidClassEntry(ZipEntry entry, JavaPlugin javaPlugin) {
135 if (entry.isDirectory() || !entry.getName().endsWith(".class") || entry.getName().contains("$")) {
136 return false;
137 }
138 String className = entry.getName().replace('/', '.').replace(".class", "");
139 return className.startsWith(javaPlugin.getClass().getPackageName());
140 }
141}
Reflection utilities for scanning a plugin JAR to discover annotated classes.
static List< Class<?> > getAutoRegisterClassesRaw(JavaPlugin javaPlugin)
Retrieves classes annotated with PulseAutoRegister (propagates exceptions).
static List< Class<?> > getAllClassesFromPlugin(JavaPlugin javaPlugin)
Loads all classes defined under the plugin’s base package from the plugin JAR.
static List< Class<?> > getAutoRegisterClasses(JavaPlugin javaPlugin)
Retrieves auto‑register classes, wrapping checked exceptions.
Marker annotation that flags a class for automatic discovery and registration.