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

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