Add OnlineMode & PluginManager

This commit is contained in:
Vankka 2022-01-25 18:25:21 +02:00
parent 9cfaae7464
commit 6f638ffb33
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
14 changed files with 449 additions and 7 deletions

View File

@ -29,11 +29,14 @@ import com.discordsrv.bukkit.listener.BukkitChatListener;
import com.discordsrv.bukkit.listener.BukkitDeathListener;
import com.discordsrv.bukkit.listener.BukkitStatusMessageListener;
import com.discordsrv.bukkit.player.BukkitPlayerProvider;
import com.discordsrv.bukkit.plugin.BukkitPluginManager;
import com.discordsrv.bukkit.scheduler.BukkitScheduler;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.debug.data.OnlineMode;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.messageforwarding.game.MinecraftToDiscordChatModule;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.common.server.ServerDiscordSRV;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Server;
@ -41,6 +44,7 @@ import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.nio.file.Path;
public class BukkitDiscordSRV extends ServerDiscordSRV<BukkitConfig, BukkitConnectionConfig> {
@ -53,6 +57,7 @@ public class BukkitDiscordSRV extends ServerDiscordSRV<BukkitConfig, BukkitConne
private final BukkitScheduler scheduler;
private final BukkitConsole console;
private final BukkitPlayerProvider playerProvider;
private final BukkitPluginManager pluginManager;
private final BukkitConnectionConfigManager connectionConfigManager;
private final BukkitConfigManager configManager;
@ -65,6 +70,7 @@ public class BukkitDiscordSRV extends ServerDiscordSRV<BukkitConfig, BukkitConne
this.scheduler = new BukkitScheduler(this);
this.console = new BukkitConsole(this);
this.playerProvider = new BukkitPlayerProvider(this);
this.pluginManager = new BukkitPluginManager(this);
// Config
this.connectionConfigManager = new BukkitConnectionConfigManager(this);
@ -115,6 +121,35 @@ public class BukkitDiscordSRV extends ServerDiscordSRV<BukkitConfig, BukkitConne
return bootstrap.getPlugin().getDescription().getVersion();
}
@Override
public PluginManager pluginManager() {
return pluginManager;
}
@Override
public OnlineMode onlineMode() {
try {
Class<?> paperConfig = Class.forName("com.destroystokyo.paper.PaperConfig");
Field velocitySupport = paperConfig.getField("velocitySupport");
Field velocityOnlineMode = paperConfig.getField("velocityOnlineMode");
if (velocitySupport.getBoolean(null) && velocityOnlineMode.getBoolean(null)) {
return OnlineMode.VELOCITY;
}
} catch (Throwable ignored) {}
try {
Class<?> spigotConfig = Class.forName("org.spigotmc.SpigotConfig");
Field bungee = spigotConfig.getField("bungee");
if (bungee.getBoolean(null)) {
return OnlineMode.BUNGEE;
}
} catch (Throwable ignored) {}
return OnlineMode.of(server().getOnlineMode());
}
@Override
public ConnectionConfigManager<BukkitConnectionConfig> connectionConfigManager() {
return connectionConfigManager;

View File

@ -0,0 +1,52 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2022 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.bukkit.plugin;
import com.discordsrv.bukkit.BukkitDiscordSRV;
import com.discordsrv.common.plugin.Plugin;
import com.discordsrv.common.plugin.PluginManager;
import org.bukkit.plugin.PluginDescriptionFile;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class BukkitPluginManager implements PluginManager {
private final BukkitDiscordSRV discordSRV;
public BukkitPluginManager(BukkitDiscordSRV discordSRV) {
this.discordSRV = discordSRV;
}
@Override
public boolean isPluginEnabled(String pluginName) {
return discordSRV.server().getPluginManager().isPluginEnabled(pluginName);
}
@Override
public List<Plugin> getPlugins() {
return Arrays.stream(discordSRV.server().getPluginManager().getPlugins())
.map(plugin -> {
PluginDescriptionFile description = plugin.getDescription();
return new Plugin(plugin.getName(), description.getVersion(), description.getAuthors());
})
.collect(Collectors.toList());
}
}

View File

@ -20,11 +20,14 @@ package com.discordsrv.bungee;
import com.discordsrv.bungee.console.BungeeConsole;
import com.discordsrv.bungee.player.BungeePlayerProvider;
import com.discordsrv.bungee.plugin.BungeePluginManager;
import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.debug.data.OnlineMode;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.common.scheduler.StandardScheduler;
import com.discordsrv.proxy.ProxyDiscordSRV;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
@ -44,6 +47,7 @@ public class BungeeDiscordSRV extends ProxyDiscordSRV<MainConfig, ConnectionConf
private final StandardScheduler scheduler;
private final BungeeConsole console;
private final BungeePlayerProvider playerProvider;
private final BungeePluginManager pluginManager;
public BungeeDiscordSRV(DiscordSRVBungeeBootstrap bootstrap, Logger logger) {
this.bootstrap = bootstrap;
@ -53,6 +57,7 @@ public class BungeeDiscordSRV extends ProxyDiscordSRV<MainConfig, ConnectionConf
this.scheduler = new StandardScheduler(this);
this.console = new BungeeConsole(this);
this.playerProvider = new BungeePlayerProvider(this);
this.pluginManager = new BungeePluginManager(this);
load();
}
@ -99,6 +104,16 @@ public class BungeeDiscordSRV extends ProxyDiscordSRV<MainConfig, ConnectionConf
return bootstrap.getPlugin().getDescription().getVersion();
}
@Override
public PluginManager pluginManager() {
return pluginManager;
}
@Override
public OnlineMode onlineMode() {
return OnlineMode.of(proxy().getConfig().isOnlineMode());
}
@Override
public ConnectionConfigManager<ConnectionConfig> connectionConfigManager() {
return null;

View File

@ -0,0 +1,56 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2022 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.bungee.plugin;
import com.discordsrv.bungee.BungeeDiscordSRV;
import com.discordsrv.common.plugin.Plugin;
import com.discordsrv.common.plugin.PluginManager;
import net.md_5.bungee.api.plugin.PluginDescription;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class BungeePluginManager implements PluginManager {
private final BungeeDiscordSRV discordSRV;
public BungeePluginManager(BungeeDiscordSRV discordSRV) {
this.discordSRV = discordSRV;
}
@Override
public boolean isPluginEnabled(String pluginName) {
return discordSRV.proxy().getPluginManager().getPlugin(pluginName) != null;
}
@Override
public List<Plugin> getPlugins() {
return discordSRV.proxy().getPluginManager().getPlugins().stream()
.map(plugin -> {
PluginDescription description = plugin.getDescription();
return new Plugin(
description.getName(),
description.getVersion(),
Collections.singletonList(description.getAuthor())
);
})
.collect(Collectors.toList());
}
}

View File

@ -26,6 +26,7 @@ import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.console.Console;
import com.discordsrv.common.debug.data.OnlineMode;
import com.discordsrv.common.discord.api.DiscordAPIImpl;
import com.discordsrv.common.discord.connection.DiscordConnectionManager;
import com.discordsrv.common.logging.impl.DiscordSRVLogger;
@ -33,6 +34,7 @@ import com.discordsrv.common.module.type.AbstractModule;
import com.discordsrv.api.module.type.Module;
import com.discordsrv.common.placeholder.PlaceholderServiceImpl;
import com.discordsrv.common.player.provider.AbstractPlayerProvider;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.common.scheduler.Scheduler;
import com.discordsrv.common.logging.Logger;
import com.github.benmanes.caffeine.cache.Caffeine;
@ -53,6 +55,8 @@ public interface DiscordSRV extends DiscordSRVApi {
Scheduler scheduler();
Console console();
String version();
PluginManager pluginManager();
OnlineMode onlineMode();
// DiscordSRVApi
@Override

View File

@ -0,0 +1,32 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2022 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.debug.data;
public enum OnlineMode {
ONLINE,
OFFLINE,
BUNGEE,
VELOCITY;
public static OnlineMode of(boolean onlineMode) {
return onlineMode ? OnlineMode.ONLINE : OnlineMode.OFFLINE;
}
}

View File

@ -0,0 +1,56 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2022 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.plugin;
import java.util.List;
public class Plugin {
private final String identifier;
private final String name;
private final String version;
private final List<String> authors;
public Plugin(String name, String version, List<String> authors) {
this(name, name, version, authors);
}
public Plugin(String identifier, String name, String version, List<String> authors) {
this.identifier = identifier;
this.name = name;
this.version = version;
this.authors = authors;
}
public String identifier() {
return identifier;
}
public String name() {
return name;
}
public String version() {
return version;
}
public List<String> authors() {
return authors;
}
}

View File

@ -0,0 +1,28 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2022 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.plugin;
import java.util.List;
public interface PluginManager {
boolean isPluginEnabled(String pluginIdentifier);
List<Plugin> getPlugins();
}

View File

@ -23,9 +23,11 @@ import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.console.Console;
import com.discordsrv.common.debug.data.OnlineMode;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.logging.backend.impl.JavaLoggerImpl;
import com.discordsrv.common.player.provider.AbstractPlayerProvider;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.common.scheduler.Scheduler;
import com.discordsrv.common.scheduler.StandardScheduler;
import org.jetbrains.annotations.NotNull;
@ -69,6 +71,16 @@ public class MockDiscordSRV extends AbstractDiscordSRV<MainConfig, ConnectionCon
return null;
}
@Override
public PluginManager pluginManager() {
return null;
}
@Override
public OnlineMode onlineMode() {
return null;
}
@Override
public @NotNull AbstractPlayerProvider<?> playerProvider() {
return null;

View File

@ -24,8 +24,10 @@ import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.console.Console;
import com.discordsrv.common.debug.data.OnlineMode;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.player.provider.AbstractPlayerProvider;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.common.scheduler.Scheduler;
import org.jetbrains.annotations.NotNull;
@ -59,6 +61,16 @@ public class MockDiscordSRV extends AbstractDiscordSRV<MainConfig, ConnectionCon
return null;
}
@Override
public PluginManager pluginManager() {
return null;
}
@Override
public OnlineMode onlineMode() {
return null;
}
@Override
public @NotNull AbstractPlayerProvider<?> playerProvider() {
return null;

View File

@ -23,13 +23,15 @@ import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.debug.data.OnlineMode;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.common.server.ServerDiscordSRV;
import com.discordsrv.sponge.console.SpongeConsole;
import com.discordsrv.sponge.player.SpongePlayerProvider;
import com.discordsrv.sponge.plugin.SpongePluginManager;
import com.discordsrv.sponge.scheduler.SpongeScheduler;
import dev.vankka.mcdependencydownload.classloader.JarInJarClassLoader;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.api.Game;
import org.spongepowered.api.event.Listener;
@ -48,6 +50,7 @@ public class SpongeDiscordSRV extends ServerDiscordSRV<MainConfig, ConnectionCon
private final SpongeScheduler scheduler;
private final SpongeConsole console;
private final SpongePlayerProvider playerProvider;
private final SpongePluginManager pluginManager;
public SpongeDiscordSRV(Logger logger, PluginContainer pluginContainer, Game game, JarInJarClassLoader classLoader, Path dataDirectory) {
this.pluginContainer = pluginContainer;
@ -58,6 +61,7 @@ public class SpongeDiscordSRV extends ServerDiscordSRV<MainConfig, ConnectionCon
this.scheduler = new SpongeScheduler(this);
this.console = new SpongeConsole(this);
this.playerProvider = new SpongePlayerProvider(this);
this.pluginManager = new SpongePluginManager(this);
load();
}
@ -97,11 +101,19 @@ public class SpongeDiscordSRV extends ServerDiscordSRV<MainConfig, ConnectionCon
@Override
public String version() {
ArtifactVersion version = pluginContainer.metadata().version();
return String.format("%s.%s.%s",
version.getMajorVersion(),
version.getMinorVersion(),
version.getIncrementalVersion());
return pluginContainer.metadata().version().toString();
}
@Override
public PluginManager pluginManager() {
return pluginManager;
}
@Override
public OnlineMode onlineMode() {
// TODO: velocity / bungee
return OnlineMode.of(game.server().isOnlineModeEnabled());
}
@Override

View File

@ -0,0 +1,56 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2022 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.sponge.plugin;
import com.discordsrv.common.plugin.Plugin;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.sponge.SpongeDiscordSRV;
import org.spongepowered.plugin.metadata.PluginMetadata;
import org.spongepowered.plugin.metadata.model.PluginContributor;
import java.util.List;
import java.util.stream.Collectors;
public class SpongePluginManager implements PluginManager {
private final SpongeDiscordSRV discordSRV;
public SpongePluginManager(SpongeDiscordSRV discordSRV) {
this.discordSRV = discordSRV;
}
@Override
public boolean isPluginEnabled(String pluginName) {
return discordSRV.game().pluginManager().plugin(pluginName).isPresent();
}
@Override
public List<Plugin> getPlugins() {
return discordSRV.game().pluginManager().plugins().stream()
.map(container -> {
PluginMetadata metadata = container.metadata();
String id = metadata.id();
List<String> authors = metadata.contributors().stream()
.map(PluginContributor::name)
.collect(Collectors.toList());
return new Plugin(id, metadata.name().orElse(id), metadata.version().toString(), authors);
})
.collect(Collectors.toList());
}
}

View File

@ -22,11 +22,14 @@ import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.debug.data.OnlineMode;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.common.scheduler.StandardScheduler;
import com.discordsrv.proxy.ProxyDiscordSRV;
import com.discordsrv.velocity.console.VelocityConsole;
import com.discordsrv.velocity.player.VelocityPlayerProvider;
import com.discordsrv.velocity.plugin.VelocityPluginManager;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.proxy.ProxyServer;
import org.jetbrains.annotations.NotNull;
@ -44,6 +47,7 @@ public class VelocityDiscordSRV extends ProxyDiscordSRV<MainConfig, ConnectionCo
private final StandardScheduler scheduler;
private final VelocityConsole console;
private final VelocityPlayerProvider playerProvider;
private final VelocityPluginManager pluginManager;
public VelocityDiscordSRV(Object plugin, Logger logger, ProxyServer proxyServer, PluginContainer pluginContainer, Path dataDirectory) {
this.plugin = plugin;
@ -55,6 +59,7 @@ public class VelocityDiscordSRV extends ProxyDiscordSRV<MainConfig, ConnectionCo
this.scheduler = new StandardScheduler(this);
this.console = new VelocityConsole(this);
this.playerProvider = new VelocityPlayerProvider(this);
this.pluginManager = new VelocityPluginManager(this);
load();
}
@ -98,7 +103,17 @@ public class VelocityDiscordSRV extends ProxyDiscordSRV<MainConfig, ConnectionCo
@Override
public String version() {
return pluginContainer.getDescription().getVersion().orElseThrow(() -> new IllegalStateException("No version"));
return container().getDescription().getVersion().orElseThrow(() -> new IllegalStateException("No version"));
}
@Override
public PluginManager pluginManager() {
return pluginManager;
}
@Override
public OnlineMode onlineMode() {
return OnlineMode.of(proxy().getConfiguration().isOnlineMode());
}
@Override

View File

@ -0,0 +1,57 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2022 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.velocity.plugin;
import com.discordsrv.common.plugin.Plugin;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.velocity.VelocityDiscordSRV;
import com.velocitypowered.api.plugin.PluginDescription;
import java.util.List;
import java.util.stream.Collectors;
public class VelocityPluginManager implements PluginManager {
private final VelocityDiscordSRV discordSRV;
public VelocityPluginManager(VelocityDiscordSRV discordSRV) {
this.discordSRV = discordSRV;
}
@Override
public boolean isPluginEnabled(String pluginName) {
return discordSRV.proxy().getPluginManager().isLoaded(pluginName);
}
@Override
public List<Plugin> getPlugins() {
return discordSRV.proxy().getPluginManager().getPlugins().stream()
.map(container -> {
PluginDescription description = container.getDescription();
String id = description.getId();
return new Plugin(
id,
description.getName().orElse(id),
description.getVersion().orElse("Unknown"),
description.getAuthors()
);
})
.collect(Collectors.toList());
}
}