From e53f8a02bad83d74de461f7b7d5f777299c36e75 Mon Sep 17 00:00:00 2001 From: mohammed jasem alaajel Date: Thu, 14 Apr 2022 01:56:17 +0400 Subject: [PATCH] add glist command *reset will be added later --- .../redisbungee/RedisBungeeBungeePlugin.java | 17 +--- .../commands/RedisBungeeCommands.java | 78 +++++++++++++++++++ 2 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/RedisBungeeCommands.java diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeePlugin.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeePlugin.java index dfcd702..1090fdd 100644 --- a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeePlugin.java +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeePlugin.java @@ -8,6 +8,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; import com.google.common.io.ByteStreams; import com.google.gson.Gson; +import com.imaginarycode.minecraft.redisbungee.commands.RedisBungeeCommands; import com.imaginarycode.minecraft.redisbungee.events.bungee.*; import com.imaginarycode.minecraft.redisbungee.internal.*; import com.imaginarycode.minecraft.redisbungee.internal.util.IOUtil; @@ -395,19 +396,9 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin } }, 0, 3, TimeUnit.SECONDS); dataManager = new BungeeDataManager(this); - /*if (configuration.isRegisterBungeeCommands()) { - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.GlistCommand(this)); - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.FindCommand(this)); - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.LastSeenCommand(this)); - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.IpCommand(this)); - } - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.SendToAll(this)); - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.ServerId(this)); - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.ServerIds()); - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.PlayerProxyCommand(this)); - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.PlistCommand(this)); - getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.DebugCommand(this)); - */ + // glist command + getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.GlistCommand(this)); + getProxy().getPluginManager().registerListener(this, new RedisBungeeListener(this, configuration.getExemptAddresses())); getProxy().getPluginManager().registerListener(this, dataManager); diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/RedisBungeeCommands.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/RedisBungeeCommands.java new file mode 100644 index 0000000..6c082aa --- /dev/null +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/RedisBungeeCommands.java @@ -0,0 +1,78 @@ +package com.imaginarycode.minecraft.redisbungee.commands; + +import com.google.common.base.Joiner; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; +import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI; +import com.imaginarycode.minecraft.redisbungee.internal.RedisBungeePlugin; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.plugin.Command; + +import java.util.Map; +import java.util.TreeSet; +import java.util.UUID; + +/** + * This class contains subclasses that are used for the commands RedisBungee overrides or includes: /glist, /find and /lastseen. + *

+ * All classes use the {@link RedisBungeeAPI}. + * + * @author tuxed + * @since 0.2.3 + */ +public class RedisBungeeCommands { + + private static String playerPlural(int num) { + return num == 1 ? num + " player is" : num + " players are"; + } + + public static class GlistCommand extends Command { + private final RedisBungeePlugin plugin; + + public GlistCommand(RedisBungeePlugin plugin) { + super("glist", "bungeecord.command.list", "redisbungee", "rglist"); + this.plugin = plugin; + } + + @Override + public void execute(final CommandSender sender, final String[] args) { + plugin.executeAsync(new Runnable() { + @Override + public void run() { + int count = plugin.getApi().getPlayerCount(); + BaseComponent[] playersOnline = new ComponentBuilder("").color(ChatColor.YELLOW) + .append(playerPlural(count) + " currently online.").create(); + if (args.length > 0 && args[0].equals("showall")) { + Multimap serverToPlayers = plugin.getApi().getServerToPlayers(); + Multimap human = HashMultimap.create(); + for (Map.Entry entry : serverToPlayers.entries()) { + human.put(entry.getKey(), plugin.getUuidTranslator().getNameFromUuid(entry.getValue(), false)); + } + for (String server : new TreeSet<>(serverToPlayers.keySet())) { + TextComponent serverName = new TextComponent(); + serverName.setColor(ChatColor.GREEN); + serverName.setText("[" + server + "] "); + TextComponent serverCount = new TextComponent(); + serverCount.setColor(ChatColor.YELLOW); + serverCount.setText("(" + serverToPlayers.get(server).size() + "): "); + TextComponent serverPlayers = new TextComponent(); + serverPlayers.setColor(ChatColor.WHITE); + serverPlayers.setText(Joiner.on(", ").join(human.get(server))); + sender.sendMessage(serverName, serverCount, serverPlayers); + } + sender.sendMessage(playersOnline); + } else { + sender.sendMessage(playersOnline); + sender.sendMessage(new ComponentBuilder("To see all players online, use /glist showall.").color(ChatColor.YELLOW).create()); + } + } + }); + } + } + + +}