feat: complate 1.13

This commit is contained in:
MiaoWoo 2019-03-09 10:28:44 +08:00
parent e455cbf996
commit 8d5902600f

View File

@ -13,7 +13,7 @@ var fakeTag;
var description = { var description = {
name: 'MiaoTag', name: 'MiaoTag',
version: '1.1', version: '1.1',
author: '喵♂呜', author: 'MiaoWoo',
config: { config: {
format: '§4§l❤' format: '§4§l❤'
}, },
@ -40,10 +40,15 @@ var config;
function load() { function load() {
config = self.getConfig(); config = self.getConfig();
fakeTag = new FakeTag(config.format);
} }
function enable() { function enable() {
registryCommand()
fakeTag = new FakeTag(config.format);
registryEvent()
}
function registryCommand() {
command.on(self, 'mtag', { command.on(self, 'mtag', {
cmd: function cmd(sender, command, args) { cmd: function cmd(sender, command, args) {
var subCommand = args[0]; var subCommand = args[0];
@ -51,7 +56,7 @@ function enable() {
case 'reload': case 'reload':
self.reloadConfig(); self.reloadConfig();
fakeTag = new FakeTag(config.format); fakeTag = new FakeTag(config.format);
console.sender(sender, "§a配置文件重载完成!", "TEST"); console.sender(sender, "§a配置文件重载完成!");
break; break;
} }
}, },
@ -59,9 +64,18 @@ function enable() {
return ['reload']; return ['reload'];
} }
}); });
}
function registryEvent() {
bukkit.players(function(p) { fakeTag.set(p) }); bukkit.players(function(p) { fakeTag.set(p) });
event.on(self, 'PlayerJoin', function(event) { fakeTag.set(event.player) }); event.on(self, 'PlayerJoin', function(event) { fakeTag.set(event.player) });
var entityUpdate = function(event) { event.on(self, 'EntityRegainHealth', entityUpdate, false);
event.on(self, 'EntityDamage', entityUpdate, false);
event.on(self, 'EntityRegainHealth', entityUpdate, false);
event.on(self, 'PlayerRespawn', entityUpdate, false);
}
function entityUpdate(event) {
var player = event.entity || event.player; var player = event.entity || event.player;
if (player instanceof org.bukkit.entity.Player) { if (player instanceof org.bukkit.entity.Player) {
setTimeout(function() { setTimeout(function() {
@ -69,20 +83,22 @@ function enable() {
}, 1); }, 1);
} }
}; };
event.on(self, 'EntityRegainHealth', entityUpdate, false);
event.on(self, 'EntityDamage', entityUpdate, false);
event.on(self, 'EntityRegainHealth', entityUpdate, false);
event.on(self, 'PlayerRespawn', entityUpdate, false);
//event.on(this, 'playerquitevent', function quit(event) removeTask(event.player));
}
function disable() { function disable() {
fakeTag.disable(); if (fakeTag) { fakeTag.disable() };
} }
function FakeTag(name) { function FakeTag(name) {
var ver1_13 = false;
// NMS CLASS // NMS CLASS
try {
var ScoreboardBaseCriteria = bukkit.nmsCls('ScoreboardBaseCriteria'); var ScoreboardBaseCriteria = bukkit.nmsCls('ScoreboardBaseCriteria');
} catch (ex) {
ver1_13 = true;
var IScoreboardCriteria = bukkit.nmsCls('IScoreboardCriteria');
var ScoreboardServer = bukkit.nmsCls("ScoreboardServer");
var ChatComponentText = bukkit.nmsCls('ChatComponentText');
}
var PacketPlayOutScoreboardScore = bukkit.nmsCls('PacketPlayOutScoreboardScore'); var PacketPlayOutScoreboardScore = bukkit.nmsCls('PacketPlayOutScoreboardScore');
var PacketPlayOutScoreboardObjective = bukkit.nmsCls('PacketPlayOutScoreboardObjective'); var PacketPlayOutScoreboardObjective = bukkit.nmsCls('PacketPlayOutScoreboardObjective');
var PacketPlayOutScoreboardDisplayObjective = bukkit.nmsCls('PacketPlayOutScoreboardDisplayObjective'); var PacketPlayOutScoreboardDisplayObjective = bukkit.nmsCls('PacketPlayOutScoreboardDisplayObjective');
@ -90,13 +106,31 @@ function FakeTag(name) {
var scoreboardManager = bukkit.$.scoreboardManager; var scoreboardManager = bukkit.$.scoreboardManager;
var mainScoreboard = scoreboardManager.mainScoreboard.handle; var mainScoreboard = scoreboardManager.mainScoreboard.handle;
// 注销对象
var objective = mainScoreboard.getObjective(name);
if (objective) {
mainScoreboard.unregisterObjective(objective);
}
try { try {
if (!ver1_13) {
// 注册tag对象 // 注册tag对象
mainScoreboard.registerObjective(name, new ScoreboardBaseCriteria(name)); objective = mainScoreboard.registerObjective(name, new ScoreboardBaseCriteria(name));
} else {
// 注册tag对象
objective = mainScoreboard.registerObjective(name,
IScoreboardCriteria.HEALTH,
new ChatComponentText(name),
IScoreboardCriteria.EnumScoreboardHealthDisplay.HEARTS);
}
} catch (ex) { } catch (ex) {
throw ex
// ignore 忽略创建错误 eg: java.lang.IllegalArgumentException: An objective with the name 'xxxxx' already exists! // ignore 忽略创建错误 eg: java.lang.IllegalArgumentException: An objective with the name 'xxxxx' already exists!
} }
var objective = mainScoreboard.getObjective(name);
if (!objective) {
throw Error("Error Can't Found MainScoreboard Objective " + name)
}
// 缓存虚拟的tag包 // 缓存虚拟的tag包
var cache = { var cache = {
@ -114,17 +148,23 @@ function FakeTag(name) {
this.update(player); this.update(player);
}; };
this.update = function(player) { function createScore(player) {
if (!ver1_13) {
var score = mainScoreboard.getPlayerScoreForObjective(player.name, objective); var score = mainScoreboard.getPlayerScoreForObjective(player.name, objective);
score.setScore(player.getHealth()); score.setScore(player.health);
var scorePack = new PacketPlayOutScoreboardScore(score); return new PacketPlayOutScoreboardScore(score);
} else {
return new PacketPlayOutScoreboardScore(ScoreboardServer.Action.CHANGE, name, player.name, player.health)
}
}
this.update = function update(player) {
var scorePack = createScore(player);
//把其他玩家缓存的包发给这个玩家 //把其他玩家缓存的包发给这个玩家
bukkit.players(function(t) { bukkit.players(function(t) {
sendPacket(t, scorePack); sendPacket(t, scorePack);
if (t.name !== player.name) { if (t.name !== player.name) {
var outher = mainScoreboard.getPlayerScoreForObjective(t.name, objective); sendPacket(player, createScore(t));
outher.setScore(t.getHealth());
sendPacket(player, new PacketPlayOutScoreboardScore(outher));
} }
}); });
}; };