mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 11:38:40 +01:00
Only load slf4j if it's not already present on the server
This commit is contained in:
parent
44d137b8f9
commit
de8fb8de90
@ -154,7 +154,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
|
|||||||
senderFactory = new BukkitSenderFactory(this);
|
senderFactory = new BukkitSenderFactory(this);
|
||||||
log = new SenderLogger(this, getConsoleSender());
|
log = new SenderLogger(this, getConsoleSender());
|
||||||
|
|
||||||
DependencyManager.loadDependencies(this, Collections.singletonList(Dependency.CAFFEINE));
|
DependencyManager.loadDependencies(this, Collections.singleton(Dependency.CAFFEINE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -194,7 +194,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
|
|||||||
configuration.loadAll();
|
configuration.loadAll();
|
||||||
|
|
||||||
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
|
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
|
||||||
DependencyManager.loadDependencies(this, storageTypes);
|
DependencyManager.loadStorageDependencies(this, storageTypes);
|
||||||
|
|
||||||
// setup the Bukkit defaults hook
|
// setup the Bukkit defaults hook
|
||||||
defaultsProvider = new DefaultsProvider();
|
defaultsProvider = new DefaultsProvider();
|
||||||
|
@ -32,6 +32,7 @@ import com.github.cheesesoftware.PowerfulPermsAPI.Group;
|
|||||||
import com.github.cheesesoftware.PowerfulPermsAPI.Permission;
|
import com.github.cheesesoftware.PowerfulPermsAPI.Permission;
|
||||||
import com.github.cheesesoftware.PowerfulPermsAPI.PermissionManager;
|
import com.github.cheesesoftware.PowerfulPermsAPI.PermissionManager;
|
||||||
import com.github.cheesesoftware.PowerfulPermsAPI.PowerfulPermsPlugin;
|
import com.github.cheesesoftware.PowerfulPermsAPI.PowerfulPermsPlugin;
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
|
||||||
import me.lucko.luckperms.api.Node;
|
import me.lucko.luckperms.api.Node;
|
||||||
import me.lucko.luckperms.api.event.cause.CreationCause;
|
import me.lucko.luckperms.api.event.cause.CreationCause;
|
||||||
@ -94,7 +95,7 @@ public class MigrationPowerfulPerms extends SubCommand<Object> {
|
|||||||
|
|
||||||
if (type == null || type != StorageType.MYSQL) {
|
if (type == null || type != StorageType.MYSQL) {
|
||||||
// We need to load the Hikari/MySQL stuff.
|
// We need to load the Hikari/MySQL stuff.
|
||||||
DependencyManager.loadDependencies(plugin, DependencyManager.STORAGE_DEPENDENCIES.get(StorageType.MYSQL));
|
DependencyManager.loadStorageDependencies(plugin, ImmutableSet.of(StorageType.MYSQL));
|
||||||
}
|
}
|
||||||
|
|
||||||
String address = args.get(0);
|
String address = args.get(0);
|
||||||
|
@ -129,7 +129,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
|
|||||||
senderFactory = new BungeeSenderFactory(this);
|
senderFactory = new BungeeSenderFactory(this);
|
||||||
log = new SenderLogger(this, getConsoleSender());
|
log = new SenderLogger(this, getConsoleSender());
|
||||||
|
|
||||||
DependencyManager.loadDependencies(this, Collections.singletonList(Dependency.CAFFEINE));
|
DependencyManager.loadDependencies(this, Collections.singleton(Dependency.CAFFEINE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -146,7 +146,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
|
|||||||
configuration.loadAll();
|
configuration.loadAll();
|
||||||
|
|
||||||
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
|
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
|
||||||
DependencyManager.loadDependencies(this, storageTypes);
|
DependencyManager.loadStorageDependencies(this, storageTypes);
|
||||||
|
|
||||||
// register events
|
// register events
|
||||||
getProxy().getPluginManager().registerListener(this, new BungeeListener(this));
|
getProxy().getPluginManager().registerListener(this, new BungeeListener(this));
|
||||||
|
@ -29,9 +29,7 @@ import lombok.experimental.UtilityClass;
|
|||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
|
|
||||||
import me.lucko.luckperms.api.PlatformType;
|
|
||||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||||
import me.lucko.luckperms.common.storage.StorageType;
|
import me.lucko.luckperms.common.storage.StorageType;
|
||||||
@ -45,10 +43,14 @@ import java.net.URL;
|
|||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Responsible for loading runtime dependencies.
|
||||||
|
*/
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class DependencyManager {
|
public class DependencyManager {
|
||||||
private static final Method ADD_URL_METHOD;
|
private static final Method ADD_URL_METHOD;
|
||||||
@ -64,7 +66,7 @@ public class DependencyManager {
|
|||||||
ADD_URL_METHOD = addUrlMethod;
|
ADD_URL_METHOD = addUrlMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Map<StorageType, List<Dependency>> STORAGE_DEPENDENCIES = ImmutableMap.<StorageType, List<Dependency>>builder()
|
private static final Map<StorageType, List<Dependency>> STORAGE_DEPENDENCIES = ImmutableMap.<StorageType, List<Dependency>>builder()
|
||||||
.put(StorageType.JSON, ImmutableList.of())
|
.put(StorageType.JSON, ImmutableList.of())
|
||||||
.put(StorageType.YAML, ImmutableList.of())
|
.put(StorageType.YAML, ImmutableList.of())
|
||||||
.put(StorageType.MONGODB, ImmutableList.of(Dependency.MONGODB_DRIVER))
|
.put(StorageType.MONGODB, ImmutableList.of(Dependency.MONGODB_DRIVER))
|
||||||
@ -75,8 +77,8 @@ public class DependencyManager {
|
|||||||
.put(StorageType.H2, ImmutableList.of(Dependency.H2_DRIVER))
|
.put(StorageType.H2, ImmutableList.of(Dependency.H2_DRIVER))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
public static void loadDependencies(LuckPermsPlugin plugin, Set<StorageType> storageTypes) {
|
public static void loadStorageDependencies(LuckPermsPlugin plugin, Set<StorageType> storageTypes) {
|
||||||
List<Dependency> dependencies = new ArrayList<>();
|
Set<Dependency> dependencies = new LinkedHashSet<>();
|
||||||
for (StorageType storageType : storageTypes) {
|
for (StorageType storageType : storageTypes) {
|
||||||
dependencies.addAll(STORAGE_DEPENDENCIES.get(storageType));
|
dependencies.addAll(STORAGE_DEPENDENCIES.get(storageType));
|
||||||
}
|
}
|
||||||
@ -85,8 +87,8 @@ public class DependencyManager {
|
|||||||
dependencies.add(Dependency.JEDIS);
|
dependencies.add(Dependency.JEDIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't load slf4j on sponge.
|
// don't load slf4j if it's already present
|
||||||
if (plugin.getServerType() == PlatformType.SPONGE) {
|
if (classExists("org.slf4j.Logger") && classExists("org.slf4j.LoggerFactory")) {
|
||||||
dependencies.remove(Dependency.SLF4J_API);
|
dependencies.remove(Dependency.SLF4J_API);
|
||||||
dependencies.remove(Dependency.SLF4J_SIMPLE);
|
dependencies.remove(Dependency.SLF4J_SIMPLE);
|
||||||
}
|
}
|
||||||
@ -94,17 +96,17 @@ public class DependencyManager {
|
|||||||
loadDependencies(plugin, dependencies);
|
loadDependencies(plugin, dependencies);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loadDependencies(LuckPermsPlugin plugin, List<Dependency> dependencies) {
|
public static void loadDependencies(LuckPermsPlugin plugin, Set<Dependency> dependencies) {
|
||||||
plugin.getLog().info("Identified the following dependencies: " + dependencies.toString());
|
plugin.getLog().info("Identified the following dependencies: " + dependencies.toString());
|
||||||
|
|
||||||
File data = new File(plugin.getDataDirectory(), "lib");
|
File libDir = new File(plugin.getDataDirectory(), "lib");
|
||||||
data.mkdirs();
|
libDir.mkdirs();
|
||||||
|
|
||||||
// Download files.
|
// Download files.
|
||||||
List<Map.Entry<Dependency, File>> toLoad = new ArrayList<>();
|
List<File> filesToLoad = new ArrayList<>();
|
||||||
for (Dependency dependency : dependencies) {
|
for (Dependency dependency : dependencies) {
|
||||||
try {
|
try {
|
||||||
toLoad.add(Maps.immutableEntry(dependency, downloadDependency(plugin, data, dependency)));
|
filesToLoad.add(downloadDependency(plugin, libDir, dependency));
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
plugin.getLog().severe("Exception whilst downloading dependency " + dependency.name());
|
plugin.getLog().severe("Exception whilst downloading dependency " + dependency.name());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -112,27 +114,27 @@ public class DependencyManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load classes.
|
// Load classes.
|
||||||
for (Map.Entry<Dependency, File> e : toLoad) {
|
for (File file : filesToLoad) {
|
||||||
try {
|
try {
|
||||||
loadJar(plugin, e.getValue());
|
loadJar(plugin, file);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
plugin.getLog().severe("Failed to load jar for dependency " + e.getKey().name());
|
plugin.getLog().severe("Failed to load dependency jar " + file.getName());
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File downloadDependency(LuckPermsPlugin plugin, File libDir, Dependency dependency) throws Exception {
|
private static File downloadDependency(LuckPermsPlugin plugin, File libDir, Dependency dependency) throws Exception {
|
||||||
String name = dependency.name().toLowerCase() + "-" + dependency.getVersion() + ".jar";
|
String fileName = dependency.name().toLowerCase() + "-" + dependency.getVersion() + ".jar";
|
||||||
|
|
||||||
File file = new File(libDir, name);
|
File file = new File(libDir, fileName);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
URL url = new URL(dependency.getUrl());
|
URL url = new URL(dependency.getUrl());
|
||||||
|
|
||||||
plugin.getLog().info("Dependency '" + name + "' could not be found. Attempting to download.");
|
plugin.getLog().info("Dependency '" + fileName + "' could not be found. Attempting to download.");
|
||||||
try (InputStream in = url.openStream()) {
|
try (InputStream in = url.openStream()) {
|
||||||
Files.copy(in, file.toPath());
|
Files.copy(in, file.toPath());
|
||||||
}
|
}
|
||||||
@ -140,7 +142,7 @@ public class DependencyManager {
|
|||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
throw new IllegalStateException("File not present. - " + file.toString());
|
throw new IllegalStateException("File not present. - " + file.toString());
|
||||||
} else {
|
} else {
|
||||||
plugin.getLog().info("Dependency '" + name + "' successfully downloaded.");
|
plugin.getLog().info("Dependency '" + fileName + "' successfully downloaded.");
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -156,7 +158,16 @@ public class DependencyManager {
|
|||||||
throw new RuntimeException("Unable to invoke URLClassLoader#addURL", e);
|
throw new RuntimeException("Unable to invoke URLClassLoader#addURL", e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Unknown classloader: " + classLoader.getClass());
|
throw new RuntimeException("Unknown classloader type: " + classLoader.getClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean classExists(String className) {
|
||||||
|
try {
|
||||||
|
Class.forName(className);
|
||||||
|
return true;
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,7 +201,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
|
|||||||
configuration.loadAll();
|
configuration.loadAll();
|
||||||
|
|
||||||
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
|
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
|
||||||
DependencyManager.loadDependencies(this, storageTypes);
|
DependencyManager.loadStorageDependencies(this, storageTypes);
|
||||||
|
|
||||||
// register events
|
// register events
|
||||||
game.getEventManager().registerListeners(this, new SpongeListener(this));
|
game.getEventManager().registerListeners(this, new SpongeListener(this));
|
||||||
|
Loading…
Reference in New Issue
Block a user