diff --git a/pom.xml b/pom.xml index c33a414..d31b217 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,31 @@ + + 4.0.0 @@ -70,9 +97,10 @@ 1.0-SNAPSHOT - me.lucko.luckperms - luckperms-api - 4.3 + net.luckperms + api + 5.0 + provided org.json diff --git a/src/main/java/com/larryTheCoder/ASkyBlock.java b/src/main/java/com/larryTheCoder/ASkyBlock.java index b1276ab..d86c654 100644 --- a/src/main/java/com/larryTheCoder/ASkyBlock.java +++ b/src/main/java/com/larryTheCoder/ASkyBlock.java @@ -27,11 +27,12 @@ package com.larryTheCoder; import cn.nukkit.Player; +import cn.nukkit.Server; import cn.nukkit.command.CommandSender; import cn.nukkit.level.Level; import cn.nukkit.level.generator.Generator; +import cn.nukkit.plugin.Plugin; import cn.nukkit.plugin.PluginManager; -import cn.nukkit.scheduler.ServerScheduler; import cn.nukkit.utils.Config; import cn.nukkit.utils.ConfigSection; import cn.nukkit.utils.TextFormat; @@ -60,6 +61,8 @@ import com.larryTheCoder.utils.Settings; import com.larryTheCoder.utils.Utils; import com.larryTheCoder.utils.integration.economy.Economy; +import com.larryTheCoder.utils.integration.luckperms.InternalPermission; +import com.larryTheCoder.utils.integration.luckperms.LuckPermsPermission; import lombok.Getter; import org.sql2o.Query; import org.sql2o.data.Table; @@ -212,14 +215,22 @@ private void initIslands() { messages = new Messages(this); messages.loadMessages(); levelCalcThread = new LevelCalcTask(this); + loadPermissionNodes(); //TopTen.topTenLoad(); pm.registerEvents(chatHandler, this); pm.registerEvents(new IslandListener(this), this); pm.registerEvents(new LavaCheck(this), this); pm.registerEvents(new PlayerEvent(this), this); - ServerScheduler pd = getServer().getScheduler(); - pd.scheduleRepeatingTask(new PluginTask(this), 20); // tick every 1 sec + } + + private void loadPermissionNodes() { + Plugin plugin = Server.getInstance().getPluginManager().getPlugin("LuckPerms"); + if (plugin == null) { + permissionHandler = new InternalPermission(); + } else { + permissionHandler = new LuckPermsPermission(); + } } /** diff --git a/src/main/java/com/larryTheCoder/ASkyBlockAPI.java b/src/main/java/com/larryTheCoder/ASkyBlockAPI.java index f9c6c71..6efd289 100644 --- a/src/main/java/com/larryTheCoder/ASkyBlockAPI.java +++ b/src/main/java/com/larryTheCoder/ASkyBlockAPI.java @@ -38,6 +38,7 @@ import com.larryTheCoder.listener.invitation.InvitationHandler; import com.larryTheCoder.schematic.SchematicHandler; import com.larryTheCoder.task.LevelCalcTask; +import com.larryTheCoder.utils.integration.luckperms.Permission; import lombok.Getter; public class ASkyBlockAPI extends PluginBase { @@ -66,4 +67,6 @@ public class ASkyBlockAPI extends PluginBase { protected SchematicHandler schematics; @Getter protected LevelCalcTask levelCalcThread; + @Getter + protected Permission permissionHandler; } diff --git a/src/main/java/com/larryTheCoder/cache/CoopData.java b/src/main/java/com/larryTheCoder/cache/CoopData.java index 759bf91..dabab34 100644 --- a/src/main/java/com/larryTheCoder/cache/CoopData.java +++ b/src/main/java/com/larryTheCoder/cache/CoopData.java @@ -154,8 +154,4 @@ public int hashCode() { return i + super.hashCode(); } - - public boolean isAdmin(Player p) { - return admins.stream().anyMatch(i -> i.equalsIgnoreCase(p.getName())); - } } diff --git a/src/main/java/com/larryTheCoder/cache/FastCache.java b/src/main/java/com/larryTheCoder/cache/FastCache.java index 7b0de2d..66fd5af 100644 --- a/src/main/java/com/larryTheCoder/cache/FastCache.java +++ b/src/main/java/com/larryTheCoder/cache/FastCache.java @@ -388,7 +388,7 @@ public void getIslandData(Position pos, Consumer resultOutput) { getIslandData(id, resultOutput); } - public void getIslandData(int id, Consumer resultOutput){ + public void getIslandData(int id, Consumer resultOutput) { FastCacheData result = dataCache.stream().filter(i -> i.anyIslandUidMatch(id)).findFirst().orElse(null); if (result == null) { plugin.getDatabase().pushQuery(new DatabaseManager.DatabaseImpl() { diff --git a/src/main/java/com/larryTheCoder/command/Commands.java b/src/main/java/com/larryTheCoder/command/Commands.java index 75f505d..6005d0e 100644 --- a/src/main/java/com/larryTheCoder/command/Commands.java +++ b/src/main/java/com/larryTheCoder/command/Commands.java @@ -71,7 +71,7 @@ private void registerCategories() { @Override public boolean execute(CommandSender sender, String label, String[] args) { Player p = sender.isPlayer() ? getPlugin().getServer().getPlayer(sender.getName()) : null; - if (!sender.hasPermission("is.command")) { + if (!getPlugin().getPermissionHandler().hasPermission(sender, "is.command")) { sender.sendMessage(getLocale(p).errorNoPermission); return true; } diff --git a/src/main/java/com/larryTheCoder/command/category/ChatCategory.java b/src/main/java/com/larryTheCoder/command/category/ChatCategory.java index 0f69c03..0882af2 100644 --- a/src/main/java/com/larryTheCoder/command/category/ChatCategory.java +++ b/src/main/java/com/larryTheCoder/command/category/ChatCategory.java @@ -60,9 +60,9 @@ public List getCommands() { public boolean canUse(CommandSender sender, String command) { switch (command.toLowerCase()) { case "chat": - return sender.hasPermission("is.command.teamChat") && sender.isPlayer(); + return hasPermission(sender, "is.command.teamChat") && sender.isPlayer(); case "messages": - return sender.hasPermission("is.command.messages") && sender.isPlayer(); + return hasPermission(sender, "is.command.messages") && sender.isPlayer(); default: return false; } diff --git a/src/main/java/com/larryTheCoder/command/category/CoopCategory.java b/src/main/java/com/larryTheCoder/command/category/CoopCategory.java index 6676168..82bc250 100644 --- a/src/main/java/com/larryTheCoder/command/category/CoopCategory.java +++ b/src/main/java/com/larryTheCoder/command/category/CoopCategory.java @@ -56,16 +56,16 @@ public List getCommands() { public boolean canUse(CommandSender sender, String command) { switch (command.toLowerCase()) { case "accept": - return sender.hasPermission("is.command.accept") && sender.isPlayer(); + return hasPermission(sender, "is.command.accept") && sender.isPlayer(); case "deny": case "reject": - return sender.hasPermission("is.command.reject") && sender.isPlayer(); + return hasPermission(sender, "is.command.reject") && sender.isPlayer(); case "invite": - return sender.hasPermission("is.command.invite") && sender.isPlayer(); + return hasPermission(sender, "is.command.invite") && sender.isPlayer(); case "kickmember": - return sender.hasPermission("is.command.kick") && sender.isPlayer(); + return hasPermission(sender, "is.command.kick") && sender.isPlayer(); case "quit": - return sender.hasPermission("is.command.quit") && sender.isPlayer(); + return hasPermission(sender, "is.command.quit") && sender.isPlayer(); } return false; } diff --git a/src/main/java/com/larryTheCoder/command/category/GenericCategory.java b/src/main/java/com/larryTheCoder/command/category/GenericCategory.java index 5f90123..81c2e67 100644 --- a/src/main/java/com/larryTheCoder/command/category/GenericCategory.java +++ b/src/main/java/com/larryTheCoder/command/category/GenericCategory.java @@ -61,23 +61,23 @@ public boolean canUse(CommandSender sender, String command) { return sender.isOp(); case "expel": case "kick": - return sender.hasPermission("is.command.expel") && sender.isPlayer(); + return hasPermission(sender, "is.command.expel") && sender.isPlayer(); case "lobby": case "spawn": case "leave": - return sender.hasPermission("is.command.leave") && sender.isPlayer(); + return hasPermission(sender, "is.command.leave") && sender.isPlayer(); case "locale": - return sender.hasPermission("is.command.lang") && sender.isPlayer(); + return hasPermission(sender, "is.command.lang") && sender.isPlayer(); case "protection": - return sender.hasPermission("is.panel.protection") && sender.isPlayer(); + return hasPermission(sender, "is.panel.protection") && sender.isPlayer(); case "settings": - return sender.hasPermission("is.panel.setting") && sender.isPlayer(); + return hasPermission(sender, "is.panel.setting") && sender.isPlayer(); case "top": - return sender.hasPermission("is.topten"); + return hasPermission(sender, "is.topten"); case "about": return true; case "download": - return sender.hasPermission("is.command.download"); + return hasPermission(sender, "is.command.download"); default: return false; } diff --git a/src/main/java/com/larryTheCoder/command/category/IslandCategory.java b/src/main/java/com/larryTheCoder/command/category/IslandCategory.java index 73f0f0f..529de69 100644 --- a/src/main/java/com/larryTheCoder/command/category/IslandCategory.java +++ b/src/main/java/com/larryTheCoder/command/category/IslandCategory.java @@ -50,15 +50,15 @@ public List getCommands() { public boolean canUse(CommandSender sender, String command) { switch (command) { case "create": - return sender.hasPermission("is.create") && sender.isPlayer(); + return hasPermission(sender, "is.create") && sender.isPlayer(); case "reset": case "delete": - return sender.hasPermission("is.command.reset") && sender.isPlayer(); + return hasPermission(sender, "is.command.reset") && sender.isPlayer(); case "home": case "sethome": - return sender.hasPermission("is.command.home") && sender.isPlayer(); + return hasPermission(sender, "is.command.home") && sender.isPlayer(); case "teleport": - return sender.hasPermission("is.command.teleport") && sender.isPlayer(); + return hasPermission(sender, "is.command.teleport") && sender.isPlayer(); } return false; diff --git a/src/main/java/com/larryTheCoder/command/category/OperatorCategory.java b/src/main/java/com/larryTheCoder/command/category/OperatorCategory.java index 076294b..2d83860 100644 --- a/src/main/java/com/larryTheCoder/command/category/OperatorCategory.java +++ b/src/main/java/com/larryTheCoder/command/category/OperatorCategory.java @@ -66,7 +66,7 @@ public List getCommands() { @Override public boolean canUse(CommandSender sender, String command) { - return sender.hasPermission("is.admin.command"); + return hasPermission(sender, "is.admin.command"); } @Override @@ -89,7 +89,7 @@ public void execute(CommandSender sender, String commandLabel, String[] args) { switch (args[0]) { case "generate": - if (!sender.hasPermission("is.admin.generate")) { + if (!hasPermission(sender, "is.admin.generate")) { sender.sendMessage(getPlugin().getLocale(pl).errorNoPermission); break; } @@ -133,7 +133,7 @@ public void execute(CommandSender sender, String commandLabel, String[] args) { sender.sendMessage(getPlugin().getPrefix() + getPlugin().getLocale(pl).errorLevelGenerated); break; case "clear": - if (!sender.hasPermission("is.admin.clear")) { + if (!hasPermission(sender, "is.admin.clear")) { sender.sendMessage(getPlugin().getLocale(pl).errorNoPermission); break; } @@ -148,14 +148,14 @@ public void execute(CommandSender sender, String commandLabel, String[] args) { break; } - if (!sender.hasPermission("is.admin.kick")) { + if (!hasPermission(sender, "is.admin.kick")) { sender.sendMessage(getPlugin().getLocale(pl).errorNoPermission); break; } getPlugin().getIslandManager().kickPlayerByAdmin(sender, args[1]); break; case "rename": - if (!sender.hasPermission("is.admin.rename")) { + if (!hasPermission(sender, "is.admin.rename")) { sender.sendMessage(getPlugin().getLocale(pl).errorNoPermission); break; } @@ -177,7 +177,7 @@ public void execute(CommandSender sender, String commandLabel, String[] args) { }); break; case "cobblestats": - if (!sender.hasPermission("is.admin.cobblestats")) { + if (!hasPermission(sender, "is.admin.cobblestats")) { sender.sendMessage(getPlugin().getLocale(pl).errorNoPermission); break; } @@ -206,7 +206,7 @@ public void execute(CommandSender sender, String commandLabel, String[] args) { } break; case "delete": - if (!sender.hasPermission("is.admin.delete")) { + if (!hasPermission(sender, "is.admin.delete")) { sender.sendMessage(getPlugin().getLocale(pl).errorNoPermission); break; } diff --git a/src/main/java/com/larryTheCoder/command/category/SubCategory.java b/src/main/java/com/larryTheCoder/command/category/SubCategory.java index aa6f49b..7d8cf36 100644 --- a/src/main/java/com/larryTheCoder/command/category/SubCategory.java +++ b/src/main/java/com/larryTheCoder/command/category/SubCategory.java @@ -110,4 +110,16 @@ public List baseCommands() { * @return The parameters required for the command. */ public abstract String getParameters(String commandName); + + /** + * Check either the sender has the permission to execute this command + * using the libraries present in the server. + * + * @param sender The command sender that executes this command. + * @param permission The permission node name. + * @return {@code true} if the player has the permission. + */ + public boolean hasPermission(CommandSender sender, String permission) { + return ASkyBlock.get().getPermissionHandler().hasPermission(sender, permission); + } } diff --git a/src/main/java/com/larryTheCoder/events/IslandCreateEvent.java b/src/main/java/com/larryTheCoder/events/IslandCreateEvent.java index a34f1ba..8c6ee3f 100644 --- a/src/main/java/com/larryTheCoder/events/IslandCreateEvent.java +++ b/src/main/java/com/larryTheCoder/events/IslandCreateEvent.java @@ -27,13 +27,8 @@ package com.larryTheCoder.events; import cn.nukkit.Player; -import cn.nukkit.Server; import cn.nukkit.event.Cancellable; -import cn.nukkit.event.Event; import cn.nukkit.event.HandlerList; -import cn.nukkit.level.Location; -import cn.nukkit.math.Vector2; -import com.larryTheCoder.ASkyBlock; import com.larryTheCoder.cache.IslandData; import lombok.Getter; @@ -52,9 +47,9 @@ public class IslandCreateEvent extends SkyBlockEvent implements Cancellable { private final int schematicId; /** - * @param player The player class who is involved in this event + * @param player The player class who is involved in this event * @param schematicId The schematic id of the island. - * @param island The data of the island. + * @param island The data of the island. */ public IslandCreateEvent(Player player, int schematicId, IslandData island) { super(player, island); diff --git a/src/main/java/com/larryTheCoder/island/TeleportLogic.java b/src/main/java/com/larryTheCoder/island/TeleportLogic.java index 395219e..63d20d6 100644 --- a/src/main/java/com/larryTheCoder/island/TeleportLogic.java +++ b/src/main/java/com/larryTheCoder/island/TeleportLogic.java @@ -79,7 +79,7 @@ public void safeTeleport(final Player player, final Location homeSweetHome, bool final Location targetLoc = homeSweetHome.clone().add(0.5, 0, 0.5); Utils.loadChunkAt(targetLoc); - if (player.hasPermission("is.bypass.wait") || (teleportDelay == 0) || force) { + if (plugin.getPermissionHandler().hasPermission(player, "is.bypass.wait") || (teleportDelay == 0) || force) { player.teleport(targetLoc); } else { player.sendMessage(plugin.getPrefix() + plugin.getLocale(player).teleportDelay.replace("{0}", "" + teleportDelay)); diff --git a/src/main/java/com/larryTheCoder/listener/IslandListener.java b/src/main/java/com/larryTheCoder/listener/IslandListener.java index 7015803..dba10e0 100644 --- a/src/main/java/com/larryTheCoder/listener/IslandListener.java +++ b/src/main/java/com/larryTheCoder/listener/IslandListener.java @@ -29,7 +29,6 @@ import cn.nukkit.Player; import cn.nukkit.block.BlockLava; import cn.nukkit.entity.item.EntityPrimedTNT; -import cn.nukkit.entity.item.EntityVehicle; import cn.nukkit.event.EventHandler; import cn.nukkit.event.EventPriority; import cn.nukkit.event.Listener; @@ -104,7 +103,7 @@ private boolean actionAllowed(Player player, Location location, SettingsFlag fla } // This permission bypasses protection - if (player.isOp() || player.hasPermission("is.mod.bypassprotect")) { + if (player.isOp() || hasPermission(player, "is.mod.bypassprotect")) { return true; } @@ -146,7 +145,7 @@ public void onPlayerMove(PlayerMoveEvent e) { if (e.getTo().getFloorX() - e.getFrom().getFloorX() == 0 && e.getTo().getFloorZ() - e.getFrom().getFloorZ() == 0) { return; } - if (e.getPlayer().isOp() && e.getPlayer().hasPermission("is.mod.bypassprotect")) { + if (e.getPlayer().isOp() && hasPermission(e.getPlayer(), "is.mod.bypassprotect")) { return; } @@ -197,7 +196,7 @@ public void onPlayerMove(PlayerMoveEvent e) { } else if (islandTo != null && (islandTo.getPlotOwner() != null)) { // Lock check if (islandTo.isLocked()) { - if (!p.isOp() && !p.hasPermission("is.mod.bypassprotect") && !p.hasPermission("is.mod.bypasslock")) { + if (!p.isOp() && !hasPermission(e.getPlayer(), "is.mod.bypassprotect") && !hasPermission(e.getPlayer(), "is.mod.bypasslock")) { if (p.riding != null) { // Dismount p.riding.mountEntity(p); @@ -262,7 +261,7 @@ public void onPlayerDropItem(PlayerDropItemEvent e) { //deb.debug("Event is not in world"); return; } - if (p.isOp() || p.hasPermission("is.mod.bypassprotect")) { + if (p.isOp() || hasPermission(p, "is.mod.bypassprotect")) { return; } // Too bad that the item is not a vector3 @@ -289,7 +288,7 @@ public void onPlayerInteract(PlayerInteractEvent e) { //deb.debug("Event is not in world"); return; } - if (p.isOp() || p.hasPermission("is.mod.bypassprotect")) { + if (p.isOp() || hasPermission(p, "is.mod.bypassprotect")) { return; } if (plugin.getIslandManager().locationIsOnIsland(p, e.getBlock()) || plugin.getIslandManager().locationIsOnIsland(p, p.getLocation())) { @@ -350,7 +349,7 @@ public void onExplosion(EntityExplodeEvent e) { if (actionAllowed(p, e.getEntity().getLocation(), SettingsFlag.BREAK_BLOCKS)) { return; } - if (p.isOp() && p.hasPermission("is.mod.bypassprotect")) { + if (p.isOp() && hasPermission(p, "is.mod.bypassprotect")) { return; } } @@ -422,9 +421,13 @@ public void onCraft(CraftItemEvent event) { if (notInWorld(player)) { return; } - if (event.getRecipe().getResult().getId() == ENDER_CHEST && !player.hasPermission("is.craft.enderchest")) { + if (event.getRecipe().getResult().getId() == ENDER_CHEST && !hasPermission(player, "is.craft.enderchest")) { player.sendMessage(plugin.getLocale(player).errorNoPermission); event.setCancelled(true); } } + + public boolean hasPermission(Player player, String permission) { + return plugin.getPermissionHandler().hasPermission(player, permission); + } } diff --git a/src/main/java/com/larryTheCoder/listener/PlayerEvent.java b/src/main/java/com/larryTheCoder/listener/PlayerEvent.java index 543799c..a1cae4c 100644 --- a/src/main/java/com/larryTheCoder/listener/PlayerEvent.java +++ b/src/main/java/com/larryTheCoder/listener/PlayerEvent.java @@ -131,7 +131,7 @@ public void onPlayerJoin(PlayerJoinEvent ex) { public void executeQuery(Connection connection) { connection.createQuery(PLAYER_INSERT_MAIN.getQuery()) .addParameter("playerName", p.getName()) - .addParameter("playerUUID", p.getLoginChainData().getXUID()) + .addParameter("playerUUID", p.getUniqueId().toString()) .addParameter("locale", p.getLoginChainData().getLanguageCode()) .addParameter("resetLeft", Settings.reset) .addParameter("banList", "") diff --git a/src/main/java/com/larryTheCoder/task/LevelCalcTask.java b/src/main/java/com/larryTheCoder/task/LevelCalcTask.java index a207c66..17a9f28 100644 --- a/src/main/java/com/larryTheCoder/task/LevelCalcTask.java +++ b/src/main/java/com/larryTheCoder/task/LevelCalcTask.java @@ -33,7 +33,6 @@ import cn.nukkit.command.CommandSender; import cn.nukkit.level.Level; import cn.nukkit.level.format.FullChunk; -import cn.nukkit.permission.PermissionAttachmentInfo; import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; import com.larryTheCoder.ASkyBlock; @@ -136,9 +135,13 @@ private void updateList() { // Get the player multiplier if it available int levelMultiplier = 1; if (targetPlayer != null) { - for (Map.Entry pType : targetPlayer.getEffectivePermissions().entrySet()) { + Map plPermission = plugin.getPermissionHandler().getPermissions(targetPlayer.getUniqueId()); + + for (Map.Entry pType : plPermission.entrySet()) { String type = pType.getKey(); - if (!type.startsWith("is.multiplier.")) { + + // Statement: The player has the multiplier, but the player do not have the permission + if (!type.startsWith("is.multiplier.") || !pType.getValue()) { continue; } diff --git a/src/main/java/com/larryTheCoder/task/UpdatePluginAsync.java b/src/main/java/com/larryTheCoder/task/UpdatePluginAsync.java deleted file mode 100644 index bfe5298..0000000 --- a/src/main/java/com/larryTheCoder/task/UpdatePluginAsync.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Adapted from the Wizardry License - * - * Copyright (c) 2016-2020 larryTheCoder and contributors - * - * Permission is hereby granted to any persons and/or organizations - * using this software to copy, modify, merge, publish, and distribute it. - * Said persons and/or organizations are not allowed to use the software or - * any derivatives of the work for commercial use or any other means to generate - * income, nor are they allowed to claim this software as their own. - * - * The persons and/or organizations are also disallowed from sub-licensing - * and/or trademarking this software without explicit permission from larryTheCoder. - * - * Any persons and/or organizations using this software must disclose their - * source code and have it publicly available, include this license, - * provide sufficient credit to the original authors of the project (IE: larryTheCoder), - * as well as provide a link to the original project. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR - * PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE - * USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.larryTheCoder.task; - -import cn.nukkit.scheduler.AsyncTask; - -public class UpdatePluginAsync extends AsyncTask { - - @Override - public void onRun() { - // TODO: Use jenkins repo? - } -} diff --git a/src/main/java/com/larryTheCoder/updater/Updater.java b/src/main/java/com/larryTheCoder/updater/Updater.java index 0e9a6ab..fe35700 100644 --- a/src/main/java/com/larryTheCoder/updater/Updater.java +++ b/src/main/java/com/larryTheCoder/updater/Updater.java @@ -35,7 +35,6 @@ import com.larryTheCoder.utils.Settings; import com.larryTheCoder.utils.Utils; import lombok.Getter; -import net.lingala.zip4j.ZipFile; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONTokener; diff --git a/src/main/java/com/larryTheCoder/utils/Utils.java b/src/main/java/com/larryTheCoder/utils/Utils.java index 54f9514..ee13746 100644 --- a/src/main/java/com/larryTheCoder/utils/Utils.java +++ b/src/main/java/com/larryTheCoder/utils/Utils.java @@ -137,7 +137,7 @@ public static String hashObject(String hashedObject) { } public static boolean canBypassTimer(Player p, String what, int seconds) { - if (p.hasPermission("is.bypass.wait")) { + if (ASkyBlock.get().getPermissionHandler().hasPermission(p, "is.bypass.wait")) { return true; } String key = what + "." + p.getName(); diff --git a/src/main/java/com/larryTheCoder/PluginTask.java b/src/main/java/com/larryTheCoder/utils/integration/luckperms/InternalPermission.java similarity index 60% rename from src/main/java/com/larryTheCoder/PluginTask.java rename to src/main/java/com/larryTheCoder/utils/integration/luckperms/InternalPermission.java index fbfc4bc..b77bdae 100644 --- a/src/main/java/com/larryTheCoder/PluginTask.java +++ b/src/main/java/com/larryTheCoder/utils/integration/luckperms/InternalPermission.java @@ -24,26 +24,37 @@ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package com.larryTheCoder; -/** - * @author larryTheCoder - */ -class PluginTask extends cn.nukkit.scheduler.PluginTask { +package com.larryTheCoder.utils.integration.luckperms; + +import cn.nukkit.IPlayer; +import cn.nukkit.Server; +import cn.nukkit.command.CommandSender; +import cn.nukkit.permission.PermissionAttachmentInfo; - private int time = 0; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; - PluginTask(ASkyBlock plugin) { - super(plugin); +public class InternalPermission extends Permission { + + @Override + public boolean hasPermission(CommandSender player, String permission) { + return player.hasPermission(permission); } @Override - public void onRun(int currentTick) { - time++; - // Release every 1 Hour - if (time == 3600) { - time = 0; + public Map getPermissions(UUID uuid) { + IPlayer player = Server.getInstance().getOfflinePlayer(uuid); + if (!player.isOnline()) { + return null; + } + + Map parsedData = new HashMap<>(); + for (Map.Entry pType : player.getPlayer().getEffectivePermissions().entrySet()) { + parsedData.put(pType.getKey(), pType.getValue().getValue()); } - } + return parsedData; + } } diff --git a/src/main/java/com/larryTheCoder/utils/integration/luckperms/LuckPermsPermission.java b/src/main/java/com/larryTheCoder/utils/integration/luckperms/LuckPermsPermission.java index e290b0f..dfec63f 100644 --- a/src/main/java/com/larryTheCoder/utils/integration/luckperms/LuckPermsPermission.java +++ b/src/main/java/com/larryTheCoder/utils/integration/luckperms/LuckPermsPermission.java @@ -28,49 +28,77 @@ package com.larryTheCoder.utils.integration.luckperms; import cn.nukkit.Player; -import com.larryTheCoder.ASkyBlock; +import cn.nukkit.command.CommandSender; import com.larryTheCoder.task.TaskManager; import com.larryTheCoder.utils.Utils; -import me.lucko.luckperms.LuckPerms; -import me.lucko.luckperms.api.LuckPermsApi; -import me.lucko.luckperms.api.Node; -import me.lucko.luckperms.api.User; +import net.luckperms.api.LuckPerms; +import net.luckperms.api.LuckPermsProvider; +import net.luckperms.api.cacheddata.CachedPermissionData; +import net.luckperms.api.context.ContextManager; +import net.luckperms.api.model.user.User; +import net.luckperms.api.model.user.UserManager; +import net.luckperms.api.query.QueryOptions; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; /** * Implementation to LuckPermsPermission * aka, PermissionEx */ -public class LuckPermsPermission { +public class LuckPermsPermission extends Permission { - private final ASkyBlock plugin; - private LuckPermsApi pubApi; + private LuckPerms luckPerms; - public LuckPermsPermission(ASkyBlock instance) { - this.plugin = instance; - this.getLuckPerms(); + public LuckPermsPermission() { + getLuckPerms(); } private void getLuckPerms() { try { - pubApi = LuckPerms.getApi(); + luckPerms = LuckPermsProvider.get(); + + Utils.send("&7Successfully integrated with LuckPerms &7plugin."); } catch (IllegalStateException ignored) { TaskManager.runTaskLater(this::getLuckPerms, 60); - return; } - Utils.send("&aSuccessfully integrated with LuckPerms plugin."); } - public boolean hasPermission(Player p, String permission) { - User user = pubApi.getUser(p.getName()); - for (Node perm : user.getPermissions()) { - if (!perm.getPermission().equalsIgnoreCase(permission) && !perm.getValue()) { - continue; - } - return true; + @Override + public boolean hasPermission(CommandSender sender, String permission) { + if (!(sender instanceof Player)) return true; + + User user = luckPerms.getUserManager().getUser(((Player) sender).getUniqueId()); + if (user == null) { + Utils.sendDebug("The user " + sender.getName() + " were not found in LuckPermsAPI"); + return false; } - // Permission either not found or doesn't applied to - // This user. - return false; + + ContextManager cm = luckPerms.getContextManager(); + + QueryOptions queryOptions = cm.getQueryOptions(user).orElse(cm.getStaticQueryOptions()); + CachedPermissionData permissionData = user.getCachedData().getPermissionData(queryOptions); + + return permissionData.checkPermission(permission).asBoolean(); } + public Map getPermissions(UUID uniqueId) { + UserManager userManager = luckPerms.getUserManager(); + CompletableFuture userFuture = userManager.loadUser(uniqueId); + + User user = userFuture.join(); + if (user == null) { + Utils.sendDebug("The UUID " + uniqueId.toString() + " were not found in LuckPermsAPI"); + return null; + } + + ContextManager cm = luckPerms.getContextManager(); + + QueryOptions queryOptions = cm.getQueryOptions(user).orElse(cm.getStaticQueryOptions()); + CachedPermissionData permissionData = user.getCachedData().getPermissionData(queryOptions); + + return permissionData.getPermissionMap(); + + } } diff --git a/src/main/java/com/larryTheCoder/utils/integration/luckperms/Permission.java b/src/main/java/com/larryTheCoder/utils/integration/luckperms/Permission.java index 322771f..3b2fa34 100644 --- a/src/main/java/com/larryTheCoder/utils/integration/luckperms/Permission.java +++ b/src/main/java/com/larryTheCoder/utils/integration/luckperms/Permission.java @@ -27,5 +27,29 @@ package com.larryTheCoder.utils.integration.luckperms; -public interface Permission { +import cn.nukkit.command.CommandSender; + +import java.util.Map; +import java.util.UUID; + +public abstract class Permission { + + /** + * Check either the sender has the permission for the given string + * + * @param sender The command sender class itself. + * @param permission The permission that need to be checked. + * @return {@code true} if the sender has the permission to execute this. + */ + public abstract boolean hasPermission(CommandSender sender, String permission); + + /** + * Gets all permissions nodes from the UUID given, this may return null if + * the UUID doesn't exists. This function is a blocking-thread operation + * and which should NEVER be called other than an async tasks or threads. + * + * @param uniqueId The player unique id that needs to be checked. + * @return {@code true} the list of permissions that the player had. + */ + public abstract Map getPermissions(UUID uuid); }