From b3815ade9835f215415700f90b4c7a1a4d9c2828 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Mon, 4 Oct 2021 15:55:42 +0100 Subject: [PATCH] Fixed duplicate reforges not working --- .../reforges/reforges/util/ReforgeLookup.java | 54 +++++++++++++++++-- .../com/willfp/reforges/reforges/Reforge.kt | 1 - 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/reforges/reforges/util/ReforgeLookup.java b/eco-core/core-plugin/src/main/java/com/willfp/reforges/reforges/util/ReforgeLookup.java index a5da7cb..1aaab8a 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/reforges/reforges/util/ReforgeLookup.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/reforges/reforges/util/ReforgeLookup.java @@ -137,21 +137,65 @@ public static void updateReforges(@NotNull final Player player) { ReforgeLookup.clearCaches(player); PLUGIN.getScheduler().run(() -> { List after = ReforgeLookup.provideReforges(player); - List added = new ArrayList<>(after); - added.removeAll(before); + Map beforeFrequency = listToFrequencyMap(before); + Map afterFrequency = listToFrequencyMap(after); + + List added = new ArrayList<>(); + List removed = new ArrayList<>(); + + for (Map.Entry entry : new HashSet<>(afterFrequency.entrySet())) { + int amount = entry.getValue(); + Reforge reforge = entry.getKey(); + + amount -= beforeFrequency.getOrDefault(reforge, 0); + + if (amount < 1) { + continue; + } + + for (int i = 0; i < amount; i++) { + added.add(reforge); + } + } + + for (Map.Entry entry : new HashSet<>(beforeFrequency.entrySet())) { + int amount = entry.getValue(); + Reforge reforge = entry.getKey(); + + amount -= afterFrequency.getOrDefault(reforge, 0); + + if (amount < 1) { + continue; + } + + for (int i = 0; i < amount; i++) { + removed.add(reforge); + } + } for (Reforge reforge : added) { reforge.handleActivation(player); } - before.removeAll(after); - - for (Reforge reforge : before) { + for (Reforge reforge : removed) { reforge.handleDeactivation(player); } }); } + private static Map listToFrequencyMap(@NotNull final List list) { + Map frequencyMap = new HashMap<>(); + for (T object : list) { + if (frequencyMap.containsKey(object)) { + frequencyMap.put(object, frequencyMap.get(object) + 1); + } else { + frequencyMap.put(object, 1); + } + } + + return frequencyMap; + } + static { registerProvider(player -> Map.of( player.getInventory().getItemInMainHand(), diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/reforges/Reforge.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/reforges/Reforge.kt index bf9a764..03efc74 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/reforges/Reforge.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/reforges/Reforge.kt @@ -70,7 +70,6 @@ class Reforge( } fun handleActivation(player: Player) { - handleDeactivation(player) for ((effect, config) in this.effects) { effect.handleEnabling(player, config) }