Skip to content

Commit 27be209

Browse files
committed
Store user mention preferences in LuckPerms meta
1 parent 75835a3 commit 27be209

File tree

4 files changed

+56
-55
lines changed

4 files changed

+56
-55
lines changed

chatformatter-core/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ dependencies {
3030
compileOnly("me.clip:placeholderapi:2.12.1")
3131
compileOnly("com.github.MilkBowl:VaultAPI:1.7.1")
3232

33+
// LuckPerms API
34+
compileOnly("net.luckperms:api:5.4")
35+
3336
// GitCheck
3437
implementation("com.eternalcode:gitcheck:1.0.0")
3538

chatformatter-core/src/main/java/com/eternalcode/formatter/ChatFormatterPlugin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public ChatFormatterPlugin(Plugin plugin) {
5656
new Metrics(plugin, 15199);
5757

5858
this.chatHandler = new ChatHandlerImpl(miniMessage, pluginConfig, this.rankProvider, this.placeholderRegistry, this.templateService);
59-
MentionPlayerSettings mentionPlayerSettings = new MentionPlayerSettings(plugin.getDataFolder(), plugin.getLogger(), configManager.getPluginConfig().mentions);
60-
59+
MentionPlayerSettings mentionPlayerSettings = new MentionPlayerSettings(server, plugin.getLogger(), configManager.getPluginConfig().mentions);
6160

6261
server.getPluginCommand("chatformatter").setExecutor(new ChatFormatterCommand(configManager, audienceProvider, miniMessage, mentionPlayerSettings));
6362

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,80 @@
11
package com.eternalcode.formatter.mention;
22

3-
import org.bukkit.configuration.file.YamlConfiguration;
3+
import net.luckperms.api.LuckPerms;
4+
import net.luckperms.api.LuckPermsProvider;
5+
import net.luckperms.api.model.user.User;
6+
import net.luckperms.api.node.types.MetaNode;
7+
import org.bukkit.Server;
48

5-
import java.io.File;
6-
import java.io.IOException;
7-
import java.util.HashMap;
8-
import java.util.Map;
99
import java.util.UUID;
1010
import java.util.logging.Logger;
1111

1212
public class MentionPlayerSettings {
1313

14-
private final File dataFile;
14+
private static final String MENTION_SOUND_META_KEY = "chatformatter-mention-sound";
15+
1516
private final Logger logger;
16-
private final Map<UUID, Boolean> settings = new HashMap<>();
1717
private final MentionConfig config;
18+
private final Server server;
19+
private LuckPerms luckPerms;
1820

19-
public MentionPlayerSettings(File dataFolder, Logger logger, MentionConfig config) {
21+
public MentionPlayerSettings(Server server, Logger logger, MentionConfig config) {
22+
this.server = server;
2023
this.logger = logger;
2124
this.config = config;
22-
this.dataFile = new File(dataFolder, "mention-settings.yml");
23-
this.load();
25+
this.initializeLuckPerms();
2426
}
2527

26-
public boolean isMentionSoundEnabled(UUID uuid) {
27-
return this.settings.getOrDefault(uuid, this.config.enabled);
28-
}
28+
private void initializeLuckPerms() {
29+
if (!this.server.getPluginManager().isPluginEnabled("LuckPerms")) {
30+
this.logger.warning("LuckPerms is not installed! Mention sound toggle feature will not work.");
31+
return;
32+
}
2933

30-
public void setMentionSoundEnabled(UUID uuid, boolean enabled) {
31-
this.settings.put(uuid, enabled);
32-
this.save();
34+
try {
35+
this.luckPerms = LuckPermsProvider.get();
36+
} catch (IllegalStateException e) {
37+
this.logger.warning("Failed to initialize LuckPerms API: " + e.getMessage());
38+
}
3339
}
3440

35-
public boolean toggleMentionSound(UUID uuid) {
36-
boolean current = this.isMentionSoundEnabled(uuid);
37-
this.setMentionSoundEnabled(uuid, !current);
38-
return !current;
39-
}
41+
public boolean isMentionSoundEnabled(UUID uuid) {
42+
if (this.luckPerms == null) {
43+
return this.config.enabled;
44+
}
4045

41-
private void load() {
42-
if (!this.dataFile.exists()) {
43-
return;
46+
User user = this.luckPerms.getUserManager().getUser(uuid);
47+
if (user == null) {
48+
return this.config.enabled;
4449
}
4550

46-
try {
47-
YamlConfiguration config = YamlConfiguration.loadConfiguration(this.dataFile);
48-
49-
for (String key : config.getKeys(false)) {
50-
try {
51-
UUID uuid = UUID.fromString(key);
52-
boolean enabled = config.getBoolean(key, this.config.enabled);
53-
this.settings.put(uuid, enabled);
54-
} catch (IllegalArgumentException e) {
55-
this.logger.warning("Invalid UUID in mention-settings.yml: " + key);
56-
}
57-
}
58-
} catch (Exception e) {
59-
this.logger.warning("Failed to load mention settings: " + e.getMessage());
51+
String metaValue = user.getCachedData().getMetaData().getMetaValue(MENTION_SOUND_META_KEY);
52+
if (metaValue == null) {
53+
return this.config.enabled;
6054
}
55+
56+
return Boolean.parseBoolean(metaValue);
6157
}
6258

63-
private void save() {
64-
try {
65-
if (!this.dataFile.exists()) {
66-
this.dataFile.getParentFile().mkdirs();
67-
this.dataFile.createNewFile();
68-
}
59+
public void setMentionSoundEnabled(UUID uuid, boolean enabled) {
60+
if (this.luckPerms == null) {
61+
this.logger.warning("Cannot set mention sound preference - LuckPerms is not available!");
62+
return;
63+
}
6964

70-
YamlConfiguration config = new YamlConfiguration();
65+
this.luckPerms.getUserManager().modifyUser(uuid, user -> {
66+
user.data().clear(node ->
67+
node instanceof MetaNode metaNode && metaNode.getMetaKey().equals(MENTION_SOUND_META_KEY)
68+
);
7169

72-
for (Map.Entry<UUID, Boolean> entry : this.settings.entrySet()) {
73-
config.set(entry.getKey().toString(), entry.getValue());
74-
}
70+
MetaNode metaNode = MetaNode.builder(MENTION_SOUND_META_KEY, Boolean.toString(enabled)).build();
71+
user.data().add(metaNode);
72+
});
73+
}
7574

76-
config.save(this.dataFile);
77-
} catch (IOException e) {
78-
this.logger.warning("Failed to save mention settings: " + e.getMessage());
79-
}
75+
public boolean toggleMentionSound(UUID uuid) {
76+
boolean current = this.isMentionSoundEnabled(uuid);
77+
this.setMentionSoundEnabled(uuid, !current);
78+
return !current;
8079
}
8180
}

chatformatter-paper-plugin/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bukkit {
1212
prefix = "ChatFormatter"
1313
author = "EternalCodeTeam"
1414
name = "ChatFormatter"
15-
softDepend = listOf("PlaceholderAPI", "Vault")
15+
softDepend = listOf("PlaceholderAPI", "Vault", "LuckPerms")
1616
version = "${project.version}"
1717

1818
commands {

0 commit comments

Comments
 (0)