Fix node change events not being called for web editor changes (#2857)

This commit is contained in:
Luck 2021-01-26 11:41:38 +00:00
parent ec296460b2
commit 284e260bc2
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 29 additions and 34 deletions

View File

@ -98,7 +98,7 @@ public class Importer implements Runnable {
if (this.merge) {
group.mergeNodes(DataType.NORMAL, nodes);
} else {
group.setNodes(DataType.NORMAL, nodes);
group.setNodes(DataType.NORMAL, nodes, false);
}
this.plugin.getStorage().saveGroup(group);
}
@ -117,7 +117,7 @@ public class Importer implements Runnable {
if (this.merge) {
user.mergeNodes(DataType.NORMAL, userData.nodes);
} else {
user.setNodes(DataType.NORMAL, userData.nodes);
user.setNodes(DataType.NORMAL, userData.nodes, false);
}
this.plugin.getStorage().saveUser(user).join();
this.plugin.getUserManager().getHouseKeeper().cleanup(user.getUniqueId());

View File

@ -72,7 +72,7 @@ public class GroupClone extends ChildCommand<Group> {
return CommandResult.NO_PERMISSION;
}
newGroup.setNodes(DataType.NORMAL, target.normalData().asList());
newGroup.setNodes(DataType.NORMAL, target.normalData().asList(), false);
Message.CLONE_SUCCESS.send(sender, target.getFormattedDisplayName(), newGroup.getFormattedDisplayName());

View File

@ -85,7 +85,7 @@ public class GroupRename extends ChildCommand<Group> {
return CommandResult.FAILURE;
}
newGroup.setNodes(DataType.NORMAL, target.normalData().asList());
newGroup.setNodes(DataType.NORMAL, target.normalData().asList(), false);
Message.RENAME_SUCCESS.send(sender, target.getFormattedDisplayName(), newGroup.getFormattedDisplayName());

View File

@ -71,7 +71,7 @@ public class UserClone extends ChildCommand<User> {
return CommandResult.NO_PERMISSION;
}
otherUser.setNodes(DataType.NORMAL, target.normalData().asList());
otherUser.setNodes(DataType.NORMAL, target.normalData().asList(), false);
Message.CLONE_SUCCESS.send(sender, target.getFormattedDisplayName(), otherUser.getFormattedDisplayName());

View File

@ -232,9 +232,13 @@ public abstract class PermissionHolder {
invalidateCache();
}
public void setNodes(DataType type, Iterable<? extends Node> set) {
getData(type).setContent(set);
public MutateResult setNodes(DataType type, Iterable<? extends Node> set, boolean callEvent) {
MutateResult res = getData(type).setContent(set);
invalidateCache();
if (callEvent) {
getPlugin().getEventDispatcher().dispatchNodeChanges(this, type, res);
}
return res;
}
public void mergeNodes(DataType type, Iterable<? extends Node> set) {
@ -426,10 +430,10 @@ public abstract class PermissionHolder {
private boolean auditTemporaryNodes(DataType dataType) {
MutateResult result = getData(dataType).removeIf(Node::hasExpired);
this.plugin.getEventDispatcher().dispatchNodeChanges(this, dataType, result);
if (!result.isEmpty()) {
invalidateCache();
}
this.plugin.getEventDispatcher().dispatchNodeChanges(this, dataType, result);
return !result.isEmpty();
}
@ -460,12 +464,11 @@ public abstract class PermissionHolder {
}
MutateResult changes = getData(dataType).add(node);
invalidateCache();
if (callEvent) {
this.plugin.getEventDispatcher().dispatchNodeChanges(this, dataType, changes);
}
invalidateCache();
return DataMutateResult.SUCCESS;
}
@ -498,9 +501,8 @@ public abstract class PermissionHolder {
if (newNode != null) {
// Remove the old Node & add the new one.
MutateResult changes = data.removeThenAdd(otherMatch, newNode);
this.plugin.getEventDispatcher().dispatchNodeChanges(this, dataType, changes);
invalidateCache();
this.plugin.getEventDispatcher().dispatchNodeChanges(this, dataType, changes);
return new MergedNodeResult(DataMutateResult.SUCCESS, newNode);
}
@ -517,9 +519,8 @@ public abstract class PermissionHolder {
}
MutateResult changes = getData(dataType).remove(node);
this.plugin.getEventDispatcher().dispatchNodeChanges(this, dataType, changes);
invalidateCache();
this.plugin.getEventDispatcher().dispatchNodeChanges(this, dataType, changes);
return DataMutateResult.SUCCESS;
}
@ -540,9 +541,8 @@ public abstract class PermissionHolder {
// Remove the old Node & add the new one.
MutateResult changes = data.removeThenAdd(otherMatch, newNode);
this.plugin.getEventDispatcher().dispatchNodeChanges(this, dataType, changes);
invalidateCache();
this.plugin.getEventDispatcher().dispatchNodeChanges(this, dataType, changes);
return new MergedNodeResult(DataMutateResult.SUCCESS, newNode);
}
@ -569,8 +569,8 @@ public abstract class PermissionHolder {
getPlugin().getUserManager().giveDefaultIfNeeded((User) this);
}
this.plugin.getEventDispatcher().dispatchNodeClear(this, dataType, changes);
invalidateCache();
this.plugin.getEventDispatcher().dispatchNodeClear(this, dataType, changes);
return true;
}
@ -586,8 +586,8 @@ public abstract class PermissionHolder {
getPlugin().getUserManager().giveDefaultIfNeeded((User) this);
}
this.plugin.getEventDispatcher().dispatchNodeClear(this, dataType, changes);
invalidateCache();
this.plugin.getEventDispatcher().dispatchNodeClear(this, dataType, changes);
return true;
}

View File

@ -40,6 +40,7 @@ import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.model.manager.group.GroupManager;
import me.lucko.luckperms.common.model.nodemap.MutateResult;
import me.lucko.luckperms.common.node.utils.NodeJsonSerializer;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.sender.Sender;
@ -54,7 +55,6 @@ import net.luckperms.api.node.Node;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@ -177,38 +177,33 @@ public class WebEditorResponse {
return false;
}
Set<Node> before = holder.normalData().asSet();
Set<Node> after = new HashSet<>(NodeJsonSerializer.deserializeNodes(changeInfo.getAsJsonArray("nodes")));
Set<Node> nodes = NodeJsonSerializer.deserializeNodes(changeInfo.getAsJsonArray("nodes"));
MutateResult res = holder.setNodes(DataType.NORMAL, nodes, true);
Set<Node> diffAdded = getAdded(before, after);
Set<Node> diffRemoved = getRemoved(before, after);
int additions = diffAdded.size();
int deletions = diffRemoved.size();
if (additions == 0 && deletions == 0) {
if (res.isEmpty()) {
return false;
}
holder.setNodes(DataType.NORMAL, after);
Set<Node> added = res.getAdded();
Set<Node> removed = res.getRemoved();
for (Node n : diffAdded) {
for (Node n : added) {
LoggedAction.build().source(this.sender).target(holder)
.description("webeditor", "add", n.getKey(), n.getValue(), n.getContexts())
.build().submit(this.plugin, this.sender);
}
for (Node n : diffRemoved) {
for (Node n : removed) {
LoggedAction.build().source(this.sender).target(holder)
.description("webeditor", "remove", n.getKey(), n.getValue(), n.getContexts())
.build().submit(this.plugin, this.sender);
}
Message.APPLY_EDITS_SUCCESS.send(this.sender, type, holder.getFormattedDisplayName());
Message.APPLY_EDITS_SUCCESS_SUMMARY.send(this.sender, additions, deletions);
for (Node n : diffAdded) {
Message.APPLY_EDITS_SUCCESS_SUMMARY.send(this.sender, added.size(), removed.size());
for (Node n : added) {
Message.APPLY_EDITS_DIFF_ADDED.send(this.sender, n);
}
for (Node n : diffRemoved) {
for (Node n : removed) {
Message.APPLY_EDITS_DIFF_REMOVED.send(this.sender, n);
}
StorageAssistant.save(holder, this.sender, this.plugin);