Added setPrefix

This commit is contained in:
hhhzzzsss 2023-06-08 21:16:26 -05:00
parent dfa928f255
commit 2bba096600
3 changed files with 60 additions and 25 deletions

View file

@ -23,6 +23,7 @@ public class CommandProcessor {
public static void initCommands() { public static void initCommands() {
commands.add(new helpCommand()); commands.add(new helpCommand());
commands.add(new setPrefixCommand());
commands.add(new playCommand()); commands.add(new playCommand());
commands.add(new stopCommand()); commands.add(new stopCommand());
commands.add(new skipCommand()); commands.add(new skipCommand());
@ -50,7 +51,7 @@ public class CommandProcessor {
// returns true if it is a command and should be cancelled // returns true if it is a command and should be cancelled
public static boolean processChatMessage(String message) { public static boolean processChatMessage(String message) {
if (message.startsWith("$")) { if (message.startsWith(Config.getConfig().prefix)) {
String[] parts = message.substring(1).split(" ", 2); String[] parts = message.substring(1).split(" ", 2);
String name = parts.length>0 ? parts[0] : ""; String name = parts.length>0 ? parts[0] : "";
String args = parts.length>1 ? parts[1] : ""; String args = parts.length>1 ? parts[1] : "";
@ -87,7 +88,7 @@ public class CommandProcessor {
return "help"; return "help";
} }
public String getSyntax() { public String getSyntax() {
return "$help [command]"; return "help [command]";
} }
public String getDescription() { public String getDescription() {
return "Lists commands or explains command"; return "Lists commands or explains command";
@ -96,7 +97,7 @@ public class CommandProcessor {
if (args.length() == 0) { if (args.length() == 0) {
StringBuilder helpMessage = new StringBuilder("§6Commands -"); StringBuilder helpMessage = new StringBuilder("§6Commands -");
for (Command c : commands) { for (Command c : commands) {
helpMessage.append(" $" + c.getName()); helpMessage.append(" " + Config.getConfig().prefix + c.getName());
} }
SongPlayer.addChatMessage(helpMessage.toString()); SongPlayer.addChatMessage(helpMessage.toString());
} }
@ -106,7 +107,7 @@ public class CommandProcessor {
SongPlayer.addChatMessage("§6------------------------------"); SongPlayer.addChatMessage("§6------------------------------");
SongPlayer.addChatMessage("§6Help: §3" + c.getName()); SongPlayer.addChatMessage("§6Help: §3" + c.getName());
SongPlayer.addChatMessage("§6Description: §3" + c.getDescription()); 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) { if (c.getAliases().length > 0) {
SongPlayer.addChatMessage("§6Aliases: §3" + String.join(", ", c.getAliases())); 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 <prefix>";
}
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 { private static class playCommand extends Command {
public String getName() { public String getName() {
return "play"; return "play";
} }
public String getSyntax() { public String getSyntax() {
return "$play <song or url>"; return "play <song or url>";
} }
public String getDescription() { public String getDescription() {
return "Plays a song"; return "Plays a song";
@ -155,7 +186,7 @@ public class CommandProcessor {
return "stop"; return "stop";
} }
public String getSyntax() { public String getSyntax() {
return "$stop"; return "stop";
} }
public String getDescription() { public String getDescription() {
return "Stops playing"; return "Stops playing";
@ -184,7 +215,7 @@ public class CommandProcessor {
return "skip"; return "skip";
} }
public String getSyntax() { public String getSyntax() {
return "$skip"; return "skip";
} }
public String getDescription() { public String getDescription() {
return "Skips current song"; return "Skips current song";
@ -209,7 +240,7 @@ public class CommandProcessor {
return "goto"; return "goto";
} }
public String getSyntax() { public String getSyntax() {
return "$goto <mm:ss>"; return "goto <mm:ss>";
} }
public String getDescription() { public String getDescription() {
return "Goes to a specific time in the song"; return "Goes to a specific time in the song";
@ -242,7 +273,7 @@ public class CommandProcessor {
return "loop"; return "loop";
} }
public String getSyntax() { public String getSyntax() {
return "$loop"; return "loop";
} }
public String getDescription() { public String getDescription() {
return "Toggles song looping"; return "Toggles song looping";
@ -273,7 +304,7 @@ public class CommandProcessor {
return new String[]{"current"}; return new String[]{"current"};
} }
public String getSyntax() { public String getSyntax() {
return "$status"; return "status";
} }
public String getDescription() { public String getDescription() {
return "Gets the status of the song that is currently playing"; return "Gets the status of the song that is currently playing";
@ -304,7 +335,7 @@ public class CommandProcessor {
return new String[]{"showQueue"}; return new String[]{"showQueue"};
} }
public String getSyntax() { public String getSyntax() {
return "$queue"; return "queue";
} }
public String getDescription() { public String getDescription() {
return "Shows the current song queue"; return "Shows the current song queue";
@ -342,7 +373,7 @@ public class CommandProcessor {
return new String[]{"list"}; return new String[]{"list"};
} }
public String getSyntax() { public String getSyntax() {
return "$songs"; return "songs";
} }
public String getDescription() { public String getDescription() {
return "Lists available songs"; return "Lists available songs";
@ -378,7 +409,7 @@ public class CommandProcessor {
return new String[]{"sc"}; return new String[]{"sc"};
} }
public String getSyntax() { public String getSyntax() {
return "$setCreativeCommand <command>"; return "setCreativeCommand <command>";
} }
public String getDescription() { public String getDescription() {
return "Sets the command used to go into creative mode"; return "Sets the command used to go into creative mode";
@ -408,7 +439,7 @@ public class CommandProcessor {
return new String[]{"ss"}; return new String[]{"ss"};
} }
public String getSyntax() { public String getSyntax() {
return "$setSurvivalCommand <command>"; return "setSurvivalCommand <command>";
} }
public String getDescription() { public String getDescription() {
return "Sets the command used to go into survival mode"; return "Sets the command used to go into survival mode";
@ -438,7 +469,7 @@ public class CommandProcessor {
return new String[]{"essentials", "useEssentials", "essentialsCommands"}; return new String[]{"essentials", "useEssentials", "essentialsCommands"};
} }
public String getSyntax() { public String getSyntax() {
return "$useEssentialsCommands"; return "useEssentialsCommands";
} }
public String getDescription() { public String getDescription() {
return "Switches to using essentials gamemode commands"; return "Switches to using essentials gamemode commands";
@ -465,7 +496,7 @@ public class CommandProcessor {
return new String[]{"vanilla", "useVanilla", "vanillaCommands"}; return new String[]{"vanilla", "useVanilla", "vanillaCommands"};
} }
public String getSyntax() { public String getSyntax() {
return "$useVanillaCommands"; return "useVanillaCommands";
} }
public String getDescription() { public String getDescription() {
return "Switches to using vanilla gamemode commands"; return "Switches to using vanilla gamemode commands";
@ -492,7 +523,7 @@ public class CommandProcessor {
return new String[]{"fakePlayer", "fp"}; return new String[]{"fakePlayer", "fp"};
} }
public String getSyntax() { public String getSyntax() {
return "$toggleFakePlayer"; return "toggleFakePlayer";
} }
public String getDescription() { public String getDescription() {
return "Shows a fake player representing your true position when playing songs"; return "Shows a fake player representing your true position when playing songs";
@ -520,7 +551,7 @@ public class CommandProcessor {
return "testSong"; return "testSong";
} }
public String getSyntax() { public String getSyntax() {
return "$testSong"; return "testSong";
} }
public String getDescription() { public String getDescription() {
return "Creates a song for testing"; return "Creates a song for testing";
@ -541,17 +572,16 @@ public class CommandProcessor {
} }
} }
// $ prefix included in command string
public static CompletableFuture<Suggestions> handleSuggestions(String text, SuggestionsBuilder suggestionsBuilder) { public static CompletableFuture<Suggestions> handleSuggestions(String text, SuggestionsBuilder suggestionsBuilder) {
if (!text.contains(" ")) { if (!text.contains(" ")) {
List<String> names = commandCompletions List<String> names = commandCompletions
.stream() .stream()
.map((commandName) -> "$"+commandName) .map((commandName) -> Config.getConfig().prefix+commandName)
.collect(Collectors.toList()); .collect(Collectors.toList());
return CommandSource.suggestMatching(names, suggestionsBuilder); return CommandSource.suggestMatching(names, suggestionsBuilder);
} else { } else {
String[] split = text.split(" "); String[] split = text.split(" ");
if (split[0].startsWith("$")) { if (split[0].startsWith(Config.getConfig().prefix)) {
String commandName = split[0].substring(1).toLowerCase(); String commandName = split[0].substring(1).toLowerCase();
if (commandMap.containsKey(commandName)) { if (commandMap.containsKey(commandName)) {
return commandMap.get(commandName).getSuggestions(split.length == 1 ? "" : split[1], suggestionsBuilder); return commandMap.get(commandName).getSuggestions(split.length == 1 ? "" : split[1], suggestionsBuilder);

View file

@ -11,6 +11,7 @@ public class Config {
public static final File CONFIG_FILE = new File(SongPlayer.SONGPLAYER_DIR, "config.json"); public static final File CONFIG_FILE = new File(SongPlayer.SONGPLAYER_DIR, "config.json");
private static final Gson gson = new Gson(); private static final Gson gson = new Gson();
public String prefix = "$";
public String creativeCommand = "gmc"; public String creativeCommand = "gmc";
public String survivalCommand = "gms"; public String survivalCommand = "gms";
public boolean showFakePlayer = false; public boolean showFakePlayer = false;
@ -21,10 +22,12 @@ public class Config {
try { try {
if (CONFIG_FILE.exists()) { if (CONFIG_FILE.exists()) {
loadConfig(); loadConfig();
} else { }
else {
saveConfig(); saveConfig();
} }
} catch (IOException e) { }
catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -50,7 +53,8 @@ public class Config {
public static void saveConfigWithErrorHandling() { public static void saveConfigWithErrorHandling() {
try { try {
Config.saveConfig(); Config.saveConfig();
} catch (IOException e) { }
catch (IOException e) {
if (SongPlayer.MC.world != null) { if (SongPlayer.MC.world != null) {
SongPlayer.addChatMessage("§cFailed to save config file"); SongPlayer.addChatMessage("§cFailed to save config file");
} }

View file

@ -1,6 +1,7 @@
package com.github.hhhzzzsss.songplayer.mixin; package com.github.hhhzzzsss.songplayer.mixin;
import com.github.hhhzzzsss.songplayer.CommandProcessor; import com.github.hhhzzzsss.songplayer.CommandProcessor;
import com.github.hhhzzzsss.songplayer.Config;
import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder; import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.client.gui.screen.ChatInputSuggestor; import net.minecraft.client.gui.screen.ChatInputSuggestor;
@ -38,7 +39,7 @@ public class ChatInputSuggestorMixin {
String textStr = this.textField.getText(); String textStr = this.textField.getText();
int cursorPos = this.textField.getCursor(); int cursorPos = this.textField.getCursor();
String preStr = textStr.substring(0, cursorPos); String preStr = textStr.substring(0, cursorPos);
if (!preStr.startsWith("$")) { if (!preStr.startsWith(Config.getConfig().prefix)) {
return; return;
} }