Skip to content

Commit

Permalink
Remove a unnecessary abstraction, improve some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vankka committed Oct 20, 2024
1 parent 7d47552 commit fdaa215
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import com.discordsrv.bukkit.scheduler.BukkitScheduler;
import com.discordsrv.bukkit.scheduler.FoliaScheduler;
import com.discordsrv.bukkit.scheduler.IBukkitScheduler;
import com.discordsrv.common.ServerDiscordSRV;
import com.discordsrv.common.AbstractDiscordSRV;
import com.discordsrv.common.abstraction.plugin.PluginManager;
import com.discordsrv.common.command.game.handler.ICommandHandler;
import com.discordsrv.common.config.configurate.manager.ConnectionConfigManager;
Expand All @@ -59,7 +59,7 @@
import java.util.List;
import java.util.Set;

public class BukkitDiscordSRV extends ServerDiscordSRV<DiscordSRVBukkitBootstrap, BukkitConfig, BukkitConnectionConfig, MessagesConfig> {
public class BukkitDiscordSRV extends AbstractDiscordSRV<DiscordSRVBukkitBootstrap, BukkitConfig, BukkitConnectionConfig, MessagesConfig> {

private BukkitAudiences audiences;
private BukkitTranslationLoader translationLoader;
Expand Down Expand Up @@ -124,6 +124,11 @@ public BukkitConsole console() {
return console;
}

@Override
public ServerType serverType() {
return ServerType.SERVER;
}

@Override
public @NotNull BukkitPlayerProvider playerProvider() {
return playerProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.discordsrv.bungee.console.BungeeConsole;
import com.discordsrv.bungee.player.BungeePlayerProvider;
import com.discordsrv.bungee.plugin.BungeePluginManager;
import com.discordsrv.common.ProxyDiscordSRV;
import com.discordsrv.common.AbstractDiscordSRV;
import com.discordsrv.common.abstraction.plugin.PluginManager;
import com.discordsrv.common.command.game.handler.ICommandHandler;
import com.discordsrv.common.config.configurate.manager.ConnectionConfigManager;
Expand All @@ -38,7 +38,7 @@
import net.md_5.bungee.api.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

public class BungeeDiscordSRV extends ProxyDiscordSRV<DiscordSRVBungeeBootstrap, MainConfig, ConnectionConfig, MessagesConfig> {
public class BungeeDiscordSRV extends AbstractDiscordSRV<DiscordSRVBungeeBootstrap, MainConfig, ConnectionConfig, MessagesConfig> {

private BungeeAudiences audiences;

Expand Down Expand Up @@ -71,6 +71,11 @@ public BungeeAudiences audiences() {
return audiences;
}

@Override
public ServerType serverType() {
return ServerType.PROXY;
}

@Override
public StandardScheduler scheduler() {
return scheduler;
Expand Down
49 changes: 44 additions & 5 deletions common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@
import com.discordsrv.common.feature.mention.MentionGameRenderingModule;
import com.discordsrv.common.feature.messageforwarding.discord.DiscordChatMessageModule;
import com.discordsrv.common.feature.messageforwarding.discord.DiscordMessageMirroringModule;
import com.discordsrv.common.feature.messageforwarding.game.JoinMessageModule;
import com.discordsrv.common.feature.messageforwarding.game.LeaveMessageModule;
import com.discordsrv.common.feature.messageforwarding.game.StartMessageModule;
import com.discordsrv.common.feature.messageforwarding.game.StopMessageModule;
import com.discordsrv.common.feature.messageforwarding.game.*;
import com.discordsrv.common.feature.mention.MentionCachingModule;
import com.discordsrv.common.feature.profile.ProfileManager;
import com.discordsrv.common.feature.update.UpdateChecker;
Expand Down Expand Up @@ -133,6 +130,7 @@ public abstract class AbstractDiscordSRV<

private final AtomicReference<Status> status = new AtomicReference<>(Status.INITIALIZED);
private final AtomicReference<Boolean> beenReady = new AtomicReference<>(false);
private boolean serverStarted = false;

// DiscordSRVApi
private EventBusImpl eventBus;
Expand Down Expand Up @@ -598,6 +596,14 @@ protected void enable() throws Throwable {
registerModule(MentionGameRenderingModule::new);
registerModule(CustomCommandModule::new);

if (serverType() == ServerType.PROXY) {
registerModule(ServerSwitchMessageModule::new);
}
if (serverType() == ServerType.SERVER) {
registerModule(AwardMessageModule::new);
registerModule(DeathMessageModule::new);
}

// Integrations
registerIntegration("com.discordsrv.common.integration.LuckPermsIntegration");

Expand All @@ -608,16 +614,49 @@ protected void enable() throws Throwable {
throw e.getCause();
}

if (serverType() == ServerType.PROXY) {
invokeServerStarted().get();
}

// Register PlayerProvider listeners
playerProvider().subscribe();
}

protected final void startedMessage() {
public final CompletableFuture<Void> invokeServerStarted() {
return scheduler().supply(() -> {
if (status().isShutdown()) {
// Already shutdown/shutting down, don't bother
return null;
}
try {
this.serverStarted();
} catch (Throwable t) {
if (status().isShutdown() && t instanceof NoClassDefFoundError) {
// Already shutdown, ignore errors for classes that already got unloaded
return null;
}
setStatus(Status.FAILED_TO_START);
disable();
logger().error("Failed to start", t);
}
return null;
});
}

@MustBeInvokedByOverriders
protected void serverStarted() {
serverStarted = true;
moduleManager().enableModules();

registerModule(StartMessageModule::new);
registerModule(StopMessageModule::new);
Optional.ofNullable(getModule(PresenceUpdaterModule.class)).ifPresent(PresenceUpdaterModule::serverStarted);
}

public boolean isServerStarted() {
return serverStarted;
}

private StorageType getStorageType() {
String backend = connectionConfig().storage.backend;
switch (backend.toLowerCase(Locale.ROOT)) {
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/com/discordsrv/common/DiscordSRV.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public interface DiscordSRV extends DiscordSRVApi {
String WEBSITE = "https://discordsrv.vankka.dev";

// Platform
ServerType serverType();
IBootstrap bootstrap();
Logger platformLogger();
Path dataDirectory();
Expand Down Expand Up @@ -172,10 +173,16 @@ default <K, V> Caffeine<K, V> caffeineBuilder() {
void runEnable();
List<ReloadResult> runReload(Set<ReloadFlag> flags, boolean silent);
CompletableFuture<Void> invokeDisable();
boolean isServerStarted();

@Nullable
default GameCommandExecutionHelper executeHelper() {
return null;
}

enum ServerType {
SERVER,
PROXY
}

}
46 changes: 0 additions & 46 deletions common/src/main/java/com/discordsrv/common/ProxyDiscordSRV.java

This file was deleted.

92 changes: 0 additions & 92 deletions common/src/main/java/com/discordsrv/common/ServerDiscordSRV.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

import java.nio.file.Path;

/**
* The plugin-/mod main "startup" class that the server sees.
*/
public interface IBootstrap {

Logger logger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import java.util.concurrent.Executors;
import java.util.function.Supplier;

/**
* A wrapper for loading in the initial dependencies, enabling and disabling {@link DiscordSRV}.
*/
public class LifecycleManager {

private final Logger logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.ServerDiscordSRV;
import com.discordsrv.common.config.main.TimedUpdaterConfig;
import com.discordsrv.common.core.logging.NamedLogger;
import com.discordsrv.common.core.module.type.AbstractModule;
Expand Down Expand Up @@ -64,9 +63,7 @@ public boolean isEnabled() {
return false;
}

return super.isEnabled() && discordSRV.isReady() &&
(!(discordSRV instanceof ServerDiscordSRV)
|| ((ServerDiscordSRV<?, ?, ?, ?>) discordSRV).isServerStarted());
return super.isEnabled() && discordSRV.isReady() && discordSRV.isServerStarted();
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions common/src/main/java/com/discordsrv/common/helper/Someone.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
* Represents a subject that is
* - a Minecraft player, or
* - a Discord user, or
* - a Minecraft player and their linked Discord user
*
* @see #withLinkedAccounts(DiscordSRV)
* @see #withUserId(DiscordSRV)
* @see #withPlayerUUID(DiscordSRV)
* @see #profile(DiscordSRV)
*/
public class Someone {

public static Someone.Resolved of(@NotNull DiscordSRVPlayer player, @NotNull DiscordUser user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import java.util.function.Consumer;

/**
* Data access for unit testing.
*/
public final class TestHelper {

private static final ThreadLocal<Consumer<Throwable>> error = new ThreadLocal<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public Path dataDirectory() {
versionInfo = new VersionInfo("JUnit", "JUnit", "JUnit", "JUnit");
}

@Override
public ServerType serverType() {
return ServerType.SERVER;
}

@Override
public Path dataDirectory() {
if (this.path == null) {
Expand Down
Loading

0 comments on commit fdaa215

Please sign in to comment.