add alias to chome ns bot suggestions

This commit is contained in:
Chayapak 2023-10-01 17:35:33 +07:00
parent 08c3773660
commit 63967e407d
3 changed files with 40 additions and 8 deletions

View file

@ -1,8 +1,12 @@
package land.chipmunk.chipmunkmod.data; package land.chipmunk.chipmunkmod.data;
import java.util.ArrayList;
import java.util.List;
public class ChomeNSBotCommand { public class ChomeNSBotCommand {
public final String name; public final String name;
public final TrustLevel trustLevel; public final TrustLevel trustLevel;
public final List<String> aliases = new ArrayList<>();
public ChomeNSBotCommand ( public ChomeNSBotCommand (
String name, String name,

View file

@ -23,6 +23,7 @@ import javax.net.ssl.HttpsURLConnection;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Mixin(value = net.minecraft.client.gui.screen.ChatScreen.class) @Mixin(value = net.minecraft.client.gui.screen.ChatScreen.class)
@ -104,9 +105,21 @@ public class ChatScreenMixin extends Screen {
.map((command) -> command.name.toLowerCase()) .map((command) -> command.name.toLowerCase())
.toList(); .toList();
if (moreOrTrustedCommands.contains(chatText.toLowerCase().split("\\s")[0])) { final List<String> aliases = new ArrayList<>();
for (ChomeNSBotCommand command : commands) {
aliases.addAll(command.aliases);
}
final String chatCommand = chatText.toLowerCase().split("\\s")[0];
final int prefixLength = ChipmunkMod.CONFIG.bots.chomens.prefix.length();
if (
moreOrTrustedCommands.contains(chatCommand) ||
aliases.contains(chatCommand.substring(prefixLength))
) {
try { try {
BotValidationUtilities.chomens(chatText.substring(ChipmunkMod.CONFIG.bots.chomens.prefix.length())); BotValidationUtilities.chomens(chatText.substring(prefixLength));
cir.setReturnValue(true); cir.setReturnValue(true);
cir.cancel(); cir.cancel();
@ -116,6 +129,8 @@ public class ChatScreenMixin extends Screen {
} }
} }
if (client == null) return;
if (chatText.startsWith("/")) { if (chatText.startsWith("/")) {
client.player.networkHandler.sendChatCommand(chatText.substring(1)); client.player.networkHandler.sendChatCommand(chatText.substring(1));
} else { } else {

View file

@ -13,7 +13,6 @@ import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class ChomeNSBotCommandSuggestions extends Listener { public class ChomeNSBotCommandSuggestions extends Listener {
@ -59,7 +58,7 @@ public class ChomeNSBotCommandSuggestions extends Listener {
final List<Component> children = component.children(); final List<Component> children = component.children();
if (children.size() == 0) return; if (children.isEmpty()) return;
final TextComponent textComponent = (TextComponent) children.get(0); final TextComponent textComponent = (TextComponent) children.get(0);
@ -68,10 +67,24 @@ public class ChomeNSBotCommandSuggestions extends Listener {
commands = children.subList(1, children.size()) commands = children.subList(1, children.size())
.stream() .stream()
.map( .map(
(eachCum) -> new ChomeNSBotCommand( (eachComponent) -> {
ChipmunkMod.CONFIG.bots.chomens.prefix + ((TextComponent) eachCum).content(), final ChomeNSBotCommand command = new ChomeNSBotCommand(
ChomeNSBotCommand.TrustLevel.valueOf(((TextComponent) eachCum.children().get(0)).content()) ChipmunkMod.CONFIG.bots.chomens.prefix + ((TextComponent) eachComponent).content(),
) ChomeNSBotCommand.TrustLevel.valueOf(((TextComponent) eachComponent.children().get(0)).content())
);
if (!Boolean.parseBoolean(((TextComponent) eachComponent.children().get(1)).content())) return command;
final List<Component> subList = eachComponent.children().subList(2, eachComponent.children().size());
for (Component aliasComponent : subList) {
final String alias = ((TextComponent) aliasComponent).content();
command.aliases.add(alias);
}
return command;
}
) )
.toList(); .toList();
} catch (Exception ignored) {} } catch (Exception ignored) {}