Skip to content

Commit b26a1af

Browse files
committed
refactor perms
1 parent 34f8c73 commit b26a1af

File tree

3 files changed

+39
-60
lines changed

3 files changed

+39
-60
lines changed

src/main/java/me/mapacheee/extendedhorizons/integration/luckperms/LuckPermsService.java

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public boolean isEnabled() {
7777
*/
7878
public int resolveMaxDistance(Player player, int fallback) {
7979
if (!enabled || handler == null)
80-
return fallback;
80+
return -1;
8181

8282
UUID id = player.getUniqueId();
8383
long now = Instant.now().getEpochSecond();
@@ -86,7 +86,7 @@ public int resolveMaxDistance(Player player, int fallback) {
8686
return ce.value;
8787
}
8888

89-
int resolved = handler.compute(player, fallback, useGroupPermissions);
89+
int resolved = handler.compute(player, -1, useGroupPermissions);
9090

9191
CacheEntry fresh = new CacheEntry();
9292
fresh.value = resolved;
@@ -125,31 +125,21 @@ public int compute(Player player, int fallback, boolean useGroupPermissions) {
125125
if (user == null)
126126
return fallback;
127127

128-
net.luckperms.api.cacheddata.CachedMetaData meta = user.getCachedData().getMetaData();
129-
String metaValue = meta.getMetaValue("extendedhorizons.max-distance");
130-
if (metaValue != null) {
131-
Integer parsed = parsePositiveInt(metaValue);
132-
if (parsed != null)
133-
return parsed;
134-
}
128+
int best = -1;
135129

136-
if (useGroupPermissions) {
137-
int best = -1;
138-
for (var node : user.getNodes()) {
139-
if (node instanceof net.luckperms.api.node.types.PermissionNode p && p.getValue()) {
140-
String perm = p.getPermission();
141-
if (perm.startsWith("extendedhorizons.max.")
142-
|| perm.startsWith("extendedhorizons.distance.")) {
143-
Optional<Integer> n = extractTrailingInt(perm);
144-
if (n.isPresent() && n.get() > best)
145-
best = n.get();
146-
}
130+
for (var node : user.getNodes()) {
131+
if (node instanceof net.luckperms.api.node.types.PermissionNode p && p.getValue()) {
132+
String perm = p.getPermission();
133+
if (perm.startsWith("extendedhorizons.view.")) {
134+
Optional<Integer> n = extractTrailingInt(perm);
135+
if (n.isPresent() && n.get() > best)
136+
best = n.get();
147137
}
148138
}
149-
if (best > 0)
150-
return best;
151139
}
152-
return fallback;
140+
141+
return best > 0 ? best : fallback;
142+
153143
} catch (Exception e) {
154144
return fallback;
155145
}
@@ -166,14 +156,5 @@ private Optional<Integer> extractTrailingInt(String key) {
166156
return Optional.empty();
167157
}
168158
}
169-
170-
private Integer parsePositiveInt(String s) {
171-
try {
172-
int v = Integer.parseInt(s.trim());
173-
return v > 0 ? v : null;
174-
} catch (NumberFormatException e) {
175-
return null;
176-
}
177-
}
178159
}
179160
}

src/main/java/me/mapacheee/extendedhorizons/viewdistance/service/ViewDistanceService.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public ViewDistanceService(ConfigService configService,
6969
public void handlePlayerJoin(Player player) {
7070
fakeChunkService.onPlayerJoin(player);
7171

72+
if (!player.hasPermission("extendedhorizons.use")) {
73+
return;
74+
}
75+
7276
if (!isPluginEnabledForWorld(player.getWorld())) {
7377
return;
7478
}
@@ -81,17 +85,15 @@ public void handlePlayerJoin(Player player) {
8185
return;
8286
}
8387

84-
int fallbackDefault = configService.get().viewDistance().defaultDistance();
88+
int maxAllowed = getAllowedMax(p);
89+
8590
int dbDistance = playerData.map(PlayerData::getViewDistance).orElse(-1);
86-
int clientDistance = p.getClientViewDistance();
8791

8892
int initialDistance;
8993
if (dbDistance > 0) {
90-
initialDistance = dbDistance;
91-
} else if (clientDistance > 0) {
92-
initialDistance = clientDistance;
94+
initialDistance = Math.min(dbDistance, maxAllowed);
9395
} else {
94-
initialDistance = fallbackDefault;
96+
initialDistance = maxAllowed;
9597
}
9698

9799
int clamped = clampDistance(p, initialDistance);
@@ -167,6 +169,10 @@ public void setPlayerDistance(Player player, int requestedDistance) {
167169
throw new IllegalArgumentException("Player must not be null");
168170
}
169171

172+
if (!player.hasPermission("extendedhorizons.use")) {
173+
return;
174+
}
175+
170176
if (!isPluginEnabledForWorld(player.getWorld())) {
171177
throw new IllegalStateException("ExtendedHorizons is disabled in this world");
172178
}
@@ -192,24 +198,28 @@ public void setPlayerDistance(Player player, int requestedDistance) {
192198
}
193199

194200
/**
195-
* Returns allowed maximum distance for this player after LuckPerms check.
201+
* Returns allowed maximum distance (effective default + limit)
196202
*/
197203
public int getAllowedMax(Player player) {
198204
String worldName = player.getWorld().getName();
199205

200-
Map<String, WorldConfig> worldSettings = configService
201-
.get().worldSettings();
206+
int baseDefault = configService.get().viewDistance().defaultDistance();
207+
208+
int permissionMax = -1;
209+
if (luckPermsService != null && luckPermsService.isEnabled()) {
210+
permissionMax = luckPermsService.resolveMaxDistance(player, -1);
211+
}
212+
int effectiveMax = (permissionMax > 0) ? permissionMax : baseDefault;
202213

203-
int configMax;
214+
Map<String, WorldConfig> worldSettings = configService.get().worldSettings();
215+
int globalCap;
204216
if (worldSettings != null && worldSettings.containsKey(worldName)) {
205-
configMax = worldSettings.get(worldName).maxDistance();
217+
globalCap = worldSettings.get(worldName).maxDistance();
206218
} else {
207-
configMax = configService.get().viewDistance().maxDistance();
219+
globalCap = configService.get().viewDistance().maxDistance();
208220
}
209221

210-
return luckPermsService != null && luckPermsService.isEnabled()
211-
? Math.min(configMax, luckPermsService.resolveMaxDistance(player, configMax))
212-
: configMax;
222+
return Math.min(effectiveMax, globalCap);
213223
}
214224

215225
private int clampDistance(Player player, int value) {

src/main/resources/plugin.yml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,8 @@ commands:
2222

2323
permissions:
2424
extendedhorizons.use:
25-
description: Basic permission to use ExtendedHorizons commands
25+
description: Basic permission to use ExtendedHorizons features
2626
default: true
27-
extendedhorizons.distance.16:
28-
description: Allow view distance up to 16 chunks
29-
default: false
30-
extendedhorizons.distance.32:
31-
description: Allow view distance up to 32 chunks
32-
default: op
33-
extendedhorizons.distance.64:
34-
description: Allow view distance up to 64 chunks
35-
default: false
36-
extendedhorizons.distance.unlimited:
37-
description: Allow unlimited view distance
38-
default: false
3927
extendedhorizons.admin:
4028
description: Access to admin commands
4129
default: op

0 commit comments

Comments
 (0)