mirror of
https://code.chipmunk.land/ChomeNS/chipmunkmod.git
synced 2025-11-13 22:16:14 +00:00
refactor: rewrite chomensbot suggestion handling
This commit is contained in:
parent
a4a9fcf0b4
commit
ef47598b78
5 changed files with 67 additions and 51 deletions
|
|
@ -1,25 +1,54 @@
|
||||||
package land.chipmunk.chipmunkmod.data;
|
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.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ChomeNSBotCommand {
|
public record ChomeNSBotCommand(String name, TrustLevel trustLevel, List<String> aliases) {
|
||||||
public final String name;
|
public static @Nullable ChomeNSBotCommand fromText(final Text component) {
|
||||||
public final TrustLevel trustLevel;
|
final String name = TextUtilities.plainOrNull(component);
|
||||||
public final List<String> aliases = new ArrayList<>();
|
if (name == null) return null;
|
||||||
|
|
||||||
public ChomeNSBotCommand (
|
final List<Text> children = component.getSiblings();
|
||||||
String name,
|
if (children.size() < 2) return null; // must have at least trust level and alias boolean
|
||||||
TrustLevel trustLevel
|
|
||||||
) {
|
final TrustLevel trustLevel = TrustLevel.fromText(children.getFirst());
|
||||||
this.name = name;
|
if (trustLevel == null) return null;
|
||||||
this.trustLevel = trustLevel;
|
|
||||||
|
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 enum TrustLevel {
|
||||||
PUBLIC,
|
PUBLIC,
|
||||||
TRUSTED,
|
TRUSTED,
|
||||||
ADMIN,
|
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 com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||||
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
||||||
import land.chipmunk.chipmunkmod.command.CommandManager;
|
import land.chipmunk.chipmunkmod.command.CommandManager;
|
||||||
|
import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand;
|
||||||
import land.chipmunk.chipmunkmod.modules.ChomeNSBotCommandSuggestions;
|
import land.chipmunk.chipmunkmod.modules.ChomeNSBotCommandSuggestions;
|
||||||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
|
||||||
import net.minecraft.client.MinecraftClient;
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
|
@ -62,7 +63,7 @@ public class ChatInputSuggestorMixin {
|
||||||
|
|
||||||
final List<String> commands = ChomeNSBotCommandSuggestions.INSTANCE.commands
|
final List<String> commands = ChomeNSBotCommandSuggestions.INSTANCE.commands
|
||||||
.stream()
|
.stream()
|
||||||
.map((command) -> command.name)
|
.map(ChomeNSBotCommand::name)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
pendingSuggestions = CommandSource.suggestMatching(
|
pendingSuggestions = CommandSource.suggestMatching(
|
||||||
|
|
|
||||||
|
|
@ -105,15 +105,15 @@ public abstract class ChatScreenMixin extends Screen {
|
||||||
final List<ChomeNSBotCommand> commands = ChomeNSBotCommandSuggestions.INSTANCE.commands;
|
final List<ChomeNSBotCommand> commands = ChomeNSBotCommandSuggestions.INSTANCE.commands;
|
||||||
|
|
||||||
final List<String> moreOrTrustedCommands = commands.stream()
|
final List<String> moreOrTrustedCommands = commands.stream()
|
||||||
.filter((command) -> command.trustLevel != ChomeNSBotCommand.TrustLevel.PUBLIC)
|
.filter(command -> command.trustLevel() != ChomeNSBotCommand.TrustLevel.PUBLIC)
|
||||||
.map((command) -> command.name.toLowerCase())
|
.map(command -> command.name().toLowerCase())
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
final List<String> aliases = new ArrayList<>();
|
final List<String> aliases = new ArrayList<>();
|
||||||
for (ChomeNSBotCommand command : commands) {
|
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];
|
final String chatCommand = chatText.toLowerCase().split("\\s")[0];
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package land.chipmunk.chipmunkmod.modules;
|
package land.chipmunk.chipmunkmod.modules;
|
||||||
|
|
||||||
import land.chipmunk.chipmunkmod.ChipmunkMod;
|
|
||||||
import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand;
|
import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand;
|
||||||
import land.chipmunk.chipmunkmod.listeners.Listener;
|
import land.chipmunk.chipmunkmod.listeners.Listener;
|
||||||
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
|
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.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
import net.minecraft.client.MinecraftClient;
|
import net.minecraft.client.MinecraftClient;
|
||||||
import net.minecraft.client.network.ClientPlayerEntity;
|
import net.minecraft.client.network.ClientPlayerEntity;
|
||||||
import net.minecraft.text.PlainTextContent;
|
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ChomeNSBotCommandSuggestions implements Listener {
|
public class ChomeNSBotCommandSuggestions implements Listener {
|
||||||
public static final String ID = "chomens_bot_request_command_suggestion";
|
public static final String ID = "chomens_bot_request_command_suggestion";
|
||||||
|
|
@ -57,40 +56,18 @@ public class ChomeNSBotCommandSuggestions implements Listener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void chatMessageReceived(Text message) {
|
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;
|
receivedSuggestions = true;
|
||||||
|
|
||||||
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) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@ import com.google.common.base.Suppliers;
|
||||||
import net.minecraft.registry.DynamicRegistryManager;
|
import net.minecraft.registry.DynamicRegistryManager;
|
||||||
import net.minecraft.registry.Registries;
|
import net.minecraft.registry.Registries;
|
||||||
import net.minecraft.text.MutableText;
|
import net.minecraft.text.MutableText;
|
||||||
|
import net.minecraft.text.PlainTextContent;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.text.TextContent;
|
||||||
|
|
||||||
public class TextUtilities {
|
public class TextUtilities {
|
||||||
public static MutableText fromJson (String json) {
|
public static MutableText fromJson (String json) {
|
||||||
|
|
@ -13,4 +15,11 @@ public class TextUtilities {
|
||||||
Suppliers.ofInstance(DynamicRegistryManager.of(Registries.REGISTRIES)).get()
|
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
Reference in a new issue