RedisBungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeCommands.java

169 lines
7.2 KiB
Java
Raw Normal View History

2013-11-15 22:55:57 +00:00
/**
* Copyright © 2013 tuxed <write@imaginarycode.com>
* This work is free. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License, Version 2,
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
*/
package com.imaginarycode.minecraft.redisbungee;
import com.google.common.base.Joiner;
import com.google.common.collect.Multimap;
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;
2013-12-15 18:33:04 +00:00
import net.md_5.bungee.api.chat.TextComponent;
2013-11-15 22:55:57 +00:00
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.plugin.Command;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.TreeSet;
2013-11-15 22:55:57 +00:00
/**
* This class contains subclasses that are used for the commands RedisBungee overrides or includes: /glist, /find and /lastseen.
* <p/>
2013-11-15 22:55:57 +00:00
* All classes use the {@link RedisBungeeAPI}.
*
* @author tuxed
* @since 0.2.3
*/
class RedisBungeeCommands {
private static final BaseComponent[] NO_PLAYER_SPECIFIED =
new ComponentBuilder("").color(ChatColor.RED).append("You must specify a player name.").create();
private static final BaseComponent[] PLAYER_NOT_FOUND =
new ComponentBuilder("").color(ChatColor.RED).append("No such player found.").create();
2014-01-17 00:49:57 +00:00
private static final BaseComponent[] NO_COMMAND_SPECIFIED =
new ComponentBuilder("").color(ChatColor.RED).append("You must specify a command to be run.").create();
2013-11-15 22:55:57 +00:00
public static class GlistCommand extends Command {
GlistCommand() {
2013-11-16 18:58:15 +00:00
super("glist", "bungeecord.command.list", "redisbungee");
2013-11-15 22:55:57 +00:00
}
@Override
public void execute(CommandSender sender, String[] args) {
int count = RedisBungee.getApi().getPlayerCount();
BaseComponent[] playersOnline = new ComponentBuilder("").color(ChatColor.YELLOW).append(String.valueOf(count))
.append(" player(s) are currently online.").create();
2013-11-15 22:55:57 +00:00
if (args.length > 0 && args[0].equals("showall")) {
2013-12-24 04:42:56 +00:00
if (RedisBungee.getConfiguration().getBoolean("canonical-glist", true)) {
Multimap<String, String> serverToPlayers = RedisBungee.getApi().getServerToPlayers();
2013-12-15 18:33:04 +00:00
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(serverToPlayers.get(server)));
sender.sendMessage(serverName, serverCount, serverPlayers);
}
2013-11-15 22:55:57 +00:00
} else {
sender.sendMessage(new ComponentBuilder("").color(ChatColor.YELLOW).append("Players: ")
.append(Joiner.on(", ").join(RedisBungee.getApi().getPlayersOnline())).create());
2013-11-15 22:55:57 +00:00
}
sender.sendMessage(playersOnline);
2013-11-15 22:55:57 +00:00
} else {
sender.sendMessage(playersOnline);
sender.sendMessage(new ComponentBuilder("").color(ChatColor.YELLOW).append("To see all players online, use /glist showall.").create());
2013-11-15 22:55:57 +00:00
}
}
}
public static class FindCommand extends Command {
FindCommand() {
2013-11-15 22:55:57 +00:00
super("find", "bungeecord.command.find");
}
@Override
public void execute(CommandSender sender, String[] args) {
if (args.length > 0) {
ServerInfo si = RedisBungee.getApi().getServerFor(args[0]);
if (si != null) {
2013-12-15 18:33:04 +00:00
TextComponent message = new TextComponent();
message.setColor(ChatColor.BLUE);
message.setText(args[0] + " is on " + si.getName() + ".");
sender.sendMessage(message);
2013-11-15 22:55:57 +00:00
} else {
sender.sendMessage(PLAYER_NOT_FOUND);
2013-11-15 22:55:57 +00:00
}
} else {
sender.sendMessage(NO_PLAYER_SPECIFIED);
2013-11-15 22:55:57 +00:00
}
}
}
public static class LastSeenCommand extends Command {
LastSeenCommand() {
2013-11-15 22:55:57 +00:00
super("lastseen", "redisbungee.command.lastseen");
}
@Override
public void execute(CommandSender sender, String[] args) {
if (args.length > 0) {
long secs = RedisBungee.getApi().getLastOnline(args[0]);
2013-12-15 18:33:04 +00:00
TextComponent message = new TextComponent();
2013-11-15 22:55:57 +00:00
if (secs == 0) {
2013-12-15 18:33:04 +00:00
message.setColor(ChatColor.GREEN);
message.setText(args[0] + " is currently online.");
sender.sendMessage(message);
2013-11-15 22:55:57 +00:00
} else if (secs != -1) {
2013-12-15 18:33:04 +00:00
message.setColor(ChatColor.BLUE);
message.setText(args[0] + " was last online on " + new SimpleDateFormat().format(secs) + ".");
2013-12-15 18:33:04 +00:00
sender.sendMessage(message);
2013-11-15 22:55:57 +00:00
} else {
2013-12-15 18:33:04 +00:00
message.setColor(ChatColor.RED);
message.setText(args[0] + " has never been online.");
sender.sendMessage(message);
2013-11-15 22:55:57 +00:00
}
} else {
sender.sendMessage(NO_PLAYER_SPECIFIED);
2013-11-15 22:55:57 +00:00
}
}
}
public static class IpCommand extends Command {
IpCommand() {
super("ip", "redisbungee.command.ip", "playerip");
}
@Override
public void execute(CommandSender sender, String[] args) {
if (args.length > 0) {
InetAddress ia = RedisBungee.getApi().getPlayerIp(args[0]);
if (ia != null) {
2013-12-15 18:33:04 +00:00
TextComponent message = new TextComponent();
message.setColor(ChatColor.GREEN);
message.setText(args[0] + " is connected from " + ia.toString() + ".");
} else {
sender.sendMessage(PLAYER_NOT_FOUND);
}
} else {
sender.sendMessage(NO_PLAYER_SPECIFIED);
}
}
}
2014-01-17 00:49:57 +00:00
public static class SendToAll extends Command {
SendToAll() {
super("sendtoall", "redisbungee.command.sendtoall");
}
@Override
public void execute(CommandSender sender, String[] args) {
if (args.length > 0) {
String command = Joiner.on(" ").skipNulls().join(args);
RedisBungee.getApi().sendProxyCommand(command);
TextComponent message = new TextComponent();
message.setColor(ChatColor.GREEN);
message.setText("Sent the command /" + command + " to all proxies.");
} else {
sender.sendMessage(NO_COMMAND_SPECIFIED);
}
}
}
2013-11-15 22:55:57 +00:00
}