下面列出了怎么用org.bukkit.permissions.PermissionAttachmentInfo的API类实例代码及写法,或者点击链接到github查看源代码。
public int getPurgeLimit(Player player) {
int limit = RedProtect.get().config.configRoot().purge.canpurge_limit;
List<Integer> limits = new ArrayList<>();
Set<PermissionAttachmentInfo> perms = player.getEffectivePermissions();
if (limit > 0) {
for (PermissionAttachmentInfo perm : perms) {
if (perm.getPermission().startsWith("redprotect.canpurge-limit.")) {
String pStr = perm.getPermission().replaceAll("[^-?0-9]+", "");
if (!pStr.isEmpty()) {
limits.add(Integer.parseInt(pStr));
}
}
}
}
if (limits.size() > 0) {
limit = Collections.max(limits);
}
return limit;
}
private int getBlockLimit(Player player) {
int limit = RedProtect.get().config.configRoot().region_settings.limit_amount;
List<Integer> limits = new ArrayList<>();
Set<PermissionAttachmentInfo> perms = player.getEffectivePermissions();
if (limit > 0) {
if (!hasVaultPerm(player, "redprotect.limits.blocks.unlimited")) {
for (PermissionAttachmentInfo perm : perms) {
if (perm.getPermission().startsWith("redprotect.limits.blocks.")) {
if (RedProtect.get().config.configRoot().region_settings.blocklimit_per_world && !hasVaultPerm(player, perm.getPermission()))
continue;
String pStr = perm.getPermission().replaceAll("[^-?0-9]+", "");
if (!pStr.isEmpty()) {
limits.add(Integer.parseInt(pStr));
}
}
}
} else {
return -1;
}
}
if (limits.size() > 0) {
limit = Collections.max(limits);
}
return limit;
}
private int getClaimLimit(Player player) {
int limit = RedProtect.get().config.configRoot().region_settings.claim.amount_per_player;
List<Integer> limits = new ArrayList<>();
Set<PermissionAttachmentInfo> perms = player.getEffectivePermissions();
if (limit > 0) {
if (!hasVaultPerm(player, "redprotect.limits.claim.unlimited")) {
for (PermissionAttachmentInfo perm : perms) {
if (perm.getPermission().startsWith("redprotect.limits.claim.")) {
if (RedProtect.get().config.configRoot().region_settings.claim.claimlimit_per_world && !hasVaultPerm(player, perm.getPermission()))
continue;
String pStr = perm.getPermission().replaceAll("[^-?0-9]+", "");
if (!pStr.isEmpty()) {
limits.add(Integer.parseInt(pStr));
}
}
}
} else {
return -1;
}
}
if (limits.size() > 0) {
limit = Collections.max(limits);
}
return limit;
}
private List<String> getSuperPerms(Sender s)
{
BukkitSender bs = (BukkitSender) s;
CommandSender sender = bs.getSender();
if (!(sender instanceof Player))
{
return new ArrayList();
}
Player p = (Player) sender;
Permissible base = Injector.getPermissible(p);
if (!(base instanceof BPPermissible))
{
return new ArrayList();
}
BPPermissible perm = (BPPermissible) base;
List<String> l = new ArrayList(perm.getEffectiveSuperPerms().size());
for (PermissionAttachmentInfo e : perm.getEffectiveSuperPerms())
{
l.add((e.getValue() ? "" : "-") + e.getPermission().toLowerCase());
}
return l;
}
@Override
public List<String> getGroups(Player player) {
RegisteredServiceProvider<Permission> serviceProvider = Bukkit.getServer().getServicesManager()
.getRegistration(Permission.class);
groupPrefix = LWC.getInstance().getConfiguration().getString("core.groupPrefix", "group.");
if (serviceProvider == null) {
return super.getGroups(player);
}
Permission perm = serviceProvider.getProvider();
try {
// the player's groups
String[] groups = perm.getPlayerGroups(player);
// fallback to superperms if it appears that they have no groups
if (groups == null || groups.length == 0) {
return super.getGroups(player);
}
List<String> groupss = Arrays.asList(groups);
for (PermissionAttachmentInfo pai : player.getEffectivePermissions()) {
if (pai.getPermission().startsWith(groupPrefix)) {
groupss.add(pai.getPermission().substring(groupPrefix.length()));
}
}
return groupss;
} catch (UnsupportedOperationException e) {
// Can be thrown by vault or asList. Thrown by Vault when using SuperPerms -
// getPlayerGroups() will throw it :-(
return super.getGroups(player);
}
}
@Override
public List<String> getGroups(Player player) {
LWC.getInstance();
List<String> groups = new ArrayList<>();
for (PermissionAttachmentInfo pai : player.getEffectivePermissions()) {
if (pai.getPermission().startsWith(groupPrefix)) {
groups.add(pai.getPermission().substring(groupPrefix.length()));
}
}
return groups;
}
/**
* Search the player's permissions for a permission and return it
* Depending on this is used, this can become O(scary)
*
* @param player
* @param prefix
* @return
*/
public PermissionAttachmentInfo searchPermissions(Player player, String prefix) {
for (PermissionAttachmentInfo attachment : player.getEffectivePermissions()) {
String permission = attachment.getPermission();
// check for the perm node
if (attachment.getValue() && permission.startsWith(prefix)) {
// Bingo!
return attachment;
}
}
return null;
}
/**
* Search permissions for an integer and if found, return it
*
* @param player
* @param prefix
* @return
*/
public int searchPermissionsForInteger(Player player, String prefix) {
PermissionAttachmentInfo attachment = searchPermissions(player, prefix);
// Not found
if (attachment == null) {
return -1;
}
// Found
return Integer.parseInt(attachment.getPermission().substring(prefix.length()));
}
@EventHandler(priority = EventPriority.HIGHEST)
public void join(final PlayerJoinEvent event) {
Player player = event.getPlayer();
resetPlayer(player);
event.getPlayer().addAttachment(lobby, Permissions.OBSERVER, true);
if (player.hasPermission("lobby.overhead-news")) {
final String datacenter = minecraftService.getLocalServer().datacenter();
final Component news = new Component(ChatColor.GREEN)
.extra(new TranslatableComponent(
"lobby.news",
new Component(ChatColor.GOLD, ChatColor.BOLD).extra(generalFormatter.publicHostname())
));
final BossBar bar = bossBarFactory.createBossBar(renderer.render(news, player), BarColor.BLUE, BarStyle.SOLID);
bar.setProgress(1);
bar.addPlayer(player);
bar.show();
}
if(!player.hasPermission("lobby.disabled-permissions-exempt")) {
for(PermissionAttachmentInfo attachment : player.getEffectivePermissions()) {
if(config.getDisabledPermissions().contains(attachment.getPermission())) {
attachment.getAttachment().setPermission(attachment.getPermission(), false);
}
}
}
int count = lobby.getServer().getOnlinePlayers().size();
if(!lobby.getServer().getOnlinePlayers().contains(event.getPlayer())) count++;
minecraftService.updateLocalServer(new SignUpdate(count));
}
@Command(
aliases = {"list"},
desc = "List all permissions",
usage = "[player] [prefix]",
min = 0,
max = 2
)
public void list(CommandContext args, CommandSender sender) throws CommandException {
CommandSender player = CommandUtils.getCommandSenderOrSelf(args, sender, 0);
String prefix = args.getString(1, "");
sender.sendMessage(ChatColor.WHITE + "Permissions for " + player.getName() + ":");
List<PermissionAttachmentInfo> perms = new ArrayList<>(player.getEffectivePermissions());
Collections.sort(perms, new Comparator<PermissionAttachmentInfo>() {
@Override
public int compare(PermissionAttachmentInfo a, PermissionAttachmentInfo b) {
return a.getPermission().compareTo(b.getPermission());
}
});
for(PermissionAttachmentInfo perm : perms) {
if(perm.getPermission().startsWith(prefix)) {
sender.sendMessage((perm.getValue() ? ChatColor.GREEN : ChatColor.RED) +
" " + perm.getPermission() +
(perm.getAttachment() == null ? "" : " (" + perm.getAttachment().getPlugin().getName() + ")"));
}
}
}
@Override
@Nullable
protected String[] get(Event e) {
final Set<String> permissions = new HashSet<>();
for (Player player : players.getArray(e))
for (final PermissionAttachmentInfo permission : player.getEffectivePermissions())
permissions.add(permission.getPermission());
return permissions.toArray(new String[permissions.size()]);
}
@Override
public @NonNull Set<PermissionAttachmentInfo> getEffectivePermissions() {
Map<String, Boolean> permissionMap = this.user.getCachedData().getPermissionData(this.queryOptionsSupplier.getQueryOptions()).getPermissionMap();
ImmutableSet.Builder<PermissionAttachmentInfo> builder = ImmutableSet.builder();
permissionMap.forEach((key, value) -> builder.add(new PermissionAttachmentInfo(this.player, key, null, value)));
return builder.build();
}
/**
* Get the shop limits of a player
* @param p Player, whose shop limits should be returned
* @return The shop limits of the given player
*/
public int getShopLimit(Player p) {
int limit = 0;
boolean useDefault = true;
for (PermissionAttachmentInfo permInfo : p.getEffectivePermissions()) {
if (permInfo.getPermission().startsWith("shopchest.limit.") && p.hasPermission(permInfo.getPermission())) {
if (permInfo.getPermission().equalsIgnoreCase(Permissions.NO_LIMIT)) {
limit = -1;
useDefault = false;
break;
} else {
String[] spl = permInfo.getPermission().split("shopchest.limit.");
if (spl.length > 1) {
try {
int newLimit = Integer.valueOf(spl[1]);
if (newLimit < 0) {
limit = -1;
break;
}
limit = Math.max(limit, newLimit);
useDefault = false;
} catch (NumberFormatException ignored) {
/* Ignore and continue */
}
}
}
}
}
if (limit < -1) limit = -1;
return (useDefault ?Config.defaultLimit : limit);
}
/**
* Add transient permission to a player.
* This operation adds a permission onto the player object in bukkit via Bukkit's permission interface.
*
* @param player Player Object
* @param permission Permission node
* @return Success or Failure
*/
public boolean playerAddTransient(Player player, String permission) {
for (PermissionAttachmentInfo paInfo : player.getEffectivePermissions()) {
if (paInfo.getAttachment() != null && paInfo.getAttachment().getPlugin().equals(plugin)) {
paInfo.getAttachment().setPermission(permission, true);
return true;
}
}
PermissionAttachment attach = player.addAttachment(plugin);
attach.setPermission(permission, true);
return true;
}
/**
* Remove transient permission from a player.
*
* @param player Player Object
* @param permission Permission node
* @return Success or Failure
*/
public boolean playerRemoveTransient(Player player, String permission) {
for (PermissionAttachmentInfo paInfo : player.getEffectivePermissions()) {
if (paInfo.getAttachment() != null && paInfo.getAttachment().getPlugin().equals(plugin)) {
paInfo.getAttachment().unsetPermission(permission);
return true;
}
}
return false;
}
private List<PermissionAttachmentInfo> addChildPerms(List<BPPermission> perms)
{
Map<String, Boolean> map = new LinkedHashMap();
for (BPPermission perm : perms)
{
map.put(perm.getPermission().startsWith("-") ? perm.getPermission().substring(1) : perm.getPermission(), !perm.getPermission().startsWith("-"));
}
return addChildPerms(map);
}
private List<PermissionAttachmentInfo> addChildPerms(Map<String, Boolean> perms)
{
List<PermissionAttachmentInfo> permlist = new LinkedList();
for (Map.Entry<String, Boolean> perm : perms.entrySet())
{
PermissionAttachmentInfo pai = new PermissionAttachmentInfo(oldPermissible, perm.getKey().toLowerCase(), null, perm.getValue());
permlist.add(pai);
Permission permission = Bukkit.getPluginManager().getPermission(pai.getPermission());
if (permission != null && !permission.getChildren().isEmpty())
{
permlist.addAll(addChildPerms(permission.getChildren()));
}
}
return permlist;
}
@Override
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return null;
}
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return perm.getEffectivePermissions();
}
@Override
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return perm.getEffectivePermissions();
}
@Override
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return getCaller().getEffectivePermissions();
}
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return perm.getEffectivePermissions();
}
@Override
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return new HashSet<>(permissions.values());
}
/**
* Find limits that are attached to the player via permissions (e.g
* lwc.protect.*.10 = 10 of any protection)
*
* @param player
* @return
*/
private List<Limit> findLimitsViaPermissions(Player player) {
List<Limit> limits = new LinkedList<>();
for (PermissionAttachmentInfo pai : player.getEffectivePermissions()) {
String permission = pai.getPermission();
boolean value = pai.getValue();
if (!value || !permission.startsWith("lwc.protect.")) {
continue;
}
String[] split = permission.substring("lwc.protect.".length()).split(".");
if (split.length != 2) {
continue;
}
String matchName = split[0];
String strCount = split[1];
int count;
try {
count = Integer.parseInt(strCount);
} catch (NumberFormatException e) {
continue;
}
if (matchName.equals("*")) {
limits.add(new DefaultLimit(count));
} else if (matchName.equals("sign")) {
limits.add(new SignLimit(count));
} else if (!matchName.equals("*") && !matchName.equals("sign")) {
Material material = materialCache.get(matchName);
if (material == null) {
continue;
}
limits.add(new BlockLimit(material, count));
} else if (EntityType.valueOf(matchName.toUpperCase()) != null) {
limits.add(new EntityLimit(EntityType.valueOf(matchName.toUpperCase()), count));
}
}
return limits;
}
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return player.getEffectivePermissions();
}
@Override
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return Collections.emptySet();
}
@Override
public PermissionAttachmentInfo getEffectivePermission(String name) {
return null;
}
@Override
public Collection<PermissionAttachmentInfo> getAttachments() {
return Collections.emptyList();
}
@Override
public Collection<PermissionAttachmentInfo> getAttachments(Plugin plugin) {
return Collections.emptyList();
}
@Override
public Collection<PermissionAttachmentInfo> getAttachments(String name) {
return Collections.emptyList();
}