Skip to content

Commit fa2408f

Browse files
adjust luckperms integration
1 parent da02479 commit fa2408f

2 files changed

Lines changed: 52 additions & 58 deletions

File tree

src/main/java/westeroscraft/LuckPermsIntegration.java

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,48 @@
11
package westeroscraft;
22

3+
import net.fabricmc.loader.api.FabricLoader;
34
import net.luckperms.api.LuckPerms;
45
import net.luckperms.api.LuckPermsProvider;
5-
import net.luckperms.api.model.user.User;
6-
import net.luckperms.api.query.QueryOptions;
6+
import net.luckperms.api.cacheddata.CachedPermissionData;
7+
import net.luckperms.api.platform.PlayerAdapter;
78
import net.minecraft.server.level.ServerPlayer;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
1011

11-
import java.util.UUID;
12-
1312
/**
1413
* Optional integration with LuckPerms for permission checks.
1514
* Gracefully handles the case when LuckPerms is not installed.
15+
* Uses the recommended PlayerAdapter API for online player permission checks.
1616
*/
1717
public class LuckPermsIntegration {
1818
private static final Logger LOGGER = LoggerFactory.getLogger("westeroscraft-essentials");
19-
private static boolean available = false;
20-
private static boolean checked = false;
19+
private static final String LUCKPERMS_MOD_ID = "luckperms";
20+
private static boolean initialized = false;
21+
private static PlayerAdapter<ServerPlayer> playerAdapter = null;
2122

2223
/**
2324
* Check if LuckPerms is available on the server.
25+
* Uses FabricLoader to check if the mod is loaded, then lazily initializes
26+
* the PlayerAdapter on first use.
2427
*/
2528
public static boolean isAvailable() {
26-
if (!checked) {
27-
checked = true;
29+
if (!FabricLoader.getInstance().isModLoaded(LUCKPERMS_MOD_ID)) {
30+
return false;
31+
}
32+
33+
if (!initialized) {
34+
initialized = true;
2835
try {
29-
Class.forName("net.luckperms.api.LuckPermsProvider");
30-
LuckPermsProvider.get();
31-
available = true;
36+
LuckPerms luckPerms = LuckPermsProvider.get();
37+
playerAdapter = luckPerms.getPlayerAdapter(ServerPlayer.class);
3238
LOGGER.info("LuckPerms integration enabled");
33-
} catch (ClassNotFoundException | IllegalStateException e) {
34-
available = false;
35-
LOGGER.info("LuckPerms not found, permission integration disabled");
39+
} catch (IllegalStateException e) {
40+
LOGGER.warn("LuckPerms is loaded but not yet available: {}", e.getMessage());
41+
initialized = false; // Allow retry on next call
42+
return false;
3643
}
3744
}
38-
return available;
39-
}
40-
41-
/**
42-
* Check if a player has a specific permission.
43-
* Returns true if LuckPerms is not available (fail-open).
44-
*
45-
* @param player The server player to check
46-
* @param permission The permission node to check
47-
* @return true if player has permission or LuckPerms is unavailable
48-
*/
49-
public static boolean hasPermission(ServerPlayer player, String permission) {
50-
if (!isAvailable()) {
51-
return true; // Fail-open when LuckPerms not installed
52-
}
53-
return hasPermissionStrict(player.getUUID(), permission);
45+
return playerAdapter != null;
5446
}
5547

5648
/**
@@ -65,32 +57,23 @@ public static boolean hasPermissionStrict(ServerPlayer player, String permission
6557
if (!isAvailable()) {
6658
return false; // Fail-closed when LuckPerms not installed
6759
}
68-
return hasPermissionStrict(player.getUUID(), permission);
60+
return checkPermission(player, permission);
6961
}
7062

7163
/**
72-
* Check if a player (by UUID) has a specific permission.
64+
* Check if a player has a specific permission using the PlayerAdapter API.
65+
* This is the recommended way to check permissions for online players.
7366
*
74-
* @param playerUuid The player's UUID
67+
* @param player The server player to check
7568
* @param permission The permission node to check
7669
* @return true if player has permission, false otherwise
7770
*/
78-
public static boolean hasPermissionStrict(UUID playerUuid, String permission) {
79-
if (!isAvailable()) {
80-
return false;
81-
}
71+
private static boolean checkPermission(ServerPlayer player, String permission) {
8272
try {
83-
LuckPerms luckPerms = LuckPermsProvider.get();
84-
User user = luckPerms.getUserManager().getUser(playerUuid);
85-
if (user == null) {
86-
return false;
87-
}
88-
return user.getCachedData()
89-
.getPermissionData(QueryOptions.nonContextual())
90-
.checkPermission(permission)
91-
.asBoolean();
73+
CachedPermissionData permissionData = playerAdapter.getPermissionData(player);
74+
return permissionData.checkPermission(permission).asBoolean();
9275
} catch (Exception e) {
93-
LOGGER.warn("Error checking permission {} for {}: {}", permission, playerUuid, e.getMessage());
76+
LOGGER.warn("Error checking permission {} for {}: {}", permission, player.getGameProfile().getName(), e.getMessage());
9477
return false;
9578
}
9679
}

src/main/java/westeroscraft/adventure/GameModeEnforcer.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
/**
1212
* Enforces Adventure mode for players with the wcessentials.forceadventuremode permission.
13-
* Players with this permission are blocked from CREATIVE and SURVIVAL modes, but can use SPECTATOR.
13+
* On login: Players are always forced to ADVENTURE mode.
14+
* On dimension change: Only CREATIVE is blocked; ADVENTURE, SURVIVAL, and SPECTATOR are allowed.
1415
* Uses LuckPerms for permission checking (fail-closed - no effect if LuckPerms not installed).
1516
*/
1617
public class GameModeEnforcer {
@@ -28,18 +29,28 @@ public static void enforceGameMode(ServerPlayer player, String context) {
2829
return;
2930
}
3031

32+
if (!LuckPermsIntegration.hasPermissionStrict(player, PERMISSION)) {
33+
return;
34+
}
35+
3136
GameType currentMode = player.gameMode.getGameModeForPlayer();
3237

33-
// Only force to ADVENTURE if player is in CREATIVE or SURVIVAL
34-
// SPECTATOR mode is allowed for players with this permission
35-
if (currentMode == GameType.CREATIVE || currentMode == GameType.SURVIVAL) {
36-
if (LuckPermsIntegration.hasPermissionStrict(player, PERMISSION)) {
37-
LOGGER.info("Player {} forced from {} to ADVENTURE mode ({})",
38-
player.getDisplayName().getString(),
39-
currentMode.getName(),
40-
context);
41-
player.setGameMode(GameType.ADVENTURE);
42-
}
38+
// On JOIN: always force to ADVENTURE
39+
// On DIMENSION_CHANGE: only force if in CREATIVE
40+
boolean shouldEnforce;
41+
if ("DIMENSION_CHANGE".equals(context)) {
42+
shouldEnforce = (currentMode == GameType.CREATIVE);
43+
} else {
44+
// LOGIN and other contexts: always enforce
45+
shouldEnforce = (currentMode != GameType.ADVENTURE);
46+
}
47+
48+
if (shouldEnforce) {
49+
LOGGER.info("Player {} forced from {} to ADVENTURE mode ({})",
50+
player.getDisplayName().getString(),
51+
currentMode.getName(),
52+
context);
53+
player.setGameMode(GameType.ADVENTURE);
4354
}
4455
}
4556

0 commit comments

Comments
 (0)