Add list command

This commit is contained in:
Dan Mulloy 2018-06-09 20:19:38 -04:00 committed by Alexander Meech
parent d6479143b8
commit 71613a923d
4 changed files with 151 additions and 1 deletions

View File

@ -63,7 +63,12 @@ public abstract class CommandBase<PluginType extends MiniPlugin> implements ICom
protected void reply(Player caller, String message)
{
caller.sendMessage(F.main(Plugin.getName(), message));
reply(caller, Plugin.getName(), message);
}
protected void reply(Player caller, String module, String message)
{
caller.sendMessage(F.main(module, message));
}
@Override

View File

@ -91,6 +91,7 @@ import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.core.youtube.YoutubeManager;
import mineplex.hub.commands.GadgetToggle;
import mineplex.hub.commands.ListCommand;
import mineplex.hub.doublejump.JumpManager;
import mineplex.hub.gimmicks.AdminPunch;
import mineplex.hub.gimmicks.SecretAreas;
@ -118,6 +119,7 @@ public class HubManager extends MiniClientPlugin<HubClient> implements IChatMess
public enum Perm implements Permission
{
GADGET_TOGGLE_COMMAND,
LIST_COMMAND,
GAMEMODE_COMMAND,
AUTO_OP,
VANISH,
@ -298,12 +300,14 @@ public class HubManager extends MiniClientPlugin<HubClient> implements IChatMess
PermissionGroup.ADMIN.setPermission(Perm.VANISH, true, true);
PermissionGroup.TRAINEE.setPermission(Perm.SPAWN_PM, true, true);
PermissionGroup.ULTRA.setPermission(Perm.JOIN_FULL, true, true);
PermissionGroup.ADMIN.setPermission(Perm.LIST_COMMAND, true, true);
}
@Override
public void addCommands()
{
addCommand(new GadgetToggle(this));
addCommand(new ListCommand(this));
}
@EventHandler(priority = EventPriority.HIGHEST)

View File

@ -0,0 +1,136 @@
package mineplex.hub.commands;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import mineplex.core.Managers;
import mineplex.core.command.CommandBase;
import mineplex.core.common.util.C;
import mineplex.hub.HubManager;
import mineplex.hub.server.ServerInfo;
import mineplex.hub.server.ServerManager;
/**
* Lists player and version distributions
*/
public class ListCommand extends CommandBase<HubManager>
{
private ServerManager _serverManager;
public ListCommand(HubManager plugin)
{
super(plugin, HubManager.Perm.LIST_COMMAND, "list");
}
private ServerManager getServerManager()
{
if (_serverManager == null)
{
_serverManager = Managers.require(ServerManager.class);
}
return _serverManager;
}
@Override
public void Execute(Player player, String[] args)
{
if (args.length == 0)
{
reply(player, "List", "There are " + C.cGreen + Bukkit.getOnlinePlayers().size() +
C.cGray + " players online.");
return;
}
switch (args[0].toLowerCase())
{
case "network": // Lists the player distribution across the network
network(player);
return;
case "group": // Lists the player distribution across a group
group(player, args);
return;
case "servers": // Lists the available servers
ServerManager manager = Managers.get(ServerManager.class);
Set<String> allServers = manager.getAllServers();
reply(player, "List", "Available servers: " + allServers);
return;
default:
reply(player, "List", "Unknown directive: " + args[0]);
}
}
private void network(Player player)
{
int total = 0;
int maxTotal = 0;
ServerManager manager = getServerManager();
Set<String> groups = manager.getServerGroups();
Map<String, Integer> players = new HashMap<>();
for (String group : groups)
{
int groupCount = 0;
Collection<ServerInfo> servers = manager.getServerList(group);
for (ServerInfo info : servers)
{
groupCount += info.CurrentPlayers;
total += info.CurrentPlayers;
maxTotal += info.MaxPlayers;
}
players.put(group, groupCount);
}
reply(player, "List", "Network-wide player count:");
for (Map.Entry<String, Integer> entry : players.entrySet())
{
if (entry.getValue() == 0) continue;
reply(player, "List", C.cYellow + entry.getKey() + C.cGray + ": " + C.cGreen +
entry.getValue() + C.cGray + " online");
}
reply(player, "List", "Total: " + C.cGreen + total + C.cGray + " / " + C.cGreen + maxTotal);
}
private void group(Player player, String[] args)
{
if (args.length < 2)
{
reply(player, "List", "You must specify a server group!");
return;
}
ServerManager manager = Managers.get(ServerManager.class);
Set<String> allServers = manager.getAllServers();
List<ServerInfo> coll = new ArrayList<>();
for (String server : allServers)
{
if (args[0].equalsIgnoreCase("all") || server.toLowerCase().startsWith(args[1].toLowerCase()))
coll.add(manager.getServerInfo(server));
}
reply(player, "List", "Server info for group " + C.cBlue + args[1] + C.cGray + ":");
for (ServerInfo info : coll)
{
String line = C.cYellow + info.Name + C.cGray + ": " + info.CurrentPlayers + "/" + info.MaxPlayers;
reply(player, "List", line);
}
reply(player,"List", C.cYellow + "---------------");
}
}

View File

@ -340,6 +340,11 @@ public class ServerManager extends MiniPlugin
return _serverKeyInfoMap.get(serverKey);
}
public Set<String> getServerGroups()
{
return _serverKeyInfoMap.keySet();
}
public Set<String> getAllServers()
{
return _serverInfoMap.keySet();