Skip to content

Commit

Permalink
Add clear old perms task
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed May 30, 2024
1 parent 29e1b0e commit b7d0d36
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/main/java/cn/dreeam/caeruleum/CaeruleumCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import cn.dreeam.caeruleum.config.Config;
import cn.dreeam.caeruleum.config.ConfigManager;
import cn.dreeam.caeruleum.listener.LocaleChange;
import cn.dreeam.caeruleum.utils.PermUtil;
import net.luckperms.api.LuckPerms;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;
import java.util.stream.Collectors;

public class CaeruleumCore extends JavaPlugin {

private static CaeruleumCore instance;
Expand All @@ -30,6 +35,7 @@ public void onEnable() {
loadConfig();
registerEvents();
initHooks();
initTasks();
//new Metrics(instance, 16810);

LOGGER.info("CaeruleumCore {} enabled. By Dreeam.", instance.getDescription().getVersion());
Expand All @@ -51,6 +57,7 @@ private void loadConfig() {
}

private void initHooks() {
// Hook LuckPerms
RegisteredServiceProvider<LuckPerms> luckPermsProvider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
if (luckPermsProvider != null) {
luckPermsAPI = luckPermsProvider.getProvider();
Expand All @@ -60,6 +67,23 @@ private void initHooks() {
}
}

private void initTasks() {
// Clear old perms task
if (!config.oldLangPermPrefixList().isEmpty()) {
Thread.startVirtualThread(
() -> Arrays.stream(Bukkit.getServer().getOfflinePlayers())
.filter(OfflinePlayer::hasPlayedBefore)
.collect(Collectors.toSet())
.forEach(
p -> config.oldLangPermPrefixList()
.forEach(
oldLangPermPrefix -> PermUtil.clearLangPerm(p.getUniqueId(), oldLangPermPrefix)
)
)
);
}
}

public static CaeruleumCore getInstance() {
return instance;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cn/dreeam/caeruleum/utils/PermUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ public static void modifyLangPerm(UUID uuid, String langPerm) {
}

public static void clearLangPerm(UUID uuid) {
clearLangPerm(uuid, CaeruleumCore.config.langPermKeyPrefix());
}

public static void clearLangPerm(UUID uuid, String permPrefix) {
CaeruleumCore.getLuckPermsAPI().getUserManager().modifyUser(uuid, user ->
user.data().clear(x -> x.getKey().startsWith(CaeruleumCore.config.langPermKeyPrefix()))
user.data().clear(x -> x.getKey().startsWith(permPrefix))
);
}
}

0 comments on commit b7d0d36

Please sign in to comment.