Skip to content

Commit

Permalink
Exempt if sender & target have same weight with exemption-luckperms a…
Browse files Browse the repository at this point in the history
…ddon (#249)

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

* Only exempt if sender & target have weights & add more tests

* I accidentally duplicated a test

* Now all tests are passing :)

* Add configuration option & default to old behavior.

* Change method name and option name
  • Loading branch information
BlueTree242 authored Dec 10, 2023
1 parent 653a0c3 commit c8e76cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@

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

import space.arim.dazzleconf.annote.ConfComments;
import space.arim.dazzleconf.annote.ConfDefault;
import space.arim.dazzleconf.annote.ConfKey;
import space.arim.libertybans.core.addon.AddonConfig;

public interface ExemptionLuckPermsConfig extends AddonConfig {
@ConfKey("exempt-same")
@ConfComments("Whether to exempt victims with the same weight as the operator.")
@ConfDefault.DefaultBoolean(false)
boolean exemptSame();
}
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().exemptSame() ? targetWeight >= senderWeight : targetWeight > senderWeight;
});
}

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.exemptSame()).thenReturn(true);
assertIsExempted(true);
}

}

0 comments on commit c8e76cb

Please sign in to comment.