From 54aed80db8eae86ab5fdda4060568b46de34a034 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 6 Apr 2020 11:55:49 +0100 Subject: [PATCH] Fix applying track reordering from the webeditor (#2144) --- .../commands/misc/ApplyEditsCommand.java | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/misc/ApplyEditsCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/misc/ApplyEditsCommand.java index 76770ebc5..01365c885 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/misc/ApplyEditsCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/misc/ApplyEditsCommand.java @@ -25,7 +25,6 @@ package me.lucko.luckperms.common.commands.misc; -import com.google.common.collect.Maps; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -59,10 +58,10 @@ import net.luckperms.api.model.data.DataType; 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.Map; import java.util.Set; import java.util.UUID; @@ -165,9 +164,8 @@ public class ApplyEditsCommand extends SingleCommand { Set before = new HashSet<>(holder.normalData().immutable().values()); Set after = new HashSet<>(NodeJsonSerializer.deserializeNodes(data.getAsJsonArray("nodes"))); - Map.Entry, Set> diff = diff(before, after); - Set diffAdded = diff.getKey(); - Set diffRemoved = diff.getValue(); + Set diffAdded = getAdded(before, after); + Set diffRemoved = getRemoved(before, after); int additions = diffAdded.size(); int deletions = diffRemoved.size(); @@ -212,23 +210,27 @@ public class ApplyEditsCommand extends SingleCommand { track = plugin.getStorage().createAndLoadTrack(id, CreationCause.WEB_EDITOR).join(); } - Set before = new LinkedHashSet<>(track.getGroups()); - Set after = new LinkedHashSet<>(); + List before = track.getGroups(); + List after = new ArrayList<>(); data.getAsJsonArray("groups").forEach(e -> after.add(e.getAsString())); - Map.Entry, Set> diff = diff(before, after); - Set diffAdded = diff.getKey(); - Set diffRemoved = diff.getValue(); + if (before.equals(after)) { + return false; + } + + Set diffAdded = getAdded(before, after); + Set diffRemoved = getRemoved(before, after); int additions = diffAdded.size(); int deletions = diffRemoved.size(); - if (additions == 0 && deletions == 0) { - return false; + track.setGroups(after); + + if (hasBeenReordered(before, after, diffAdded, diffRemoved)) { + LoggedAction.build().source(sender).target(track) + .description("webeditor", "reorder", after) + .build().submit(plugin, sender); } - - track.setGroups(new ArrayList<>(after)); - for (String n : diffAdded) { LoggedAction.build().source(sender).target(track) .description("webeditor", "add", n) @@ -245,12 +247,8 @@ public class ApplyEditsCommand extends SingleCommand { Message.APPLY_EDITS_SUCCESS.send(sender, "track", track.getName()); Message.APPLY_EDITS_SUCCESS_SUMMARY.send(sender, additions, additionsSummary, deletions, deletionsSummary); - for (String n : diffAdded) { - Message.APPLY_EDITS_DIFF_ADDED.send(sender, n); - } - for (String n : diffRemoved) { - Message.APPLY_EDITS_DIFF_REMOVED.send(sender, n); - } + Message.APPLY_EDITS_DIFF_REMOVED.send(sender, before); + Message.APPLY_EDITS_DIFF_ADDED.send(sender, after); StorageAssistant.save(track, sender, plugin); return true; } @@ -315,17 +313,26 @@ public class ApplyEditsCommand extends SingleCommand { (n.hasExpiry() ? " &7(" + DurationFormatter.CONCISE.format(n.getExpiryDuration()) + ")" : ""); } - private static Map.Entry, Set> diff(Set before, Set after) { - // entries in before but not after are being removed - // entries in after but not before are being added - + private static Set getAdded(Collection before, Collection after) { Set added = new LinkedHashSet<>(after); added.removeAll(before); + return added; + } + private static Set getRemoved(Collection before, Collection after) { Set removed = new LinkedHashSet<>(before); removed.removeAll(after); + return removed; + } - return Maps.immutableEntry(added, removed); + private static boolean hasBeenReordered(List before, List after, Collection diffAdded, Collection diffRemoved) { + after = new ArrayList<>(after); + before = new ArrayList<>(before); + + after.removeAll(diffAdded); + before.removeAll(diffRemoved); + + return !before.equals(after); } @Override