diff --git a/bukkit/src/main/java/me/leoko/advancedban/bukkit/BukkitMethods.java b/bukkit/src/main/java/me/leoko/advancedban/bukkit/BukkitMethods.java index a0e2053f..aa623098 100644 --- a/bukkit/src/main/java/me/leoko/advancedban/bukkit/BukkitMethods.java +++ b/bukkit/src/main/java/me/leoko/advancedban/bukkit/BukkitMethods.java @@ -8,6 +8,7 @@ import me.leoko.advancedban.manager.DatabaseManager; import me.leoko.advancedban.manager.PunishmentManager; import me.leoko.advancedban.manager.UUIDManager; +import me.leoko.advancedban.utils.Permissionable; import me.leoko.advancedban.utils.Punishment; import me.leoko.advancedban.utils.tabcompletion.TabCompleter; import org.bstats.bukkit.Metrics; @@ -183,16 +184,12 @@ public boolean hasPerms(Object player, String perms) { } @Override - public boolean hasOfflinePerms(String name, String perms) { - if (permissionVault == null) - return false; - + public Permissionable getOfflinePermissionPlayer(String name) { OfflinePlayer player = Bukkit.getOfflinePlayer(name); + if(permissionVault == null || player == null || !player.hasPlayedBefore()) + return permission -> false; - if (player == null || !player.hasPlayedBefore()) - return false; - - return permissionVault.apply(player, perms); + return permission -> permissionVault.apply(player, permission); } @SuppressWarnings("deprecation") diff --git a/bungee/src/main/java/me/leoko/advancedban/bungee/BungeeMethods.java b/bungee/src/main/java/me/leoko/advancedban/bungee/BungeeMethods.java index e7ccd9d3..32dfa621 100644 --- a/bungee/src/main/java/me/leoko/advancedban/bungee/BungeeMethods.java +++ b/bungee/src/main/java/me/leoko/advancedban/bungee/BungeeMethods.java @@ -10,11 +10,11 @@ import me.leoko.advancedban.bungee.event.PunishmentEvent; import me.leoko.advancedban.bungee.event.RevokePunishmentEvent; import me.leoko.advancedban.bungee.listener.CommandReceiverBungee; -import me.leoko.advancedban.bungee.utils.LuckPermsPermissionProvider; -import me.leoko.advancedban.bungee.utils.OfflinePermissionProvider; +import me.leoko.advancedban.bungee.utils.LuckPermsOfflineUser; import me.leoko.advancedban.manager.DatabaseManager; import me.leoko.advancedban.manager.PunishmentManager; import me.leoko.advancedban.manager.UUIDManager; +import me.leoko.advancedban.utils.Permissionable; import me.leoko.advancedban.utils.Punishment; import me.leoko.advancedban.utils.tabcompletion.TabCompleter; import net.md_5.bungee.api.ChatColor; @@ -52,13 +52,14 @@ public class BungeeMethods implements MethodInterface { private Configuration layouts; private Configuration mysql; - private OfflinePermissionProvider offlinePermissionProvider; + private final boolean luckPermsSupport; public BungeeMethods() { if (ProxyServer.getInstance().getPluginManager().getPlugin("LuckPerms") != null) { - offlinePermissionProvider = new LuckPermsPermissionProvider(); + luckPermsSupport = true; log("[AdvancedBan] Offline permission support through LuckPerms active"); } else { + luckPermsSupport = false; log("[AdvancedBan] No offline permission support through LuckPerms"); } } @@ -185,22 +186,10 @@ public boolean hasPerms(Object player, String perms) { } @Override - public void requestOfflinePermissionPlayer(String name) { - if(offlinePermissionProvider != null) { - offlinePermissionProvider.requestOfflinePermissionPlayer(name); - } - } + public Permissionable getOfflinePermissionPlayer(String name) { + if(luckPermsSupport) return new LuckPermsOfflineUser(name); - @Override - public void releaseOfflinePermissionPlayer(String name) { - if(offlinePermissionProvider != null) { - offlinePermissionProvider.releaseOfflinePermissionPlayer(name); - } - } - - @Override - public boolean hasOfflinePerms(String name, String perms) { - return offlinePermissionProvider != null && offlinePermissionProvider.hasOfflinePerms(name, perms); + return permission -> false; } @Override diff --git a/bungee/src/main/java/me/leoko/advancedban/bungee/utils/LuckPermsOfflineUser.java b/bungee/src/main/java/me/leoko/advancedban/bungee/utils/LuckPermsOfflineUser.java new file mode 100644 index 00000000..830974af --- /dev/null +++ b/bungee/src/main/java/me/leoko/advancedban/bungee/utils/LuckPermsOfflineUser.java @@ -0,0 +1,25 @@ +package me.leoko.advancedban.bungee.utils; + +import me.leoko.advancedban.utils.Permissionable; +import net.luckperms.api.LuckPermsProvider; +import net.luckperms.api.model.user.User; +import net.luckperms.api.model.user.UserManager; + +import java.util.UUID; + +public class LuckPermsOfflineUser implements Permissionable { + private User permissionUser; + + public LuckPermsOfflineUser(String name) { + final UserManager userManager = LuckPermsProvider.get().getUserManager(); + final UUID uuid = userManager.lookupUniqueId(name).join(); + if (uuid != null) { + this.permissionUser = userManager.loadUser(uuid).join(); + } + } + + @Override + public boolean hasPermission(String permission) { + return permissionUser != null && permissionUser.getCachedData().getPermissionData().checkPermission(permission).asBoolean(); + } +} diff --git a/bungee/src/main/java/me/leoko/advancedban/bungee/utils/LuckPermsPermissionProvider.java b/bungee/src/main/java/me/leoko/advancedban/bungee/utils/LuckPermsPermissionProvider.java deleted file mode 100644 index 3791be8e..00000000 --- a/bungee/src/main/java/me/leoko/advancedban/bungee/utils/LuckPermsPermissionProvider.java +++ /dev/null @@ -1,42 +0,0 @@ -package me.leoko.advancedban.bungee.utils; - -import net.luckperms.api.LuckPerms; -import net.luckperms.api.LuckPermsProvider; -import net.luckperms.api.model.user.User; -import net.luckperms.api.model.user.UserManager; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -public class LuckPermsPermissionProvider implements OfflinePermissionProvider{ - private LuckPerms luckPerms; - private Map cachedOfflinePerms = new HashMap<>(); - - public LuckPermsPermissionProvider() { - this.luckPerms = LuckPermsProvider.get(); - } - - @Override - public void requestOfflinePermissionPlayer(String name) { - final UserManager userManager = luckPerms.getUserManager(); - final UUID uuid = userManager.lookupUniqueId(name).join(); - if (uuid != null) { - final User user = userManager.loadUser(uuid).join(); - if (user != null) { - cachedOfflinePerms.put(name, user); - } - } - } - - @Override - public void releaseOfflinePermissionPlayer(String name) { - cachedOfflinePerms.remove(name); - } - - @Override - public boolean hasOfflinePerms(String name, String perms) { - final User user = cachedOfflinePerms.get(name); - return user != null && user.getCachedData().getPermissionData().checkPermission(perms).asBoolean(); - } -} diff --git a/bungee/src/main/java/me/leoko/advancedban/bungee/utils/OfflinePermissionProvider.java b/bungee/src/main/java/me/leoko/advancedban/bungee/utils/OfflinePermissionProvider.java deleted file mode 100644 index 0a04c579..00000000 --- a/bungee/src/main/java/me/leoko/advancedban/bungee/utils/OfflinePermissionProvider.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.leoko.advancedban.bungee.utils; - -public interface OfflinePermissionProvider { - void requestOfflinePermissionPlayer(String name); - void releaseOfflinePermissionPlayer(String name); - boolean hasOfflinePerms(String name, String perms); -} diff --git a/core/src/main/java/me/leoko/advancedban/MethodInterface.java b/core/src/main/java/me/leoko/advancedban/MethodInterface.java index 629273ec..5740088e 100644 --- a/core/src/main/java/me/leoko/advancedban/MethodInterface.java +++ b/core/src/main/java/me/leoko/advancedban/MethodInterface.java @@ -1,5 +1,6 @@ package me.leoko.advancedban; +import me.leoko.advancedban.utils.Permissionable; import me.leoko.advancedban.utils.Punishment; import me.leoko.advancedban.utils.tabcompletion.TabCompleter; @@ -151,27 +152,12 @@ public interface MethodInterface { boolean hasPerms(Object player, String perms); /** - * Load necessary permission data for an offline permission check + * Get a Permissionable player that can be checked for offline permissions. * - * @param name the player's name + * @param name the name of the user + * @return the offline permission user */ - default void requestOfflinePermissionPlayer(String name){} - - /** - * Release cached permission data of a player after all requests are done - * - * @param name the player's name - */ - default void releaseOfflinePermissionPlayer(String name){} - - /** - * Check if an offline player has the given permission. - * - * @param name server intern identifier for player - * @param perms the perms - * @return the boolean - */ - boolean hasOfflinePerms(String name, String perms); + Permissionable getOfflinePermissionPlayer(String name); /** * Check whether player is online. diff --git a/core/src/main/java/me/leoko/advancedban/utils/Permissionable.java b/core/src/main/java/me/leoko/advancedban/utils/Permissionable.java new file mode 100644 index 00000000..fd648ec6 --- /dev/null +++ b/core/src/main/java/me/leoko/advancedban/utils/Permissionable.java @@ -0,0 +1,15 @@ +package me.leoko.advancedban.utils; + +/** + * The Permissionable interface defines a way to query whether the implementing object has a certain permission. + */ +@FunctionalInterface +public interface Permissionable { + /** + * Checks whether the implementing object has the given permission + * + * @param permission the permission to check + * @return the result + */ + boolean hasPermission(String permission); +} diff --git a/core/src/main/java/me/leoko/advancedban/utils/commands/PunishmentProcessor.java b/core/src/main/java/me/leoko/advancedban/utils/commands/PunishmentProcessor.java index 0e4c6c59..43aa0be9 100644 --- a/core/src/main/java/me/leoko/advancedban/utils/commands/PunishmentProcessor.java +++ b/core/src/main/java/me/leoko/advancedban/utils/commands/PunishmentProcessor.java @@ -6,6 +6,7 @@ import me.leoko.advancedban.manager.PunishmentManager; import me.leoko.advancedban.manager.TimeManager; import me.leoko.advancedban.utils.Command; +import me.leoko.advancedban.utils.Permissionable; import me.leoko.advancedban.utils.Punishment; import me.leoko.advancedban.utils.PunishmentType; @@ -118,17 +119,17 @@ private static boolean processExempt(String name, String target, Object sender, MethodInterface mi = Universal.get().getMethods(); String dataName = name.toLowerCase(); - boolean onlineExempt = false; + boolean exempt = false; if (mi.isOnline(dataName)) { Object onlineTarget = mi.getPlayer(dataName); - onlineExempt = canNotPunish((perms) -> mi.hasPerms(sender, perms), (perms) -> mi.hasPerms(onlineTarget, perms), type.getName()); + exempt = canNotPunish((perms) -> mi.hasPerms(sender, perms), (perms) -> mi.hasPerms(onlineTarget, perms), type.getName()); + } else { + final Permissionable offlinePermissionPlayer = mi.getOfflinePermissionPlayer(name); + exempt = Universal.get().isExemptPlayer(dataName) || + canNotPunish((perms) -> mi.hasPerms(sender, perms), offlinePermissionPlayer::hasPermission, type.getName()); } - mi.requestOfflinePermissionPlayer(name); - boolean offlineExempt = !onlineExempt && (Universal.get().isExemptPlayer(dataName) || canNotPunish((perms) -> mi.hasPerms(sender, perms), (perms) -> mi.hasOfflinePerms(name, perms), type.getName())); - mi.releaseOfflinePermissionPlayer(name); - - if (onlineExempt || offlineExempt) { + if (exempt) { MessageManager.sendMessage(sender, type.getBasic().getName() + ".Exempt", true, "NAME", name); return true; diff --git a/core/src/test/java/me/leoko/advancedban/TestMethods.java b/core/src/test/java/me/leoko/advancedban/TestMethods.java index 841b8fba..e3923d86 100644 --- a/core/src/test/java/me/leoko/advancedban/TestMethods.java +++ b/core/src/test/java/me/leoko/advancedban/TestMethods.java @@ -1,21 +1,21 @@ package me.leoko.advancedban; +import me.leoko.advancedban.utils.Permissionable; import me.leoko.advancedban.utils.Punishment; import me.leoko.advancedban.utils.tabcompletion.TabCompleter; -import static org.junit.jupiter.api.Assertions.fail; - import java.io.File; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import static org.junit.jupiter.api.Assertions.fail; + /** * Created by Leo on 07.08.2017. */ public class TestMethods implements MethodInterface { - private final File dataFolder; public TestMethods(File dataFolder){ @@ -128,8 +128,8 @@ public boolean hasPerms(Object player, String perms) { } @Override - public boolean hasOfflinePerms(String name, String perms) { - return false; + public Permissionable getOfflinePermissionPlayer(String name) { + return permission -> false; } @Override