From 40c542a50a4bf4bc2062f89b96c8ac3249c09993 Mon Sep 17 00:00:00 2001 From: mohammed jasem alaajel Date: Sun, 14 Apr 2024 05:54:08 +0400 Subject: [PATCH] new command infrastructure --- RedisBungee-API/build.gradle.kts | 1 - RedisBungee-Bungee/build.gradle.kts | 1 + .../BungeeCommandPlatformHelper.java | 17 ++++++ .../minecraft/redisbungee/RedisBungee.java | 12 ++++ RedisBungee-Commands/build.gradle.kts | 24 ++++++++ .../redisbungee/commands/CommandLoader.java | 23 ++++++++ .../commands/CommandRedisBungee.java | 56 +++++++++++++++++++ .../commands/utils/AdventureBaseCommand.java | 26 +++++++++ .../commands/utils/CommandPlatformHelper.java | 34 +++++++++++ RedisBungee-Velocity/build.gradle.kts | 1 + .../RedisBungeeVelocityPlugin.java | 12 +++- .../VelocityCommandPlatformHelper.java | 26 +++++++++ settings.gradle.kts | 1 + 13 files changed, 232 insertions(+), 2 deletions(-) create mode 100644 RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeeCommandPlatformHelper.java create mode 100644 RedisBungee-Commands/build.gradle.kts create mode 100644 RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandLoader.java create mode 100644 RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungee.java create mode 100644 RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/AdventureBaseCommand.java create mode 100644 RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/CommandPlatformHelper.java create mode 100644 RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityCommandPlatformHelper.java diff --git a/RedisBungee-API/build.gradle.kts b/RedisBungee-API/build.gradle.kts index 5050b4b..3504124 100644 --- a/RedisBungee-API/build.gradle.kts +++ b/RedisBungee-API/build.gradle.kts @@ -19,7 +19,6 @@ dependencies { api(libs.adventure.legacy) api(libs.adventure.plain) api(libs.adventure.miniMessage) - implementation(libs.acf.core) } description = "RedisBungee interfaces" diff --git a/RedisBungee-Bungee/build.gradle.kts b/RedisBungee-Bungee/build.gradle.kts index e11d326..a8a9721 100644 --- a/RedisBungee-Bungee/build.gradle.kts +++ b/RedisBungee-Bungee/build.gradle.kts @@ -15,6 +15,7 @@ dependencies { implementation(libs.adventure.platforms.bungeecord) implementation(libs.adventure.gson) implementation(libs.acf.bungeecord) + implementation(project(":RedisBungee-Commands")) } description = "RedisBungee Bungeecord implementation" diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeeCommandPlatformHelper.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeeCommandPlatformHelper.java new file mode 100644 index 0000000..019d06f --- /dev/null +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeeCommandPlatformHelper.java @@ -0,0 +1,17 @@ +package com.imaginarycode.minecraft.redisbungee; + +import co.aikar.commands.BungeeCommandIssuer; +import co.aikar.commands.CommandIssuer; +import com.imaginarycode.minecraft.redisbungee.commands.utils.CommandPlatformHelper; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer; + +public class BungeeCommandPlatformHelper extends CommandPlatformHelper { + + @Override + public void sendMessage(CommandIssuer issuer, Component component) { + BungeeCommandIssuer bIssuer = (BungeeCommandIssuer) issuer; + bIssuer.getIssuer().sendMessage(BungeeComponentSerializer.get().serialize(component)); + } + +} diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index 83e58af..4577ac1 100644 --- a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -10,6 +10,7 @@ package com.imaginarycode.minecraft.redisbungee; +import co.aikar.commands.BungeeCommandManager; import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager; import com.imaginarycode.minecraft.redisbungee.api.ProxyDataManager; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; @@ -27,6 +28,8 @@ import com.imaginarycode.minecraft.redisbungee.api.util.InitialUtils; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.NameFetcher; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDFetcher; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator; +import com.imaginarycode.minecraft.redisbungee.commands.CommandLoader; +import com.imaginarycode.minecraft.redisbungee.commands.utils.CommandPlatformHelper; import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent; @@ -70,6 +73,7 @@ public class RedisBungee extends Plugin implements RedisBungeePlugin commandManager, RedisBungeeConfiguration configuration) { + commandManager.registerCommand(new CommandRedisBungee()); + // todo: config options to disable each command + } + +} diff --git a/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungee.java b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungee.java new file mode 100644 index 0000000..ce253ab --- /dev/null +++ b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungee.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2013-present RedisBungee contributors + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ + +package com.imaginarycode.minecraft.redisbungee.commands; + +import co.aikar.commands.CommandIssuer; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Default; +import com.imaginarycode.minecraft.redisbungee.Constants; +import com.imaginarycode.minecraft.redisbungee.commands.utils.AdventureBaseCommand; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.event.HoverEvent; +import net.kyori.adventure.text.event.HoverEventSource; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; + +@CommandAlias("rb|redisbungee") +@CommandPermission("redisbungee.use") +public class CommandRedisBungee extends AdventureBaseCommand { + + private static final String message = """ + This proxy is running RedisBungee Limework's fork + ======================================== + RedisBungee version: + Build date: + Commit: + ========================================"""; + + @Default + public static void info(CommandIssuer issuer) { + sendMessage( + issuer, + MiniMessage.miniMessage() + .deserialize( + message, + Placeholder.component("version", Component.text(Constants.VERSION)), + Placeholder.component("build-date", Component.text(Constants.BUILD_DATE)), + Placeholder.component( + "commit", + Component.text(Constants.GIT_COMMIT) + .clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, Constants.getGithubCommitLink())) + .hoverEvent(HoverEvent.showText(Component.text("Click me to open: " + Constants.getGithubCommitLink()))) + ))); + } + + +} diff --git a/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/AdventureBaseCommand.java b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/AdventureBaseCommand.java new file mode 100644 index 0000000..873e8ee --- /dev/null +++ b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/AdventureBaseCommand.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2013-present RedisBungee contributors + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ + +package com.imaginarycode.minecraft.redisbungee.commands.utils; + +import co.aikar.commands.BaseCommand; +import co.aikar.commands.CommandIssuer; +import net.kyori.adventure.text.Component; + +/** + * this just dumb class that wraps the adventure stuff into base command + */ +public abstract class AdventureBaseCommand extends BaseCommand { + + public static void sendMessage(CommandIssuer issuer, Component component) { + CommandPlatformHelper.getPlatformHelper().sendMessage(issuer, component); + } + +} diff --git a/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/CommandPlatformHelper.java b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/CommandPlatformHelper.java new file mode 100644 index 0000000..b5da19a --- /dev/null +++ b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/CommandPlatformHelper.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2013-present RedisBungee contributors + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ + +package com.imaginarycode.minecraft.redisbungee.commands.utils; + +import co.aikar.commands.CommandIssuer; +import net.kyori.adventure.text.Component; + + +public abstract class CommandPlatformHelper { + + private static CommandPlatformHelper SINGLETON; + + public abstract void sendMessage(CommandIssuer issuer, Component component); + + public static void init(CommandPlatformHelper platformHelper) { + if (SINGLETON != null) { + throw new IllegalStateException("tried to re init Platform Helper"); + } + SINGLETON = platformHelper; + } + + public static CommandPlatformHelper getPlatformHelper() { + return SINGLETON; + } + +} diff --git a/RedisBungee-Velocity/build.gradle.kts b/RedisBungee-Velocity/build.gradle.kts index f9d301f..390a9d7 100644 --- a/RedisBungee-Velocity/build.gradle.kts +++ b/RedisBungee-Velocity/build.gradle.kts @@ -22,6 +22,7 @@ dependencies { } compileOnly(libs.platform.velocity) annotationProcessor(libs.platform.velocity) + implementation(project(":RedisBungee-Commands")) implementation(libs.acf.velocity) } diff --git a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java index e0b3129..2d250f0 100644 --- a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java +++ b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java @@ -10,11 +10,14 @@ package com.imaginarycode.minecraft.redisbungee; +import co.aikar.commands.VelocityCommandManager; import com.google.inject.Inject; import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager; import com.imaginarycode.minecraft.redisbungee.api.ProxyDataManager; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; +import com.imaginarycode.minecraft.redisbungee.commands.CommandLoader; +import com.imaginarycode.minecraft.redisbungee.commands.utils.CommandPlatformHelper; import com.imaginarycode.minecraft.redisbungee.api.config.LangConfiguration; import com.imaginarycode.minecraft.redisbungee.api.config.loaders.ConfigLoader; import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; @@ -91,6 +94,8 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, Con new LegacyChannelIdentifier("legacy:redisbungee") ); + private VelocityCommandManager commandManager; + @Inject public RedisBungeeVelocityPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) { this.server = server; @@ -264,7 +269,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, Con @Override public void initialize() { logInfo("Initializing RedisBungee....."); - ; // start heartbeat task // heartbeat and clean up this.heartbeatTask = server.getScheduler().buildTask(this, this.proxyDataManager::publishHeartbeat).repeat(Duration.ofSeconds(1)).schedule(); @@ -279,6 +283,11 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, Con // register plugin messages IDENTIFIERS.forEach(getProxy().getChannelRegistrar()::register); + // load commands + CommandPlatformHelper.init(new VelocityCommandPlatformHelper()); + this.commandManager = new VelocityCommandManager(this.getProxy(), this); + CommandLoader.initCommands(this.commandManager, configuration()); + logInfo("RedisBungee initialized successfully "); } @@ -308,6 +317,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, Con } catch (InterruptedException e) { throw new RuntimeException(e); } + if (commandManager != null) commandManager.unregisterCommands(); logInfo("RedisBungee shutdown complete"); } diff --git a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityCommandPlatformHelper.java b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityCommandPlatformHelper.java new file mode 100644 index 0000000..07cd9de --- /dev/null +++ b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityCommandPlatformHelper.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2013-present RedisBungee contributors + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ + +package com.imaginarycode.minecraft.redisbungee; + +import co.aikar.commands.CommandIssuer; +import co.aikar.commands.VelocityCommandIssuer; +import com.imaginarycode.minecraft.redisbungee.commands.utils.CommandPlatformHelper; +import net.kyori.adventure.text.Component; + +public class VelocityCommandPlatformHelper extends CommandPlatformHelper { + + @Override + public void sendMessage(CommandIssuer issuer, Component component) { + VelocityCommandIssuer vIssuer = (VelocityCommandIssuer) issuer; + vIssuer.getIssuer().sendMessage(component); + } + +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 81024f8..f3ff03f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -7,6 +7,7 @@ pluginManagement { rootProject.name = "RedisBungee-Parent" include(":RedisBungee-Velocity") +include(":RedisBungee-Commands") include(":RedisBungee-Bungee") include(":RedisBungee-API")