Skip to content

Commit

Permalink
Add option to disable default modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jul 13, 2024
1 parent 1ef0fb6 commit 61f6558
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
39 changes: 23 additions & 16 deletions plugin/src/main/java/org/battleplugins/arena/BattleArena.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ public void onLoad() {

this.info("Loading BattleArena {} for {}", this.getPluginMeta().getVersion(), Version.getServerVersion());

this.loadConfig();

Path dataFolder = this.getDataFolder().toPath();
this.arenasPath = dataFolder.resolve("arenas");
Path modulesPath = dataFolder.resolve("modules");

Util.copyDirectories(this.getFile(), modulesPath, "modules");
Util.copyDirectories(this.getFile(), modulesPath, "modules", this.config.getDisabledModules().toArray(String[]::new));

this.moduleLoader = new ArenaModuleLoader(this, this.getClassLoader(), modulesPath);
try {
Expand All @@ -118,21 +120,6 @@ public void onEnable() {
}

private void enable() {
// Copy our default configs
this.saveDefaultConfig();

File configFile = new File(this.getDataFolder(), "config.yml");
Configuration config = YamlConfiguration.loadConfiguration(configFile);
try {
this.config = ArenaConfigParser.newInstance(configFile.toPath(), BattleArenaConfig.class, config);
} catch (ParseException e) {
ParseException.handle(e);

this.error("Failed to load BattleArena configuration! Disabling plugin.");
this.getServer().getPluginManager().disablePlugin(this);
return;
}

this.debugMode = this.config.isDebugMode();

if (Files.notExists(this.arenasPath)) {
Expand Down Expand Up @@ -267,6 +254,10 @@ public void reload() {
new BattleArenaReloadEvent(this).callEvent();

this.disable();

// Reload the config
this.loadConfig();

this.enable();

// Reload loaders - has to be done this way for third party
Expand Down Expand Up @@ -750,6 +741,22 @@ private void clearDynamicMaps() {
}
}

private void loadConfig() {
this.saveDefaultConfig();

File configFile = new File(this.getDataFolder(), "config.yml");
Configuration config = YamlConfiguration.loadConfiguration(configFile);
try {
this.config = ArenaConfigParser.newInstance(configFile.toPath(), BattleArenaConfig.class, config);
} catch (ParseException e) {
ParseException.handle(e);

this.error("Failed to load BattleArena configuration! Disabling plugin.");
this.getServer().getPluginManager().disablePlugin(this);
return;
}
}

/**
* Returns the instance of the plugin.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public class BattleArenaConfig {
@ArenaOption(name = "max-dynamic-maps", description = "The maximum number of dynamic maps an Arena can have allocated at once.", required = true)
private int maxDynamicMaps;

@ArenaOption(name = "disabled-modules", description = "Modules that are disabled by default.")
private List<String> disabledModules;

@ArenaOption(name = "events", description = "The configured events.", required = true)
private Map<String, List<EventOptions>> events;

@ArenaOption(name = "debug-mode", description = "Whether debug mode is enabled.")
private boolean debugMode;

Expand All @@ -45,8 +48,12 @@ public int getMaxDynamicMaps() {
return this.maxDynamicMaps;
}

public List<String> getDisabledModules() {
return this.disabledModules == null ? List.of() : List.copyOf(this.disabledModules);
}

public Map<String, List<EventOptions>> getEvents() {
return this.events;
return Map.copyOf(this.events);
}

public boolean isDebugMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void loadModules() throws IOException {

public void enableModules() {
this.modules.values().forEach(module -> {
if (this.plugin.getMainConfig().getDisabledModules().contains(module.module().id())) {
this.plugin.info("Module {} is disabled in the configuration. Skipping...", module.module().name());
return;
}

if (module.mainClass() instanceof ArenaModuleInitializer moduleInitializer) {
Bukkit.getPluginManager().registerEvents(moduleInitializer, this.plugin);
}
Expand Down
8 changes: 7 additions & 1 deletion plugin/src/main/java/org/battleplugins/arena/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static <T> void copyFields(T oldInstance, T newInstance) {
}
}

public static void copyDirectories(File jarFile, Path outputPath, String directory) {
public static void copyDirectories(File jarFile, Path outputPath, String directory, String... ignoredFiles) {
Path jarPath = jarFile.toPath();
try {
if (Files.notExists(outputPath)) {
Expand All @@ -177,6 +177,12 @@ public static void copyDirectories(File jarFile, Path outputPath, String directo
return;
}

for (String ignoredFile : ignoredFiles) {
if (relativePath.toString().startsWith(ignoredFile)) {
return;
}
}

try {
Files.createDirectories(targetPath.getParent());
Files.copy(path, targetPath);
Expand Down
4 changes: 4 additions & 0 deletions plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ max-backups: 5
# Set to -1 to disable this limit.
max-dynamic-maps: 5

# Modules that are disabled by default. BattleArena comes pre-installed with
# multiple modules that can be disabled below if their behavior is not desired
disabled-modules: []

# Event configurations
events:
FFA:
Expand Down

0 comments on commit 61f6558

Please sign in to comment.