Skip to content

Commit

Permalink
Fixed duplicate reforges not working
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Oct 4, 2021
1 parent 7ed6e59 commit b3815ad
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,65 @@ public static void updateReforges(@NotNull final Player player) {
ReforgeLookup.clearCaches(player);
PLUGIN.getScheduler().run(() -> {
List<Reforge> after = ReforgeLookup.provideReforges(player);
List<Reforge> added = new ArrayList<>(after);
added.removeAll(before);
Map<Reforge, Integer> beforeFrequency = listToFrequencyMap(before);
Map<Reforge, Integer> afterFrequency = listToFrequencyMap(after);

List<Reforge> added = new ArrayList<>();
List<Reforge> removed = new ArrayList<>();

for (Map.Entry<Reforge, Integer> 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<Reforge, Integer> 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 <T> Map<T, Integer> listToFrequencyMap(@NotNull final List<T> list) {
Map<T, Integer> 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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class Reforge(
}

fun handleActivation(player: Player) {
handleDeactivation(player)
for ((effect, config) in this.effects) {
effect.handleEnabling(player, config)
}
Expand Down

0 comments on commit b3815ad

Please sign in to comment.