Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed May 27, 2024
1 parent a95677e commit a90ded1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
28 changes: 17 additions & 11 deletions src/main/java/cn/dreeam/caeruleum/listener/LocaleChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,35 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLocaleChangeEvent;

import java.util.Set;
import java.util.List;
import java.util.UUID;

public class LocaleChange implements Listener {

@EventHandler
public void onLocaleChange(PlayerLocaleChangeEvent e) {
// Should add threshold to prevent lag
// Should add threshold to prevent lag Or use async queue to execute all logic.
String locale = e.locale().toString();
UUID uuid = e.getPlayer().getUniqueId();
List<String> langPerms = PermUtil.getLangPerm(uuid);

Set<String> langPerm = PermUtil.getLangPerm(uuid);
if (false/*allowedLocale*/) {
// Return or give default/fallback
return;
}

if (langPerms.size() > 1) {
PermUtil.clearLangPerm(uuid);
}

if (true/*allowedLocale*/) {
String langPermNode = CaeruleumCore.config.langPermKeyPrefix() + locale;
String langPermNode = CaeruleumCore.config.langPermKeyPrefix() + locale;

if (PermUtil.hasLangPerm(langPerm)) {
if (!langPerm.contains(langPermNode)) {
PermUtil.modifyLangPerm(uuid, langPermNode);
}
} else {
PermUtil.applyLangPerm(uuid, langPermNode);
if (PermUtil.hasLangPerm(langPerms)) {
if (!langPerms.getFirst().equals(langPermNode)) {
PermUtil.modifyLangPerm(uuid, langPermNode);
}
} else {
PermUtil.applyLangPerm(uuid, langPermNode);
}
}
}
15 changes: 10 additions & 5 deletions src/main/java/cn/dreeam/caeruleum/utils/PermUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import net.luckperms.api.node.NodeType;
import net.luckperms.api.node.types.PermissionNode;

import java.util.Set;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

public class PermUtil {

Expand All @@ -21,16 +20,16 @@ public static User getUser(UUID uuid) {
return userFuture.join();
}

public static Set<String> getLangPerm(UUID uuid) {
public static List<String> getLangPerm(UUID uuid) {
User user = getUser(uuid);

return user.getNodes(NodeType.PERMISSION).stream()
.map(PermissionNode::getPermission)
.filter(s -> s.startsWith(CaeruleumCore.config.langPermKeyPrefix()))
.collect(Collectors.toSet());
.toList();
}

public static boolean hasLangPerm(Set<String> perms) {
public static boolean hasLangPerm(List<String> perms) {
return perms.size() == 1;
}

Expand All @@ -47,4 +46,10 @@ public static void modifyLangPerm(UUID uuid, String langPerm) {
}
);
}

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

0 comments on commit a90ded1

Please sign in to comment.