Remove a unnecessary abstraction, improve some documentation

This commit is contained in:
Vankka 2024-10-20 14:42:18 +03:00
parent 7d4755240f
commit fdaa215601
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
13 changed files with 98 additions and 153 deletions

View File

@ -40,7 +40,7 @@ import com.discordsrv.bukkit.requiredlinking.BukkitRequiredLinkingModule;
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;
@ -59,7 +59,7 @@ import java.lang.reflect.Field;
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;
@ -124,6 +124,11 @@ public class BukkitDiscordSRV extends ServerDiscordSRV<DiscordSRVBukkitBootstrap
return console;
}
@Override
public ServerType serverType() {
return ServerType.SERVER;
}
@Override
public @NotNull BukkitPlayerProvider playerProvider() {
return playerProvider;

View File

@ -22,7 +22,7 @@ import com.discordsrv.bungee.command.game.handler.BungeeCommandHandler;
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;
@ -38,7 +38,7 @@ import net.md_5.bungee.api.ProxyServer;
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;
@ -71,6 +71,11 @@ public class BungeeDiscordSRV extends ProxyDiscordSRV<DiscordSRVBungeeBootstrap,
return audiences;
}
@Override
public ServerType serverType() {
return ServerType.PROXY;
}
@Override
public StandardScheduler scheduler() {
return scheduler;

View File

@ -75,10 +75,7 @@ import com.discordsrv.common.feature.linking.impl.StorageLinker;
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;
@ -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;
@ -598,6 +596,14 @@ public abstract class AbstractDiscordSRV<
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");
@ -608,16 +614,49 @@ public abstract class AbstractDiscordSRV<
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)) {

View File

@ -72,6 +72,7 @@ public interface DiscordSRV extends DiscordSRVApi {
String WEBSITE = "https://discordsrv.vankka.dev";
// Platform
ServerType serverType();
IBootstrap bootstrap();
Logger platformLogger();
Path dataDirectory();
@ -172,10 +173,16 @@ public interface DiscordSRV extends DiscordSRVApi {
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
}
}

View File

@ -1,46 +0,0 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2024 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.discordsrv.common;
import com.discordsrv.common.abstraction.bootstrap.IBootstrap;
import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.messages.MessagesConfig;
import com.discordsrv.common.feature.messageforwarding.game.ServerSwitchMessageModule;
public abstract class ProxyDiscordSRV<
B extends IBootstrap,
C extends MainConfig,
CC extends ConnectionConfig,
MC extends MessagesConfig
> extends AbstractDiscordSRV<B, C, CC, MC> {
public ProxyDiscordSRV(B bootstrap) {
super(bootstrap);
}
@Override
protected void enable() throws Throwable {
super.enable();
registerModule(ServerSwitchMessageModule::new);
startedMessage();
}
}

View File

@ -1,92 +0,0 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2024 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.discordsrv.common;
import com.discordsrv.common.abstraction.bootstrap.IBootstrap;
import com.discordsrv.common.abstraction.player.provider.ServerPlayerProvider;
import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.messages.MessagesConfig;
import com.discordsrv.common.core.scheduler.ServerScheduler;
import com.discordsrv.common.feature.messageforwarding.game.AwardMessageModule;
import com.discordsrv.common.feature.messageforwarding.game.DeathMessageModule;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
public abstract class ServerDiscordSRV<
B extends IBootstrap,
C extends MainConfig,
CC extends ConnectionConfig,
MC extends MessagesConfig
> extends AbstractDiscordSRV<B, C, CC, MC> {
private boolean serverStarted = false;
public ServerDiscordSRV(B bootstrap) {
super(bootstrap);
}
@Override
public abstract ServerScheduler scheduler();
@Override
public abstract @NotNull ServerPlayerProvider<?, ?> playerProvider();
@Override
protected void enable() throws Throwable {
super.enable();
registerModule(AwardMessageModule::new);
registerModule(DeathMessageModule::new);
}
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();
startedMessage();
}
public boolean isServerStarted() {
return serverStarted;
}
}

View File

@ -23,6 +23,9 @@ import dev.vankka.dependencydownload.classpath.ClasspathAppender;
import java.nio.file.Path;
/**
* The plugin-/mod main "startup" class that the server sees.
*/
public interface IBootstrap {
Logger logger();

View File

@ -36,6 +36,9 @@ import java.util.concurrent.ExecutorService;
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;

View File

@ -21,7 +21,6 @@ package com.discordsrv.common.feature.channel;
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;
@ -64,9 +63,7 @@ public class TimedUpdaterModule extends AbstractModule<DiscordSRV> {
return false;
}
return super.isEnabled() && discordSRV.isReady() &&
(!(discordSRV instanceof ServerDiscordSRV)
|| ((ServerDiscordSRV<?, ?, ?, ?>) discordSRV).isServerStarted());
return super.isEnabled() && discordSRV.isReady() && discordSRV.isServerStarted();
}
@Override

View File

@ -29,6 +29,17 @@ import java.util.Objects;
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) {

View File

@ -20,6 +20,9 @@ package com.discordsrv.common.helper;
import java.util.function.Consumer;
/**
* Data access for unit testing.
*/
public final class TestHelper {
private static final ThreadLocal<Consumer<Throwable>> error = new ThreadLocal<>();

View File

@ -105,6 +105,11 @@ public class MockDiscordSRV extends AbstractDiscordSRV<IBootstrap, MainConfig, C
versionInfo = new VersionInfo("JUnit", "JUnit", "JUnit", "JUnit");
}
@Override
public ServerType serverType() {
return ServerType.SERVER;
}
@Override
public Path dataDirectory() {
if (this.path == null) {

View File

@ -18,7 +18,7 @@
package com.discordsrv.velocity;
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;
@ -41,7 +41,7 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarFile;
public class VelocityDiscordSRV extends ProxyDiscordSRV<DiscordSRVVelocityBootstrap, MainConfig, ConnectionConfig, MessagesConfig> {
public class VelocityDiscordSRV extends AbstractDiscordSRV<DiscordSRVVelocityBootstrap, MainConfig, ConnectionConfig, MessagesConfig> {
private final StandardScheduler scheduler;
private final VelocityConsole console;
@ -83,6 +83,11 @@ public class VelocityDiscordSRV extends ProxyDiscordSRV<DiscordSRVVelocityBootst
return bootstrap.proxyServer();
}
@Override
public ServerType serverType() {
return ServerType.PROXY;
}
@Override
public StandardScheduler scheduler() {
return scheduler;