Skip to content

Commit

Permalink
Add option to purge invalid/outdated user data if someone is heavily …
Browse files Browse the repository at this point in the history
…modifying pool/quest configs in production
  • Loading branch information
ErikSzabo committed Aug 9, 2024
1 parent c067f52 commit 6a56cdd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
38 changes: 34 additions & 4 deletions src/main/java/gg/auroramc/quests/api/data/QuestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.google.common.collect.Sets;
import gg.auroramc.aurora.api.user.UserDataHolder;
import gg.auroramc.aurora.api.util.NamespacedId;
import gg.auroramc.quests.AuroraQuests;
import gg.auroramc.quests.api.quest.Quest;
import gg.auroramc.quests.api.quest.QuestPool;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

public class QuestData extends UserDataHolder {
private final Map<String, PoolRollData> rolledQuests = Maps.newConcurrentMap();
Expand Down Expand Up @@ -190,4 +191,33 @@ public void initFrom(@Nullable ConfigurationSection data) {

poolUnlocks.addAll(data.getStringList("pool_unlocks"));
}

public void purgeInvalidData(Collection<QuestPool> questPools) {
var poolIds = questPools.stream().map(QuestPool::getId).collect(Collectors.toSet());

completedCount.keySet().removeIf(poolId -> !poolIds.contains(poolId));
progression.keySet().removeIf(poolId -> !poolIds.contains(poolId));
questUnlocks.keySet().removeIf(poolId -> !poolIds.contains(poolId));
rolledQuests.keySet().removeIf(poolId -> !poolIds.contains(poolId));
completedQuests.keySet().removeIf(poolId -> !poolIds.contains(poolId));
poolUnlocks.removeIf(poolId -> !poolIds.contains(poolId));

for (var pool : questPools) {
var questIds = pool.getQuests().stream().map(Quest::getId).collect(Collectors.toSet());

if (progression.containsKey(pool.getId())) {
progression.get(pool.getId()).keySet().removeIf(id -> !questIds.contains(id));
}

if (questUnlocks.containsKey(pool.getId())) {
questUnlocks.get(pool.getId()).removeIf(id -> !questIds.contains(id));
}

if (completedQuests.containsKey(pool.getId())) {
completedQuests.get(pool.getId()).removeIf(id -> !questIds.contains(id));
}
}

dirty.set(true);
}
}
1 change: 1 addition & 0 deletions src/main/java/gg/auroramc/quests/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@Getter
public class Config extends AuroraConfig {
private Boolean debug = false;
private Boolean purgeInvalidDataOnLogin = false;
private String language = "en";
private Map<String, String> difficulties;
private Boolean preventCreativeMode = false;
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/gg/auroramc/quests/listener/PlayerListener.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package gg.auroramc.quests.listener;

import gg.auroramc.aurora.api.AuroraAPI;
import gg.auroramc.aurora.api.events.user.AuroraUserLoadedEvent;
import gg.auroramc.aurora.api.message.Chat;
import gg.auroramc.aurora.api.message.Placeholder;
import gg.auroramc.quests.AuroraQuests;
import gg.auroramc.quests.api.data.QuestData;
import gg.auroramc.quests.api.event.QuestCompletedEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashSet;
import java.util.concurrent.CompletableFuture;

public class PlayerListener implements Listener {
Expand All @@ -25,13 +28,9 @@ public void onPlayerLoaded(AuroraUserLoadedEvent event) {
if (player == null) return;

if (event.isAsynchronous()) {
roll(player);
plugin.getQuestManager().getRewardAutoCorrector().correctRewards(player);
initPlayer(player);
} else {
CompletableFuture.runAsync(() -> {
roll(player);
plugin.getQuestManager().getRewardAutoCorrector().correctRewards(player);
});
CompletableFuture.runAsync(() -> initPlayer(player));
}
}

Expand All @@ -40,7 +39,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
plugin.getQuestManager().handlePlayerQuit(event.getPlayer().getUniqueId());
}

private void roll(Player player) {
private void initPlayer(Player player) {
plugin.getQuestManager().tryUnlockQuestPools(player);
plugin.getQuestManager().tryStartGlobalQuests(player);

Expand All @@ -50,6 +49,13 @@ private void roll(Player player) {

var msg = plugin.getConfigManager().getMessageConfig().getReRolledTarget();
Chat.sendMessage(player, msg, Placeholder.of("{pool}", String.join(", ", pools.stream().map(p -> p.getConfig().getName()).toList())));

plugin.getQuestManager().getRewardAutoCorrector().correctRewards(player);

if (plugin.getConfigManager().getConfig().getPurgeInvalidDataOnLogin()) {
AuroraAPI.getUserManager().getUser(player).getData(QuestData.class)
.purgeInvalidData(plugin.getQuestManager().getQuestPools());
}
}

@EventHandler
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ debug: false
language: en
prevent-creative-mode: true

# Only enable this if you are heavily modifying your quest pools/quests data in production (WHICH YOU SHOULDN'T)
purge-invalid-data-on-login: false

# Timer to try to unlock global quests and quest pools if for some reason the event driven method doesn't work
unlock-task:
enabled: false
Expand Down

0 comments on commit 6a56cdd

Please sign in to comment.