Allow us to add permissions safely via the Permissions object.

This commit is contained in:
Eric Stokes 2011-08-20 22:50:06 -06:00
parent c503849107
commit f11f3dd88e

View File

@ -6,6 +6,8 @@ import java.util.logging.Level;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
@ -176,4 +178,71 @@ public class MVPermissions implements PermissionsInterface {
return true;
}
public void addPermission(String string, PermissionDefault defaultValue) {
if (this.plugin.getServer().getPluginManager().getPermission(string) == null) {
Permission permission = new Permission(string, defaultValue);
this.plugin.getServer().getPluginManager().addPermission(permission);
this.addToParentPerms(string);
}
}
private void addToParentPerms(String permString) {
String permStringChopped = permString.replace(".*", "");
String[] seperated = permStringChopped.split("\\.");
String parentPermString = getParentPerm(seperated);
if (parentPermString == null) {
addToRootPermission("*", permStringChopped);
addToRootPermission("*.*", permStringChopped);
return;
}
Permission parentPermission = this.plugin.getServer().getPluginManager().getPermission(parentPermString);
// Creat parent and grandparents
if (parentPermission == null) {
parentPermission = new Permission(parentPermString);
this.plugin.getServer().getPluginManager().addPermission(parentPermission);
this.addToParentPerms(parentPermString);
}
// Create actual perm.
Permission actualPermission = this.plugin.getServer().getPluginManager().getPermission(permString);
// Extra check just to make sure the actual one is added
if (actualPermission == null) {
actualPermission = new Permission(permString);
this.plugin.getServer().getPluginManager().addPermission(actualPermission);
}
if (!parentPermission.getChildren().containsKey(permString)) {
parentPermission.getChildren().put(actualPermission.getName(), true);
this.plugin.getServer().getPluginManager().recalculatePermissionDefaults(parentPermission);
}
}
private void addToRootPermission(String rootPerm, String permStringChopped) {
Permission rootPermission = this.plugin.getServer().getPluginManager().getPermission(rootPerm);
if (rootPermission == null) {
rootPermission = new Permission(rootPerm);
this.plugin.getServer().getPluginManager().addPermission(rootPermission);
}
rootPermission.getChildren().put(permStringChopped + ".*", true);
this.plugin.getServer().getPluginManager().recalculatePermissionDefaults(rootPermission);
}
/**
* If the given permission was 'multiverse.core.tp.self', this would return 'multiverse.core.tp.*'.
*
* @param seperated
* @return
*/
private String getParentPerm(String[] seperated) {
if (seperated.length == 1) {
return null;
}
String returnString = "";
for (int i = 0; i < seperated.length - 1; i++) {
returnString += seperated[i] + ".";
}
return returnString + "*";
}
}