refactor: improve the chat messages checking a bit

This commit is contained in:
Chayapak 2025-03-26 08:52:21 +07:00
parent 05854223b1
commit b6133600f5
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
5 changed files with 68 additions and 74 deletions

View file

@ -1 +1 @@
2198
2206

View file

@ -222,11 +222,11 @@ public class ChomeNSModIntegrationPlugin implements ChatPlugin.Listener, Players
final String id = textComponent.content();
if (!id.equals(ID)) return true;
if (component.children().size() != 1) return true;
if (!(component.children().getFirst() instanceof TextComponent dataComponent)) return true;
if (
!id.equals(ID) ||
component.children().size() != 1 ||
!(component.children().getFirst() instanceof TextComponent dataComponent)
) return true;
final String data = dataComponent.content();

View file

@ -22,34 +22,29 @@ public class CommandSpyPlugin implements ChatPlugin.Listener {
}
@Override
public boolean systemMessageReceived(Component component, String string, String ansi) {
TextComponent textComponent;
public boolean systemMessageReceived (Component component, String string, String ansi) {
final List<Component> children = component.children();
try {
textComponent = (TextComponent) component;
if (
!(component instanceof TextComponent textComponent) ||
children.size() != 2 ||
textComponent.style().isEmpty() ||
(
textComponent.color() != NamedTextColor.AQUA &&
textComponent.color() != NamedTextColor.YELLOW
) ||
!(children.getFirst() instanceof TextComponent) ||
!(children.getLast() instanceof TextComponent)
) return true;
final List<Component> children = textComponent.children();
final String username = textComponent.content();
final String command = ComponentUtilities.stringify(children.getLast());
if (
(
textComponent.color() != NamedTextColor.AQUA ||
textComponent.color() != NamedTextColor.YELLOW ||
textComponent.style().isEmpty()
) &&
children.size() != 2
) return true;
final PlayerEntry sender = bot.players.getEntry(username);
if (!((TextComponent) children.getFirst()).content().equals(": ")) return true;
if (sender == null) return true;
final String username = textComponent.content();
final String command = ComponentUtilities.stringify(children.get(1));
final PlayerEntry sender = bot.players.getEntry(username);
if (sender == null) return true;
for (Listener listener : listeners) listener.commandReceived(sender, command);
} catch (ClassCastException ignored) {}
for (Listener listener : listeners) listener.commandReceived(sender, command);
return true;
}

View file

@ -10,53 +10,51 @@ import java.util.ArrayList;
import java.util.List;
public class CommandSuggestionPlugin implements ChatPlugin.Listener {
private final Bot bot;
public static final String ID = "chomens_bot_request_command_suggestion";
public final String id = "chomens_bot_request_command_suggestion";
private final Bot bot;
public CommandSuggestionPlugin (Bot bot) {
this.bot = bot;
bot.chat.addListener(this);
}
@Override
public boolean systemMessageReceived(Component component, String string, String ansi) {
if (!(component instanceof TextComponent idComponent)) return true;
final List<Component> children = component.children();
if (!idComponent.content().equals(id)) return true;
if (
!(component instanceof TextComponent idComponent) ||
!idComponent.content().equals(ID) ||
children.size() != 1 ||
!(children.getFirst() instanceof TextComponent playerComponent)
) return true;
try {
final List<Component> children = component.children();
final String player = playerComponent.content();
if (children.size() != 1) return true;
final List<Component> output = new ArrayList<>();
output.add(Component.text(ID));
final String player = ((TextComponent) children.getFirst()).content();
for (Command command : CommandHandlerPlugin.commands) {
if (command.consoleOnly) continue;
final List<Component> output = new ArrayList<>();
output.add(Component.text(id));
final boolean hasAliases = command.aliases.length != 0;
for (Command command : CommandHandlerPlugin.commands) {
if (command.consoleOnly) continue;
Component outputComponent = Component
.text(command.name)
.append(Component.text(command.trustLevel.name()))
.append(Component.text(hasAliases));
final boolean hasAliases = command.aliases.length != 0;
Component outputComponent = Component
.text(command.name)
.append(Component.text(command.trustLevel.name()))
.append(Component.text(hasAliases));
if (hasAliases) {
for (String alias : command.aliases) outputComponent = outputComponent.append(Component.text(alias));
}
output.add(outputComponent);
if (hasAliases) {
for (String alias : command.aliases) outputComponent = outputComponent.append(Component.text(alias));
}
bot.chat.tellraw(Component.join(JoinConfiguration.noSeparators(), output), player);
output.add(outputComponent);
}
return false;
} catch (Exception ignored) {}
bot.chat.tellraw(Component.join(JoinConfiguration.noSeparators(), output), player);
return true;
return false;
}
}

View file

@ -31,22 +31,23 @@ public class QueryPlugin extends Bot.Listener implements ChatPlugin.Listener {
public boolean systemMessageReceived (Component component, String string, String ansi) {
if (!(component instanceof TextComponent textComponent)) return true;
final UUID inputId = UUIDUtilities.tryParse(textComponent.content());
if (inputId == null || !ids.contains(inputId)) return true;
ids.remove(inputId);
final List<Component> children = component.children();
if (
children.size() > 2 ||
!(children.getFirst() instanceof TextComponent firstChild)
) return false;
try {
final UUID inputId = UUIDUtilities.tryParse(textComponent.content());
if (inputId == null || !ids.contains(inputId)) return true;
final long transactionId = Long.parseLong(firstChild.content());
ids.remove(inputId);
final List<Component> children = component.children();
if (children.size() > 2) return false;
if (!(children.getFirst() instanceof TextComponent)) return true;
final long transactionId = Long.parseLong(((TextComponent) children.getFirst()).content());
if (!transactions.containsKey(transactionId)) return true;
if (!transactions.containsKey(transactionId)) return false;
final CompletableFuture<String> future = transactions.get(transactionId);
@ -67,9 +68,9 @@ public class QueryPlugin extends Bot.Listener implements ChatPlugin.Listener {
}
return false;
} catch (ClassCastException | NumberFormatException ignored) {}
return true;
} catch (NumberFormatException e) {
return true;
}
}
private Triple<CompletableFuture<String>, Long, UUID> getFutureAndId () {