Skip to content

Commit

Permalink
Fix /ba reload not reloading some modules properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Dec 14, 2024
1 parent 8fee4cc commit 02a507e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.battleplugins.arena.module.classes;

import org.battleplugins.arena.BattleArena;
import org.battleplugins.arena.config.ArenaConfigParser;
import org.battleplugins.arena.config.ParseException;
import org.battleplugins.arena.event.BattleArenaPostInitializeEvent;
import org.battleplugins.arena.event.BattleArenaReloadedEvent;
import org.battleplugins.arena.event.action.EventActionType;
import org.battleplugins.arena.event.arena.ArenaCreateExecutorEvent;
import org.battleplugins.arena.module.ArenaModule;
Expand Down Expand Up @@ -35,30 +37,44 @@ public class Classes implements ArenaModuleInitializer {

@EventHandler
public void onPostInitialize(BattleArenaPostInitializeEvent event) {
ArenaModuleContainer<Classes> container = event.getBattleArena()
this.onLoad(event.getBattleArena(), true);
}

@EventHandler
public void onReloaded(BattleArenaReloadedEvent event) {
this.onLoad(event.getBattleArena(), false);
}

private void onLoad(BattleArena plugin, boolean initial) {
ArenaModuleContainer<Classes> container = plugin
.<Classes>module(ID)
.orElseThrow();

Path dataFolder = event.getBattleArena().getDataFolder().toPath();
Path dataFolder = plugin.getDataFolder().toPath();
Path classesPath = dataFolder.resolve("classes.yml");
if (Files.notExists(classesPath)) {
InputStream inputStream = container.getResource("classes.yml");
try {
Files.copy(inputStream, classesPath);
} catch (Exception e) {
event.getBattleArena().error("Failed to copy classes.yml to data folder!", e);
container.disable("Failed to copy classes.yml to data folder!");
plugin.error("Failed to copy classes.yml to data folder!", e);

if (initial) {
container.disable("Failed to copy classes.yml to data folder!");
}
return;
}
}

Configuration classesConfig = YamlConfiguration.loadConfiguration(classesPath.toFile());
try {
this.classes = ArenaConfigParser.newInstance(classesPath, ClassesConfig.class, classesConfig, event.getBattleArena());
this.classes = ArenaConfigParser.newInstance(classesPath, ClassesConfig.class, classesConfig, plugin);
} catch (ParseException e) {
ParseException.handle(e);

container.disable("Failed to parse classes.yml!");
if (initial) {
container.disable("Failed to parse classes.yml!");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.battleplugins.arena.module.scoreboard;

import org.battleplugins.arena.BattleArena;
import org.battleplugins.arena.config.ArenaConfigParser;
import org.battleplugins.arena.config.ParseException;
import org.battleplugins.arena.event.ArenaListener;
import org.battleplugins.arena.event.BattleArenaPostInitializeEvent;
import org.battleplugins.arena.event.BattleArenaReloadedEvent;
import org.battleplugins.arena.event.action.EventActionType;
import org.battleplugins.arena.event.arena.ArenaInitializeEvent;
import org.battleplugins.arena.module.ArenaModule;
Expand Down Expand Up @@ -38,19 +40,31 @@ public Scoreboards() {

@EventHandler
public void onPostInitialize(BattleArenaPostInitializeEvent event) {
ArenaModuleContainer<Scoreboards> container = event.getBattleArena()
this.onLoad(event.getBattleArena(), true);
}

@EventHandler
public void onReloaded(BattleArenaReloadedEvent event) {
this.onLoad(event.getBattleArena(), false);
}

private void onLoad(BattleArena plugin, boolean initial) {
ArenaModuleContainer<Scoreboards> container = plugin
.<Scoreboards>module(ID)
.orElseThrow();

Path dataFolder = event.getBattleArena().getDataFolder().toPath();
Path dataFolder = plugin.getDataFolder().toPath();
Path scoreboardsPath = dataFolder.resolve("scoreboards.yml");
if (Files.notExists(scoreboardsPath)) {
InputStream inputStream = container.getResource("scoreboards.yml");
try {
Files.copy(inputStream, scoreboardsPath);
} catch (Exception e) {
event.getBattleArena().error("Failed to copy scoreboards.yml to data folder!", e);
container.disable("Failed to copy scoreboards.yml to data folder!");
plugin.error("Failed to copy scoreboards.yml to data folder!", e);

if (initial) {
container.disable("Failed to copy scoreboards.yml to data folder!");
}
return;
}
}
Expand All @@ -61,7 +75,9 @@ public void onPostInitialize(BattleArenaPostInitializeEvent event) {
} catch (ParseException e) {
ParseException.handle(e);

container.disable("Failed to parse scoreboards.yml!");
if (initial) {
container.disable("Failed to parse scoreboards.yml!");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.battleplugins.arena.config.ArenaConfigParser;
import org.battleplugins.arena.config.ParseException;
import org.battleplugins.arena.event.BattleArenaPostInitializeEvent;
import org.battleplugins.arena.event.BattleArenaReloadedEvent;
import org.battleplugins.arena.module.ArenaModule;
import org.battleplugins.arena.module.ArenaModuleContainer;
import org.battleplugins.arena.module.ArenaModuleInitializer;
Expand All @@ -13,7 +14,6 @@
import org.bukkit.event.EventHandler;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -38,19 +38,30 @@ public Tournaments() {

@EventHandler
public void onPostInitialize(BattleArenaPostInitializeEvent event) {
ArenaModuleContainer<Tournaments> container = event.getBattleArena()
this.onLoad(event.getBattleArena(), true);
}

@EventHandler
public void onReloaded(BattleArenaReloadedEvent event) {
this.onLoad(event.getBattleArena(), false);
}

private void onLoad(BattleArena plugin, boolean initial) {
ArenaModuleContainer<Tournaments> container = plugin
.<Tournaments>module(ID)
.orElseThrow();

Path dataFolder = event.getBattleArena().getDataFolder().toPath();
Path dataFolder = plugin.getDataFolder().toPath();
Path tournamentPath = dataFolder.resolve("tournament-config.yml");
if (Files.notExists(tournamentPath)) {
InputStream inputStream = container.getResource("tournament-config.yml");
try {
Files.copy(inputStream, tournamentPath);
} catch (Exception e) {
event.getBattleArena().error("Failed to copy tournament-config.yml to data folder!", e);
container.disable("Failed to copy tournament-config.yml to data folder!");
plugin.error("Failed to copy tournament-config.yml to data folder!", e);
if (initial) {
container.disable("Failed to copy tournament-config.yml to data folder!");
}
return;
}
}
Expand All @@ -61,7 +72,9 @@ public void onPostInitialize(BattleArenaPostInitializeEvent event) {
} catch (ParseException e) {
ParseException.handle(e);

container.disable("Failed to parse tournament-config.yml!");
if (initial) {
container.disable("Failed to parse tournament-config.yml!");
}
}
}

Expand Down

0 comments on commit 02a507e

Please sign in to comment.