Updated GroupUsers support to use official API.

This commit is contained in:
sk89q 2011-01-17 19:45:25 -08:00
parent 65a61c9be7
commit 75c21623da

View File

@ -19,19 +19,16 @@
package com.sk89q.bukkit.migration;
import java.lang.reflect.*;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import com.bukkit.authorblues.GroupUsers.GroupUsers;
import com.bukkit.authorblues.GroupUsers.GroupUsersPlayerListener;
public class GroupUsersPemissionsResolver implements PermissionsResolver {
private Server server;
private GroupUsersPlayerListener listener;
private GroupUsers groupUsers;
public void load() {
@ -47,19 +44,8 @@ public class GroupUsersPemissionsResolver implements PermissionsResolver {
throw new MissingPluginException();
}
GroupUsers groupUsers = (GroupUsers)plugin;
try {
Field field = groupUsers.getClass().getDeclaredField("playerListener");
field.setAccessible(true);
listener = (GroupUsersPlayerListener)field.get(groupUsers);
} catch (SecurityException e) {
throw new PluginAccessException();
} catch (NoSuchFieldException e) {
throw new PluginAccessException();
} catch (IllegalArgumentException e) {
throw new PluginAccessException();
} catch (IllegalAccessException e) {
throw new PluginAccessException();
groupUsers = (GroupUsers)plugin;
} catch (ClassCastException e) {
throw new PluginAccessException();
}
@ -68,25 +54,37 @@ public class GroupUsersPemissionsResolver implements PermissionsResolver {
public boolean hasPermission(String name, String permission) {
try {
Player player = server.getPlayer(name);
if (player == null) {
return false;
}
PlayerChatEvent event = new PlayerChatEvent(Event.Type.PLAYER_CHAT, player, permission);
listener.onPlayerCommand(event);
return !event.isCancelled();
if (player == null) return false;
return groupUsers.playerCanUseCommand(player, permission);
} catch (Throwable t) {
t.printStackTrace();
return false;
}
}
public boolean inGroup(String player, String group) {
public boolean inGroup(String name, String group) {
try {
Player player = server.getPlayer(name);
if (player == null) return false;
return groupUsers.isInGroup(player, group);
} catch (Throwable t) {
t.printStackTrace();
return false;
}
}
public String[] getGroups(String player) {
public String[] getGroups(String name) {
try {
Player player = server.getPlayer(name);
if (player == null) return new String[0];
List<String> groups = groupUsers.getGroups();
if (groups == null) return new String[0];
return groups.toArray(new String[groups.size()]);
} catch (Throwable t) {
t.printStackTrace();
return new String[0];
}
}
public static class PluginAccessException extends Exception {
private static final long serialVersionUID = 7044832912491608706L;