Skip to content

Commit

Permalink
feat: introduce Extension#afterServerStarted method which will be c…
Browse files Browse the repository at this point in the history
…alled after the server is started
  • Loading branch information
smartcmd committed Jan 6, 2025
1 parent 5a38983 commit c526ba2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Unless otherwise specified, any version comparison below is the comparison of se
- Introduced [sentry](https://www.sentry.io) to capture exception and upload them to sentry server automatically, which
helps us to track and fix bug more efficiently. Sentry is only enabled in non-dev version.
- Server version will also be uploaded to bStats now.
- Introduced `Extension#afterServerStarted` method which will be called after the server is started.

### Changed

Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/org/allaymc/server/Allay.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
public final class Allay {

public static final DynamicURLClassLoader EXTRA_RESOURCE_CLASS_LOADER = new DynamicURLClassLoader(Allay.class.getClassLoader());
private static final ExtensionManager EXTENSION_MANAGER = new ExtensionManager(Path.of("extensions"));
public static final ExtensionManager EXTENSION_MANAGER = new ExtensionManager(Path.of("extensions"));

public static Dashboard DASHBOARD;

Expand Down
3 changes: 2 additions & 1 deletion server/src/main/java/org/allaymc/server/AllayServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ public void start(long initialTime) {

Metrics.AllayMetrics.startMetrics();
if (SETTINGS.genericSettings().enableGui()) {
Allay.DASHBOARD.serverStarted();
Allay.DASHBOARD.afterServerStarted();
}
Allay.EXTENSION_MANAGER.afterServerStarted();
this.gameLoop.startLoop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
*/
public abstract class Extension {
public abstract void main(String[] args);

public void afterServerStarted() {
// noop
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class ExtensionManager {
private static final PathMatcher PATH_MATCHER = FileSystems.getDefault().getPathMatcher("glob:**.jar");

private final Path source;
private final List<Extension> extensionInstances = new ArrayList<>();

public ExtensionManager(Path source) {
this.source = source;
Expand All @@ -35,16 +36,19 @@ public void loadExtensions(String[] args) {
Files.createDirectory(source);
}

List<Runnable> entrances = new ArrayList<>();
try (var stream = Files.walk(source)) {
stream.filter(path -> PATH_MATCHER.matches(path) && Files.isRegularFile(path))
.forEach(extensionPath -> loadExtension(extensionPath, args, entrances));
.forEach(extensionPath -> loadExtension(extensionPath, args));
}

entrances.forEach(Runnable::run);
this.extensionInstances.forEach(extension -> extension.main(args));
}

private void loadExtension(Path extensionPath, String[] args, List<Runnable> entrances) {
public void afterServerStarted() {
this.extensionInstances.forEach(Extension::afterServerStarted);
}

private void loadExtension(Path extensionPath, String[] args) {
log.info(I18n.get().tr(TrKeys.A_EXTENSION_LOADING, extensionPath));
Allay.EXTRA_RESOURCE_CLASS_LOADER.addJar(extensionPath);

Expand All @@ -62,11 +66,12 @@ private void loadExtension(Path extensionPath, String[] args, List<Runnable> ent
Extension extensionInstance;
try {
extensionInstance = (Extension) mainClass.getConstructor().newInstance();
extensionInstance.main(args);
} catch (Exception e) {
throw new ExtensionException(I18n.get().tr(TrKeys.A_EXTENSION_CONSTRUCT_INSTANCE_ERROR, extensionPath, e));
}

entrances.add(() -> extensionInstance.main(args));
extensionInstances.add(extensionInstance);
}

@SneakyThrows
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/org/allaymc/server/gui/Dashboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void write(byte @NonNull [] b) throws IOException {
};
}

public void serverStarted() {
public void afterServerStarted() {
Runnable guiUpdateTask = () -> {
// Update ram graph
final long freeMemory = Runtime.getRuntime().freeMemory();
Expand Down

0 comments on commit c526ba2

Please sign in to comment.