From 2bba096600fcc125b3a0717727e4683d61df3f68 Mon Sep 17 00:00:00 2001 From: hhhzzzsss Date: Thu, 8 Jun 2023 21:16:26 -0500 Subject: [PATCH] Added setPrefix --- .../songplayer/CommandProcessor.java | 72 +++++++++++++------ .../github/hhhzzzsss/songplayer/Config.java | 10 ++- .../mixin/ChatInputSuggestorMixin.java | 3 +- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java index f61a2c8..8095c04 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java @@ -23,6 +23,7 @@ public class CommandProcessor { public static void initCommands() { commands.add(new helpCommand()); + commands.add(new setPrefixCommand()); commands.add(new playCommand()); commands.add(new stopCommand()); commands.add(new skipCommand()); @@ -50,7 +51,7 @@ public class CommandProcessor { // returns true if it is a command and should be cancelled public static boolean processChatMessage(String message) { - if (message.startsWith("$")) { + if (message.startsWith(Config.getConfig().prefix)) { String[] parts = message.substring(1).split(" ", 2); String name = parts.length>0 ? parts[0] : ""; String args = parts.length>1 ? parts[1] : ""; @@ -87,7 +88,7 @@ public class CommandProcessor { return "help"; } public String getSyntax() { - return "$help [command]"; + return "help [command]"; } public String getDescription() { return "Lists commands or explains command"; @@ -96,7 +97,7 @@ public class CommandProcessor { if (args.length() == 0) { StringBuilder helpMessage = new StringBuilder("§6Commands -"); for (Command c : commands) { - helpMessage.append(" $" + c.getName()); + helpMessage.append(" " + Config.getConfig().prefix + c.getName()); } SongPlayer.addChatMessage(helpMessage.toString()); } @@ -106,7 +107,7 @@ public class CommandProcessor { SongPlayer.addChatMessage("§6------------------------------"); SongPlayer.addChatMessage("§6Help: §3" + c.getName()); SongPlayer.addChatMessage("§6Description: §3" + c.getDescription()); - SongPlayer.addChatMessage("§6Usage: §3" + c.getSyntax()); + SongPlayer.addChatMessage("§6Usage: §3" + Config.getConfig().prefix + c.getSyntax()); if (c.getAliases().length > 0) { SongPlayer.addChatMessage("§6Aliases: §3" + String.join(", ", c.getAliases())); } @@ -122,12 +123,42 @@ public class CommandProcessor { } } + private static class setPrefixCommand extends Command { + public String getName() { + return "setPrefix"; + } + public String[] getAliases() { + return new String[]{"prefix"}; + } + public String getSyntax() { + return "setPrefix "; + } + public String getDescription() { + return "Sets the command prefix used by SongPlayer"; + } + public boolean processCommand(String args) { + if (args.contains(" ")) { + SongPlayer.addChatMessage("§cPrefix cannot contain a space"); + return true; + } + else if (args.length() > 0) { + Config.getConfig().prefix = args; + SongPlayer.addChatMessage("§6Set prefix to " + args); + Config.saveConfigWithErrorHandling(); + return true; + } + else { + return false; + } + } + } + private static class playCommand extends Command { public String getName() { return "play"; } public String getSyntax() { - return "$play "; + return "play "; } public String getDescription() { return "Plays a song"; @@ -155,7 +186,7 @@ public class CommandProcessor { return "stop"; } public String getSyntax() { - return "$stop"; + return "stop"; } public String getDescription() { return "Stops playing"; @@ -184,7 +215,7 @@ public class CommandProcessor { return "skip"; } public String getSyntax() { - return "$skip"; + return "skip"; } public String getDescription() { return "Skips current song"; @@ -209,7 +240,7 @@ public class CommandProcessor { return "goto"; } public String getSyntax() { - return "$goto "; + return "goto "; } public String getDescription() { return "Goes to a specific time in the song"; @@ -242,7 +273,7 @@ public class CommandProcessor { return "loop"; } public String getSyntax() { - return "$loop"; + return "loop"; } public String getDescription() { return "Toggles song looping"; @@ -273,7 +304,7 @@ public class CommandProcessor { return new String[]{"current"}; } public String getSyntax() { - return "$status"; + return "status"; } public String getDescription() { return "Gets the status of the song that is currently playing"; @@ -304,7 +335,7 @@ public class CommandProcessor { return new String[]{"showQueue"}; } public String getSyntax() { - return "$queue"; + return "queue"; } public String getDescription() { return "Shows the current song queue"; @@ -342,7 +373,7 @@ public class CommandProcessor { return new String[]{"list"}; } public String getSyntax() { - return "$songs"; + return "songs"; } public String getDescription() { return "Lists available songs"; @@ -378,7 +409,7 @@ public class CommandProcessor { return new String[]{"sc"}; } public String getSyntax() { - return "$setCreativeCommand "; + return "setCreativeCommand "; } public String getDescription() { return "Sets the command used to go into creative mode"; @@ -408,7 +439,7 @@ public class CommandProcessor { return new String[]{"ss"}; } public String getSyntax() { - return "$setSurvivalCommand "; + return "setSurvivalCommand "; } public String getDescription() { return "Sets the command used to go into survival mode"; @@ -438,7 +469,7 @@ public class CommandProcessor { return new String[]{"essentials", "useEssentials", "essentialsCommands"}; } public String getSyntax() { - return "$useEssentialsCommands"; + return "useEssentialsCommands"; } public String getDescription() { return "Switches to using essentials gamemode commands"; @@ -465,7 +496,7 @@ public class CommandProcessor { return new String[]{"vanilla", "useVanilla", "vanillaCommands"}; } public String getSyntax() { - return "$useVanillaCommands"; + return "useVanillaCommands"; } public String getDescription() { return "Switches to using vanilla gamemode commands"; @@ -492,7 +523,7 @@ public class CommandProcessor { return new String[]{"fakePlayer", "fp"}; } public String getSyntax() { - return "$toggleFakePlayer"; + return "toggleFakePlayer"; } public String getDescription() { return "Shows a fake player representing your true position when playing songs"; @@ -520,7 +551,7 @@ public class CommandProcessor { return "testSong"; } public String getSyntax() { - return "$testSong"; + return "testSong"; } public String getDescription() { return "Creates a song for testing"; @@ -541,17 +572,16 @@ public class CommandProcessor { } } - // $ prefix included in command string public static CompletableFuture handleSuggestions(String text, SuggestionsBuilder suggestionsBuilder) { if (!text.contains(" ")) { List names = commandCompletions .stream() - .map((commandName) -> "$"+commandName) + .map((commandName) -> Config.getConfig().prefix+commandName) .collect(Collectors.toList()); return CommandSource.suggestMatching(names, suggestionsBuilder); } else { String[] split = text.split(" "); - if (split[0].startsWith("$")) { + if (split[0].startsWith(Config.getConfig().prefix)) { String commandName = split[0].substring(1).toLowerCase(); if (commandMap.containsKey(commandName)) { return commandMap.get(commandName).getSuggestions(split.length == 1 ? "" : split[1], suggestionsBuilder); diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/Config.java b/src/main/java/com/github/hhhzzzsss/songplayer/Config.java index 6c15bfa..55523f1 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/Config.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/Config.java @@ -11,6 +11,7 @@ public class Config { public static final File CONFIG_FILE = new File(SongPlayer.SONGPLAYER_DIR, "config.json"); private static final Gson gson = new Gson(); + public String prefix = "$"; public String creativeCommand = "gmc"; public String survivalCommand = "gms"; public boolean showFakePlayer = false; @@ -21,10 +22,12 @@ public class Config { try { if (CONFIG_FILE.exists()) { loadConfig(); - } else { + } + else { saveConfig(); } - } catch (IOException e) { + } + catch (IOException e) { e.printStackTrace(); } } @@ -50,7 +53,8 @@ public class Config { public static void saveConfigWithErrorHandling() { try { Config.saveConfig(); - } catch (IOException e) { + } + catch (IOException e) { if (SongPlayer.MC.world != null) { SongPlayer.addChatMessage("§cFailed to save config file"); } diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ChatInputSuggestorMixin.java b/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ChatInputSuggestorMixin.java index 2466ea0..a37e996 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ChatInputSuggestorMixin.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/mixin/ChatInputSuggestorMixin.java @@ -1,6 +1,7 @@ package com.github.hhhzzzsss.songplayer.mixin; import com.github.hhhzzzsss.songplayer.CommandProcessor; +import com.github.hhhzzzsss.songplayer.Config; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.client.gui.screen.ChatInputSuggestor; @@ -38,7 +39,7 @@ public class ChatInputSuggestorMixin { String textStr = this.textField.getText(); int cursorPos = this.textField.getCursor(); String preStr = textStr.substring(0, cursorPos); - if (!preStr.startsWith("$")) { + if (!preStr.startsWith(Config.getConfig().prefix)) { return; }