ScriptsLab
WikiDownloadsSourcesSupport
ScriptsLab
DocumentationDownloadsGitHubDiscord

© 2026 ScriptsLab

Back to src/main/java/com/scriptslab/core/config
J

ConfigurationManager.java

Java · 171 lines · 4.7 KB

src/main/java/com/scriptslab/core/config/ConfigurationManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.scriptslab.core.config;

import com.scriptslab.ScriptsLabPlugin;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

/**
 * Centralized configuration manager.
 * Handles main config, messages, and module configs.
 */
public final class ConfigurationManager {
    
    private final ScriptsLabPlugin plugin;
    private final Map<String, FileConfiguration> configs;
    private final LegacyComponentSerializer serializer;
    
    private FileConfiguration messagesConfig;
    
    public ConfigurationManager(ScriptsLabPlugin plugin) {
        this.plugin = plugin;
        this.configs = new HashMap<>();
        this.serializer = LegacyComponentSerializer.legacyAmpersand();
        
        // Load messages
        loadMessages();
    }
    
    /**
     * Loads the messages configuration.
     */
    private void loadMessages() {
        File messagesFile = new File(plugin.getDataFolder(), "messages.yml");
        
        if (!messagesFile.exists()) {
            plugin.saveResource("messages.yml", false);
        }
        
        messagesConfig = YamlConfiguration.loadConfiguration(messagesFile);
    }
    
    /**
     * Reloads all configurations.
     */
    public void reload() {
        plugin.reloadConfig();
        configs.clear();
        loadMessages();
        plugin.getLogger().info("Configurations reloaded");
    }
    
    /**
     * Gets a configuration by name.
     * 
     * @param name config name (without .yml)
     * @return configuration
     */
    public FileConfiguration getConfig(String name) {
        if (name.equals("config")) {
            return plugin.getConfig();
        }
        
        if (name.equals("messages")) {
            return messagesConfig;
        }
        
        return configs.computeIfAbsent(name, this::loadConfig);
    }
    
    /**
     * Loads a configuration file.
     */
    private FileConfiguration loadConfig(String name) {
        File configFile = new File(plugin.getDataFolder(), name + ".yml");
        
        if (!configFile.exists()) {
            plugin.getLogger().warning("Config file not found: " + name + ".yml");
            return new YamlConfiguration();
        }
        
        return YamlConfiguration.loadConfiguration(configFile);
    }
    
    /**
     * Saves a configuration.
     * 
     * @param name config name
     * @param config configuration to save
     */
    public void saveConfig(String name, FileConfiguration config) {
        try {
            File configFile = new File(plugin.getDataFolder(), name + ".yml");
            config.save(configFile);
        } catch (Exception e) {
            plugin.getLogger().log(Level.SEVERE, "Failed to save config: " + name, e);
        }
    }
    
    // === Message Methods ===
    
    /**
     * Gets a message from messages.yml.
     * 
     * @param path message path
     * @return message string
     */
    public String getMessage(String path) {
        return messagesConfig.getString(path, path);
    }
    
    /**
     * Gets a message with placeholders replaced.
     * 
     * @param path message path
     * @param placeholders placeholder map
     * @return formatted message
     */
    public String getMessage(String path, Map<String, String> placeholders) {
        String message = getMessage(path);
        
        if (placeholders != null) {
            for (Map.Entry<String, String> entry : placeholders.entrySet()) {
                message = message.replace("{" + entry.getKey() + "}", entry.getValue());
            }
        }
        
        return message;
    }
    
    /**
     * Gets a message as a Component.
     * 
     * @param path message path
     * @return component
     */
    public Component getMessageComponent(String path) {
        return serializer.deserialize(getMessage(path));
    }
    
    /**
     * Gets a message as a Component with placeholders.
     * 
     * @param path message path
     * @param placeholders placeholder map
     * @return component
     */
    public Component getMessageComponent(String path, Map<String, String> placeholders) {
        return serializer.deserialize(getMessage(path, placeholders));
    }
    
    /**
     * Creates a placeholder map.
     * 
     * @param pairs key-value pairs
     * @return placeholder map
     */
    public static Map<String, String> placeholders(String... pairs) {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < pairs.length - 1; i += 2) {
            map.put(pairs[i], pairs[i + 1]);
        }
        return map;
    }
}