Skip to content

Commit

Permalink
fix: use UserDataRecalculateEvent instead node events (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom authored Sep 2, 2023
1 parent d7fcfaa commit 68553db
Showing 1 changed file with 9 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.event.EventBus;
import net.luckperms.api.event.node.NodeAddEvent;
import net.luckperms.api.event.node.NodeClearEvent;
import net.luckperms.api.event.node.NodeRemoveEvent;
import net.luckperms.api.event.user.UserDataRecalculateEvent;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.group.GroupManager;
import net.luckperms.api.model.user.User;
Expand Down Expand Up @@ -61,50 +59,16 @@ public PlayerNodeChangeListener(LPMinestomPlugin plugin, LuckPerms luckPerms) {

public void register() {
EventBus eventBus = this.luckPerms.getEventBus();
eventBus.subscribe(this.plugin, NodeAddEvent.class, this::onNodeAdd);
eventBus.subscribe(this.plugin, NodeRemoveEvent.class, this::onNodeRemove);
eventBus.subscribe(this.plugin, NodeClearEvent.class, this::onNodeClear);
eventBus.subscribe(this.plugin, UserDataRecalculateEvent.class, this::onUserDataRecalculate);
}

private void onNodeAdd(NodeAddEvent e) {
if (!e.isUser()) {
return;
}

User target = (User) e.getTarget();
Node node = e.getNode();
Player player = MinecraftServer.getConnectionManager().getPlayer(target.getUniqueId());
if(player == null) {
throw new IllegalArgumentException("Player must be online");
}
setPermissionsFromNodes(List.of(node), player, luckPerms.getGroupManager());
}

private void onNodeRemove(NodeRemoveEvent e) {
if (!e.isUser()) {
return;
}

User target = (User) e.getTarget();
Node node = e.getNode();
private void onUserDataRecalculate(UserDataRecalculateEvent e) {
User target = (User) e.getUser();

Player player = MinecraftServer.getConnectionManager().getPlayer(target.getUniqueId());
if(player == null) {
throw new IllegalArgumentException("Player must be online");
}
setPermissionsFromNodes(List.of(node), player, luckPerms.getGroupManager());
}
private void onNodeClear(NodeClearEvent e) {
if (!e.isUser()) {
return;
}

User target = (User) e.getTarget();
Set<Node> nodes = e.getNodes();
Player player = MinecraftServer.getConnectionManager().getPlayer(target.getUniqueId());
if(player == null) {
throw new IllegalArgumentException("Player must be online");
}
setPermissionsFromNodes(nodes, player, luckPerms.getGroupManager());
if(player == null) return;

setPermissionsFromNodes(e.getUser().getNodes(), player, luckPerms.getGroupManager());
}

public static void setPermissionsFromNodes(Collection<Node> nodes, Player player, GroupManager groupManager) {
Expand Down Expand Up @@ -153,4 +117,4 @@ public static void setPermissionsFromNodes(Collection<Node> nodes, Player player
player.refreshCommands();
}

}
}

0 comments on commit 68553db

Please sign in to comment.