mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-02-05 15:55:28 +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.Bukkit;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public final class ExpansionManager {
|
public final class ExpansionManager
|
||||||
private final PlaceholderAPIPlugin plugin;
|
{
|
||||||
|
|
||||||
public ExpansionManager(PlaceholderAPIPlugin instance) {
|
@NotNull
|
||||||
plugin = instance;
|
private final File folder;
|
||||||
|
@NotNull
|
||||||
|
private final PlaceholderAPIPlugin plugin;
|
||||||
|
|
||||||
File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), "expansions");
|
public ExpansionManager(@NotNull final PlaceholderAPIPlugin plugin)
|
||||||
if (!f.exists()) {
|
{
|
||||||
f.mkdirs();
|
this.plugin = plugin;
|
||||||
}
|
this.folder = new File(plugin.getDataFolder(), "expansions");
|
||||||
}
|
|
||||||
|
|
||||||
public PlaceholderExpansion getRegisteredExpansion(String name) {
|
if (!this.folder.exists() && !folder.mkdirs())
|
||||||
for (Entry<String, PlaceholderHook> hook : PlaceholderAPI.getPlaceholders().entrySet()) {
|
{
|
||||||
if (hook.getValue() instanceof PlaceholderExpansion) {
|
plugin.getLogger().log(Level.WARNING, "failed to create expansions folder!");
|
||||||
if (name.equalsIgnoreCase(hook.getKey())) {
|
}
|
||||||
return (PlaceholderExpansion) hook.getValue();
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
return null;
|
||||||
if (expansion == null || expansion.getIdentifier() == null) {
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (expansion instanceof Configurable) {
|
public boolean registerExpansion(@NotNull final PlaceholderExpansion expansion)
|
||||||
Map<String, Object> defaults = ((Configurable) expansion).getDefaults();
|
{
|
||||||
String pre = "expansions." + expansion.getIdentifier() + ".";
|
if (expansion.getIdentifier() == null)
|
||||||
FileConfiguration cfg = plugin.getConfig();
|
{
|
||||||
boolean save = false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (defaults != null) {
|
if (expansion instanceof Configurable)
|
||||||
for (Entry<String, Object> entries : defaults.entrySet()) {
|
{
|
||||||
if (entries.getKey() == null || entries.getKey().isEmpty()) {
|
Map<String, Object> defaults = ((Configurable) expansion).getDefaults();
|
||||||
continue;
|
String pre = "expansions." + expansion.getIdentifier() + ".";
|
||||||
}
|
FileConfiguration cfg = plugin.getConfig();
|
||||||
|
boolean save = false;
|
||||||
|
|
||||||
if (entries.getValue() == null) {
|
if (defaults != null)
|
||||||
if (cfg.contains(pre + entries.getKey())) {
|
{
|
||||||
save = true;
|
for (Entry<String, Object> entries : defaults.entrySet())
|
||||||
cfg.set(pre + entries.getKey(), null);
|
{
|
||||||
}
|
if (entries.getKey() == null || entries.getKey().isEmpty())
|
||||||
} else {
|
{
|
||||||
if (!cfg.contains(pre + entries.getKey())) {
|
continue;
|
||||||
save = true;
|
}
|
||||||
cfg.set(pre + entries.getKey(), entries.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (save) {
|
if (entries.getValue() == null)
|
||||||
plugin.saveConfig();
|
{
|
||||||
plugin.reloadConfig();
|
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) {
|
if (save)
|
||||||
VersionSpecific nms = (VersionSpecific) expansion;
|
{
|
||||||
if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) {
|
plugin.saveConfig();
|
||||||
plugin.getLogger()
|
plugin.reloadConfig();
|
||||||
.info(
|
}
|
||||||
"Your server version is not compatible with expansion: " + expansion.getIdentifier()
|
}
|
||||||
+ " version: " + expansion.getVersion());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!expansion.canRegister()) {
|
if (expansion instanceof VersionSpecific)
|
||||||
return false;
|
{
|
||||||
}
|
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()) {
|
if (!expansion.canRegister() || !expansion.register())
|
||||||
return false;
|
{
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (expansion instanceof Listener) {
|
if (expansion instanceof Listener)
|
||||||
Listener l = (Listener) expansion;
|
{
|
||||||
Bukkit.getPluginManager().registerEvents(l, plugin);
|
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) {
|
if (expansion instanceof Taskable)
|
||||||
((Taskable) expansion).start();
|
{
|
||||||
}
|
((Taskable) expansion).start();
|
||||||
|
}
|
||||||
|
|
||||||
if (plugin.getExpansionCloud() != null) {
|
if (plugin.getExpansionCloud() != null)
|
||||||
CloudExpansion ce = plugin.getExpansionCloud().getCloudExpansion(expansion.getIdentifier());
|
{
|
||||||
|
final CloudExpansion cloudExpansion = plugin.getExpansionCloud().getCloudExpansion(expansion.getIdentifier());
|
||||||
|
|
||||||
if (ce != null) {
|
if (cloudExpansion != null)
|
||||||
ce.setHasExpansion(true);
|
{
|
||||||
if (!ce.getLatestVersion().equals(expansion.getVersion())) {
|
cloudExpansion.setHasExpansion(true);
|
||||||
ce.setShouldUpdate(true);
|
if (!cloudExpansion.getLatestVersion().equals(expansion.getVersion()))
|
||||||
}
|
{
|
||||||
}
|
cloudExpansion.setShouldUpdate(true);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public PlaceholderExpansion registerExpansion(String fileName) {
|
@Nullable
|
||||||
List<Class<?>> subs = FileUtil.getClasses("expansions", fileName, PlaceholderExpansion.class);
|
public PlaceholderExpansion registerExpansion(@NotNull final String fileName)
|
||||||
if (subs == null || subs.isEmpty()) {
|
{
|
||||||
return null;
|
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
|
// only register the first instance found as an expansion jar should only have 1 class
|
||||||
// extending PlaceholderExpansion
|
// extending PlaceholderExpansion
|
||||||
PlaceholderExpansion ex = createInstance(subs.get(0));
|
final PlaceholderExpansion expansion = createInstance(subs.get(0));
|
||||||
if (registerExpansion(ex)) {
|
if (expansion != null && registerExpansion(expansion))
|
||||||
return ex;
|
{
|
||||||
}
|
return expansion;
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerAllExpansions() {
|
public void registerAllExpansions()
|
||||||
if (plugin == null) {
|
{
|
||||||
return;
|
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);
|
for (final Class<? extends PlaceholderExpansion> clazz : subs)
|
||||||
if (subs == null || subs.isEmpty()) {
|
{
|
||||||
return;
|
final PlaceholderExpansion expansion = createInstance(clazz);
|
||||||
}
|
if (expansion == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for (Class<?> klass : subs) {
|
try
|
||||||
PlaceholderExpansion ex = createInstance(klass);
|
{
|
||||||
if (ex != null) {
|
registerExpansion(expansion);
|
||||||
try {
|
}
|
||||||
registerExpansion(ex);
|
catch (final Exception ex)
|
||||||
} catch (Exception e) {
|
{
|
||||||
plugin.getLogger().info("Couldn't register " + ex.getIdentifier() + " expansion");
|
plugin.getLogger().log(Level.WARNING, "Couldn't register " + expansion.getIdentifier() + " expansion", ex);
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private PlaceholderExpansion createInstance(Class<?> klass) {
|
@Nullable
|
||||||
if (klass == null) {
|
private PlaceholderExpansion createInstance(@NotNull final Class<? extends PlaceholderExpansion> clazz)
|
||||||
return null;
|
{
|
||||||
}
|
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;
|
return null;
|
||||||
if (!PlaceholderExpansion.class.isAssignableFrom(klass)) {
|
}
|
||||||
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;
|
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.File;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarInputStream;
|
import java.util.jar.JarInputStream;
|
||||||
|
|
||||||
public class FileUtil {
|
public class FileUtil
|
||||||
|
{
|
||||||
|
|
||||||
public static List<Class<?>> getClasses(String folder, Class<?> type) {
|
@NotNull
|
||||||
return getClasses(folder, null, type);
|
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) {
|
@NotNull
|
||||||
List<Class<?>> list = new ArrayList<>();
|
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 {
|
try
|
||||||
File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), folder);
|
{
|
||||||
if (!f.exists()) {
|
final FilenameFilter filter =
|
||||||
return list;
|
(dir, name) -> name.endsWith(".jar") && (target == null || name.replace(".jar", "").equalsIgnoreCase(target.replace(".jar", "")));
|
||||||
}
|
|
||||||
|
|
||||||
FilenameFilter fileNameFilter = (dir, name) -> {
|
final File[] jars = folder.listFiles(filter);
|
||||||
if (fileName != null) {
|
if (jars == null)
|
||||||
return name.endsWith(".jar") && name.replace(".jar", "")
|
{
|
||||||
.equalsIgnoreCase(fileName.replace(".jar", ""));
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
return name.endsWith(".jar");
|
final List<@NotNull Class<? extends T>> list = new ArrayList<>();
|
||||||
};
|
|
||||||
|
|
||||||
File[] jars = f.listFiles(fileNameFilter);
|
for (File file : jars)
|
||||||
if (jars == null) {
|
{
|
||||||
return list;
|
gather(file.toURI().toURL(), clazz, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (File file : jars) {
|
return list;
|
||||||
list = gather(file.toURI().toURL(), list, type);
|
}
|
||||||
}
|
catch (Throwable t)
|
||||||
|
{
|
||||||
|
// THIS SHOULD NOT BE EATEN LIKE THIS.
|
||||||
|
}
|
||||||
|
|
||||||
return list;
|
return Collections.emptyList();
|
||||||
} catch (Throwable t) {
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
final Class<?> loaded = loader.loadClass(name.substring(0, name.lastIndexOf('.')).replace('/', '.'));
|
||||||
if (list == null) {
|
if (clazz.isAssignableFrom(loaded))
|
||||||
list = new ArrayList<>();
|
{
|
||||||
}
|
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