mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2024-12-23 09:47:38 +00:00
WEPIF Improvement: code clean out, hardcoding removal and refactoring. Added Service discovery to PluginPermissionsResolver
This commit is contained in:
parent
102e5f142e
commit
4f80e78a7e
@ -142,4 +142,9 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver {
|
||||
|
||||
return groups.toArray(new String[groups.size()]);
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "No known permissions plugin detected. Using configuration file for permissions.";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.bukkit.util.config.Configuration;
|
||||
|
||||
public class DinnerPermsResolver implements PermissionsResolver {
|
||||
|
||||
@ -34,7 +35,15 @@ public class DinnerPermsResolver implements PermissionsResolver {
|
||||
public DinnerPermsResolver(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
|
||||
public static PermissionsResolver factory(Server server, Configuration config) {
|
||||
if(!config.getBoolean("dinnerperms", true)){
|
||||
return null;
|
||||
}
|
||||
|
||||
return new DinnerPermsResolver(server);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
// Permissions are already loaded
|
||||
}
|
||||
@ -84,4 +93,10 @@ public class DinnerPermsResolver implements PermissionsResolver {
|
||||
}
|
||||
return groupNames.toArray(new String[groupNames.size()]);
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "Using the Bukkit Permissions API.";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -28,28 +28,49 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.HashMap;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.util.config.Configuration;
|
||||
|
||||
public class FlatFilePermissionsResolver implements PermissionsResolver {
|
||||
private Map<String,Set<String>> userPermissionsCache;
|
||||
private Set<String> defaultPermissionsCache;
|
||||
private Map<String,Set<String>> userGroups;
|
||||
|
||||
public FlatFilePermissionsResolver() {
|
||||
protected File groupFile;
|
||||
protected File userFile;
|
||||
|
||||
public static PermissionsResolver factory(Server server, Configuration config){
|
||||
File groups = new File("perms_groups.txt");
|
||||
File users = new File("perms_users.txt");
|
||||
|
||||
if(!groups.exists() || !users.exists()){
|
||||
return null;
|
||||
}
|
||||
|
||||
return new FlatFilePermissionsResolver(groups, users);
|
||||
}
|
||||
|
||||
public FlatFilePermissionsResolver(){
|
||||
this(new File("perms_groups.txt"), new File("perms_users.txt"));
|
||||
}
|
||||
|
||||
public FlatFilePermissionsResolver(File groupFile, File userFile) {
|
||||
this.groupFile = groupFile;
|
||||
this.userFile = userFile;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean filesExists() {
|
||||
return (new File("perms_groups.txt")).exists()
|
||||
&& (new File("perms_users.txt")).exists();
|
||||
return (new File("perms_groups.txt")).exists() && (new File("perms_users.txt")).exists();
|
||||
}
|
||||
|
||||
public Map<String,Set<String>> loadGroupPermissions() {
|
||||
Map<String,Set<String>> userGroupPermissions = new HashMap<String,Set<String>>();
|
||||
|
||||
File file = new File("perms_groups.txt");
|
||||
FileReader input = null;
|
||||
|
||||
try {
|
||||
input = new FileReader(file);
|
||||
input = new FileReader(this.groupFile);
|
||||
BufferedReader buff = new BufferedReader(input);
|
||||
|
||||
String line;
|
||||
@ -99,11 +120,10 @@ public class FlatFilePermissionsResolver implements PermissionsResolver {
|
||||
defaultPermissionsCache = userGroupPermissions.get("default");
|
||||
}
|
||||
|
||||
File file = new File("perms_users.txt");
|
||||
FileReader input = null;
|
||||
|
||||
try {
|
||||
input = new FileReader(file);
|
||||
input = new FileReader(this.userFile);
|
||||
BufferedReader buff = new BufferedReader(input);
|
||||
|
||||
String line;
|
||||
@ -191,4 +211,9 @@ public class FlatFilePermissionsResolver implements PermissionsResolver {
|
||||
|
||||
return groups.toArray(new String[groups.size()]);
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "perms_groups.txt and perms_users.txt detected! Using flat file permissions.";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,34 +26,37 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import com.nijikokun.bukkit.Permissions.Permissions;
|
||||
import com.sk89q.bukkit.migration.PermissionsResolverManager.MissingPluginException;
|
||||
import org.bukkit.util.config.Configuration;
|
||||
|
||||
public class NijiPermissionsResolver implements PermissionsResolver {
|
||||
private Server server;
|
||||
private Permissions api;
|
||||
|
||||
public static PermissionsResolver factory(Server server, Configuration config) {
|
||||
PluginManager pluginManager = server.getPluginManager();
|
||||
|
||||
Plugin plugin = pluginManager.getPlugin("Permissions");
|
||||
|
||||
// Check if plugin is loaded and have Permissions interface
|
||||
if(plugin == null || !(plugin instanceof Permissions)){
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check for fake permissions
|
||||
if(config.getBoolean("ignore-nijiperms-bridges", true) && isFakeNijiPerms(plugin)){
|
||||
return null;
|
||||
}
|
||||
|
||||
return new NijiPermissionsResolver(server, (Permissions)plugin);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
|
||||
}
|
||||
|
||||
public NijiPermissionsResolver(Server server, boolean ignoreBridges)
|
||||
throws PluginAccessException, MissingPluginException {
|
||||
public NijiPermissionsResolver(Server server, Permissions plugin) {
|
||||
this.server = server;
|
||||
PluginManager manager = server.getPluginManager();
|
||||
|
||||
Plugin plugin = manager.getPlugin("Permissions");
|
||||
if (plugin == null) {
|
||||
throw new MissingPluginException();
|
||||
}
|
||||
if (!checkRealNijiPerms(ignoreBridges)) {
|
||||
throw new MissingPluginException();
|
||||
}
|
||||
|
||||
try {
|
||||
api = (Permissions)plugin;
|
||||
} catch (ClassCastException e) {
|
||||
throw new PluginAccessException();
|
||||
}
|
||||
this.api = plugin;
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
@ -129,6 +132,7 @@ public class NijiPermissionsResolver implements PermissionsResolver {
|
||||
private static final long serialVersionUID = 7044832912491608706L;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean checkRealNijiPerms(boolean ignoreBridges) {
|
||||
if (!ignoreBridges) {
|
||||
return true;
|
||||
@ -139,4 +143,14 @@ public class NijiPermissionsResolver implements PermissionsResolver {
|
||||
}
|
||||
return permsCommand.getPlugin().getDescription().getName().equals("Permissions");
|
||||
}
|
||||
|
||||
public static boolean isFakeNijiPerms(Plugin plugin){
|
||||
PluginCommand permsCommand = Bukkit.getServer().getPluginCommand("permissions");
|
||||
|
||||
return !(permsCommand.getPlugin().equals(plugin));
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "Permissions plugin detected! Using Permissions plugin for permissions.";
|
||||
}
|
||||
}
|
||||
|
@ -25,18 +25,25 @@ import org.bukkit.entity.Player;
|
||||
import ru.tehkode.permissions.PermissionManager;
|
||||
import ru.tehkode.permissions.PermissionUser;
|
||||
|
||||
import com.sk89q.bukkit.migration.PermissionsResolverManager.MissingPluginException;
|
||||
import org.bukkit.util.config.Configuration;
|
||||
|
||||
public class PermissionsExResolver implements PermissionsResolver {
|
||||
private final PermissionManager manager;
|
||||
private final Server server;
|
||||
|
||||
public PermissionsExResolver(Server server) throws MissingPluginException {
|
||||
this.server = server;
|
||||
manager = server.getServicesManager().load(PermissionManager.class);
|
||||
if (manager == null) {
|
||||
throw new MissingPluginException();
|
||||
|
||||
public static PermissionsResolver factory(Server server, Configuration config){
|
||||
PermissionManager manager = server.getServicesManager().load(PermissionManager.class);
|
||||
|
||||
if(manager == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
return new PermissionsExResolver(server, manager);
|
||||
}
|
||||
|
||||
public PermissionsExResolver(Server server, PermissionManager manager) {
|
||||
this.server = server;
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
@ -68,4 +75,7 @@ public class PermissionsExResolver implements PermissionsResolver {
|
||||
return user.getGroupsNames();
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "PermissionsEx detected! Using PermissionsEx for permissions.";
|
||||
}
|
||||
}
|
||||
|
@ -19,10 +19,8 @@
|
||||
|
||||
package com.sk89q.bukkit.migration;
|
||||
|
||||
public interface PermissionsResolver {
|
||||
public interface PermissionsResolver extends PermissionsProvider {
|
||||
public void load();
|
||||
public boolean hasPermission(String name, String permission);
|
||||
public boolean hasPermission(String worldName, String name, String permission);
|
||||
public boolean inGroup(String player, String group);
|
||||
public String[] getGroups(String player);
|
||||
|
||||
public String getDetectionMessage();
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ package com.sk89q.bukkit.migration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
import org.bukkit.Server;
|
||||
@ -52,12 +53,19 @@ public class PermissionsResolverManager implements PermissionsResolver {
|
||||
"\r\n";
|
||||
|
||||
private Server server;
|
||||
private PermissionsResolver perms;
|
||||
private PermissionsResolver permissionResolver;
|
||||
private PermissionsResolverServerListener listener;
|
||||
private Configuration permsConfig;
|
||||
private Configuration config;
|
||||
private String name;
|
||||
private Logger logger;
|
||||
protected boolean ignoreNijiPermsBridges;
|
||||
|
||||
protected Class<? extends PermissionsResolver>[] availableResolvers = new Class[]{
|
||||
PluginPermissionsResolver.class,
|
||||
PermissionsExResolver.class,
|
||||
NijiPermissionsResolver.class,
|
||||
DinnerPermsResolver.class,
|
||||
FlatFilePermissionsResolver.class
|
||||
};
|
||||
|
||||
@Deprecated
|
||||
public PermissionsResolverManager(Configuration config, Server server, String name, Logger logger) {
|
||||
@ -72,104 +80,59 @@ public class PermissionsResolverManager implements PermissionsResolver {
|
||||
this.server = plugin.getServer();
|
||||
this.name = name;
|
||||
this.logger = logger;
|
||||
new PermissionsResolverServerListener(this, plugin);
|
||||
this.listener = new PermissionsResolverServerListener(this, plugin);
|
||||
|
||||
loadConfig(new File("wepif.yml"));
|
||||
findResolver();
|
||||
}
|
||||
|
||||
public void findResolver() {
|
||||
if (tryPluginPermissionsResolver()) return;
|
||||
if (tryNijiPermissions()) return;
|
||||
if (tryPermissionsEx()) return;
|
||||
if (tryDinnerPerms()) return;
|
||||
if (tryFlatFilePermissions()) return;
|
||||
|
||||
perms = new ConfigurationPermissionsResolver(permsConfig);
|
||||
logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions.");
|
||||
}
|
||||
|
||||
private boolean tryNijiPermissions() {
|
||||
try {
|
||||
perms = new NijiPermissionsResolver(server, ignoreNijiPermsBridges);
|
||||
logger.info(name + ": Permissions plugin detected! Using Permissions plugin for permissions.");
|
||||
return true;
|
||||
} catch (Throwable e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean tryPermissionsEx() {
|
||||
try {
|
||||
perms = new PermissionsExResolver(server);
|
||||
logger.info(name + ": PermissionsEx detected! Using PermissionsEx for permissions.");
|
||||
return true;
|
||||
} catch (Throwable e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean tryFlatFilePermissions() {
|
||||
if (FlatFilePermissionsResolver.filesExists()) {
|
||||
perms = new FlatFilePermissionsResolver();
|
||||
logger.info(name + ": perms_groups.txt and perms_users.txt detected! Using flat file permissions.");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean tryPluginPermissionsResolver() {
|
||||
for (Plugin plugin : server.getPluginManager().getPlugins()) {
|
||||
if (plugin instanceof PermissionsProvider) {
|
||||
perms = new PluginPermissionsResolver(
|
||||
(PermissionsProvider) plugin);
|
||||
logger.info(name + ": Using plugin '"
|
||||
+ plugin.getDescription().getName() + "' for permissions.");
|
||||
return true;
|
||||
for (Class resolverClass : availableResolvers){
|
||||
try {
|
||||
Method factoryMethod = resolverClass.getMethod("factory", Server.class, Configuration.class);
|
||||
|
||||
this.permissionResolver = (PermissionsResolver)factoryMethod.invoke(null, this.server, this.config);
|
||||
|
||||
if(this.permissionResolver != null){
|
||||
logger.info(name + ": " + this.permissionResolver.getDetectionMessage());
|
||||
return;
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean tryDinnerPerms() {
|
||||
if (!permsConfig.getBoolean("dinnerperms", true)) {
|
||||
return false;
|
||||
}
|
||||
perms = new DinnerPermsResolver(server);
|
||||
logger.info(name + ": Using the Bukkit Permissions API.");
|
||||
return true;
|
||||
permissionResolver = new ConfigurationPermissionsResolver(config);
|
||||
logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions.");
|
||||
}
|
||||
|
||||
|
||||
public void setPluginPermissionsResolver(Plugin plugin) {
|
||||
if (!(plugin instanceof PermissionsProvider)) {
|
||||
return;
|
||||
}
|
||||
|
||||
perms = new PluginPermissionsResolver(
|
||||
(PermissionsProvider) plugin);
|
||||
logger.info(name + ": Using plugin '"
|
||||
+ plugin.getDescription().getName() + "' for permissions.");
|
||||
permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
|
||||
logger.info(name + ": Using plugin '" + plugin.getDescription().getName() + "' for permissions.");
|
||||
}
|
||||
|
||||
public void load() {
|
||||
perms.load();
|
||||
permissionResolver.load();
|
||||
}
|
||||
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
return perms.hasPermission(name, permission);
|
||||
return permissionResolver.hasPermission(name, permission);
|
||||
}
|
||||
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return perms.hasPermission(worldName, name, permission);
|
||||
return permissionResolver.hasPermission(worldName, name, permission);
|
||||
}
|
||||
|
||||
public boolean inGroup(String player, String group) {
|
||||
return perms.inGroup(player, group);
|
||||
return permissionResolver.inGroup(player, group);
|
||||
}
|
||||
|
||||
public String[] getGroups(String player) {
|
||||
return perms.getGroups(player);
|
||||
return permissionResolver.getGroups(player);
|
||||
}
|
||||
|
||||
private boolean loadConfig(File file) {
|
||||
@ -181,30 +144,30 @@ public class PermissionsResolverManager implements PermissionsResolver {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
permsConfig = new Configuration(file);
|
||||
permsConfig.load();
|
||||
List<String> keys = permsConfig.getKeys();
|
||||
permsConfig.setHeader(CONFIG_HEADER);
|
||||
config = new Configuration(file);
|
||||
config.load();
|
||||
List<String> keys = config.getKeys();
|
||||
config.setHeader(CONFIG_HEADER);
|
||||
if (!keys.contains("dinnerperms")) {
|
||||
permsConfig.setProperty("dinnerperms", permsConfig.getBoolean("dinner-perms", true));
|
||||
config.setProperty("dinnerperms", config.getBoolean("dinner-perms", true));
|
||||
isUpdated = true;
|
||||
}
|
||||
if (!keys.contains("ignore-nijiperms-bridges")) {
|
||||
permsConfig.setProperty("ignore-nijiperms-bridges", true);
|
||||
config.setProperty("ignore-nijiperms-bridges", true);
|
||||
isUpdated = true;
|
||||
}
|
||||
ignoreNijiPermsBridges = permsConfig.getBoolean("ignore-nijiperms-bridges", true);
|
||||
|
||||
if (keys.contains("dinner-perms")) {
|
||||
permsConfig.removeProperty("dinner-perms");
|
||||
config.removeProperty("dinner-perms");
|
||||
isUpdated = true;
|
||||
}
|
||||
if (!keys.contains("permissions")) {
|
||||
ConfigurationPermissionsResolver.generateDefaultPerms(permsConfig);
|
||||
ConfigurationPermissionsResolver.generateDefaultPerms(config);
|
||||
isUpdated = true;
|
||||
}
|
||||
if (isUpdated) {
|
||||
logger.info("WEPIF: Updated config file");
|
||||
permsConfig.save();
|
||||
config.save();
|
||||
}
|
||||
return isUpdated;
|
||||
}
|
||||
@ -221,4 +184,8 @@ public class PermissionsResolverManager implements PermissionsResolver {
|
||||
private static final long serialVersionUID = 7044832912491608706L;
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "Using WEPIF for permissions";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -52,9 +52,7 @@ public class PermissionsResolverServerListener extends ServerListener {
|
||||
String name = plugin.getDescription().getName();
|
||||
if (plugin instanceof PermissionsProvider) {
|
||||
manager.setPluginPermissionsResolver(plugin);
|
||||
} else if ((name.equalsIgnoreCase("Permissions") &&
|
||||
NijiPermissionsResolver.checkRealNijiPerms(manager.ignoreNijiPermsBridges)) ||
|
||||
name.equalsIgnoreCase("PermissionsEx")) {
|
||||
} else if ("Permissions".equals(name) || "PermissionsEx".equals(name)) {
|
||||
manager.findResolver();
|
||||
manager.load();
|
||||
}
|
||||
@ -70,10 +68,7 @@ public class PermissionsResolverServerListener extends ServerListener {
|
||||
Plugin plugin = event.getPlugin();
|
||||
String name = plugin.getDescription().getName();
|
||||
|
||||
if (plugin instanceof PermissionsProvider
|
||||
|| (name.equalsIgnoreCase("Permissions") &&
|
||||
NijiPermissionsResolver.checkRealNijiPerms(manager.ignoreNijiPermsBridges)) ||
|
||||
name.equalsIgnoreCase("PermissionsEx")) {
|
||||
if (plugin instanceof PermissionsProvider || "Permissions".equals(name) || "PermissionsEx".equals(name)) {
|
||||
manager.findResolver();
|
||||
manager.load();
|
||||
}
|
||||
@ -86,4 +81,6 @@ public class PermissionsResolverServerListener extends ServerListener {
|
||||
this, Priority.Normal, plugin);
|
||||
manager.setServerListener(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -19,12 +19,37 @@
|
||||
|
||||
package com.sk89q.bukkit.migration;
|
||||
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.util.config.Configuration;
|
||||
|
||||
public class PluginPermissionsResolver implements PermissionsResolver {
|
||||
|
||||
protected PermissionsProvider resolver;
|
||||
protected Plugin plugin;
|
||||
|
||||
public PluginPermissionsResolver(PermissionsProvider resolver) {
|
||||
public static PermissionsResolver factory(Server server, Configuration config){
|
||||
// Looking for service
|
||||
RegisteredServiceProvider<PermissionsProvider> serviceProvider = server.getServicesManager().getRegistration(PermissionsProvider.class);
|
||||
|
||||
if(serviceProvider != null){
|
||||
return new PluginPermissionsResolver(serviceProvider.getProvider(), serviceProvider.getPlugin());
|
||||
}
|
||||
|
||||
// Looking for plugin
|
||||
for (Plugin plugin : server.getPluginManager().getPlugins()) {
|
||||
if(plugin instanceof PermissionsProvider){
|
||||
return new PluginPermissionsResolver((PermissionsProvider)plugin, plugin);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public PluginPermissionsResolver(PermissionsProvider resolver, Plugin permissionsPlugin) {
|
||||
this.resolver = resolver;
|
||||
this.plugin = permissionsPlugin;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
@ -46,4 +71,8 @@ public class PluginPermissionsResolver implements PermissionsResolver {
|
||||
return resolver.getGroups(player);
|
||||
}
|
||||
|
||||
public String getDetectionMessage() {
|
||||
return "Using plugin '" + this.plugin.getDescription().getName() + "' for permissions.";
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user