Minor rewrite of Permissions package, all the stuff is now in one place.

This commit is contained in:
snowleo 2011-08-30 23:26:23 +02:00
parent ecc1a0acea
commit 057c9cb3f5
13 changed files with 349 additions and 145 deletions

View File

@ -30,6 +30,7 @@ import org.bukkit.command.CommandSender;
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.commands.NoChargeException;
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.register.payment.Methods;
import com.earth2me.essentials.signs.SignBlockListener;
import com.earth2me.essentials.signs.SignEntityListener;
@ -65,7 +66,7 @@ public class Essentials extends JavaPlugin implements IEssentials
private transient final Methods paymentMethod = new Methods();
private transient final static boolean enableErrorLogging = false;
private transient final EssentialsErrorHandler errorHandler = new EssentialsErrorHandler();
private transient IPermissionsHandler permissionsHandler;
private transient PermissionsHandler permissionsHandler;
private transient UserMap userMap;
@Override
@ -90,7 +91,7 @@ public class Essentials extends JavaPlugin implements IEssentials
this.initialize(null, server, new PluginDescriptionFile(new FileReader(new File("src" + File.separator + "plugin.yml"))), dataFolder, null, null);
settings = new Settings(this);
userMap = new UserMap(this);
permissionsHandler = new ConfigPermissionsHandler(this);
permissionsHandler = new PermissionsHandler(this, false);
Economy.setEss(this);
}
@ -151,6 +152,7 @@ public class Essentials extends JavaPlugin implements IEssentials
LOGGER.log(Level.INFO, Util.i18n("bukkitFormatChanged"));
}
permissionsHandler = new PermissionsHandler(this, settings.useBukkitPermissions());
final EssentialsPluginListener serverListener = new EssentialsPluginListener(this);
pm.registerEvent(Type.PLUGIN_ENABLE, serverListener, Priority.Low, this);
pm.registerEvent(Type.PLUGIN_DISABLE, serverListener, Priority.Low, this);
@ -665,17 +667,11 @@ public class Essentials extends JavaPlugin implements IEssentials
}
@Override
public IPermissionsHandler getPermissionsHandler()
public PermissionsHandler getPermissionsHandler()
{
return permissionsHandler;
}
@Override
public void setPermissionsHandler(final IPermissionsHandler handler)
{
this.permissionsHandler = handler;
}
@Override
public BanWorkaround getBans()
{

View File

@ -1,19 +1,10 @@
package com.earth2me.essentials;
import com.earth2me.essentials.perm.BPermissionsHandler;
import com.earth2me.essentials.perm.ConfigPermissionsHandler;
import com.earth2me.essentials.perm.Permissions3Handler;
import com.earth2me.essentials.perm.Permissions2Handler;
import com.earth2me.essentials.perm.PermissionsBukkitHandler;
import com.earth2me.essentials.perm.PermissionsExHandler;
import com.earth2me.essentials.perm.SuperpermsHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.event.server.ServerListener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
public class EssentialsPluginListener extends ServerListener implements IConf
@ -29,7 +20,7 @@ public class EssentialsPluginListener extends ServerListener implements IConf
@Override
public void onPluginEnable(final PluginEnableEvent event)
{
checkPermissions();
ess.getPermissionsHandler().checkPermissions();
if (!ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().setMethod(event.getPlugin()))
{
LOGGER.log(Level.INFO, "[Essentials] Payment method found (" + ess.getPaymentMethod().getMethod().getName() + " version: " + ess.getPaymentMethod().getMethod().getVersion() + ")");
@ -38,9 +29,9 @@ public class EssentialsPluginListener extends ServerListener implements IConf
}
@Override
public void onPluginDisable(PluginDisableEvent event)
public void onPluginDisable(final PluginDisableEvent event)
{
checkPermissions();
ess.getPermissionsHandler().checkPermissions();
// Check to see if the plugin thats being disabled is the one we are using
if (ess.getPaymentMethod() != null && ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().checkDisabled(event.getPlugin()))
{
@ -48,84 +39,10 @@ public class EssentialsPluginListener extends ServerListener implements IConf
}
}
private void checkPermissions()
{
final PluginManager pm = ess.getServer().getPluginManager();
final Plugin permissionsExPlugin = pm.getPlugin("PermissionsEx");
if (permissionsExPlugin == null || !permissionsExPlugin.isEnabled())
{
final Plugin permissionsPlugin = pm.getPlugin("Permissions");
if (permissionsPlugin == null || !permissionsPlugin.isEnabled())
{
final Plugin permissionsBukkitPlugin = pm.getPlugin("PermissionsBukkit");
final Plugin bPermissionsPlugin = pm.getPlugin("bPermissions");
if (permissionsBukkitPlugin != null && permissionsBukkitPlugin.isEnabled())
{
if (!(ess.getPermissionsHandler() instanceof PermissionsBukkitHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using PermissionsBukkit based permissions.");
ess.setPermissionsHandler(new PermissionsBukkitHandler(permissionsBukkitPlugin));
}
}
else if (bPermissionsPlugin != null && bPermissionsPlugin.isEnabled())
{
if (!(ess.getPermissionsHandler() instanceof BPermissionsHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using bPermissions based permissions.");
ess.setPermissionsHandler(new BPermissionsHandler());
}
}
else if (ess.getSettings().useBukkitPermissions())
{
if (!(ess.getPermissionsHandler() instanceof SuperpermsHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using superperms based permissions.");
ess.setPermissionsHandler(new SuperpermsHandler());
}
}
else
{
if (!(ess.getPermissionsHandler() instanceof ConfigPermissionsHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using config based permissions. Enable superperms in config.");
ess.setPermissionsHandler(new ConfigPermissionsHandler(ess));
}
}
}
else
{
if (permissionsPlugin.getDescription().getVersion().charAt(0) == '3')
{
if (!(ess.getPermissionsHandler() instanceof Permissions3Handler))
{
LOGGER.log(Level.INFO, "Essentials: Using Permissions 3 based permissions.");
ess.setPermissionsHandler(new Permissions3Handler(permissionsPlugin));
}
}
else
{
if (!(ess.getPermissionsHandler() instanceof Permissions2Handler))
{
LOGGER.log(Level.INFO, "Essentials: Using Permissions 2 based permissions.");
ess.setPermissionsHandler(new Permissions2Handler(permissionsPlugin));
}
}
}
}
else
{
if (!(ess.getPermissionsHandler() instanceof PermissionsExHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using PermissionsEx based permissions.");
ess.setPermissionsHandler(new PermissionsExHandler());
}
}
}
@Override
public void reloadConfig()
{
checkPermissions();
ess.getPermissionsHandler().setUseSuperperms(ess.getSettings().useBukkitPermissions());
ess.getPermissionsHandler().checkPermissions();
}
}

View File

@ -1,6 +1,6 @@
package com.earth2me.essentials;
import com.earth2me.essentials.perm.IPermissionsHandler;
import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.register.payment.Methods;
import org.bukkit.World;
import org.bukkit.command.Command;
@ -57,9 +57,7 @@ public interface IEssentials extends Plugin
TNTExplodeListener getTNTListener();
IPermissionsHandler getPermissionsHandler();
void setPermissionsHandler(IPermissionsHandler handler);
PermissionsHandler getPermissionsHandler();
void showError(final CommandSender sender, final Throwable exception, final String commandLabel);

View File

@ -19,28 +19,29 @@ public class BPermissionsHandler extends SuperpermsHandler
@Override
public String getGroup(final Player base)
{
final PermissionSet pset = wpm.getPermissionSet(base.getWorld());
if (pset == null)
{
return "default";
}
final List<String> groups = pset.getGroups(base);
final List<String> groups = getGroups(base);
if (groups == null || groups.isEmpty())
{
return "default";
return null;
}
return groups.get(0);
}
@Override
public boolean inGroup(final Player base, final String group)
public List<String> getGroups(final Player base)
{
final PermissionSet pset = wpm.getPermissionSet(base.getWorld());
if (pset == null)
{
return false;
return null;
}
final List<String> groups = pset.getGroups(base);
return pset.getGroups(base);
}
@Override
public boolean inGroup(final Player base, final String group)
{
final List<String> groups = getGroups(base);
if (groups == null || groups.isEmpty())
{
return false;

View File

@ -1,22 +1,30 @@
package com.earth2me.essentials.perm;
import com.earth2me.essentials.IEssentials;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class ConfigPermissionsHandler implements IPermissionsHandler
{
private final transient IEssentials ess;
public ConfigPermissionsHandler(final IEssentials ess)
public ConfigPermissionsHandler(final Plugin ess)
{
this.ess = ess;
this.ess = (IEssentials)ess;
}
@Override
public String getGroup(final Player base)
{
return "default";
return null;
}
@Override
public List<String> getGroups(final Player base)
{
return null;
}
@Override
@ -42,12 +50,12 @@ public class ConfigPermissionsHandler implements IPermissionsHandler
@Override
public String getPrefix(final Player base)
{
return "";
return null;
}
@Override
public String getSuffix(final Player base)
{
return "";
return null;
}
}

View File

@ -1,13 +1,15 @@
package com.earth2me.essentials.perm;
import java.util.List;
import org.bukkit.entity.Player;
public interface IPermissionsHandler
{
String getGroup(Player base);
List<String> getGroups(Player base);
boolean canBuild(Player base, String group);
boolean inGroup(Player base, String group);
@ -17,5 +19,4 @@ public interface IPermissionsHandler
String getPrefix(Player base);
String getSuffix(Player base);
}

View File

@ -0,0 +1,51 @@
package com.earth2me.essentials.perm;
import java.util.Collections;
import java.util.List;
import org.bukkit.entity.Player;
public class NullPermissionsHandler implements IPermissionsHandler
{
@Override
public String getGroup(final Player base)
{
return null;
}
@Override
public List<String> getGroups(final Player base)
{
return Collections.emptyList();
}
@Override
public boolean canBuild(final Player base, final String group)
{
return false;
}
@Override
public boolean inGroup(final Player base, final String group)
{
return false;
}
@Override
public boolean hasPermission(final Player base, final String node)
{
return false;
}
@Override
public String getPrefix(final Player base)
{
return null;
}
@Override
public String getSuffix(final Player base)
{
return null;
}
}

View File

@ -2,6 +2,8 @@ package com.earth2me.essentials.perm;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import java.util.Arrays;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -18,8 +20,13 @@ public class Permissions2Handler implements IPermissionsHandler
@Override
public String getGroup(final Player base)
{
final String group = permissionHandler.getGroup(base.getWorld().getName(), base.getName());
return group == null ? "default" : group;
return permissionHandler.getGroup(base.getWorld().getName(), base.getName());
}
@Override
public List<String> getGroups(final Player base)
{
return Arrays.asList(permissionHandler.getGroups(base.getWorld().getName(), base.getName()));
}
@Override
@ -43,14 +50,12 @@ public class Permissions2Handler implements IPermissionsHandler
@Override
public String getPrefix(final Player base)
{
final String prefix = permissionHandler.getGroupPrefix(base.getWorld().getName(), getGroup(base));
return prefix == null ? "" : prefix;
return permissionHandler.getGroupPrefix(base.getWorld().getName(), getGroup(base));
}
@Override
public String getSuffix(final Player base)
{
final String suffix = permissionHandler.getGroupSuffix(base.getWorld().getName(), getGroup(base));
return suffix == null ? "" : suffix;
return permissionHandler.getGroupSuffix(base.getWorld().getName(), getGroup(base));
}
}

View File

@ -2,6 +2,8 @@ package com.earth2me.essentials.perm;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import java.util.Arrays;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -21,6 +23,12 @@ public class Permissions3Handler implements IPermissionsHandler
return permissionHandler.getPrimaryGroup(base.getWorld().getName(), base.getName());
}
@Override
public List<String> getGroups(final Player base)
{
return Arrays.asList(permissionHandler.getGroups(base.getWorld().getName(), base.getName()));
}
@Override
public boolean canBuild(final Player base, final String group)
{
@ -50,5 +58,4 @@ public class Permissions3Handler implements IPermissionsHandler
{
return permissionHandler.getUserSuffix(base.getWorld().getName(), base.getName());
}
}

View File

@ -3,6 +3,8 @@ package com.earth2me.essentials.perm;
import com.platymuus.bukkit.permissions.Group;
import com.platymuus.bukkit.permissions.PermissionInfo;
import com.platymuus.bukkit.permissions.PermissionsPlugin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -20,32 +22,49 @@ public class PermissionsBukkitHandler extends SuperpermsHandler
@Override
public String getGroup(final Player base)
{
final PermissionInfo info = plugin.getPlayerInfo(base.getName());
if (info == null)
{
return "default";
}
final List<Group> groups = info.getGroups();
final List<Group> groups = getPBGroups(base);
if (groups == null || groups.isEmpty())
{
return "default";
return null;
}
return groups.get(0).getName();
}
@Override
public boolean inGroup(final Player base, final String group)
public List<String> getGroups(final Player base)
{
final List<Group> groups = getPBGroups(base);
if (groups.size() == 1)
{
return Collections.singletonList(groups.get(0).getName());
}
final List<String> groupNames = new ArrayList<String>(groups.size());
for (Group group : groups)
{
groupNames.add(group.getName());
}
return groupNames;
}
private List<Group> getPBGroups(final Player base)
{
final PermissionInfo info = plugin.getPlayerInfo(base.getName());
if (info == null)
{
return false;
return Collections.emptyList();
}
final List<Group> groups = info.getGroups();
if (groups == null || groups.isEmpty())
{
return false;
return Collections.emptyList();
}
return groups;
}
@Override
public boolean inGroup(final Player base, final String group)
{
final List<Group> groups = getPBGroups(base);
for (Group group1 : groups)
{
if (group1.getName().equalsIgnoreCase(group))

View File

@ -1,5 +1,7 @@
package com.earth2me.essentials.perm;
import java.util.Arrays;
import java.util.List;
import org.bukkit.entity.Player;
import ru.tehkode.permissions.PermissionManager;
import ru.tehkode.permissions.PermissionUser;
@ -21,11 +23,22 @@ public class PermissionsExHandler implements IPermissionsHandler
final PermissionUser user = manager.getUser(base.getName());
if (user == null)
{
return "default";
return null;
}
return user.getGroupsNames()[0];
}
@Override
public List<String> getGroups(final Player base)
{
final PermissionUser user = manager.getUser(base.getName());
if (user == null)
{
return null;
}
return Arrays.asList(user.getGroupsNames());
}
@Override
public boolean canBuild(final Player base, final String group)
{
@ -62,7 +75,7 @@ public class PermissionsExHandler implements IPermissionsHandler
final PermissionUser user = manager.getUser(base.getName());
if (user == null)
{
return "";
return null;
}
return user.getPrefix();
}
@ -73,7 +86,7 @@ public class PermissionsExHandler implements IPermissionsHandler
final PermissionUser user = manager.getUser(base.getName());
if (user == null)
{
return "";
return null;
}
return user.getSuffix();
}

View File

@ -0,0 +1,179 @@
package com.earth2me.essentials.perm;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
public class PermissionsHandler implements IPermissionsHandler
{
private transient IPermissionsHandler handler = new NullPermissionsHandler();
private transient String defaultGroup = "default";
private final transient Plugin plugin;
private final static Logger LOGGER = Logger.getLogger("Minecraft");
private transient boolean useSuperperms = false;
public PermissionsHandler(final Plugin plugin)
{
this.plugin = plugin;
}
public PermissionsHandler(final Plugin plugin, final boolean useSuperperms)
{
this.plugin = plugin;
this.useSuperperms = useSuperperms;
}
public PermissionsHandler(final Plugin plugin, final String defaultGroup)
{
this.plugin = plugin;
this.defaultGroup = defaultGroup;
}
@Override
public String getGroup(final Player base)
{
String group = handler.getGroup(base);
if (group == null)
{
group = defaultGroup;
}
return group;
}
@Override
public List<String> getGroups(final Player base)
{
List<String> groups = handler.getGroups(base);
if (groups == null || groups.isEmpty())
{
groups = Collections.singletonList(defaultGroup);
}
return Collections.unmodifiableList(groups);
}
@Override
public boolean canBuild(final Player base, final String group)
{
return handler.canBuild(base, group);
}
@Override
public boolean inGroup(final Player base, final String group)
{
return handler.inGroup(base, group);
}
@Override
public boolean hasPermission(final Player base, final String node)
{
return handler.inGroup(base, node);
}
@Override
public String getPrefix(final Player base)
{
String prefix = handler.getPrefix(base);
if (prefix == null)
{
prefix = "";
}
return prefix;
}
@Override
public String getSuffix(final Player base)
{
String suffix = handler.getSuffix(base);
if (suffix == null)
{
suffix = "";
}
return suffix;
}
public void checkPermissions()
{
final PluginManager pluginManager = plugin.getServer().getPluginManager();
final Plugin permExPlugin = pluginManager.getPlugin("PermissionsEx");
if (permExPlugin == null || !permExPlugin.isEnabled())
{
final Plugin permBukkitPlugin = pluginManager.getPlugin("PermissionsBukkit");
final Plugin bPermPlugin = pluginManager.getPlugin("bPermissions");
final Plugin permPlugin = pluginManager.getPlugin("Permissions");
if (permBukkitPlugin != null && permBukkitPlugin.isEnabled())
{
if (!(handler instanceof PermissionsBukkitHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using PermissionsBukkit based permissions.");
handler = new PermissionsBukkitHandler(permBukkitPlugin);
}
}
else if (bPermPlugin != null && bPermPlugin.isEnabled())
{
if (!(handler instanceof BPermissionsHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using bPermissions based permissions.");
handler = new BPermissionsHandler();
}
}
else if (permPlugin == null || !permPlugin.isEnabled())
{
if (useSuperperms)
{
if (!(handler instanceof SuperpermsHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using superperms based permissions.");
handler = new SuperpermsHandler();
}
}
else
{
if (!(handler instanceof ConfigPermissionsHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using config based permissions. Enable superperms in config.");
handler = new ConfigPermissionsHandler(plugin);
}
}
}
else
{
if (permPlugin.getDescription().getVersion().charAt(0) == '3')
{
if (!(handler instanceof Permissions3Handler))
{
LOGGER.log(Level.INFO, "Essentials: Using Permissions 3 based permissions.");
handler = new Permissions3Handler(permPlugin);
}
}
else
{
if (!(handler instanceof Permissions2Handler))
{
LOGGER.log(Level.INFO, "Essentials: Using Permissions 2 based permissions.");
handler = new Permissions2Handler(permPlugin);
}
}
}
}
else
{
if (!(handler instanceof PermissionsExHandler))
{
LOGGER.log(Level.INFO, "Essentials: Using PermissionsEx based permissions.");
handler = new PermissionsExHandler();
}
}
}
public void setUseSuperperms(final boolean useSuperperms)
{
this.useSuperperms = useSuperperms;
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.perm;
import java.util.List;
import org.bukkit.entity.Player;
@ -8,7 +9,13 @@ public class SuperpermsHandler implements IPermissionsHandler
@Override
public String getGroup(final Player base)
{
return "default";
return null;
}
@Override
public List<String> getGroups(final Player base)
{
return null;
}
@Override
@ -31,14 +38,16 @@ public class SuperpermsHandler implements IPermissionsHandler
return false;
}
final String[] parts = node.split("\\.");
final StringBuilder sb = new StringBuilder();
final StringBuilder builder = new StringBuilder(node.length());
for (String part : parts)
{
if (base.hasPermission(sb.toString() + "*"))
builder.append('*');
if (base.hasPermission(builder.toString()))
{
return true;
}
sb.append(part).append(".");
builder.deleteCharAt(builder.length() - 1);
builder.append(part).append('.');
}
return base.hasPermission(node);
}
@ -46,12 +55,12 @@ public class SuperpermsHandler implements IPermissionsHandler
@Override
public String getPrefix(final Player base)
{
return "";
return null;
}
@Override
public String getSuffix(final Player base)
{
return "";
return null;
}
}