feat: add tellraw & Proxy utils
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
e4bb878648
commit
7bf5f53afd
27
packages/common/src/proxy.ts
Normal file
27
packages/common/src/proxy.ts
Normal file
@ -0,0 +1,27 @@
|
||||
let createProxy = eval(`
|
||||
function(handle){ return new JSAdapter(handle) }
|
||||
`)
|
||||
export interface ProxyHandle {
|
||||
get: (target: any, name: string) => any
|
||||
set: (target: any, name: string, value: any) => void
|
||||
construct: (target: any, ...args: any[]) => any
|
||||
has: (target: any, name: string) => boolean
|
||||
ownKeys: (target: any) => string[]
|
||||
values: (target: any) => any[];
|
||||
call: (target: any, name: string, ...args: any[]) => any
|
||||
delete: (target: any, name: string) => boolean
|
||||
}
|
||||
export class Proxy {
|
||||
static newProxy(target: any, handle: Partial<ProxyHandle>) {
|
||||
return createProxy({
|
||||
__get__: (name: string) => handle.get ? handle.get(target, name) : target[name],
|
||||
__put__: (name: string, value: any) => handle.set ? handle.set(target, name, value) : target[name] = value,
|
||||
__call__: (name: string, ...args: any) => handle.call ? handle.call(target, name, ...args) : target[name](...args),
|
||||
__new__: (...args: any) => handle.construct ? handle.construct(target, ...args) : new target(...args),
|
||||
__getIds__: () => handle.ownKeys ? handle.ownKeys(target) : Object.keys(target),
|
||||
__getValues__: () => handle.values ? handle.values(target) : Object.values(target),
|
||||
__has__: (name: string) => handle.has ? handle.has(target, name) : Object.getOwnPropertyDescriptor(target, name) != undefined,
|
||||
__delete__: (name: string) => handle.delete ? handle.delete(target, name) : delete target[name]
|
||||
})
|
||||
}
|
||||
}
|
101
packages/common/src/tellraw.ts
Normal file
101
packages/common/src/tellraw.ts
Normal file
@ -0,0 +1,101 @@
|
||||
class ChatMessagePart {
|
||||
private internal: any = {}
|
||||
|
||||
get text() {
|
||||
return this.internal.text
|
||||
}
|
||||
set text(text: string) {
|
||||
this.internal.text = text
|
||||
}
|
||||
|
||||
click(action: string, value: string) {
|
||||
this.internal.clickEvent = { action, value }
|
||||
}
|
||||
|
||||
hover(action: string, value: string) {
|
||||
this.internal.hoverEvent = { action, value }
|
||||
}
|
||||
|
||||
convert() {
|
||||
return this.internal;
|
||||
}
|
||||
}
|
||||
|
||||
class Tellraw {
|
||||
static duplicateChar = '§卐'
|
||||
static create() {
|
||||
return new Tellraw().then(Tellraw.duplicateChar);
|
||||
}
|
||||
|
||||
private cache: string = '';
|
||||
private parts = [new ChatMessagePart()];
|
||||
|
||||
then(part: ChatMessagePart | string) {
|
||||
if (typeof part === "string") {
|
||||
var newPart = new ChatMessagePart();
|
||||
newPart.text = part
|
||||
this.then(newPart);
|
||||
return this;
|
||||
}
|
||||
var last = this.latest();
|
||||
if (!last.text) {
|
||||
last.text = part.text;
|
||||
} else {
|
||||
this.parts.push(part);
|
||||
}
|
||||
this.cache = null;
|
||||
}
|
||||
|
||||
text(text: string) {
|
||||
this.latest().text = text;
|
||||
return this;
|
||||
}
|
||||
|
||||
tip(text: string) {
|
||||
this.latest().hover("show_text", text);
|
||||
return this;
|
||||
}
|
||||
|
||||
item(text: string) {
|
||||
this.latest().hover("show_item", text);
|
||||
return this;
|
||||
}
|
||||
|
||||
command(command: string) {
|
||||
this.latest().click("run_command", command);
|
||||
return this;
|
||||
}
|
||||
|
||||
suggest(url: string) {
|
||||
this.latest().click("suggest_command", url);
|
||||
return this;
|
||||
}
|
||||
|
||||
file(path: string) {
|
||||
this.latest().click("open_file", path);
|
||||
return this;
|
||||
}
|
||||
|
||||
link(url: string) {
|
||||
this.latest().click("open_url", url);
|
||||
return this;
|
||||
}
|
||||
|
||||
latest() {
|
||||
return this.parts[this.parts.length - 1];
|
||||
}
|
||||
|
||||
json() {
|
||||
if (!this.cache) {
|
||||
var temp = [];
|
||||
this.parts.forEach(function(t) {
|
||||
temp.push(t.convert());
|
||||
});
|
||||
this.cache = JSON.stringify(temp);
|
||||
console.trace(this.cache);
|
||||
}
|
||||
return this.cache;
|
||||
}
|
||||
}
|
||||
|
||||
export default Tellraw
|
Loading…
Reference in New Issue
Block a user