001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.bukkit.permissions; 020 021import com.plotsquared.bukkit.player.BukkitOfflinePlayer; 022import com.plotsquared.bukkit.player.BukkitPlayer; 023import com.plotsquared.core.permissions.ConsolePermissionProfile; 024import com.plotsquared.core.permissions.PermissionHandler; 025import com.plotsquared.core.permissions.PermissionProfile; 026import com.plotsquared.core.player.ConsolePlayer; 027import com.plotsquared.core.player.OfflinePlotPlayer; 028import com.plotsquared.core.player.PlotPlayer; 029import net.milkbowl.vault.permission.Permission; 030import org.bukkit.Bukkit; 031import org.bukkit.OfflinePlayer; 032import org.bukkit.plugin.RegisteredServiceProvider; 033import org.checkerframework.checker.nullness.qual.NonNull; 034import org.checkerframework.checker.nullness.qual.Nullable; 035 036import java.util.EnumSet; 037import java.util.Optional; 038import java.util.Set; 039 040public class VaultPermissionHandler implements PermissionHandler { 041 042 private Permission permissions; 043 044 @Override 045 public void initialize() { 046 if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) { 047 throw new IllegalStateException("Vault is not present on the server"); 048 } 049 RegisteredServiceProvider<Permission> permissionProvider = 050 Bukkit.getServer().getServicesManager().getRegistration(Permission.class); 051 if (permissionProvider != null) { 052 this.permissions = permissionProvider.getProvider(); 053 } 054 } 055 056 @NonNull 057 @Override 058 public Optional<PermissionProfile> getPermissionProfile( 059 @NonNull PlotPlayer<?> playerPlotPlayer 060 ) { 061 if (playerPlotPlayer instanceof final BukkitPlayer bukkitPlayer) { 062 return Optional.of(new VaultPermissionProfile(bukkitPlayer.getPlatformPlayer())); 063 } else if (playerPlotPlayer instanceof ConsolePlayer) { 064 return Optional.of(ConsolePermissionProfile.INSTANCE); 065 } 066 return Optional.empty(); 067 } 068 069 @NonNull 070 @Override 071 public Optional<PermissionProfile> getPermissionProfile( 072 @NonNull OfflinePlotPlayer offlinePlotPlayer 073 ) { 074 if (offlinePlotPlayer instanceof BukkitOfflinePlayer) { 075 return Optional.of(new VaultPermissionProfile(((BukkitOfflinePlayer) offlinePlotPlayer).player)); 076 } 077 return Optional.empty(); 078 } 079 080 @NonNull 081 @Override 082 public Set<PermissionHandlerCapability> getCapabilities() { 083 return EnumSet.of( 084 PermissionHandlerCapability.PER_WORLD_PERMISSIONS, 085 PermissionHandlerCapability.ONLINE_PERMISSIONS, 086 PermissionHandlerCapability.OFFLINE_PERMISSIONS 087 ); 088 } 089 090 091 private final class VaultPermissionProfile implements PermissionProfile { 092 093 private final OfflinePlayer offlinePlayer; 094 095 private VaultPermissionProfile(final @NonNull OfflinePlayer offlinePlayer) { 096 this.offlinePlayer = offlinePlayer; 097 } 098 099 @Override 100 public boolean hasPermission( 101 final @Nullable String world, 102 final @NonNull String permission 103 ) { 104 if (permissions == null) { 105 return false; 106 } 107 if (world == null && offlinePlayer instanceof BukkitPlayer) { 108 return permissions.playerHas(((BukkitPlayer) offlinePlayer).getPlatformPlayer(), permission); 109 } 110 return permissions.playerHas(world, offlinePlayer, permission); 111 } 112 113 @Override 114 public boolean hasKeyedPermission( 115 final @Nullable String world, 116 final @NonNull String stub, 117 final @NonNull String key 118 ) { 119 if (permissions == null) { 120 return false; 121 } 122 if (world == null && offlinePlayer instanceof BukkitPlayer) { 123 return permissions.playerHas( 124 ((BukkitPlayer) offlinePlayer).getPlatformPlayer(), 125 stub + ".*" 126 ) || permissions.playerHas(((BukkitPlayer) offlinePlayer).getPlatformPlayer(), stub + "." + key); 127 } 128 return permissions.playerHas(world, offlinePlayer, stub + ".*") || permissions.playerHas(world, offlinePlayer, 129 stub + "." + key 130 ); 131 } 132 133 } 134 135}