Compare commits

..

4 commits

Author SHA1 Message Date
743072d8e3 FindAltsCommand 2024-10-26 18:11:28 +07:00
a4f7b0e6a8 forgor to change the thingy to use CommandException 2024-10-26 17:10:36 +07:00
d0f0911f8c Change SeenCommand invalid entry message 2024-10-26 17:08:37 +07:00
ef5ec8dc27 fix 2024-10-26 17:06:43 +07:00
4 changed files with 94 additions and 5 deletions

View file

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

View file

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

View file

@ -58,6 +58,7 @@ public class CommandHandlerPlugin {
registerCommand(new IPFilterCommand());
registerCommand(new StopCommand());
registerCommand(new GrepLogCommand());
registerCommand(new FindAltsCommand());
}
public boolean disabled = false;

View file

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