Skip to content

Commit

Permalink
1.3.11 - Fix and optimize permission auto corrector
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSzabo committed Dec 15, 2024
1 parent 6ffe3c0 commit a21864f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {
}

group = "gg.auroramc"
version = "1.3.10"
version = "1.3.11"

repositories {
flatDir {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.luckperms.api.util.Tristate;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

public class PermissionCorrector implements RewardCorrector {
Expand All @@ -17,6 +18,8 @@ public void correctRewards(Player player) {
var plugin = AuroraQuests.getInstance();
var manager = plugin.getQuestManager();

List<Node> nodesToAdd = new ArrayList<>();

for (var pool : manager.getQuestPools()) {
// Correct global quests
if (pool.isGlobal()) {
Expand All @@ -28,7 +31,7 @@ public void correctRewards(Player player) {
if (permissionReward.getPermissions() == null || permissionReward.getPermissions().isEmpty())
continue;
var nodes = permissionReward.buildNodes(player, quest.getPlaceholders(player));
updatePermissionNodes(player, nodes);
nodesToAdd.addAll(nodes);
}
}
}
Expand All @@ -49,24 +52,40 @@ public void correctRewards(Player player) {

var nodes = permissionReward.buildNodes(player, placeholders);

updatePermissionNodes(player, nodes);
nodesToAdd.addAll(nodes);

}
}
}
}

if (nodesToAdd.isEmpty()) return;

updatePermissionNodes(player, nodesToAdd);
}

private void updatePermissionNodes(Player player, List<Node> nodes) {
LuckPermsProvider.get().getUserManager().modifyUser(player.getUniqueId(), user -> {
for (var node : nodes) {
var hasPermission = user.data().contains(node, NodeEqualityPredicate.EXACT);
var user = LuckPermsProvider.get().getUserManager().getUser(player.getUniqueId());
if (user == null) {
AuroraQuests.logger().severe("User " + player.getName() + " is not loaded in LuckPerms, failed to correct permission rewards!");
return;
}

if (hasPermission.equals(Tristate.UNDEFINED)) {
AuroraQuests.logger().debug("Permission " + node.getKey() + " is undefined for player " + player.getName());
user.data().add(node);
}
int addedNodes = 0;

for (var node : nodes) {
var hasPermission = user.data().contains(node, NodeEqualityPredicate.EXACT);

if (hasPermission.equals(Tristate.UNDEFINED)) {
AuroraQuests.logger().debug("Permission " + node.getKey() + " is undefined for player " + player.getName());
user.data().add(node);
addedNodes++;
}
});
}

if (addedNodes > 0) {
LuckPermsProvider.get().getUserManager().saveUser(user);
AuroraQuests.logger().debug("Added " + addedNodes + " permission nodes to player " + player.getName());
}
}
}

0 comments on commit a21864f

Please sign in to comment.