refactor: make queries less spammy in console like bukkit.lastKnownName and seen <uuid>

fix: `*mail sendselecteditem` and `*music playitem` invalid/doesn't exist/empty check not working
This commit is contained in:
ChomeNS
2025-04-05 17:52:08 +07:00
parent ac44bedf8c
commit 65cc3971af
6 changed files with 124 additions and 55 deletions

View File

@@ -78,7 +78,7 @@ public class MailCommand extends Command {
future.thenApply(output -> {
try {
if (output == null) {
if (output.isEmpty()) {
throw new CommandException(Component.text("Player has no `message` NBT tag in their selected item's minecraft:custom_data"));
}

View File

@@ -191,7 +191,7 @@ public class MusicCommand extends Command {
);
future.thenApply(output -> {
if (output == null) {
if (output.isEmpty()) {
context.sendOutput(Component.text("Player has no `SongItemData.SongData` NBT tag in their selected item's minecraft:custom_data").color(NamedTextColor.RED));
return null;
}

View File

@@ -193,7 +193,8 @@ public class CorePlugin
}
// thanks chipmunk for this new tellraw method
public CompletableFuture<Component> runTracked (String command) {
public CompletableFuture<Component> runTracked (String command) { return runTracked(false, command); }
public CompletableFuture<Component> runTracked (boolean useCargo, String command) {
if (!ready || command.length() > 32767) return null;
if (!bot.options.useCore) return null;
@@ -209,7 +210,7 @@ public class CorePlugin
final CompletableFuture<Component> trackedFuture = new CompletableFuture<>();
final CompletableFuture<String> future = bot.query.block(coreBlock, "LastOutput");
final CompletableFuture<String> future = bot.query.block(useCargo, coreBlock, "LastOutput");
future.thenApply(output -> {
if (output == null) return null;

View File

@@ -99,7 +99,7 @@ public class PlayersPlugin extends Bot.Listener implements TickPlugin.Listener {
return;
}
final CompletableFuture<Component> trackedCoreFuture = bot.core.runTracked("essentials:seen " + target.profile.getIdAsString());
final CompletableFuture<Component> trackedCoreFuture = bot.core.runTracked(true, "essentials:seen " + target.profile.getIdAsString());
if (trackedCoreFuture == null) {
outputFuture.complete(null);
@@ -251,7 +251,7 @@ public class PlayersPlugin extends Bot.Listener implements TickPlugin.Listener {
}
private CompletableFuture<String> getLastKnownName (String uuid) {
return bot.query.entity(uuid, "bukkit.lastKnownName");
return bot.query.entity(true, uuid, "bukkit.lastKnownName");
}
private void check (PlayerEntry target) {

View File

@@ -2,51 +2,119 @@ package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.util.UUIDUtilities;
import net.kyori.adventure.text.BlockNBTComponent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.*;
import org.apache.commons.lang3.tuple.Triple;
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;
public class QueryPlugin extends Bot.Listener implements ChatPlugin.Listener, TickPlugin.Listener {
private static final String ID = "chomens_bot_query";
public class QueryPlugin extends Bot.Listener implements ChatPlugin.Listener {
private final Bot bot;
public long nextTransactionId = 0;
public final Map<Long, CompletableFuture<String>> transactions = new HashMap<>();
public final List<UUID> ids = new ArrayList<>();
public AtomicLong nextTransactionId = new AtomicLong(0);
public final Map<Long, CompletableFuture<String>> transactions = new ConcurrentHashMap<>();
public final List<UUID> ids = Collections.synchronizedList(new ArrayList<>());
public final Queue<Component> cargosQueue = new ConcurrentLinkedQueue<>();
public QueryPlugin (Bot bot) {
this.bot = bot;
bot.addListener(this);
bot.chat.addListener(this);
bot.tick.addListener(this);
}
@Override
public void onSecondTick () {
if (cargosQueue.isEmpty()) return;
sendQueueComponent(cargosQueue);
cargosQueue.clear();
}
@Override
public boolean systemMessageReceived (Component component, String string, String ansi) {
if (!(component instanceof TextComponent textComponent)) return true;
if (
!(component instanceof TranslatableComponent rootTranslatable) ||
!rootTranslatable.key().equals(ID)
) return true;
final List<TranslationArgument> arguments = rootTranslatable.arguments();
if (arguments.size() != 1) return false;
final Component cargosComponent = arguments.getFirst().asComponent();
if (
!(cargosComponent instanceof TextComponent cargosTextComponent) ||
!cargosTextComponent.content().isEmpty()
) return false;
final List<Component> cargos = cargosComponent.children();
for (Component cargo : cargos) processCargo(cargo);
return false;
}
private void sendQueueComponent (Component component) {
final Queue<Component> queue = new LinkedList<>();
queue.add(component);
// this is required so the joined component will be
// {"extra":["abc query here"],"text":""}
// instead of
// {"text":"abc query here"}
// even though that increases size,
// i think it will be messy to implement that
queue.add(Component.empty());
sendQueueComponent(queue);
}
private void sendQueueComponent (Queue<Component> queue) {
bot.chat.tellraw(
Component
.translatable(
ID,
Component.join(
JoinConfiguration.noSeparators(),
queue
)
),
bot.profile.getId()
);
}
private void processCargo (Component cargo) {
if (!(cargo instanceof TextComponent textComponent)) return;
final UUID inputId = UUIDUtilities.tryParse(textComponent.content());
if (inputId == null || !ids.contains(inputId)) return true;
if (inputId == null || !ids.contains(inputId)) return;
ids.remove(inputId);
final List<Component> children = component.children();
final List<Component> children = cargo.children();
if (
children.size() > 2 ||
!(children.getFirst() instanceof TextComponent firstChild)
) return false;
) return;
try {
final long transactionId = Long.parseLong(firstChild.content());
if (!transactions.containsKey(transactionId)) return false;
if (!transactions.containsKey(transactionId)) return;
final CompletableFuture<String> future = transactions.get(transactionId);
@@ -61,15 +129,11 @@ public class QueryPlugin extends Bot.Listener implements ChatPlugin.Listener {
future.complete(stringOutput);
}
return false;
} catch (NumberFormatException e) {
return true;
}
} catch (NumberFormatException ignored) { }
}
private Triple<CompletableFuture<String>, Long, UUID> getFutureAndId () {
final long transactionId = nextTransactionId++;
final long transactionId = nextTransactionId.getAndIncrement();
final CompletableFuture<String> future = new CompletableFuture<>();
transactions.put(transactionId, future);
@@ -80,51 +144,55 @@ public class QueryPlugin extends Bot.Listener implements ChatPlugin.Listener {
return Triple.of(future, transactionId, id);
}
public CompletableFuture<String> block (Vector3i location, String path) {
public CompletableFuture<String> block (Vector3i location, String path) { return block(false, location, path); }
public CompletableFuture<String> block (boolean useCargo, Vector3i location, String path) {
final Triple<CompletableFuture<String>, Long, UUID> triple = getFutureAndId();
final UUID id = triple.getRight();
final long transactionId = triple.getMiddle();
bot.chat.tellraw(
Component
.text(id.toString())
.append(Component.text(transactionId))
.append(
Component.blockNBT(
path,
final Component component = Component
.text(id.toString())
.append(Component.text(transactionId))
.append(
Component.blockNBT(
path,
BlockNBTComponent.WorldPos.worldPos(
BlockNBTComponent.WorldPos.Coordinate.absolute(location.getX()),
BlockNBTComponent.WorldPos.Coordinate.absolute(location.getY()),
BlockNBTComponent.WorldPos.Coordinate.absolute(location.getZ())
)
BlockNBTComponent.WorldPos.worldPos(
BlockNBTComponent.WorldPos.Coordinate.absolute(location.getX()),
BlockNBTComponent.WorldPos.Coordinate.absolute(location.getY()),
BlockNBTComponent.WorldPos.Coordinate.absolute(location.getZ())
)
),
bot.profile.getId()
);
)
);
if (useCargo) cargosQueue.add(component);
else sendQueueComponent(component);
return triple.getLeft();
}
public CompletableFuture<String> entity (String selector, String path) {
public CompletableFuture<String> entity (String selector, String path) { return entity(false, selector, path); }
public CompletableFuture<String> entity (boolean useCargo, String selector, String path) {
final Triple<CompletableFuture<String>, Long, UUID> triple = getFutureAndId();
final UUID id = triple.getRight();
final long transactionId = triple.getMiddle();
bot.chat.tellraw(
Component
.text(id.toString())
.append(Component.text(transactionId))
.append(
Component.entityNBT(
path,
selector
)
),
bot.profile.getId()
);
final Component component = Component
.text(id.toString())
.append(Component.text(transactionId))
.append(
Component.entityNBT(
path,
selector
)
);
if (useCargo) cargosQueue.add(component);
else sendQueueComponent(component);
return triple.getLeft();
}
@@ -133,7 +201,7 @@ public class QueryPlugin extends Bot.Listener implements ChatPlugin.Listener {
@Override
public void disconnected (DisconnectedEvent event) {
nextTransactionId = 0;
nextTransactionId.set(0);
transactions.clear();
ids.clear();
}