Reload and About Commands (#32)

This commit is contained in:
ironboundred 2023-03-31 18:46:56 -05:00 committed by GitHub
parent 81d4a9a711
commit 88d1c4f6cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 6 deletions

View File

@ -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

View File

@ -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));
}
}

View File

@ -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<String> 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"));
}
}
}

View File

@ -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<TabPlayer> players;
private final ConcurrentLinkedQueue<String> 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