Plex-FAWE/src/main/java/com/sk89q/bukkit/migration/DinnerPermsResolver.java

88 lines
2.9 KiB
Java
Raw Normal View History

2011-09-19 06:24:21 +00:00
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2011-08-02 04:02:15 +00:00
package com.sk89q.bukkit.migration;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;
import java.util.ArrayList;
import java.util.List;
public class DinnerPermsResolver implements PermissionsResolver {
private static final String GROUP_PREFIX = "group.";
private final Server server;
public DinnerPermsResolver(Server server) {
this.server = server;
}
2011-08-02 04:02:15 +00:00
public void load() {
// Permissions are already loaded
}
public boolean hasPermission(String name, String permission) {
2011-09-26 05:32:12 +00:00
Player player = server.getPlayerExact(name);
2011-09-24 19:24:10 +00:00
if (player == null) {
2011-08-02 04:02:15 +00:00
return false; // Permissions are only registered for online players
2011-09-24 19:24:10 +00:00
}
2011-09-26 05:32:12 +00:00
if (player.hasPermission("*") || player.hasPermission(permission)) {
2011-08-02 04:02:15 +00:00
return true;
2011-09-24 19:24:10 +00:00
}
int dotPos = permission.lastIndexOf(".");
while (dotPos > -1) {
2011-09-24 19:24:10 +00:00
if (player.hasPermission(permission.substring(0, dotPos + 1) + "*")) {
return true;
2011-09-24 19:24:10 +00:00
}
dotPos = permission.lastIndexOf(".", dotPos - 1);
2011-08-02 04:02:15 +00:00
}
return false;
}
public boolean hasPermission(String worldName, String name, String permission) {
return hasPermission(name, permission); // no per-world ability to check permissions in dinnerperms
}
public boolean inGroup(String name, String group) {
Player player = server.getPlayer(name);
2011-09-24 19:24:10 +00:00
if (player == null) {
2011-08-02 04:02:15 +00:00
return false;
2011-09-24 19:24:10 +00:00
}
2011-08-02 04:02:15 +00:00
return player.hasPermission(GROUP_PREFIX + group);
}
public String[] getGroups(String name) {
Player player = server.getPlayer(name);
2011-09-24 19:24:10 +00:00
if (player == null) {
2011-08-02 04:02:15 +00:00
return new String[0];
2011-09-24 19:24:10 +00:00
}
2011-08-02 04:02:15 +00:00
List<String> groupNames = new ArrayList<String>();
for (PermissionAttachmentInfo permAttach : player.getEffectivePermissions()) {
String perm = permAttach.getPermission();
2011-09-24 19:24:10 +00:00
if (!(perm.startsWith(GROUP_PREFIX) && permAttach.getValue())) {
2011-08-02 04:02:15 +00:00
continue;
2011-09-24 19:24:10 +00:00
}
groupNames.add(perm.substring(GROUP_PREFIX.length(), perm.length()));
2011-08-02 04:02:15 +00:00
}
2011-09-24 19:24:10 +00:00
return groupNames.toArray(new String[groupNames.size()]);
2011-08-02 04:02:15 +00:00
}
}