feat: support Chat Colors

Ref: https://www.curseforge.com/minecraft/mc-mods/chat-colours
This commit is contained in:
evenwan 2024-09-14 14:28:32 +08:00
parent 5a0aa9694d
commit 2cfacd4a70
6 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,35 @@
import { CharacterBuilder, TextBuilder } from "../builder/text";
import { GradientProcessor, GradientProcessorConstructor } from "../processor-core";
class ChatColorsAdapterClazz extends GradientProcessor {
constructor(tags: RichTagChunk, colors: HexColorString[], options?: GradientProcessAdapterOptions) {
super(tags, colors, options);
}
override get format(): FormatPresets {
return {
bold: ["**", "**"],
italic: ["*", "*"],
underlined: ["__", "__"],
strikethrough: ["~~", "~~"],
obfuscated: "",
reset: "",
};
}
processor(chunk: Chunk): string {
const textBuilder = new TextBuilder();
textBuilder.withFormat(chunk.format?.bold, this.format.bold);
textBuilder.withFormat(chunk.format?.italic, this.format.italic);
textBuilder.withFormat(chunk.format?.underlined, this.format.underlined);
textBuilder.withFormat(chunk.format?.strikethrough, this.format.strikethrough);
chunk.tags.forEach((tag) => {
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(tag.color, "[{color}]"));
});
return textBuilder.build();
}
}
export const ChatColorsAdapter: GradientProcessorConstructor = ChatColorsAdapterClazz;

View File

@ -13,6 +13,7 @@ import { MotdAdapter } from "./adapter/motd";
import { BBCodeAdapter } from "./adapter/bbcode";
import { HTMLAdapter } from "./adapter/html";
import { CSVAdapter } from "./adapter/csv";
import { ChatColorsAdapter } from "./adapter/chat-colors";
interface AdapterMapValue<T = any> {
label: `processor.adapter.${string}.label`;
@ -43,6 +44,7 @@ export const adapterMap = new Map<KeyOfAdapterMap, AdapterMapValue>([
},
],
["trchat", { label: "processor.adapter.trchat.label", sample: "&{#RRGGBB}", adapter: TrChatAdapter }],
["chat-colors", { label: "processor.adapter.chat_colors.label", sample: "[#RRGGBB]", adapter: ChatColorsAdapter }],
["motd", { label: "processor.adapter.motd.label", sample: "\\u00A7X", adapter: MotdAdapter }],
["bbcode", { label: "processor.adapter.bbcode.label", sample: "[color=#RRGGBB]", adapter: BBCodeAdapter }],
[
@ -68,6 +70,7 @@ export const adapterMapKey = [
"minedown",
"stringified-nbt",
"trchat",
"chat-colors",
"motd",
"bbcode",
"json",

View File

@ -122,6 +122,9 @@ export default {
trchat: {
label: "TrChat",
},
chat_colors: {
label: "Chat Colors",
},
motd: {
label: "MOTD",
},

View File

@ -124,6 +124,9 @@ export default {
trchat: {
label: "TrChat",
},
chat_colors: {
label: "Chat Colors",
},
motd: {
label: "MOTD",
},

View File

@ -119,6 +119,9 @@ export default {
trchat: {
label: "TrChat",
},
chat_colors: {
label: "Chat Colors",
},
motd: {
label: "MOTD",
},

View File

@ -118,6 +118,9 @@ export default {
trchat: {
label: "TrChat",
},
chat_colors: {
label: "Chat Colors",
},
motd: {
label: "MOTD",
},