mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2024-11-18 00:46:55 +01:00
cleaned up fileutil and expansion manager
This commit is contained in:
parent
42992de312
commit
e7ce84e7fc
@ -28,186 +28,207 @@ import me.clip.placeholderapi.util.FileUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public final class ExpansionManager {
|
||||
private final PlaceholderAPIPlugin plugin;
|
||||
public final class ExpansionManager
|
||||
{
|
||||
|
||||
public ExpansionManager(PlaceholderAPIPlugin instance) {
|
||||
plugin = instance;
|
||||
@NotNull
|
||||
private final File folder;
|
||||
@NotNull
|
||||
private final PlaceholderAPIPlugin plugin;
|
||||
|
||||
File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), "expansions");
|
||||
if (!f.exists()) {
|
||||
f.mkdirs();
|
||||
}
|
||||
}
|
||||
public ExpansionManager(@NotNull final PlaceholderAPIPlugin plugin)
|
||||
{
|
||||
this.plugin = plugin;
|
||||
this.folder = new File(plugin.getDataFolder(), "expansions");
|
||||
|
||||
public PlaceholderExpansion getRegisteredExpansion(String name) {
|
||||
for (Entry<String, PlaceholderHook> hook : PlaceholderAPI.getPlaceholders().entrySet()) {
|
||||
if (hook.getValue() instanceof PlaceholderExpansion) {
|
||||
if (name.equalsIgnoreCase(hook.getKey())) {
|
||||
return (PlaceholderExpansion) hook.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!this.folder.exists() && !folder.mkdirs())
|
||||
{
|
||||
plugin.getLogger().log(Level.WARNING, "failed to create expansions folder!");
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
public PlaceholderExpansion getRegisteredExpansion(String name)
|
||||
{
|
||||
for (Entry<String, PlaceholderHook> hook : PlaceholderAPI.getPlaceholders().entrySet())
|
||||
{
|
||||
if (hook.getValue() instanceof PlaceholderExpansion)
|
||||
{
|
||||
if (name.equalsIgnoreCase(hook.getKey()))
|
||||
{
|
||||
return (PlaceholderExpansion) hook.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean registerExpansion(PlaceholderExpansion expansion) {
|
||||
if (expansion == null || expansion.getIdentifier() == null) {
|
||||
return false;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (expansion instanceof Configurable) {
|
||||
Map<String, Object> defaults = ((Configurable) expansion).getDefaults();
|
||||
String pre = "expansions." + expansion.getIdentifier() + ".";
|
||||
FileConfiguration cfg = plugin.getConfig();
|
||||
boolean save = false;
|
||||
public boolean registerExpansion(@NotNull final PlaceholderExpansion expansion)
|
||||
{
|
||||
if (expansion.getIdentifier() == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (defaults != null) {
|
||||
for (Entry<String, Object> entries : defaults.entrySet()) {
|
||||
if (entries.getKey() == null || entries.getKey().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (expansion instanceof Configurable)
|
||||
{
|
||||
Map<String, Object> defaults = ((Configurable) expansion).getDefaults();
|
||||
String pre = "expansions." + expansion.getIdentifier() + ".";
|
||||
FileConfiguration cfg = plugin.getConfig();
|
||||
boolean save = false;
|
||||
|
||||
if (entries.getValue() == null) {
|
||||
if (cfg.contains(pre + entries.getKey())) {
|
||||
save = true;
|
||||
cfg.set(pre + entries.getKey(), null);
|
||||
}
|
||||
} else {
|
||||
if (!cfg.contains(pre + entries.getKey())) {
|
||||
save = true;
|
||||
cfg.set(pre + entries.getKey(), entries.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (defaults != null)
|
||||
{
|
||||
for (Entry<String, Object> entries : defaults.entrySet())
|
||||
{
|
||||
if (entries.getKey() == null || entries.getKey().isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (save) {
|
||||
plugin.saveConfig();
|
||||
plugin.reloadConfig();
|
||||
}
|
||||
}
|
||||
if (entries.getValue() == null)
|
||||
{
|
||||
if (cfg.contains(pre + entries.getKey()))
|
||||
{
|
||||
save = true;
|
||||
cfg.set(pre + entries.getKey(), null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!cfg.contains(pre + entries.getKey()))
|
||||
{
|
||||
save = true;
|
||||
cfg.set(pre + entries.getKey(), entries.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (expansion instanceof VersionSpecific) {
|
||||
VersionSpecific nms = (VersionSpecific) expansion;
|
||||
if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) {
|
||||
plugin.getLogger()
|
||||
.info(
|
||||
"Your server version is not compatible with expansion: " + expansion.getIdentifier()
|
||||
+ " version: " + expansion.getVersion());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (save)
|
||||
{
|
||||
plugin.saveConfig();
|
||||
plugin.reloadConfig();
|
||||
}
|
||||
}
|
||||
|
||||
if (!expansion.canRegister()) {
|
||||
return false;
|
||||
}
|
||||
if (expansion instanceof VersionSpecific)
|
||||
{
|
||||
VersionSpecific nms = (VersionSpecific) expansion;
|
||||
if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion()))
|
||||
{
|
||||
plugin.getLogger()
|
||||
.info(
|
||||
"Your server version is not compatible with expansion: " + expansion.getIdentifier()
|
||||
+ " version: " + expansion.getVersion());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!expansion.register()) {
|
||||
return false;
|
||||
}
|
||||
if (!expansion.canRegister() || !expansion.register())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (expansion instanceof Listener) {
|
||||
Listener l = (Listener) expansion;
|
||||
Bukkit.getPluginManager().registerEvents(l, plugin);
|
||||
}
|
||||
if (expansion instanceof Listener)
|
||||
{
|
||||
Bukkit.getPluginManager().registerEvents(((Listener) expansion), plugin);
|
||||
}
|
||||
|
||||
plugin.getLogger().info("Successfully registered expansion: " + expansion.getIdentifier());
|
||||
plugin.getLogger().info("Successfully registered expansion: " + expansion.getIdentifier());
|
||||
|
||||
if (expansion instanceof Taskable) {
|
||||
((Taskable) expansion).start();
|
||||
}
|
||||
if (expansion instanceof Taskable)
|
||||
{
|
||||
((Taskable) expansion).start();
|
||||
}
|
||||
|
||||
if (plugin.getExpansionCloud() != null) {
|
||||
CloudExpansion ce = plugin.getExpansionCloud().getCloudExpansion(expansion.getIdentifier());
|
||||
if (plugin.getExpansionCloud() != null)
|
||||
{
|
||||
final CloudExpansion cloudExpansion = plugin.getExpansionCloud().getCloudExpansion(expansion.getIdentifier());
|
||||
|
||||
if (ce != null) {
|
||||
ce.setHasExpansion(true);
|
||||
if (!ce.getLatestVersion().equals(expansion.getVersion())) {
|
||||
ce.setShouldUpdate(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cloudExpansion != null)
|
||||
{
|
||||
cloudExpansion.setHasExpansion(true);
|
||||
if (!cloudExpansion.getLatestVersion().equals(expansion.getVersion()))
|
||||
{
|
||||
cloudExpansion.setShouldUpdate(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public PlaceholderExpansion registerExpansion(String fileName) {
|
||||
List<Class<?>> subs = FileUtil.getClasses("expansions", fileName, PlaceholderExpansion.class);
|
||||
if (subs == null || subs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
@Nullable
|
||||
public PlaceholderExpansion registerExpansion(@NotNull final String fileName)
|
||||
{
|
||||
final List<Class<? extends PlaceholderExpansion>> subs = FileUtil.getClasses(folder, PlaceholderExpansion.class, fileName);
|
||||
if (subs.isEmpty())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// only register the first instance found as an expansion jar should only have 1 class
|
||||
// extending PlaceholderExpansion
|
||||
PlaceholderExpansion ex = createInstance(subs.get(0));
|
||||
if (registerExpansion(ex)) {
|
||||
return ex;
|
||||
}
|
||||
// only register the first instance found as an expansion jar should only have 1 class
|
||||
// extending PlaceholderExpansion
|
||||
final PlaceholderExpansion expansion = createInstance(subs.get(0));
|
||||
if (expansion != null && registerExpansion(expansion))
|
||||
{
|
||||
return expansion;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void registerAllExpansions() {
|
||||
if (plugin == null) {
|
||||
return;
|
||||
}
|
||||
public void registerAllExpansions()
|
||||
{
|
||||
final List<@NotNull Class<? extends PlaceholderExpansion>> subs = FileUtil.getClasses(folder, PlaceholderExpansion.class);
|
||||
if (subs.isEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
List<Class<?>> subs = FileUtil.getClasses("expansions", null, PlaceholderExpansion.class);
|
||||
if (subs == null || subs.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (final Class<? extends PlaceholderExpansion> clazz : subs)
|
||||
{
|
||||
final PlaceholderExpansion expansion = createInstance(clazz);
|
||||
if (expansion == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (Class<?> klass : subs) {
|
||||
PlaceholderExpansion ex = createInstance(klass);
|
||||
if (ex != null) {
|
||||
try {
|
||||
registerExpansion(ex);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().info("Couldn't register " + ex.getIdentifier() + " expansion");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
registerExpansion(expansion);
|
||||
}
|
||||
catch (final Exception ex)
|
||||
{
|
||||
plugin.getLogger().log(Level.WARNING, "Couldn't register " + expansion.getIdentifier() + " expansion", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private PlaceholderExpansion createInstance(Class<?> klass) {
|
||||
if (klass == null) {
|
||||
return null;
|
||||
}
|
||||
@Nullable
|
||||
private PlaceholderExpansion createInstance(@NotNull final Class<? extends PlaceholderExpansion> clazz)
|
||||
{
|
||||
try
|
||||
{
|
||||
return clazz.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
catch (final Throwable ex)
|
||||
{
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to load placeholder expansion from class: " + clazz.getName(), ex);
|
||||
}
|
||||
|
||||
PlaceholderExpansion ex = null;
|
||||
if (!PlaceholderExpansion.class.isAssignableFrom(klass)) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
Constructor<?>[] c = klass.getConstructors();
|
||||
if (c.length == 0) {
|
||||
ex = (PlaceholderExpansion) klass.newInstance();
|
||||
} else {
|
||||
for (Constructor<?> con : c) {
|
||||
if (con.getParameterTypes().length == 0) {
|
||||
ex = (PlaceholderExpansion) klass.newInstance();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
plugin.getLogger()
|
||||
.severe("Failed to init placeholder expansion from class: " + klass.getName());
|
||||
plugin.getLogger().severe(t.getMessage());
|
||||
}
|
||||
|
||||
return ex;
|
||||
}
|
||||
}
|
||||
|
@ -20,89 +20,85 @@
|
||||
*/
|
||||
package me.clip.placeholderapi.util;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarInputStream;
|
||||
|
||||
public class FileUtil {
|
||||
public class FileUtil
|
||||
{
|
||||
|
||||
public static List<Class<?>> getClasses(String folder, Class<?> type) {
|
||||
return getClasses(folder, null, type);
|
||||
}
|
||||
@NotNull
|
||||
public static <T> List<@NotNull Class<? extends T>> getClasses(@NotNull final File folder, @NotNull final Class<T> clazz)
|
||||
{
|
||||
return getClasses(folder, clazz, null);
|
||||
}
|
||||
|
||||
public static List<Class<?>> getClasses(String folder, String fileName, Class<?> type) {
|
||||
List<Class<?>> list = new ArrayList<>();
|
||||
@NotNull
|
||||
public static <T> List<@NotNull Class<? extends T>> getClasses(@NotNull final File folder, @NotNull final Class<T> clazz, @Nullable final String target)
|
||||
{
|
||||
if (!folder.exists())
|
||||
{
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
try {
|
||||
File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), folder);
|
||||
if (!f.exists()) {
|
||||
return list;
|
||||
}
|
||||
try
|
||||
{
|
||||
final FilenameFilter filter =
|
||||
(dir, name) -> name.endsWith(".jar") && (target == null || name.replace(".jar", "").equalsIgnoreCase(target.replace(".jar", "")));
|
||||
|
||||
FilenameFilter fileNameFilter = (dir, name) -> {
|
||||
if (fileName != null) {
|
||||
return name.endsWith(".jar") && name.replace(".jar", "")
|
||||
.equalsIgnoreCase(fileName.replace(".jar", ""));
|
||||
}
|
||||
final File[] jars = folder.listFiles(filter);
|
||||
if (jars == null)
|
||||
{
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return name.endsWith(".jar");
|
||||
};
|
||||
final List<@NotNull Class<? extends T>> list = new ArrayList<>();
|
||||
|
||||
File[] jars = f.listFiles(fileNameFilter);
|
||||
if (jars == null) {
|
||||
return list;
|
||||
}
|
||||
for (File file : jars)
|
||||
{
|
||||
gather(file.toURI().toURL(), clazz, list);
|
||||
}
|
||||
|
||||
for (File file : jars) {
|
||||
list = gather(file.toURI().toURL(), list, type);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
// THIS SHOULD NOT BE EATEN LIKE THIS.
|
||||
}
|
||||
|
||||
return list;
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
private static <T> void gather(@NotNull final URL jar, @NotNull final Class<T> clazz, @NotNull final List<@NotNull Class<? extends T>> list) throws IOException, ClassNotFoundException
|
||||
{
|
||||
try (final URLClassLoader loader = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader()); final JarInputStream stream = new JarInputStream(jar.openStream()))
|
||||
{
|
||||
JarEntry entry;
|
||||
while ((entry = stream.getNextJarEntry()) != null)
|
||||
{
|
||||
final String name = entry.getName();
|
||||
if (name == null || name.isEmpty() || !name.endsWith(".class"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
private static List<Class<?>> gather(URL jar, List<Class<?>> list, Class<?> clazz) {
|
||||
if (list == null) {
|
||||
list = new ArrayList<>();
|
||||
}
|
||||
final Class<?> loaded = loader.loadClass(name.substring(0, name.lastIndexOf('.')).replace('/', '.'));
|
||||
if (clazz.isAssignableFrom(loaded))
|
||||
{
|
||||
list.add(loaded.asSubclass(clazz));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try (URLClassLoader cl = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader());
|
||||
JarInputStream jis = new JarInputStream(jar.openStream())) {
|
||||
|
||||
while (true) {
|
||||
JarEntry j = jis.getNextJarEntry();
|
||||
if (j == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
String name = j.getName();
|
||||
if (name == null || name.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (name.endsWith(".class")) {
|
||||
name = name.replace("/", ".");
|
||||
String cname = name.substring(0, name.lastIndexOf(".class"));
|
||||
|
||||
Class<?> c = cl.loadClass(cname);
|
||||
if (clazz.isAssignableFrom(c)) {
|
||||
list.add(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user