From 88d1c4f6cf1aa11a6216f1b8effd60b092ba2352 Mon Sep 17 00:00:00 2001 From: ironboundred <44921987+ironboundred@users.noreply.github.com> Date: Fri, 31 Mar 2023 18:46:56 -0500 Subject: [PATCH] Reload and About Commands (#32) --- build.gradle | 2 + .../net/william278/velocitab/Velocitab.java | 19 ++++-- .../velocitab/commands/VelocitabCommand.java | 67 +++++++++++++++++++ .../velocitab/tab/PlayerTabList.java | 25 ++++++- 4 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/william278/velocitab/commands/VelocitabCommand.java diff --git a/build.gradle b/build.gradle index 0d33982..550f874 100644 --- a/build.gradle +++ b/build.gradle @@ -35,6 +35,7 @@ dependencies { implementation 'net.william278:Annotaml:2.0.1' implementation 'dev.dejvokep:boosted-yaml:1.3.1' implementation 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT' + implementation 'net.william278:DesertWell:1.1.1' annotationProcessor 'org.projectlombok:lombok:1.18.26' } @@ -59,6 +60,7 @@ shadowJar { relocate 'de.themoep', 'net.william278.velocitab.libraries' relocate 'dev.dejvokep.boostedyaml', 'net.william278.velocitab.libraries' relocate 'net.william278.annotaml', 'net.william278.velocitab.libraries.annotaml' + relocate 'net.william278.desertwell', 'net.william278.velocitab.libraries.desertwell' dependencies { //noinspection GroovyAssignabilityCheck diff --git a/src/main/java/net/william278/velocitab/Velocitab.java b/src/main/java/net/william278/velocitab/Velocitab.java index 13aca36..69cc256 100644 --- a/src/main/java/net/william278/velocitab/Velocitab.java +++ b/src/main/java/net/william278/velocitab/Velocitab.java @@ -4,10 +4,12 @@ import com.google.inject.Inject; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.Plugin; +import com.velocitypowered.api.plugin.PluginDescription; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import net.william278.annotaml.Annotaml; +import net.william278.velocitab.commands.VelocitabCommand; import net.william278.velocitab.config.Formatter; import net.william278.velocitab.config.Settings; import net.william278.velocitab.hook.Hook; @@ -25,10 +27,7 @@ import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; +import java.util.*; @Plugin(id = "velocitab") public class Velocitab { @@ -54,6 +53,7 @@ public class Velocitab { loadHooks(); prepareScoreboardManager(); prepareTabList(); + registerCommands(); logger.info("Successfully enabled Velocitab"); } @@ -72,7 +72,7 @@ public class Velocitab { return getSettings().getFormatter(); } - private void loadSettings() { + public void loadSettings() { try { settings = Annotaml.create( new File(dataDirectory.toFile(), "config.yml"), @@ -143,4 +143,13 @@ public class Velocitab { ); } + public PluginDescription getDescription() { + return server.getPluginManager().getPlugin("velocitab").get().getDescription(); + } + + private void registerCommands() { + server.getCommandManager().register( + server.getCommandManager().metaBuilder("velocitab").build(), + new VelocitabCommand(this)); + } } diff --git a/src/main/java/net/william278/velocitab/commands/VelocitabCommand.java b/src/main/java/net/william278/velocitab/commands/VelocitabCommand.java new file mode 100644 index 0000000..d48d764 --- /dev/null +++ b/src/main/java/net/william278/velocitab/commands/VelocitabCommand.java @@ -0,0 +1,67 @@ +package net.william278.velocitab.commands; + +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.command.SimpleCommand; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; +import net.william278.desertwell.AboutMenu; +import net.william278.desertwell.Version; +import net.william278.velocitab.Velocitab; + +import java.util.List; + +public class VelocitabCommand implements SimpleCommand { + private final AboutMenu aboutMenu; + private final Velocitab plugin; + + public VelocitabCommand(Velocitab plugin) { + this.plugin = plugin; + this.aboutMenu = AboutMenu.create("Velocitab") + .withDescription(plugin.getDescription().getDescription().get()) + .withVersion(Version.fromString(plugin.getDescription().getVersion().get(), "-")) + .addAttribution("Author", + AboutMenu.Credit.of("William278").withDescription("Click to visit website").withUrl("https://william278.net")) + .addAttribution("Contributors", + AboutMenu.Credit.of("Ironboundred").withDescription("Coding"), + AboutMenu.Credit.of("Emibergo02").withDescription("Coding")) + .addButtons( + AboutMenu.Link.of("https://william278.net/docs/velocitab").withText("Docs").withIcon("⛏"), + AboutMenu.Link.of("https://discord.gg/tVYhJfyDWG").withText("Discord").withIcon("⭐").withColor("#6773f5"), + AboutMenu.Link.of("https://modrinth.com/plugin/velocitab").withText("Modrinth").withIcon("X").withColor("#589143")); + } + + @Override + public void execute(Invocation invocation) { + if (invocation.arguments().length >= 1) { + if (invocation.arguments()[0].equalsIgnoreCase("reload")) { + reloadSettings(invocation.source()); + return; + } + } + + sendAboutInfo(invocation.source()); + } + + @Override + public List suggest(Invocation invocation) { + if (invocation.source().hasPermission("velocitab.command.reload")) { + return List.of("about", "reload"); + } else { + return List.of("about"); + } + } + + private void sendAboutInfo(CommandSource source) { + source.sendMessage(aboutMenu.toMineDown().toComponent()); + } + + private void reloadSettings(CommandSource source) { + if (source.hasPermission("velocitab.command.reload")) { + plugin.loadSettings(); + plugin.getTabList().reloadUpdate(); + source.sendMessage(Component.text("Velocitab has been reloaded!").color(TextColor.color(255, 199, 31))); + } else { + source.sendMessage(Component.text("You do not have permission to use this command")); + } + } +} diff --git a/src/main/java/net/william278/velocitab/tab/PlayerTabList.java b/src/main/java/net/william278/velocitab/tab/PlayerTabList.java index df98fe3..77efadc 100644 --- a/src/main/java/net/william278/velocitab/tab/PlayerTabList.java +++ b/src/main/java/net/william278/velocitab/tab/PlayerTabList.java @@ -8,6 +8,7 @@ import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.player.TabList; import com.velocitypowered.api.proxy.player.TabListEntry; import com.velocitypowered.api.proxy.server.ServerInfo; +import com.velocitypowered.api.scheduler.ScheduledTask; import net.kyori.adventure.text.Component; import net.william278.velocitab.Velocitab; import net.william278.velocitab.config.Placeholder; @@ -26,6 +27,7 @@ public class PlayerTabList { private final Velocitab plugin; private final ConcurrentLinkedQueue players; private final ConcurrentLinkedQueue fallbackServers; + private ScheduledTask updateTask; public PlayerTabList(@NotNull Velocitab plugin) { this.plugin = plugin; @@ -183,7 +185,7 @@ public class PlayerTabList { // Update the tab list periodically private void updatePeriodically(int updateRate) { - plugin.getServer().getScheduler() + updateTask = plugin.getServer().getScheduler() .buildTask(plugin, () -> { if (players.isEmpty()) { return; @@ -197,6 +199,27 @@ public class PlayerTabList { .schedule(); } + // Update all players since there was a reload of the config + public void reloadUpdate() { + if (players.isEmpty()) { + return; + } + + if (updateTask != null) { + updateTask.cancel(); + } + // If the update time is set to 0 do not schedule the updater + if (plugin.getSettings().getUpdateRate() > 0) { + this.updatePeriodically(plugin.getSettings().getUpdateRate()); + } else { + players.forEach(player -> { + this.updatePlayer(player); + player.sendHeaderAndFooter(this); + }); + } + + } + /** * Get the servers in the same group as the given server * If the server is not in a group, use fallback