feat: add LuckPerms hook, close #542

This commit is contained in:
William278 committed 2025-03-02 15:47:44 +00:00
1 parent 93d67473d9
commit 4b62fb16dd
8 files changed
+155 -23

No files matched your search

@@ -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();
@@ -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()) {
@@ -0,0 +1,78 @@
/*
* This file is part of HuskHomes, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <[email protected]>
* 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<Integer> 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;
}
}
}
@@ -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<Integer> 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<Integer> 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<Integer> 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<Integer> 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;
}
}
}