Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exempt if sender & target has same weight with exemption-luckperms addon #249

Merged
merged 6 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

package space.arim.libertybans.core.addon.exempt.luckperms;

import space.arim.dazzleconf.annote.ConfComments;
import space.arim.dazzleconf.annote.ConfDefault;
A248 marked this conversation as resolved.
Show resolved Hide resolved
import space.arim.libertybans.core.addon.AddonConfig;

public interface ExemptionLuckPermsConfig extends AddonConfig {

A248 marked this conversation as resolved.
Show resolved Hide resolved
@ConfComments("Whether to exempt punishment if operator & target have the same weight.")
A248 marked this conversation as resolved.
Show resolved Hide resolved
@ConfDefault.DefaultBoolean(false)
boolean exempt_same();
A248 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ public CompletionStage<Boolean> isExempted(CmdSender sender, String category, Vi
return userManager.loadUser(senderUuid).thenCombine(userManager.loadUser(targetUuid), (senderUser, targetUser) -> {
int senderWeight = calculateUserMaxWeight(senderUser);
int targetWeight = calculateUserMaxWeight(targetUser);
return targetWeight > senderWeight;
if (senderWeight == -1 && targetWeight == -1) return false;
return addon.config().exempt_same() ? targetWeight >= senderWeight : targetWeight > senderWeight;
A248 marked this conversation as resolved.
Show resolved Hide resolved
});
}

private int calculateUserMaxWeight(User user) {
int maxWeight = 0;
int maxWeight = -1;
for (Group group : user.getInheritedGroups(user.getQueryOptions())) {
int weight = group.getWeight().orElse(0);
int weight = group.getWeight().orElse(-1);
if (weight > maxWeight) {
maxWeight = weight;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import net.luckperms.api.model.user.User;
import net.luckperms.api.model.user.UserManager;
import net.luckperms.api.query.QueryOptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -53,6 +52,7 @@ public class LuckPermsExemptProviderTest {
private UUID targetUuid;
private User senderUser;
private User targetUser;
private ExemptionLuckPermsConfig config;
private LuckPermsExemptProvider exemptProvider;

public LuckPermsExemptProviderTest(@Mock ExemptionLuckPermsAddon addon) {
Expand All @@ -68,6 +68,7 @@ public void setup(@Mock ExemptionLuckPermsConfig config, @Mock LuckPerms luckPer
this.senderUser = senderUser;
this.targetUser = targetUser;
when(config.enable()).thenReturn(true);
this.config = config;
when(addon.config()).thenReturn(config);
when(addon.luckPerms()).thenReturn(luckPerms);
when(luckPerms.getUserManager()).thenReturn(userManager);
Expand Down Expand Up @@ -124,4 +125,19 @@ public void targetHasExemptWeightLowerThanOperatorWeight() {
assertIsExempted(false);
}

@Test
public void targetHasExemptWeightSameAsOperatorWeight() {
setGroups(senderUser, null, null, 4, null, null, 5, 20);
setGroups(targetUser, 3, null, 10, null, 20, 9);
assertIsExempted(false);
}

@Test
public void targetHasExemptWeightSameAsOperatorWeightOptionEnabled() {
setGroups(senderUser, null, null, 4, null, null, 5, 20);
setGroups(targetUser, 3, null, 10, null, 20, 9);
when(config.exempt_same()).thenReturn(true);
A248 marked this conversation as resolved.
Show resolved Hide resolved
assertIsExempted(true);
}

}
Loading