Compare commits
4 commits
a948ef84e2
...
743072d8e3
Author | SHA1 | Date | |
---|---|---|---|
743072d8e3 | |||
a4f7b0e6a8 | |||
d0f0911f8c | |||
ef5ec8dc27 |
4 changed files with 94 additions and 5 deletions
|
@ -0,0 +1,88 @@
|
|||
package me.chayapak1.chomens_bot.commands;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.command.Command;
|
||||
import me.chayapak1.chomens_bot.command.CommandContext;
|
||||
import me.chayapak1.chomens_bot.command.CommandException;
|
||||
import me.chayapak1.chomens_bot.command.TrustLevel;
|
||||
import me.chayapak1.chomens_bot.data.PlayerEntry;
|
||||
import me.chayapak1.chomens_bot.plugins.PlayersPersistentDataPlugin;
|
||||
import me.chayapak1.chomens_bot.util.ColorUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FindAltsCommand extends Command {
|
||||
public FindAltsCommand() {
|
||||
super(
|
||||
"findalts",
|
||||
"Finds players with the same IP address",
|
||||
new String[] { "<player>", "<ip>" },
|
||||
new String[] { "alts", "sameip" },
|
||||
TrustLevel.PUBLIC,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component execute(CommandContext context) throws Exception {
|
||||
final Bot bot = context.bot;
|
||||
|
||||
String targetIP;
|
||||
|
||||
final String player = context.getString(true, true);
|
||||
|
||||
final PlayerEntry playerEntry = bot.players.getEntry(player);
|
||||
|
||||
if (playerEntry == null) targetIP = player;
|
||||
else {
|
||||
final CompletableFuture<String> future = bot.players.getPlayerIP(playerEntry);
|
||||
|
||||
targetIP = future.get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
if (targetIP == null) throw new CommandException(Component.text("Could not find player's IP address"));
|
||||
|
||||
final Stream<String> matches = PlayersPersistentDataPlugin.playersObject.deepCopy().entrySet() // is calling deepCopy necessary?
|
||||
.stream()
|
||||
.filter(
|
||||
entry -> {
|
||||
final JsonObject ipsObject = entry
|
||||
.getValue().getAsJsonObject().getAsJsonObject("ips");
|
||||
|
||||
if (ipsObject == null) return false;
|
||||
|
||||
final JsonElement currentServerIP = ipsObject.get(bot.host + ":" + bot.port);
|
||||
|
||||
if (currentServerIP == null) return false;
|
||||
|
||||
return currentServerIP.getAsString().equals(targetIP);
|
||||
}
|
||||
)
|
||||
.map(Map.Entry::getKey);
|
||||
|
||||
Component component = Component
|
||||
.translatable("Possible alts for the player %s:")
|
||||
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
|
||||
.arguments(Component.text(player).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)))
|
||||
.appendNewline();
|
||||
|
||||
int i = 0;
|
||||
for (String name : matches.toList()) {
|
||||
component = component
|
||||
.append(
|
||||
Component
|
||||
.text(name)
|
||||
.color((i++ & 1) == 0 ? ColorUtilities.getColorByString(bot.config.colorPalette.primary) : ColorUtilities.getColorByString(bot.config.colorPalette.secondary))
|
||||
)
|
||||
.appendSpace();
|
||||
}
|
||||
|
||||
return component;
|
||||
}
|
||||
}
|
|
@ -43,16 +43,16 @@ public class SeenCommand extends Command {
|
|||
}
|
||||
|
||||
final JsonElement playerElement = PlayersPersistentDataPlugin.playersObject.get(player);
|
||||
if (playerElement == null) return Component.translatable(
|
||||
if (playerElement == null) throw new CommandException(Component.translatable(
|
||||
"%s was never seen",
|
||||
Component.text(player)
|
||||
).color(NamedTextColor.RED);
|
||||
));
|
||||
|
||||
final JsonObject lastSeen = playerElement.getAsJsonObject().get("lastSeen").getAsJsonObject();
|
||||
|
||||
final JsonElement time = lastSeen.get("time");
|
||||
|
||||
if (time == null) throw new CommandException(Component.text("time is null.. (possible invalid entry)"));
|
||||
if (time == null) throw new CommandException(Component.text("This player does not have the `lastSeen.time` entry in the database for some reason."));
|
||||
|
||||
final DateTime dateTime = new DateTime(time.getAsLong(), DateTimeZone.UTC);
|
||||
|
||||
|
|
|
@ -58,6 +58,7 @@ public class CommandHandlerPlugin {
|
|||
registerCommand(new IPFilterCommand());
|
||||
registerCommand(new StopCommand());
|
||||
registerCommand(new GrepLogCommand());
|
||||
registerCommand(new FindAltsCommand());
|
||||
}
|
||||
|
||||
public boolean disabled = false;
|
||||
|
|
|
@ -31,8 +31,8 @@ public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener {
|
|||
public void playerJoined(PlayerEntry target) {
|
||||
final JsonObject object = new JsonObject();
|
||||
object.addProperty("uuid", target.profile.getIdAsString());
|
||||
object.add("lastSeen", new JsonObject());
|
||||
object.add("ips", new JsonObject());
|
||||
if (!object.has("lastSeen")) object.add("lastSeen", new JsonObject());
|
||||
if (!object.has("ips")) object.add("ips", new JsonObject());
|
||||
|
||||
final CompletableFuture<String> future = bot.players.getPlayerIP(target);
|
||||
|
||||
|
|
Loading…
Reference in a new issue