diff --git a/common/build.gradle b/common/build.gradle index a79259e3..704ce740 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -25,6 +25,7 @@ dependencies { compileOnly 'com.github.BlueMap-Minecraft:BlueMapAPI:2.6.0' compileOnly 'us.dynmap:DynmapCoreAPI:3.4' compileOnly 'maven.modrinth:pl3xmap:1.20.4-489' + compileOnly 'net.luckperms:api:5.4' compileOnly 'org.projectlombok:lombok:1.18.36' compileOnly "redis.clients:jedis:${jedis_version}" diff --git a/common/src/main/java/net/william278/huskhomes/config/Settings.java b/common/src/main/java/net/william278/huskhomes/config/Settings.java index 5ec7537d..b2600a5c 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -419,6 +419,33 @@ public final class Settings { } } + @Comment("Plan hook settings. Docs: https://william278.net/docs/huskhomes/plan-hook") + private PlanHookSettings plan = new PlanHookSettings(); + + @Getter + @Configuration + @NoArgsConstructor + public static class PlanHookSettings { + + @Comment("Hook into Player Analytics to provide HuskHomes statistics in your web dashboard.") + private boolean enabled = true; + + } + + + @Comment("LuckPerms hook settings. Docs: https://william278.net/docs/huskhomes/luckperms-hook") + private LuckPermsHookSettings luckperms = new LuckPermsHookSettings(); + + @Getter + @Configuration + @NoArgsConstructor + public static class LuckPermsHookSettings { + + @Comment("Hook into LuckPerms for more accurate numerical permission calculations") + private boolean enabled = true; + + } + @Comment("Web map hook settings. Docs: https://william278.net/docs/huskhomes/map-hooks") private MapHookSettings mapHook = new MapHookSettings(); diff --git a/common/src/main/java/net/william278/huskhomes/hook/HookProvider.java b/common/src/main/java/net/william278/huskhomes/hook/HookProvider.java index 1c76db77..dedfc828 100644 --- a/common/src/main/java/net/william278/huskhomes/hook/HookProvider.java +++ b/common/src/main/java/net/william278/huskhomes/hook/HookProvider.java @@ -111,9 +111,12 @@ public interface HookProvider extends MapHookProvider { final Settings settings = getPlugin().getSettings(); // Common hooks - if (isDependencyAvailable("Plan")) { + if (isDependencyAvailable("Plan") && settings.getPlan().isEnabled()) { hooks.add(new PlanHook(getPlugin())); } + if (isDependencyAvailable("LuckPerms") && settings.getLuckperms().isEnabled()) { + hooks.add(new LuckPermsHook(getPlugin())); + } // Map hooks if (settings.getMapHook().isEnabled()) { diff --git a/common/src/main/java/net/william278/huskhomes/hook/LuckPermsHook.java b/common/src/main/java/net/william278/huskhomes/hook/LuckPermsHook.java new file mode 100644 index 00000000..105eb364 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/hook/LuckPermsHook.java @@ -0,0 +1,78 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.huskhomes.hook; + +import net.luckperms.api.LuckPerms; +import net.luckperms.api.LuckPermsProvider; +import net.luckperms.api.model.user.User; +import net.luckperms.api.node.Node; +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.user.OnlineUser; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.List; + +@PluginHook( + name = "LuckPerms", + register = PluginHook.Register.ON_ENABLE +) +public class LuckPermsHook extends Hook { + + private LuckPerms api; + + public LuckPermsHook(@NotNull HuskHomes plugin) { + super(plugin); + } + + @Override + public void load() { + this.api = LuckPermsProvider.get(); + } + + @Override + public void unload() { + this.api = null; + } + + @NotNull + public List getNumericalPermissions(@NotNull OnlineUser online, @NotNull String nodePrefix) { + final User user = api.getUserManager().getUser(online.getUuid()); + if (user == null) { + return List.of(); + } + return user.getNodes().stream().filter(Node::getValue).filter(n -> n.getKey().startsWith(nodePrefix)) + .filter(perm -> canParse(perm, nodePrefix)) + .map(perm -> Integer.parseInt(perm.getKey().substring(nodePrefix.length()))) + .sorted(Collections.reverseOrder()).toList(); + + } + + // Remove node prefix from the permission and parse as an integer + private static boolean canParse(@NotNull Node permission, @NotNull String nodePrefix) { + try { + Integer.parseInt(permission.getKey().substring(nodePrefix.length())); + return true; + } catch (NumberFormatException e) { + return false; + } + } + +} diff --git a/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java index 3d86cbed..a2cdb859 100644 --- a/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java +++ b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java @@ -28,6 +28,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.title.Title; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.config.Locales; +import net.william278.huskhomes.hook.LuckPermsHook; import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.teleport.Teleportable; @@ -231,7 +232,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs * @param stack whether to stack numerical permissions that grant the user extra max homes * @return the maximum number of homes this user may set */ - public final int getMaxHomes(final int defaultMaxHomes, final boolean stack) { + public final int getMaxHomes(int defaultMaxHomes, boolean stack) { final List homes = getNumericalPermissions("huskhomes.max_homes."); if (homes.isEmpty()) { return defaultMaxHomes; @@ -250,7 +251,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs * @param stack whether to stack numerical permissions that grant the user extra public homes * @return the number of public home slots this user may set */ - public int getMaxPublicHomes(final int defaultPublicHomes, final boolean stack) { + public int getMaxPublicHomes(int defaultPublicHomes, boolean stack) { final List homes = getNumericalPermissions("huskhomes.max_public_homes."); if (homes.isEmpty()) { return defaultPublicHomes; @@ -268,7 +269,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs * @param defaultTeleportWarmup the default teleport warmup time, if no perms are set * @return the largest permission node value for teleport warmup */ - public int getMaxTeleportWarmup(final int defaultTeleportWarmup) { + public int getMaxTeleportWarmup(int defaultTeleportWarmup) { final List homes = getNumericalPermissions("huskhomes.teleport_warmup."); if (homes.isEmpty()) { return defaultTeleportWarmup; @@ -303,20 +304,23 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs */ @NotNull protected List getNumericalPermissions(@NotNull String nodePrefix) { - return getPermissions().entrySet().stream() - .filter(Map.Entry::getValue) - .filter(permission -> permission.getKey().startsWith(nodePrefix)) - .filter(permission -> { - try { - // Remove node prefix from the permission and parse as an integer - Integer.parseInt(permission.getKey().substring(nodePrefix.length())); - } catch (final NumberFormatException e) { - return false; - } - return true; - }) - .map(permission -> Integer.parseInt(permission.getKey().substring(nodePrefix.length()))) - .sorted(Collections.reverseOrder()) - .toList(); + return plugin.getHook(LuckPermsHook.class) + .map(hook -> hook.getNumericalPermissions(this, nodePrefix)) + .orElseGet(() -> getPermissions().entrySet().stream().filter(Map.Entry::getValue) + .filter(perm -> perm.getKey().startsWith(nodePrefix)) + .filter(perm -> canParse(perm.getKey(), nodePrefix)) + .map(p -> Integer.parseInt(p.getKey().substring(nodePrefix.length()))) + .sorted(Collections.reverseOrder()).toList()); } + + // Remove node prefix from the permission and parse as an integer + private static boolean canParse(@NotNull String perm, @NotNull String nodePrefix) { + try { + Integer.parseInt(perm.substring(nodePrefix.length())); + return true; + } catch (NumberFormatException e) { + return false; + } + } + } \ No newline at end of file diff --git a/docs/Config-Files.md b/docs/Config-Files.md index 8540d0dc..3755146b 100644 --- a/docs/Config-Files.md +++ b/docs/Config-Files.md @@ -182,6 +182,14 @@ economy: RANDOM_TELEPORT: 25.0 # Specify how many homes players can set for free, before they need to pay for more slots free_home_slots: 5 +# Plan hook settings. Docs: https://william278.net/docs/huskhomes/plan-hook +plan: + # Hook into Player Analytics to provide HuskHomes statistics in your web dashboard + enabled: true +# LuckPerms hook settings. Docs: https://william278.net/docs/huskhomes/luckperms-hook +luckperms: + # Hook into LuckPerms for more accurate numerical permission calculations + enabled: true # Web map hook settings. Docs: https://william278.net/docs/huskhomes/map-hooks map_hook: # Display public homes/warps on your Dynmap, BlueMap or Pl3xMap diff --git a/docs/LuckPerms-Hook.md b/docs/LuckPerms-Hook.md new file mode 100644 index 00000000..ce1a96b9 --- /dev/null +++ b/docs/LuckPerms-Hook.md @@ -0,0 +1,10 @@ +HuskHomes supports directly hooking into [LuckPerms](https://luckperms.net) for better numerical permission calculation support. + +## Requirements +- HuskHomes v4.9.4+ +- LuckPerms v5.4.1690+ + +## Setup +1. Install LuckPerms on your server(s) alongside HuskHomes +2. Ensure `luckperms.enabled` is set to `true` in your [`config.yml` file](config-files) +3. Restart your servers. HuskHomes will automatically begin querying LuckPerms for numerical permission checks \ No newline at end of file diff --git a/docs/Plan-Hook.md b/docs/Plan-Hook.md index 335a45e5..2c2e1925 100644 --- a/docs/Plan-Hook.md +++ b/docs/Plan-Hook.md @@ -1,4 +1,4 @@ -HuskHomes supports displaying statistics about user sethomes on your [Player Analytics](https://github.com/plan-player-analytics/Plan) (Plan) web panel. +HuskHomes supports displaying statistics about user homes & warps on your [Player Analytics](https://playeranalytics.net) (Plan) web panel. ![Plan hook screenshots](https://raw.githubusercontent.com/WiIIiam278/HuskHomes/master/images/plan-hook.png) @@ -7,6 +7,7 @@ HuskHomes supports displaying statistics about user sethomes on your [Player Ana - Plan v5.4.1690+ ## Setup -1. Install Plan to your Spigot server(s) with HuskHomes installed -2. Configure Plan as necessary and restart your servers -3. Data will start showing up on Player pages on the "Plugins" panel, under "HuskHomes" \ No newline at end of file +1. Install Plan on your server(s) alongside HuskHomes +2. Ensure `plan.enabled` is set to `true` in your [`config.yml` file](config-files) +3. Configure Plan as necessary and restart your servers +4. Data will start showing up on Player pages on the "Plugins" panel, under "HuskHomes" \ No newline at end of file