61 lines
2.1 KiB
Java
Raw Normal View History

package com.intellectualcrafters.plot.util;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.plotsquared.general.commands.CommandCaller;
2015-09-11 20:09:22 +10:00
public class Permissions
{
public static boolean hasPermission(final PlotPlayer player, final C c)
{
return hasPermission(player, c.s());
2015-07-14 03:42:02 +10:00
}
2015-09-11 20:09:22 +10:00
public static boolean hasPermission(final PlotPlayer player, final String perm)
{
return hasPermission((CommandCaller) player, perm);
}
2015-09-11 20:09:22 +10:00
public static boolean hasPermission(final CommandCaller player, String perm)
{
if ((player == null) || player.hasPermission(C.PERMISSION_ADMIN.s()) || player.hasPermission(C.PERMISSION_STAR.s())) { return true; }
if (player.hasPermission(perm)) { return true; }
perm = perm.toLowerCase().replaceAll("^[^a-z|0-9|\\.|_|-]", "");
2015-09-11 20:09:22 +10:00
final String[] nodes = perm.split("\\.");
final StringBuilder n = new StringBuilder();
2015-09-11 20:09:22 +10:00
for (int i = 0; i < (nodes.length - 1); i++)
{
n.append(nodes[i] + ("."));
2015-09-11 20:09:22 +10:00
if (!perm.equals(n + C.PERMISSION_STAR.s()))
{
if (player.hasPermission(n + C.PERMISSION_STAR.s())) { return true; }
}
}
return false;
}
2015-09-11 20:09:22 +10:00
public static boolean hasPermission(final PlotPlayer player, final String perm, final boolean notify)
{
if (!hasPermission(player, perm))
{
if (notify)
{
2015-08-11 23:42:46 +10:00
MainUtil.sendMessage(player, C.NO_PERMISSION_EVENT, perm);
}
2015-08-11 23:42:46 +10:00
return false;
}
2015-08-11 23:42:46 +10:00
return true;
}
2015-09-11 20:09:22 +10:00
public static int hasPermissionRange(final PlotPlayer player, final String stub, final int range)
{
if ((player == null) || player.hasPermission(C.PERMISSION_ADMIN.s()) || player.hasPermission(C.PERMISSION_STAR.s())) { return Integer.MAX_VALUE; }
if (player.hasPermission(stub + ".*")) { return Integer.MAX_VALUE; }
for (int i = range; i > 0; i--)
{
if (player.hasPermission(stub + "." + i)) { return i; }
}
return 0;
}
}