fix,refactor: fix findalts and use thenApplyAsync instead of thenApply for non-blocking

This commit is contained in:
Chayapak 2024-11-16 18:41:34 +07:00
parent 9b824e195a
commit 65aa230260
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
10 changed files with 21 additions and 23 deletions

View file

@ -103,7 +103,7 @@ public class CommandBlockCommand extends Command {
if (future == null) return;
future.thenApply(output -> {
future.thenApplyAsync(output -> {
context.sendOutput(output);
return output;

View file

@ -38,7 +38,7 @@ public class EvalCommand extends Command {
final CompletableFuture<EvalOutput> future = bot.eval.run(command);
future.thenApply((output) -> {
future.thenApplyAsync(output -> {
if (output.isError()) context.sendOutput(Component.text(output.output()).color(NamedTextColor.RED));
else context.sendOutput(Component.text(output.output()));

View file

@ -5,7 +5,6 @@ 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;
@ -14,7 +13,6 @@ 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 {
@ -33,25 +31,25 @@ public class FindAltsCommand extends Command {
public Component execute(CommandContext context) throws Exception {
final Bot bot = context.bot;
boolean argumentIsIP = true;
String targetIP;
final String player = context.getString(true, true);
final PlayerEntry playerEntry = bot.players.getEntry(player);
if (playerEntry == null) targetIP = player;
if (playerEntry == null) return handle(bot, player, true, player);
else {
argumentIsIP = false;
final CompletableFuture<String> future = bot.players.getPlayerIP(playerEntry);
targetIP = future.get(5, TimeUnit.SECONDS);
future.thenApplyAsync(targetIP -> {
context.sendOutput(handle(bot, targetIP, false, player));
return targetIP;
});
}
if (targetIP == null) throw new CommandException(Component.text("Could not find player's IP address"));
return null;
}
private Component handle (Bot bot, String targetIP, boolean argumentIsIP, String player) {
final Stream<String> matches = PlayersPersistentDataPlugin.playersObject.deepCopy().entrySet() // is calling deepCopy necessary?
.stream()
.filter(

View file

@ -106,7 +106,7 @@ public class MailCommand extends Command {
throw new CommandException(Component.text("There was an error while sending your mail"));
}
future.thenApply(output -> {
future.thenApplyAsync(output -> {
try {
final List<Component> children = output.children();

View file

@ -206,7 +206,7 @@ public class MusicCommand extends Command {
throw new CommandException(Component.text("There was an error while getting your data"));
}
future.thenApply(output -> {
future.thenApplyAsync(output -> {
final List<Component> children = output.children();
if (
@ -264,7 +264,7 @@ public class MusicCommand extends Command {
throw new CommandException(Component.text("There was an error while getting your data"));
}
future.thenApply(output -> {
future.thenApplyAsync(output -> {
final List<Component> children = output.children();
if (

View file

@ -239,7 +239,7 @@ public class CorePlugin extends PositionPlugin.Listener {
final String stringLastOutput = ((TextComponent) children.get(1)).content();
future.complete(Component.join(JoinConfiguration.separator(Component.space()), GsonComponentSerializer.gson().deserialize(stringLastOutput).children()));
future.complete(Component.join(JoinConfiguration.separator(Component.empty()), GsonComponentSerializer.gson().deserialize(stringLastOutput).children()));
return false;
} catch (ClassCastException | NumberFormatException ignored) {}

View file

@ -40,7 +40,7 @@ public class IPFilterPlugin extends PlayersPlugin.Listener {
if (future == null) return;
future.thenApply(output -> {
future.thenApplyAsync(output -> {
handleIP(output, target);
return output;

View file

@ -50,7 +50,7 @@ public class PlayersPersistentDataPlugin extends PlayersPlugin.Listener {
future.completeOnTimeout(null, 5, TimeUnit.SECONDS);
future.thenApply(output -> {
future.thenApplyAsync(output -> {
if (output != null) {
object.getAsJsonObject("ips").addProperty(bot.host + ":" + bot.port, output);
}

View file

@ -68,10 +68,10 @@ public class PlayersPlugin extends Bot.Listener {
if (trackedCoreFuture == null) return null;
trackedCoreFuture.thenApply(output -> {
trackedCoreFuture.thenApplyAsync(output -> {
final List<Component> children = output.children();
String stringified = ComponentUtilities.stringify(Component.join(JoinConfiguration.separator(Component.space()), children));
String stringified = ComponentUtilities.stringify(Component.join(JoinConfiguration.separator(Component.empty()), children));
if (!stringified.startsWith(" - IP Address: ")) return output;
@ -192,7 +192,7 @@ public class PlayersPlugin extends Bot.Listener {
final PlayerEntry target = getEntry(uuid);
if (target == null) return;
bot.tabComplete.tabComplete("/minecraft:scoreboard players add ").thenApply(packet -> {
bot.tabComplete.tabComplete("/minecraft:scoreboard players add ").thenApplyAsync(packet -> {
final String[] matches = packet.getMatches();
final Component[] tooltips = packet.getTooltips();
final String username = target.profile.getName();

View file

@ -27,7 +27,7 @@ public class ServerPluginsManagerPlugin extends Bot.Listener {
public void connected(ConnectedEvent event) {
final CompletableFuture<ClientboundCommandSuggestionsPacket> future = bot.tabComplete.tabComplete("/ver ");
future.thenApply((packet) -> {
future.thenApplyAsync((packet) -> {
final String[] matches = packet.getMatches();
// should i just use the plugins as the String array instead of a list?