DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamLuckPerms.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamLuckPerms;
25
26import com.dreamfirestudios.dreamcore.DreamCore;
27import net.luckperms.api.model.group.Group;
28import net.luckperms.api.model.user.User;
29import net.luckperms.api.node.Node;
30import org.bukkit.entity.Player;
31
32import java.util.UUID;
33
66public final class DreamLuckPerms {
67
68 private DreamLuckPerms() { /* utility class */ }
69
80 public record GroupResult<T>(boolean success, T enumValue, String rawValue) {}
81
105 public static <T extends Enum<T>> boolean isPlayerInGroup(Player player, T group) {
106 return player.hasPermission("group." + group);
107 }
108
139 public static <T extends Enum<T>> GroupResult<T> tryGetPlayerGroup(Player player, Class<T> enumClass) {
140 for (T constant : enumClass.getEnumConstants()) {
141 String groupName = constant.name().toLowerCase();
142 if (player.hasPermission("group." + groupName)) {
143 return new GroupResult<>(true, constant, groupName);
144 }
145 }
146 return new GroupResult<>(false, null, null);
147 }
148
162 public static User getUser(Player player) {
163 return DreamCore.LuckPerms.getPlayerAdapter(Player.class).getUser(player);
164 }
165
180 public static User getUser(UUID playerUUID) {
181 return DreamCore.LuckPerms.getUserManager().getUser(playerUUID);
182 }
183
198 public static <T extends Enum<T>> Group getGroup(T groupName) {
199 return DreamCore.LuckPerms.getGroupManager().getGroup(groupName.toString());
200 }
201
223 public static <T extends Enum<T>> void addPermission(User user, T permission) {
224 user.data().add(Node.builder(permission.toString()).build());
225 DreamCore.LuckPerms.getUserManager().saveUser(user);
226 }
227
241 public static <T extends Enum<T>> void addPermission(UUID playerUUID, T permission) {
242 DreamCore.LuckPerms.getUserManager().modifyUser(playerUUID, user -> {
243 user.data().add(Node.builder(permission.toString()).build());
244 });
245 }
246
260 public static void addPermission(User user, Node node) {
261 user.data().add(node);
262 DreamCore.LuckPerms.getUserManager().saveUser(user);
263 }
264
277 public static void addPermission(UUID playerUUID, Node node) {
278 DreamCore.LuckPerms.getUserManager().modifyUser(playerUUID, user -> {
279 user.data().add(node);
280 });
281 }
282
298 public static <T extends Enum<T>> boolean hasPermission(User user, T permission) {
299 return user.getCachedData().getPermissionData().checkPermission(permission.toString()).asBoolean();
300 }
301}
Convenience utilities for interacting with LuckPerms within DreamCore.
static User getUser(UUID playerUUID)
Retrieves the LuckPerms User by UUID if loaded.
static< T extends Enum< T > GroupResult< T > tryGetPlayerGroup(Player player, Class< T > enumClass)
Attempts to determine which enum-based group a player belongs to by scanning group....
record GroupResult< T >(boolean success, T enumValue, String rawValue)
Simple result object for group queries, carrying success flag, parsed enum, and raw string.
static< T extends Enum< T > void addPermission(UUID playerUUID, T permission)
Adds a string-based permission (from enum) to a user referenced by UUID.
static< T extends Enum< T > void addPermission(User user, T permission)
Adds a string-based permission (from enum) to the provided User and saves it.
static< T extends Enum< T > boolean hasPermission(User user, T permission)
Checks whether a User has a given enum-based permission according to cached data.
static< T extends Enum< T > boolean isPlayerInGroup(Player player, T group)
Checks if a player is in a given group by testing the conventional group.<name> permission.
static< T extends Enum< T > Group getGroup(T groupName)
Gets a LuckPerms Group by enum name.
static void addPermission(UUID playerUUID, Node node)
Adds a concrete LuckPerms Node to a user by UUID via modifyUser.
static void addPermission(User user, Node node)
Adds a concrete LuckPerms Node to a User and saves it.
static User getUser(Player player)
Retrieves the LuckPerms User object for an online player.