diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java index bf25807..cbf8bb1 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/IPFilterPlugin.java @@ -4,12 +4,8 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.data.PlayerEntry; -import me.chayapak1.chomens_bot.util.ComponentUtilities; import me.chayapak1.chomens_bot.util.PersistentDataUtilities; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.JoinConfiguration; -import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -40,21 +36,12 @@ public class IPFilterPlugin extends PlayersPlugin.Listener { } private void check (PlayerEntry target) { - final CompletableFuture future = bot.core.runTracked("essentials:seen " + target.profile.getIdAsString()); + final CompletableFuture future = bot.players.getPlayerIP(target); if (future == null) return; future.thenApply(output -> { - final List children = output.children(); - - String stringified = ComponentUtilities.stringify(Component.join(JoinConfiguration.separator(Component.space()), children)); - - if (!stringified.startsWith(" - IP Address: ")) return output; - - stringified = stringified.trim().substring(" - IP Address: ".length()); - if (stringified.startsWith("/")) stringified = stringified.substring(1); - - handleIP(stringified, target); + handleIP(output, target); return output; }); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPersistentDataPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPersistentDataPlugin.java index d875aa6..141d427 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPersistentDataPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPersistentDataPlugin.java @@ -1,11 +1,14 @@ package me.chayapak1.chomens_bot.plugins; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.data.PlayerEntry; import me.chayapak1.chomens_bot.util.PersistentDataUtilities; import java.time.Instant; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener { public static JsonObject playersObject = new JsonObject(); @@ -26,12 +29,33 @@ public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener { @Override public void playerJoined(PlayerEntry target) { - if (playersObject.has(getName(target))) return; - final JsonObject object = new JsonObject(); object.addProperty("uuid", target.profile.getIdAsString()); object.add("lastSeen", new JsonObject()); + object.add("ips", new JsonObject()); + final CompletableFuture future = bot.players.getPlayerIP(target); + + if (future == null) { + setPersistentEntry(target, object); + return; + } + + future.completeOnTimeout(null, 5, TimeUnit.SECONDS); + + future.thenApply(output -> { + if (output != null) { + object.getAsJsonObject("ips").addProperty(bot.host + ":" + bot.port, output); + } + + setPersistentEntry(target, object); + + return output; + }); + } + + // is this bad? + private void setPersistentEntry (PlayerEntry target, JsonObject object) { playersObject.add(getName(target), object); PersistentDataUtilities.put("players", playersObject); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java index 436ca4a..987cf68 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java @@ -1,5 +1,7 @@ package me.chayapak1.chomens_bot.plugins; +import me.chayapak1.chomens_bot.util.ComponentUtilities; +import net.kyori.adventure.text.JoinConfiguration; import org.geysermc.mcprotocollib.network.Session; import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent; import org.geysermc.mcprotocollib.network.packet.Packet; @@ -16,6 +18,7 @@ import java.util.ArrayList; import java.util.EnumSet; import java.util.List; import java.util.UUID; +import java.util.concurrent.CompletableFuture; public class PlayersPlugin extends Bot.Listener { private final Bot bot; @@ -58,6 +61,31 @@ public class PlayersPlugin extends Bot.Listener { } } + public CompletableFuture getPlayerIP (PlayerEntry target) { + final CompletableFuture outputFuture = new CompletableFuture<>(); + + final CompletableFuture trackedCoreFuture = bot.core.runTracked("essentials:seen " + target.profile.getIdAsString()); + + if (trackedCoreFuture == null) return null; + + trackedCoreFuture.thenApply(output -> { + final List children = output.children(); + + String stringified = ComponentUtilities.stringify(Component.join(JoinConfiguration.separator(Component.space()), children)); + + if (!stringified.startsWith(" - IP Address: ")) return output; + + stringified = stringified.trim().substring(" - IP Address: ".length()); + if (stringified.startsWith("/")) stringified = stringified.substring(1); + + outputFuture.complete(stringified); + + return output; + }); + + return outputFuture; + } + public final PlayerEntry getEntry (UUID uuid) { for (PlayerEntry candidate : list) { if (candidate.profile.getId().equals(uuid)) {