refactor: rewrite chomensbot suggestion handling
This commit is contained in:
parent
a4a9fcf0b4
commit
ef47598b78
5 changed files with 67 additions and 51 deletions
src/main/java/land/chipmunk/chipmunkmod
|
@ -1,25 +1,54 @@
|
|||
package land.chipmunk.chipmunkmod.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
||||
import land.chipmunk.chipmunkmod.util.TextUtilities;
|
||||
import net.minecraft.text.Text;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ChomeNSBotCommand {
|
||||
public final String name;
|
||||
public final TrustLevel trustLevel;
|
||||
public final List<String> aliases = new ArrayList<>();
|
||||
public record ChomeNSBotCommand(String name, TrustLevel trustLevel, List<String> aliases) {
|
||||
public static @Nullable ChomeNSBotCommand fromText(final Text component) {
|
||||
final String name = TextUtilities.plainOrNull(component);
|
||||
if (name == null) return null;
|
||||
|
||||
public ChomeNSBotCommand (
|
||||
String name,
|
||||
TrustLevel trustLevel
|
||||
) {
|
||||
this.name = name;
|
||||
this.trustLevel = trustLevel;
|
||||
final List<Text> children = component.getSiblings();
|
||||
if (children.size() < 2) return null; // must have at least trust level and alias boolean
|
||||
|
||||
final TrustLevel trustLevel = TrustLevel.fromText(children.getFirst());
|
||||
if (trustLevel == null) return null;
|
||||
|
||||
final String hasAliasesString = TextUtilities.plainOrNull(children.get(1));
|
||||
if (hasAliasesString == null) return null;
|
||||
|
||||
final boolean hasAliases = Boolean.parseBoolean(hasAliasesString);
|
||||
if (!hasAliases) return new ChomeNSBotCommand(name, trustLevel, List.of());
|
||||
|
||||
final List<String> aliases = children.stream()
|
||||
.skip(2)
|
||||
.map(TextUtilities::plainOrNull)
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
return new ChomeNSBotCommand(
|
||||
ChipmunkMod.CONFIG.bots.chomens.prefix + name, trustLevel, aliases);
|
||||
}
|
||||
|
||||
public enum TrustLevel {
|
||||
PUBLIC,
|
||||
TRUSTED,
|
||||
ADMIN,
|
||||
OWNER
|
||||
OWNER;
|
||||
|
||||
public static TrustLevel fromText(final Text component) {
|
||||
final String trustLevelString = TextUtilities.plainOrNull(component);
|
||||
if (trustLevelString == null) return null;
|
||||
|
||||
try {
|
||||
return TrustLevel.valueOf(trustLevelString);
|
||||
} catch (final IllegalArgumentException ignored) {}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import com.mojang.brigadier.suggestion.Suggestions;
|
|||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
||||
import land.chipmunk.chipmunkmod.command.CommandManager;
|
||||
import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand;
|
||||
import land.chipmunk.chipmunkmod.modules.ChomeNSBotCommandSuggestions;
|
||||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
|
@ -62,7 +63,7 @@ public class ChatInputSuggestorMixin {
|
|||
|
||||
final List<String> commands = ChomeNSBotCommandSuggestions.INSTANCE.commands
|
||||
.stream()
|
||||
.map((command) -> command.name)
|
||||
.map(ChomeNSBotCommand::name)
|
||||
.toList();
|
||||
|
||||
pendingSuggestions = CommandSource.suggestMatching(
|
||||
|
|
|
@ -105,15 +105,15 @@ public abstract class ChatScreenMixin extends Screen {
|
|||
final List<ChomeNSBotCommand> commands = ChomeNSBotCommandSuggestions.INSTANCE.commands;
|
||||
|
||||
final List<String> moreOrTrustedCommands = commands.stream()
|
||||
.filter((command) -> command.trustLevel != ChomeNSBotCommand.TrustLevel.PUBLIC)
|
||||
.map((command) -> command.name.toLowerCase())
|
||||
.filter(command -> command.trustLevel() != ChomeNSBotCommand.TrustLevel.PUBLIC)
|
||||
.map(command -> command.name().toLowerCase())
|
||||
.toList();
|
||||
|
||||
final List<String> aliases = new ArrayList<>();
|
||||
for (ChomeNSBotCommand command : commands) {
|
||||
if (command.trustLevel == ChomeNSBotCommand.TrustLevel.PUBLIC) continue;
|
||||
if (command.trustLevel() == ChomeNSBotCommand.TrustLevel.PUBLIC) continue;
|
||||
|
||||
aliases.addAll(command.aliases);
|
||||
aliases.addAll(command.aliases());
|
||||
}
|
||||
|
||||
final String chatCommand = chatText.toLowerCase().split("\\s")[0];
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package land.chipmunk.chipmunkmod.modules;
|
||||
|
||||
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
||||
import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand;
|
||||
import land.chipmunk.chipmunkmod.listeners.Listener;
|
||||
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
|
||||
|
@ -9,11 +8,11 @@ import net.kyori.adventure.text.Component;
|
|||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.network.ClientPlayerEntity;
|
||||
import net.minecraft.text.PlainTextContent;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ChomeNSBotCommandSuggestions implements Listener {
|
||||
public static final String ID = "chomens_bot_request_command_suggestion";
|
||||
|
@ -57,40 +56,18 @@ public class ChomeNSBotCommandSuggestions implements Listener {
|
|||
|
||||
@Override
|
||||
public void chatMessageReceived(Text message) {
|
||||
try {
|
||||
final List<Text> children = message.getSiblings();
|
||||
final List<Text> children = message.getSiblings();
|
||||
if (children.isEmpty()) return;
|
||||
|
||||
if (children.isEmpty()) return;
|
||||
final Text textComponent = children.getFirst();
|
||||
if (!textComponent.getString().equals(ID)) return;
|
||||
|
||||
final Text textComponent = children.getFirst();
|
||||
commands = children.stream()
|
||||
.skip(1)
|
||||
.map(ChomeNSBotCommand::fromText)
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
|
||||
if (!textComponent.getString().equals(ID)) return;
|
||||
|
||||
commands = children.subList(1, children.size())
|
||||
.stream()
|
||||
.map(
|
||||
(eachComponent) -> {
|
||||
final ChomeNSBotCommand command = new ChomeNSBotCommand(
|
||||
ChipmunkMod.CONFIG.bots.chomens.prefix + ((PlainTextContent) eachComponent.getContent()).string(),
|
||||
ChomeNSBotCommand.TrustLevel.valueOf(eachComponent.getSiblings().getFirst().getString())
|
||||
);
|
||||
|
||||
if (!Boolean.parseBoolean(eachComponent.getSiblings().get(1).getString())) return command;
|
||||
|
||||
final List<Text> subList = eachComponent.getSiblings().subList(2, eachComponent.getSiblings().size());
|
||||
|
||||
for (Text aliasComponent : subList) {
|
||||
final String alias = aliasComponent.getString();
|
||||
|
||||
command.aliases.add(alias);
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
)
|
||||
.toList();
|
||||
|
||||
receivedSuggestions = true;
|
||||
} catch (Exception ignored) {}
|
||||
receivedSuggestions = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ import com.google.common.base.Suppliers;
|
|||
import net.minecraft.registry.DynamicRegistryManager;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.text.PlainTextContent;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.text.TextContent;
|
||||
|
||||
public class TextUtilities {
|
||||
public static MutableText fromJson (String json) {
|
||||
|
@ -13,4 +15,11 @@ public class TextUtilities {
|
|||
Suppliers.ofInstance(DynamicRegistryManager.of(Registries.REGISTRIES)).get()
|
||||
);
|
||||
}
|
||||
|
||||
public static String plainOrNull(final Text text) {
|
||||
final TextContent content = text.getContent();
|
||||
if (!(content instanceof PlainTextContent plainContent)) return null;
|
||||
|
||||
return plainContent.string();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue