replace magic string identifiers with MessageIdentifiers constants

This commit is contained in:
Levent
2026-03-24 19:49:54 +00:00
parent 3f8f8cfab9
commit c9f0df5116
3 changed files with 23 additions and 18 deletions
@@ -9,6 +9,7 @@ import me.alpha432.oyvey.util.TextUtil;
import me.alpha432.oyvey.util.player.ChatUtil;
import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
import static me.alpha432.oyvey.features.commands.MessageIdentifiers.*;
public abstract class Command extends Feature {
public static final int NO_OP = -1;
@@ -25,7 +26,7 @@ public abstract class Command extends Feature {
public abstract void createArgumentBuilder(LiteralArgumentBuilder<CommandManager> builder);
protected int success(String message, Object... format) {
sendMessage(message, "success", format);
sendMessage(message, SUCCESS, format);
return NO_OP;
}
@@ -34,7 +35,7 @@ public abstract class Command extends Feature {
}
protected int fail(String message, Object... format) {
sendMessage("{red} " + format, "fail");
sendMessage("{red} " + message, FAIL, format);
return NO_OP;
}
@@ -59,16 +60,12 @@ public abstract class Command extends Feature {
}
public static void sendMessage(String message, String identifier) {
if (message == null) {
return;
}
if (message == null) return;
ChatUtil.sendMessage(TextUtil.text(message), identifier);
}
public static void sendMessage(String message, String identifier, Object... format) {
if (message == null) {
return;
}
if (message == null) return;
ChatUtil.sendMessage(TextUtil.text(message, format), identifier);
}
@@ -79,4 +76,4 @@ public abstract class Command extends Feature {
public static <T> RequiredArgumentBuilder<CommandManager, T> argument(String name, ArgumentType<T> type) {
return RequiredArgumentBuilder.argument(name, type);
}
}
}
@@ -0,0 +1,7 @@
package me.alpha432.oyvey.features.commands;
public class MessageIdentifiers {
public static final String GENERAL = "general";
public static final String SUCCESS = "success";
public static final String FAIL = "fail";
}
@@ -17,6 +17,7 @@ import java.util.*;
import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
import static me.alpha432.oyvey.features.commands.Command.SINGLE_FAILURE;
import static me.alpha432.oyvey.features.commands.MessageIdentifiers.GENERAL;
public class CommandManager extends Feature implements Jsonable {
private static final Logger LOGGER = LogManager.getLogger("Commands");
@@ -45,16 +46,16 @@ public class CommandManager extends Feature implements Jsonable {
public void onChatSent(String message) {
try {
int result = dispatcher.execute(message.substring(
commandPrefix.length()).trim(), this);
int result = dispatcher.execute(message.substring(commandPrefix.length()).trim(), this);
if (result == SINGLE_SUCCESS) {
Command.sendMessage("{green} Command executed successfully", "general");
Command.sendMessage("{green} Command executed successfully", GENERAL);
} else if (result == SINGLE_FAILURE) {
Command.sendMessage("{red} Failed to execute command", "general");
Command.sendMessage("{red} Failed to execute command", GENERAL);
}
} catch (CommandSyntaxException e) {
LOGGER.error("Failed to execute command", e);
Command.sendMessage("{red} %s", e.getMessage());
Command.sendMessage("{red} %s", GENERAL, e.getMessage());
}
}
@@ -101,10 +102,10 @@ public class CommandManager extends Feature implements Jsonable {
@Override
public void fromJson(JsonElement element) {
if (element == null || !element.isJsonObject()) {
return;
}
if (element == null || !element.isJsonObject()) return;
JsonObject object = element.getAsJsonObject();
if (object.has("prefix")) {
setCommandPrefix(object.get("prefix").getAsString());
}
@@ -114,4 +115,4 @@ public class CommandManager extends Feature implements Jsonable {
public String getFileName() {
return "commands.json";
}
}
}