ScriptsLab
WikiDownloadsSourcesSupport
ScriptsLab
DocumentationDownloadsGitHubDiscord

© 2026 ScriptsLab

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

ModuleManagerImpl.java

Java · 334 lines · 12.1 KB

src/main/java/com/scriptslab/core/module/ModuleManagerImpl.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.scriptslab.core.module;

import com.scriptslab.api.module.Module;
import com.scriptslab.api.module.ModuleManager;
import com.scriptslab.api.module.ModuleDescriptor;
import com.scriptslab.core.module.BaseModule;
import com.scriptslab.core.module.DependencyResolver;

import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Thread-safe implementation of ModuleManager.
 */
public final class ModuleManagerImpl implements ModuleManager {
    
    private final Path modulesDirectory;
    private final Logger logger;
    private final Map<String, Module> modules;
    private final Map<String, ModuleDescriptor> descriptors;
    private final DependencyResolver dependencyResolver;
    
    public ModuleManagerImpl(Path modulesDirectory) {
        this.modulesDirectory = modulesDirectory;
        this.logger = Logger.getLogger("ModuleManager");
        this.modules = new ConcurrentHashMap<>();
        this.descriptors = new ConcurrentHashMap<>();
        this.dependencyResolver = new DependencyResolver();
        
        // Create modules directory
        modulesDirectory.toFile().mkdirs();
    }
    
    @Override
    public CompletableFuture<Integer> loadAllModules() {
        return CompletableFuture.supplyAsync(() -> {
            logger.info("Loading modules from " + modulesDirectory);
            
            File[] moduleFiles = modulesDirectory.toFile().listFiles();
            if (moduleFiles == null || moduleFiles.length == 0) {
                logger.info("No modules found");
                return 0;
            }
            
            // Load all module descriptors first
            List<ModuleDescriptor> allDescriptors = new ArrayList<>();
            for (File file : moduleFiles) {
                if (file.isDirectory()) {
                    try {
                        ModuleDescriptor descriptor = loadDescriptor(file.toPath());
                        allDescriptors.add(descriptor);
                        descriptors.put(descriptor.id(), descriptor);
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, "Failed to load descriptor from " + file.getName(), e);
                    }
                }
            }
            
            // Validate dependencies
            try {
                dependencyResolver.validateDependencies(allDescriptors);
            } catch (DependencyResolver.MissingDependencyException e) {
                logger.severe(e.getMessage());
                return 0;
            }
            
            // Resolve load order
            List<String> loadOrder;
            try {
                loadOrder = dependencyResolver.resolveLoadOrder(allDescriptors);
            } catch (DependencyResolver.CyclicDependencyException e) {
                logger.severe(e.getMessage());
                return 0;
            }
            
            logger.info("Module load order: " + loadOrder);
            
            // Load modules in order
            int loaded = 0;
            for (String moduleId : loadOrder) {
                ModuleDescriptor descriptor = descriptors.get(moduleId);
                if (descriptor != null && descriptor.enabled()) {
                    try {
                        Path modulePath = modulesDirectory.resolve(moduleId);
                        loadModule(modulePath).join();
                        loaded++;
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, "Failed to load module " + moduleId, e);
                    }
                }
            }
            
            logger.info("Loaded " + loaded + " modules");
            return loaded;
        });
    }
    
    @Override
    public CompletableFuture<Module> loadModule(Path modulePath) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                // Load descriptor
                ModuleDescriptor descriptor = loadDescriptor(modulePath);
                descriptor.validate();
                
                // Check if already loaded
                if (modules.containsKey(descriptor.id())) {
                    logger.warning("Module " + descriptor.id() + " is already loaded");
                    return modules.get(descriptor.id());
                }
                
                // Create module instance
                Module module = createModuleInstance(descriptor, modulePath);
                
                // Load the module
                module.onLoad().join();
                
                modules.put(descriptor.id(), module);
                logger.info("Loaded module: " + descriptor.name() + " v" + descriptor.version());
                
                return module;
                
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Failed to load module from " + modulePath, e);
                throw new RuntimeException(e);
            }
        });
    }
    
    @Override
    public CompletableFuture<Void> enableModule(String moduleId) {
        return CompletableFuture.runAsync(() -> {
            Module module = modules.get(moduleId);
            if (module == null) {
                throw new IllegalArgumentException("Module not found: " + moduleId);
            }
            
            if (module.isEnabled()) {
                logger.warning("Module " + moduleId + " is already enabled");
                return;
            }
            
            try {
                module.onEnable().join();
                logger.info("Enabled module: " + module.getName());
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Failed to enable module " + moduleId, e);
                throw new RuntimeException(e);
            }
        });
    }
    
    @Override
    public CompletableFuture<Void> disableModule(String moduleId) {
        return CompletableFuture.runAsync(() -> {
            Module module = modules.get(moduleId);
            if (module == null) {
                throw new IllegalArgumentException("Module not found: " + moduleId);
            }
            
            if (!module.isEnabled()) {
                logger.warning("Module " + moduleId + " is already disabled");
                return;
            }
            
            try {
                module.onDisable().join();
                logger.info("Disabled module: " + module.getName());
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Failed to disable module " + moduleId, e);
                throw new RuntimeException(e);
            }
        });
    }
    
    @Override
    public CompletableFuture<Void> reloadModule(String moduleId) {
        return CompletableFuture.runAsync(() -> {
            Module module = modules.get(moduleId);
            if (module == null) {
                throw new IllegalArgumentException("Module not found: " + moduleId);
            }
            
            try {
                module.onReload().join();
                logger.info("Reloaded module: " + module.getName());
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Failed to reload module " + moduleId, e);
                throw new RuntimeException(e);
            }
        });
    }
    
    @Override
    public CompletableFuture<Void> unloadModule(String moduleId) {
        return CompletableFuture.runAsync(() -> {
            Module module = modules.get(moduleId);
            if (module == null) {
                throw new IllegalArgumentException("Module not found: " + moduleId);
            }
            
            try {
                if (module.isEnabled()) {
                    module.onDisable().join();
                }
                modules.remove(moduleId);
                descriptors.remove(moduleId);
                logger.info("Unloaded module: " + module.getName());
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Failed to unload module " + moduleId, e);
                throw new RuntimeException(e);
            }
        });
    }
    
    @Override
    public Optional<Module> getModule(String moduleId) {
        return Optional.ofNullable(modules.get(moduleId));
    }
    
    @Override
    public Collection<Module> getModules() {
        return Collections.unmodifiableCollection(modules.values());
    }
    
    @Override
    public Collection<Module> getEnabledModules() {
        return modules.values().stream()
                .filter(Module::isEnabled)
                .toList();
    }
    
    @Override
    public boolean isLoaded(String moduleId) {
        return modules.containsKey(moduleId);
    }
    
    @Override
    public boolean isEnabled(String moduleId) {
        Module module = modules.get(moduleId);
        return module != null && module.isEnabled();
    }
    
    @Override
    public Path getModulesDirectory() {
        return modulesDirectory;
    }
    
    @Override
    public List<String> resolveLoadOrder(Collection<ModuleDescriptor> moduleDescriptors) {
        return dependencyResolver.resolveLoadOrder(moduleDescriptors);
    }
    
    /**
     * Loads module descriptor from module.yml.
     */
    private ModuleDescriptor loadDescriptor(Path modulePath) {
        File descriptorFile = modulePath.resolve("module.yml").toFile();
        
        if (!descriptorFile.exists()) {
            throw new IllegalStateException("module.yml not found in " + modulePath);
        }
        
        YamlConfiguration yaml = YamlConfiguration.loadConfiguration(descriptorFile);
        
        String id = yaml.getString("id");
        String name = yaml.getString("name", id);
        String version = yaml.getString("version", "1.0.0");
        String description = yaml.getString("description", "");
        List<String> authors = yaml.getStringList("authors");
        Set<String> dependencies = new HashSet<>(yaml.getStringList("dependencies"));
        Set<String> softDependencies = new HashSet<>(yaml.getStringList("soft-dependencies"));
        
        String loadOrderStr = yaml.getString("load-order", "STARTUP");
        ModuleDescriptor.LoadOrder loadOrder = ModuleDescriptor.LoadOrder.valueOf(loadOrderStr.toUpperCase());
        
        boolean enabled = yaml.getBoolean("enabled", true);
        
        return new ModuleDescriptor(
                id, name, version, description, authors,
                dependencies, softDependencies, loadOrder, enabled
        );
    }
    
    /**
     * Creates a module instance.
     * For now, uses reflection to instantiate built-in modules.
     * In production, this would load from JAR files.
     */
    private Module createModuleInstance(ModuleDescriptor descriptor, Path modulePath) {
        // Try to load built-in module class
        String className = "com.myplugin.modules." + descriptor.id() + "." + 
                          capitalize(descriptor.id()) + "Module";
        
        try {
            Class<?> moduleClass = Class.forName(className);
            return (Module) moduleClass
                    .getConstructor(ModuleDescriptor.class, Path.class)
                    .newInstance(descriptor, modulePath);
        } catch (ClassNotFoundException e) {
            // Module class not found, create generic module
            logger.warning("Module class not found: " + className + ", using generic module");
            return new GenericModule(descriptor, modulePath);
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Failed to instantiate module " + descriptor.id(), e);
            throw new RuntimeException(e);
        }
    }
    
    private String capitalize(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
    
    /**
     * Generic module implementation for modules without custom class.
     */
    private static class GenericModule extends BaseModule {
        
        GenericModule(ModuleDescriptor descriptor, Path dataFolder) {
            super(descriptor, dataFolder);
        }
    }
}