feat: 分离TellRaw类库

This commit is contained in:
coding 2018-05-15 13:33:52 +00:00
parent 3d3bf20acd
commit 89cab251c7
2 changed files with 142 additions and 136 deletions

View File

@ -0,0 +1,139 @@
/*global Java, base, module, exports, require*/
var chat = require('api/chat');
var ChatMessagePart = function () {
var text;
var clickEventAction;
var clickEventValue;
var hoverEventAction;
var hoverEventValue;
var insertion;
this.click = function (action, value) {
this.clickEventAction = action;
this.clickEventValue = value;
}
this.hover = function (action, value) {
this.hoverEventAction = action;
this.hoverEventValue = value;
console.log(this.toJson());
}
this.convert = function () {
var str = {};
if (this.text) {
str.text = this.text;
}
if (this.clickEventAction) {
str.clickEvent = {
"action": this.clickEventAction,
"value": this.clickEventValue
}
}
if (this.hoverEventAction) {
str.hoverEvent = {
"action": this.hoverEventAction,
"value": this.hoverEventValue
}
}
if (this.insertion) {
str.insertion = this.insertion;
}
return str;
}
}
var Tellraw = function () {
var parts = [new ChatMessagePart()];
var self = this;
var cache = null;
this.then = function (part) {
if (typeof part === "string") {
var newPart = new ChatMessagePart();
newPart.text = part;
this.then(newPart);
return self;
}
var last = this.latest();
if (!last.text) {
last.text = part.text;
} else {
parts.push(part);
}
this.cache = null;
}
this.text = function (text) {
this.latest().text = text;
return this;
}
this.tip = function (str) {
if (toString.call(str) === "[object Array]") {
str = str.join("\n");
}
this.latest().hover("show_text", str);
return this;
}
this.item = function (str) {
this.latest().hover("show_item", str);
return this;
}
this.cmd = this.command = function (command) {
this.latest().click("run_command", command);
return this;
}
this.suggest = function (url) {
this.latest().click("suggest_command", url);
return this;
}
this.file = function (path) {
this.latest().click("open_file", path);
return this;
}
this.link = function (url) {
this.latest().click("open_url", url);
return this;
}
this.latest = function () {
return parts[parts.length - 1];
}
this.json = function () {
if (!this.cache) {
var temp = [];
parts.forEach(function (t) {
temp.push(t.convert());
})
this.cache = JSON.stringify(temp);
console.debug(this.cache);
}
return this.cache;
}
this.send = function (player) {
chat.json(player, self.json());
}
this.sendAll = function () {
server.players(function sendAllMessage(p) {
self.send(p);
})
}
}
Tellraw.create = function () {
return new Tellraw().then(Tellraw.duplicateChar);
}
Tellraw.duplicateChar = '§卐';
exports = module.exports = Tellraw;

View File

@ -10,8 +10,7 @@ var command = require('api/command');
var server = require('api/server');
var fs = require('fs');
var chat = require('api/chat');
var tellraw = require('tellraw');
var utils = require('utils')
var description = {
@ -166,8 +165,6 @@ function registerEvent() {
}
}
var DuplicateChar = '§卐';
function handlerBukkitChat(event) {
sendChat(event.player, event.message, function() { event.setCancelled(true); });
}
@ -176,7 +173,7 @@ function handlerSpongeChat(event) {
var player = event.getCause().first(org.spongepowered.api.entity.living.player.Player.class).orElse(null);
if (player == null) { return; }
var plain = event.getRawMessage().toPlain();
if (plain.startsWith(DuplicateChar)) {
if (plain.startsWith(tellraw.duplicateChar)) {
return;
}
sendChat(player, plain, function() { event.setMessageCancelled(true) });
@ -189,7 +186,7 @@ function sendChat(player, plain, callback) {
return;
}
callback();
var tr = new Tellraw().then(DuplicateChar);
var tr = tellraw.create();
chat_format.format_list.forEach(function setStyle(format) {
var style = style_formats[format];
if (style) {
@ -247,136 +244,6 @@ function disable() {
console.log('卸载', description.name, '插件!');
}
/*global Java, base, module, exports, require*/
var ChatMessagePart = function () {
var text;
var clickEventAction;
var clickEventValue;
var hoverEventAction;
var hoverEventValue;
var insertion;
this.click = function (action, value) {
this.clickEventAction = action;
this.clickEventValue = value;
}
this.hover = function (action, value) {
this.hoverEventAction = action;
this.hoverEventValue = value;
console.log(this.toJson())
}
this.convert = function () {
var str = {};
if (this.text) {
str.text = this.text;
}
if (this.clickEventAction) {
str.clickEvent = {
"action": this.clickEventAction,
"value": this.clickEventValue
}
}
if (this.hoverEventAction) {
str.hoverEvent = {
"action": this.hoverEventAction,
"value": this.hoverEventValue
}
}
if (this.insertion) {
str.insertion = this.insertion;
}
return str;
}
}
var Tellraw = function () {
var parts = [new ChatMessagePart()];
var self = this;
var cache = null;
this.then = function (part) {
if (typeof part === "string") {
var newPart = new ChatMessagePart();
newPart.text = part;
this.then(newPart);
return self;
}
var last = this.latest();
if (!last.text) {
last.text = part.text;
} else {
parts.push(part);
}
this.cache = null;
}
this.text = function (text) {
this.latest().text = text;
return this;
}
this.tip = function (str) {
if (toString.call(str) === "[object Array]") {
str = str.join("\n");
}
this.latest().hover("show_text", str);
return this;
}
this.item = function (str) {
this.latest().hover("show_item", str);
return this;
}
this.cmd = this.command = function (command) {
this.latest().click("run_command", command);
return this;
}
this.suggest = function (url) {
this.latest().click("suggest_command", url);
return this;
}
this.file = function (path) {
this.latest().click("open_file", path);
return this;
}
this.link = function (url) {
this.latest().click("open_url", url);
return this;
}
this.latest = function () {
return parts[parts.length - 1];
}
this.json = function () {
if (!this.cache) {
var temp = [];
parts.forEach(function (t) {
temp.push(t.convert());
})
this.cache = JSON.stringify(temp);
console.debug(this.cache);
}
return this.cache;
}
this.send = function (player) {
chat.json(player, self.json());
}
this.sendAll = function () {
server.players(function sendAllMessage(p) {
self.send(p);
})
}
}
module.exports = {
description: description,
load: load,