Better Chat Color Utility Handling for Commands

This commit is contained in:
Kam committed 2025-02-18 16:11:58 -05:00
1 parent 3cac4e764f
commit eb899061bf
24 files changed
+202 -26

No files matched your search

@@ -9,6 +9,9 @@ import noppes.npcs.controllers.data.PlayerData;
import java.util.List;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class AnimationCommand extends CommandKamkeelBase {
@Override
@@ -17,6 +17,8 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.util.List;
import static kamkeel.npcs.util.ColorUtil.*;
public class CloneCommand extends CommandKamkeelBase {
@Override
@@ -10,6 +10,9 @@ import noppes.npcs.api.IWorld;
import noppes.npcs.scripted.NpcAPI;
import noppes.npcs.scripted.event.CustomNPCsEvent;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendMessage;
public class CommandCommand extends CommandKamkeelBase {
@Override
@@ -55,18 +55,6 @@ public abstract class CommandKamkeelBase extends CommandBase{
return !subcommands.isEmpty();
}
protected static void sendMessage(ICommandSender sender, String message, Object... obs) {
sender.addChatMessage(new ChatComponentTranslation(message, obs));
}
protected static void sendResult(ICommandSender sender, String message, Object... obs) {
sender.addChatMessage(new ChatComponentTranslation("\u00A76[\u00A7eCNPC+\u00A76]\u00A77 " + message, obs));
}
protected static void sendError(ICommandSender sender, String message, Object... obs) {
sender.addChatMessage(new ChatComponentTranslation("\u00A76[\u00A7eCNPC+\u00A76]\u00A74 Error:\u00A7c " + message, obs));
}
@Retention(value = RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SubCommand {
@@ -20,6 +20,9 @@ import noppes.npcs.controllers.PlayerDataController;
import java.util.Arrays;
import java.util.Set;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class ConfigCommand extends CommandKamkeelBase {
@@ -11,6 +11,9 @@ import noppes.npcs.controllers.data.PlayerData;
import java.util.Collection;
import java.util.List;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class DialogCategoryCommand extends CommandKamkeelBase {
@Override
@@ -20,6 +20,9 @@ import noppes.npcs.entity.EntityDialogNpc;
import java.util.Collection;
import java.util.List;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class DialogCommand extends CommandKamkeelBase {
@Override
@@ -13,6 +13,9 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class EffectCommand extends CommandKamkeelBase {
@Override
@@ -26,7 +29,7 @@ public class EffectCommand extends CommandKamkeelBase {
}
@SubCommand(desc = "Lists all Effects")
public void infoAll(ICommandSender sender, String[] args) throws CommandException {
public void list(ICommandSender sender, String[] args) throws CommandException {
CustomEffectController controller = CustomEffectController.getInstance();
if (controller.indexMapper.isEmpty()) {
sendError(sender, "No effects found.");
@@ -11,6 +11,9 @@ import noppes.npcs.controllers.data.PlayerFactionData;
import java.util.Arrays;
import java.util.List;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class FactionCommand extends CommandKamkeelBase {
public Faction selectedFaction;
@@ -7,6 +7,9 @@ import net.minecraft.util.ChatComponentTranslation;
import java.lang.reflect.Method;
import java.util.Map.Entry;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendMessage;
public class HelpCommand extends CommandKamkeelBase{
private CommandKamkeel parent;
@@ -22,6 +22,9 @@ import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;
import java.util.List;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class NpcCommand extends CommandKamkeelBase {
public EntityNPCInterface selectedNpc;
@@ -9,6 +9,9 @@ import noppes.npcs.controllers.data.SkinOverlay;
import java.util.List;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class OverlayCommand extends CommandKamkeelBase {
@Override
@@ -16,6 +16,9 @@ import noppes.npcs.controllers.data.QuestData;
import java.util.Collection;
import java.util.List;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class QuestCategoryCommand extends CommandKamkeelBase {
@Override
@@ -21,6 +21,9 @@ import noppes.npcs.util.ValueUtil;
import java.util.Collection;
import java.util.List;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class QuestCommand extends CommandKamkeelBase {
@Override
@@ -3,6 +3,9 @@ package kamkeel.npcs.command;
import net.minecraft.command.ICommandSender;
import noppes.npcs.controllers.ScriptController;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class ScriptCommand extends CommandKamkeelBase {
@@ -20,6 +20,8 @@ import noppes.npcs.entity.EntityNPCInterface;
import java.util.*;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class SlayCommand extends CommandKamkeelBase {
public Map<String, Class<?>> SlayMap = new LinkedHashMap<String, Class<?>>();
@@ -7,6 +7,9 @@ import net.minecraft.util.ChatComponentTranslation;
import java.lang.reflect.Method;
import java.util.Map.Entry;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendMessage;
public class CommandHelpProfile extends CommandProfileBase {
private CommandProfile parent;
@@ -12,6 +12,8 @@ import net.minecraft.nbt.NBTTagCompound;
import java.util.Map;
import java.util.UUID;
import static kamkeel.npcs.util.ColorUtil.*;
public class CommandProfileAdmin extends CommandProfileBase {
@Override
@@ -4,7 +4,10 @@ import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import noppes.npcs.CustomNpcsPermissions;
import java.lang.annotation.ElementType;
@@ -16,6 +19,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class CommandProfileBase extends CommandBase{
public Map<String, Method> subcommands = new HashMap<String, Method>();
@@ -55,19 +60,7 @@ public abstract class CommandProfileBase extends CommandBase{
return !subcommands.isEmpty();
}
protected static void sendMessage(ICommandSender sender, String message, Object... obs) {
sender.addChatMessage(new ChatComponentTranslation(message, obs));
}
protected static void sendResult(ICommandSender sender, String message, Object... obs) {
sender.addChatMessage(new ChatComponentTranslation("\u00A76[\u00A7eCNPC+\u00A76]\u00A77 " + message, obs));
}
protected static void sendError(ICommandSender sender, String message, Object... obs) {
sender.addChatMessage(new ChatComponentTranslation("\u00A76[\u00A7eCNPC+\u00A76]\u00A74 Error:\u00A7c " + message, obs));
}
@Retention(value = RetentionPolicy.RUNTIME)
@Retention(value = RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SubCommand {
@@ -6,6 +6,9 @@ import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class CommandProfileChange extends CommandProfileBase {
@Override
@@ -9,6 +9,9 @@ import net.minecraft.entity.player.EntityPlayer;
import java.util.Map;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendMessage;
public class CommandProfileList extends CommandProfileBase {
@Override
@@ -6,6 +6,9 @@ import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class CommandProfileRemove extends CommandProfileBase {
@Override
@@ -7,6 +7,9 @@ import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import static kamkeel.npcs.util.ColorUtil.sendError;
import static kamkeel.npcs.util.ColorUtil.sendResult;
public class CommandProfileRename extends CommandProfileBase {
@Override
@@ -1,5 +1,11 @@
package kamkeel.npcs.util;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
public class ColorUtil {
@@ -28,4 +34,133 @@ public class ColorUtil {
float b = (hex & 0xFF) / 255.0F;
return new float[]{r, g, b};
}
/**
* Helper method that retrieves the EnumChatFormatting associated with a given formatting code character.
*
* @param c The formatting code character (e.g., '6' or 'l').
* @return The matching EnumChatFormatting, or null if not found.
*/
public static EnumChatFormatting getFormattingByChar(char c) {
for (EnumChatFormatting format : EnumChatFormatting.values()) {
if (format.getFormattingCode() == c) {
return format;
}
}
return null;
}
/**
* Assembles a composite IChatComponent from a raw text string with formatting codes (using §).
* Each time the formatting changes, a new component is created and appended as a sibling.
*
* @param text The raw text with formatting codes.
* @return A composite IChatComponent reflecting the formatting changes.
*/
public static IChatComponent assembleComponent(String text) {
// Final composite component
ChatComponentText composite = new ChatComponentText("");
// Accumulates text until a formatting code change
ChatComponentText current = new ChatComponentText("");
// Start with a fresh ChatStyle
ChatStyle currentStyle = new ChatStyle();
int i = 0;
while (i < text.length()) {
char c = text.charAt(i);
if (c == '\u00A7' && i + 1 < text.length()) {
// When a formatting code is encountered, first add any accumulated text as a sibling.
if (current.getUnformattedText().length() > 0) {
current.setChatStyle(currentStyle.createDeepCopy());
composite.appendSibling(current);
current = new ChatComponentText("");
}
char code = text.charAt(i + 1);
EnumChatFormatting formatting = getFormattingByChar(code);
if (formatting != null) {
// If it’s a color code (0-9, a-f), then it resets previous formatting.
if ("0123456789abcdef".indexOf(Character.toLowerCase(code)) >= 0) {
currentStyle = new ChatStyle().setColor(formatting);
} else {
// For modifiers (BOLD, ITALIC, etc.)
if (formatting == EnumChatFormatting.RESET) {
currentStyle = new ChatStyle();
} else {
switch (formatting) {
case BOLD:
currentStyle = currentStyle.setBold(Boolean.TRUE);
break;
case ITALIC:
currentStyle = currentStyle.setItalic(Boolean.TRUE);
break;
case UNDERLINE:
currentStyle = currentStyle.setUnderlined(Boolean.TRUE);
break;
case STRIKETHROUGH:
currentStyle = currentStyle.setStrikethrough(Boolean.TRUE);
break;
case OBFUSCATED:
currentStyle = currentStyle.setObfuscated(Boolean.TRUE);
break;
default:
break;
}
}
}
}
i += 2; // Skip the § and the code character.
} else {
// Accumulate normal characters.
current.appendText(String.valueOf(c));
i++;
}
}
// Append any trailing text.
if (current.getUnformattedText().length() > 0) {
current.setChatStyle(currentStyle.createDeepCopy());
composite.appendSibling(current);
}
return composite;
}
private static String LABEL = "\u00A7e[CNPC+] ";
/**
* Assembles a composite chat component from a raw text string with formatting codes,
* and immediately sends it to the specified ICommandSender.
*
* @param sender The ICommandSender to send the message to.
* @param text The raw text (with § codes) to be assembled and sent.
*/
public static void sendMessage(ICommandSender sender, String text) {
text = "\u00A77" + text;
IChatComponent component = assembleComponent(text);
sender.addChatMessage(component);
}
public static void sendMessage(ICommandSender sender, String format, Object... obs) {
sendMessage(sender, String.format(format, obs));
}
public static void sendResult(ICommandSender sender, String text) {
text = LABEL + "\u00A7a" + text;
IChatComponent component = assembleComponent(text);
sender.addChatMessage(component);
}
public static void sendResult(ICommandSender sender, String format, Object... obs) {
sendResult(sender, String.format(format, obs));
}
public static void sendError(ICommandSender sender, String text) {
text = LABEL + "\u00A7c" + text;
IChatComponent component = assembleComponent(text);
sender.addChatMessage(component);
}
public static void sendError(ICommandSender sender, String format, Object... obs) {
sendError(sender, String.format(format, obs));
}
}