|
1 | 1 | package com.eternalcode.formatter.mention; |
2 | 2 |
|
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; |
4 | 8 |
|
5 | | -import java.io.File; |
6 | | -import java.io.IOException; |
7 | | -import java.util.HashMap; |
8 | | -import java.util.Map; |
9 | 9 | import java.util.UUID; |
10 | 10 | import java.util.logging.Logger; |
11 | 11 |
|
12 | 12 | public class MentionPlayerSettings { |
13 | 13 |
|
14 | | - private final File dataFile; |
| 14 | + private static final String MENTION_SOUND_META_KEY = "chatformatter-mention-sound"; |
| 15 | + |
15 | 16 | private final Logger logger; |
16 | | - private final Map<UUID, Boolean> settings = new HashMap<>(); |
17 | 17 | private final MentionConfig config; |
| 18 | + private final Server server; |
| 19 | + private LuckPerms luckPerms; |
18 | 20 |
|
19 | | - public MentionPlayerSettings(File dataFolder, Logger logger, MentionConfig config) { |
| 21 | + public MentionPlayerSettings(Server server, Logger logger, MentionConfig config) { |
| 22 | + this.server = server; |
20 | 23 | this.logger = logger; |
21 | 24 | this.config = config; |
22 | | - this.dataFile = new File(dataFolder, "mention-settings.yml"); |
23 | | - this.load(); |
| 25 | + this.initializeLuckPerms(); |
24 | 26 | } |
25 | 27 |
|
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 | + } |
29 | 33 |
|
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 | + } |
33 | 39 | } |
34 | 40 |
|
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 | + } |
40 | 45 |
|
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; |
44 | 49 | } |
45 | 50 |
|
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; |
60 | 54 | } |
| 55 | + |
| 56 | + return Boolean.parseBoolean(metaValue); |
61 | 57 | } |
62 | 58 |
|
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 | + } |
69 | 64 |
|
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 | + ); |
71 | 69 |
|
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 | + } |
75 | 74 |
|
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; |
80 | 79 | } |
81 | 80 | } |
0 commit comments