Handle integrations loading later better

This commit is contained in:
Vankka 2024-12-04 21:18:01 +02:00
parent 2cbb2ede2f
commit 56c1baa830
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
23 changed files with 144 additions and 45 deletions

View File

@ -240,6 +240,7 @@ public class BukkitDiscordSRV extends AbstractDiscordSRV<DiscordSRVBukkitBootstr
protected void disable() {
super.disable();
pluginManager.disable();
audiences.close();
}

View File

@ -69,26 +69,9 @@ public class DiscordSRVBukkitBootstrap extends BukkitBootstrap implements IBoots
@Override
public void onEnable() {
lifecycleManager.loadAndEnable(() -> this.discordSRV = new BukkitDiscordSRV(this));
if (discordSRV == null) return;
boolean isFolia = false;
try {
Class.forName("io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler");
isFolia = true;
} catch (ClassNotFoundException ignored) {}
if (isFolia) {
if (discordSRV != null) {
discordSRV.invokeServerStarted();
}
return;
}
// Run a task on the main thread 1 tick later, so essentially when the server has finished booting
getPlugin().getServer().getScheduler().runTaskLater(getPlugin(), () -> {
if (discordSRV != null) {
discordSRV.invokeServerStarted();
}
}, 1L);
discordSRV.scheduler().runOnMainThreadLaterInTicks(() -> discordSRV.invokeServerStarted(), 1);
}
@Override

View File

@ -60,7 +60,7 @@ public class EssentialsXIntegration
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "Essentials";
}
@ -164,7 +164,7 @@ public class EssentialsXIntegration
@Override
public @NotNull String getOwnerName() {
return getIntegrationName();
return getIntegrationId();
}
@Override

View File

@ -50,7 +50,7 @@ public class PlaceholderAPIIntegration extends PluginIntegration<BukkitDiscordSR
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "PlaceholderAPI";
}

View File

@ -50,7 +50,7 @@ public class VaultIntegration extends PluginIntegration<BukkitDiscordSRV> implem
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "Vault";
}

View File

@ -50,7 +50,7 @@ public class ChattyChatIntegration extends PluginIntegration<BukkitDiscordSRV> i
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "Chatty";
}
@ -115,7 +115,7 @@ public class ChattyChatIntegration extends PluginIntegration<BukkitDiscordSRV> i
@Override
public @NotNull String getOwnerName() {
return getIntegrationName();
return getIntegrationId();
}
@Override

View File

@ -35,7 +35,7 @@ public class GriefPreventionChatIntegration extends PluginIntegration<BukkitDisc
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "GriefPrevention";
}
@ -52,7 +52,8 @@ public class GriefPreventionChatIntegration extends PluginIntegration<BukkitDisc
@Subscribe(priority = EventPriority.EARLY)
public void onGameChatMessageReceive(GameChatMessageReceiveEvent event) {
GriefPrevention griefPrevention = (GriefPrevention) discordSRV.server().getPluginManager().getPlugin(getIntegrationName());
GriefPrevention griefPrevention = (GriefPrevention) discordSRV.server().getPluginManager().getPlugin(
getIntegrationId());
if (griefPrevention == null) {
return;
}

View File

@ -55,7 +55,7 @@ public class LunaChatIntegration extends PluginIntegration<BukkitDiscordSRV> imp
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "LunaChat";
}
@ -134,7 +134,7 @@ public class LunaChatIntegration extends PluginIntegration<BukkitDiscordSRV> imp
@Override
public @NotNull String getOwnerName() {
return getIntegrationName();
return getIntegrationId();
}
@Override

View File

@ -55,7 +55,7 @@ public class McMMOChatIntegration extends PluginIntegration<BukkitDiscordSRV> im
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "mcMMO";
}
@ -130,7 +130,7 @@ public class McMMOChatIntegration extends PluginIntegration<BukkitDiscordSRV> im
@Override
public @NotNull String getOwnerName() {
return getIntegrationName();
return getIntegrationId();
}
@Override

View File

@ -51,7 +51,7 @@ public class TownyChatIntegration extends PluginIntegration<BukkitDiscordSRV> im
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "TownyChat";
}
@ -125,7 +125,7 @@ public class TownyChatIntegration extends PluginIntegration<BukkitDiscordSRV> im
@Override
public @NotNull String getOwnerName() {
return getIntegrationName();
return getIntegrationId();
}
@Override

View File

@ -54,7 +54,7 @@ public class VentureChatIntegration extends PluginIntegration<BukkitDiscordSRV>
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "VentureChat";
}
@ -128,7 +128,7 @@ public class VentureChatIntegration extends PluginIntegration<BukkitDiscordSRV>
@Override
public @NotNull String getOwnerName() {
return getIntegrationName();
return getIntegrationId();
}
@Override

View File

@ -26,6 +26,7 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
@ -53,6 +54,11 @@ public class BukkitPlayerProvider extends ServerPlayerProvider<BukkitPlayer, Buk
}
}
@Override
public void unsubscribe() {
HandlerList.unregisterAll(this);
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerLogin(PlayerLoginEvent event) {
if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) {

View File

@ -21,18 +21,29 @@ package com.discordsrv.bukkit.plugin;
import com.discordsrv.bukkit.BukkitDiscordSRV;
import com.discordsrv.common.abstraction.plugin.Plugin;
import com.discordsrv.common.abstraction.plugin.PluginManager;
import com.discordsrv.common.events.integration.IntegrationLifecycleEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.PluginDescriptionFile;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class BukkitPluginManager implements PluginManager {
public class BukkitPluginManager implements PluginManager, Listener {
private final BukkitDiscordSRV discordSRV;
public BukkitPluginManager(BukkitDiscordSRV discordSRV) {
this.discordSRV = discordSRV;
discordSRV.server().getPluginManager().registerEvents(this, discordSRV.plugin());
}
public void disable() {
HandlerList.unregisterAll(this);
}
@Override
@ -49,4 +60,20 @@ public class BukkitPluginManager implements PluginManager {
})
.collect(Collectors.toList());
}
@EventHandler
public void onPluginEnable(PluginEnableEvent event) {
discordSRV.eventBus().publish(new IntegrationLifecycleEvent(
event.getPlugin().getName(),
IntegrationLifecycleEvent.Type.ENABLE
));
}
@EventHandler
public void onPluginDisable(PluginDisableEvent event) {
discordSRV.eventBus().publish(new IntegrationLifecycleEvent(
event.getPlugin().getName(),
IntegrationLifecycleEvent.Type.DISABLE
));
}
}

View File

@ -42,6 +42,11 @@ public class BungeePlayerProvider extends AbstractPlayerProvider<BungeePlayer, B
}
}
@Override
public void unsubscribe() {
discordSRV.proxy().getPluginManager().unregisterListener(this);
}
@EventHandler(priority = Byte.MIN_VALUE) // Runs first
public void onPostLogin(PostLoginEvent event) {
addPlayer(event.getPlayer(), false);

View File

@ -713,6 +713,10 @@ public abstract class AbstractDiscordSRV<
return;
}
this.status.set(Status.SHUTTING_DOWN);
// Unregister PlayerProvider listeners
playerProvider().unsubscribe();
eventBus().publish(new DiscordSRVShuttingDownEvent());
eventBus().shutdown();
try {

View File

@ -63,6 +63,7 @@ public abstract class AbstractPlayerProvider<T extends IPlayer, DT extends Disco
}
public abstract void subscribe();
public abstract void unsubscribe();
protected void addPlayer(UUID uuid, T player, boolean initial) {
this.players.put(uuid, player);

View File

@ -33,7 +33,9 @@ import com.discordsrv.common.core.logging.Logger;
import com.discordsrv.common.core.logging.NamedLogger;
import com.discordsrv.common.core.module.type.AbstractModule;
import com.discordsrv.common.core.module.type.ModuleDelegate;
import com.discordsrv.common.core.module.type.PluginIntegration;
import com.discordsrv.common.discord.connection.jda.JDAConnectionManager;
import com.discordsrv.common.events.integration.IntegrationLifecycleEvent;
import com.discordsrv.common.feature.debug.DebugGenerateEvent;
import com.discordsrv.common.feature.debug.file.TextDebugFile;
import com.discordsrv.common.util.function.CheckedFunction;
@ -211,6 +213,18 @@ public class ModuleManager {
reload();
}
@Subscribe
public void onIntegrationLifecycle(IntegrationLifecycleEvent event) {
String integrationIdentifier = event.integrationIdentifier();
for (Module module : modules) {
AbstractModule<?> abstractModule = getAbstract(module);
if (abstractModule instanceof PluginIntegration
&& ((PluginIntegration<?>) abstractModule).getIntegrationId().equals(integrationIdentifier)) {
enableOrDisableAsNeeded(abstractModule, discordSRV.isReady(), true);
}
}
}
public List<DiscordSRVApi.ReloadResult> reload() {
return reloadAndEnableModules(true);
}
@ -240,7 +254,7 @@ public class ModuleManager {
return results;
}
private List<DiscordSRVApi.ReloadResult> enableOrDisableAsNeeded(AbstractModule<?> module, boolean isReady, boolean reload) {
private List<DiscordSRVApi.ReloadResult> enableOrDisableAsNeeded(AbstractModule<?> module, boolean isReady, boolean mayReload) {
boolean canBeEnabled = isReady || module.canEnableBeforeReady();
if (!canBeEnabled) {
return Collections.emptyList();
@ -278,7 +292,7 @@ public class ModuleManager {
List<DiscordSRVApi.ReloadResult> results = enable(module);
if (results != null) {
reloadResults.addAll(results);
} else if (reload) {
} else if (mayReload) {
reloadResults.addAll(reload(module));
}
}

View File

@ -38,16 +38,16 @@ public abstract class PluginIntegration<DT extends DiscordSRV> extends AbstractM
* @return the id (when available) or name of the plugin or mod
*/
@NotNull
public abstract String getIntegrationName();
public abstract String getIntegrationId();
@Override
@MustBeInvokedByOverriders
public boolean isEnabled() {
String integrationName = getIntegrationName();
if (discordSRV.config().integrations.disabledIntegrations.contains(integrationName)) {
String integrationId = getIntegrationId();
if (discordSRV.config().integrations.disabledIntegrations.contains(integrationId)) {
return false;
}
if (!discordSRV.pluginManager().isPluginEnabled(integrationName)) {
if (!discordSRV.pluginManager().isPluginEnabled(integrationId)) {
return false;
}
return super.isEnabled();

View File

@ -0,0 +1,46 @@
/*
* 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.events.integration;
import com.discordsrv.api.events.Event;
public class IntegrationLifecycleEvent implements Event {
private final String integrationIdentifier;
private final Type type;
public IntegrationLifecycleEvent(String integrationIdentifier, Type type) {
this.integrationIdentifier = integrationIdentifier;
this.type = type;
}
public String integrationIdentifier() {
return integrationIdentifier;
}
public Type getType() {
return type;
}
public enum Type {
ENABLE,
DISABLE,
RELOAD
}
}

View File

@ -66,7 +66,7 @@ public class LuckPermsIntegration extends PluginIntegration<DiscordSRV> implemen
}
@Override
public @NotNull String getIntegrationName() {
public @NotNull String getIntegrationId() {
return "LuckPerms";
}

View File

@ -185,6 +185,11 @@ public class MockDiscordSRV extends AbstractDiscordSRV<IBootstrap, MainConfig, C
public void subscribe() {
playerProviderSubscribed = true;
}
@Override
public void unsubscribe() {
playerProviderSubscribed = false;
}
};
}

View File

@ -42,6 +42,11 @@ public class VelocityPlayerProvider extends AbstractPlayerProvider<VelocityPlaye
}
}
@Override
public void unsubscribe() {
discordSRV.proxy().getEventManager().unregisterListener(discordSRV.plugin(), this);
}
@Subscribe(order = PostOrder.FIRST)
public void onPostLogin(PostLoginEvent event) {
addPlayer(event.getPlayer(), false);

View File

@ -24,6 +24,7 @@ import com.discordsrv.velocity.VelocityDiscordSRV;
import com.velocitypowered.api.plugin.PluginDescription;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
public class VelocityPluginManager implements PluginManager {
@ -35,8 +36,8 @@ public class VelocityPluginManager implements PluginManager {
}
@Override
public boolean isPluginEnabled(String pluginName) {
return discordSRV.proxy().getPluginManager().isLoaded(pluginName);
public boolean isPluginEnabled(String pluginIdentifier) {
return discordSRV.proxy().getPluginManager().isLoaded(pluginIdentifier.toLowerCase(Locale.ROOT));
}
@Override