feat: 新增案例插件
This commit is contained in:
		
							
								
								
									
										67
									
								
								src/main/resources/plugins/HelloWorld.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/main/resources/plugins/HelloWorld.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
'use strict';
 | 
			
		||||
/**
 | 
			
		||||
 * Hello Wrold 测试插件
 | 
			
		||||
 */
 | 
			
		||||
/*global Java, base, module, exports, require*/
 | 
			
		||||
 | 
			
		||||
var event = require('modules/event');
 | 
			
		||||
var command = require('modules/command');
 | 
			
		||||
var papi = require('./ext/papi');
 | 
			
		||||
var join;
 | 
			
		||||
 | 
			
		||||
var description = {
 | 
			
		||||
    name: 'HelloWorld',
 | 
			
		||||
    version: '1.0',
 | 
			
		||||
    //commands: {
 | 
			
		||||
    //    'hello': {
 | 
			
		||||
    //        description: 'HelloWorld主命令'
 | 
			
		||||
    //    }
 | 
			
		||||
    //}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
function load() {
 | 
			
		||||
    console.log('载入 Hello Wrold 测试插件!');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function enable() {
 | 
			
		||||
    command.on(this, 'hello', {
 | 
			
		||||
        cmd: function (sender, command, args) {
 | 
			
		||||
            console.log(command, Array.prototype.join.call(args, ' '));
 | 
			
		||||
            return true;
 | 
			
		||||
        },
 | 
			
		||||
        tab: function (sender, command, args) {
 | 
			
		||||
            switch (args.length) {
 | 
			
		||||
                case 1:
 | 
			
		||||
                    return "arg1";
 | 
			
		||||
                case 2:
 | 
			
		||||
                    return "arg2";
 | 
			
		||||
                case 3:
 | 
			
		||||
                    return "arg3";
 | 
			
		||||
                default:
 | 
			
		||||
                    return 'default';
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
    console.log('启用 Hello Wrold 测试插件!');
 | 
			
		||||
    join = event.on(this, 'playerloginevent', function join(event) {
 | 
			
		||||
        // noinspection JSUnresolvedVariable
 | 
			
		||||
        console.debug('玩家', event.player.name, "触发事件", event.name);
 | 
			
		||||
        setTimeout(function () {
 | 
			
		||||
            // noinspection JSUnresolvedVariable
 | 
			
		||||
            event.player.sendMessage(papi.$(event.player, "§a欢迎来到 §bMiaoScript §a的世界! 当前在线: %server_online%"));
 | 
			
		||||
        }, 10);
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function disable() {
 | 
			
		||||
    console.log('卸载 Hello Wrold 测试插件!');
 | 
			
		||||
    // 可以不用关闭事件 程序将自动处理
 | 
			
		||||
    // event.off(join);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    description: description,
 | 
			
		||||
    load: load,
 | 
			
		||||
    enable: enable,
 | 
			
		||||
    disable: disable
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										148
									
								
								src/main/resources/plugins/MiaoTag.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										148
									
								
								src/main/resources/plugins/MiaoTag.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,148 @@
 | 
			
		||||
'use strict';
 | 
			
		||||
/**
 | 
			
		||||
 * MiaoTag
 | 
			
		||||
 * 可兼容任何记分板
 | 
			
		||||
 */
 | 
			
		||||
/*global Java, base, module, exports, require*/
 | 
			
		||||
 | 
			
		||||
var fs = require('core/fs');
 | 
			
		||||
var event = require('modules/event');
 | 
			
		||||
var bukkit = require('modules/bukkit');
 | 
			
		||||
var command = require('modules/command');
 | 
			
		||||
 | 
			
		||||
var papi = require('plugins/ext/papi');
 | 
			
		||||
 | 
			
		||||
var fakeTag;
 | 
			
		||||
 | 
			
		||||
var description = {
 | 
			
		||||
    name: 'MiaoTag',
 | 
			
		||||
    version: '1.0.1',
 | 
			
		||||
    author: '喵♂呜',
 | 
			
		||||
    config: {
 | 
			
		||||
        update: 20,
 | 
			
		||||
        format: '§4§l❤'
 | 
			
		||||
    },
 | 
			
		||||
    commands: {
 | 
			
		||||
        'mtag': {
 | 
			
		||||
            description: 'MiaoTag主命令',
 | 
			
		||||
            usage: '',
 | 
			
		||||
            permission: 'MiaoTag.admin',
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
    permissions: {
 | 
			
		||||
        'MiaoTag.default': {
 | 
			
		||||
            default: true,
 | 
			
		||||
            description: '默认权限 赋予玩家'
 | 
			
		||||
        },
 | 
			
		||||
        'MiaoTag.admin': {
 | 
			
		||||
            default: false,
 | 
			
		||||
            description: '管理权限'
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
var config;
 | 
			
		||||
 | 
			
		||||
function load() {
 | 
			
		||||
    config = self.getConfig();
 | 
			
		||||
    fakeTag = new FakeTag(config.format);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function enable() {
 | 
			
		||||
    command.on(self, 'mtag', {
 | 
			
		||||
        cmd: function cmd (sender, command, args){
 | 
			
		||||
            var subcommand = args[0];
 | 
			
		||||
            switch (subcommand) {
 | 
			
		||||
                case 'reload':
 | 
			
		||||
                    self.reloadConfig();
 | 
			
		||||
                    break;
 | 
			
		||||
                case 'test':
 | 
			
		||||
                    // var IllegalStateException = Java.type("java.lang.IllegalStateException");
 | 
			
		||||
                    // throw new IllegalStateException('TEST');
 | 
			
		||||
                    throw new TypeError('TEST');
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
        tab: function tab (sender, command, args){
 | 
			
		||||
            return ['reload'];
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
    bukkit.players(function (p) fakeTag.set(p));
 | 
			
		||||
    event.on(self, 'PlayerJoin', function (event) fakeTag.set(event.player));
 | 
			
		||||
    event.on(self, 'EntityDamage', function (event) {
 | 
			
		||||
        var player = event.entity;
 | 
			
		||||
        if(player instanceof org.bukkit.entity.Player){
 | 
			
		||||
           setTimeout(function () {
 | 
			
		||||
               fakeTag.update(player);
 | 
			
		||||
           }, 1);
 | 
			
		||||
        }
 | 
			
		||||
    }, false);
 | 
			
		||||
    //event.on(this, 'playerquitevent', function quit(event) removeTask(event.player));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function disable() {
 | 
			
		||||
    fakeTag.disable();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function FakeTag(name) {
 | 
			
		||||
    // NMS CLASS
 | 
			
		||||
    var Scoreboard = bukkit.nmsCls('Scoreboard');
 | 
			
		||||
    var ScoreboardScore = bukkit.nmsCls('ScoreboardScore');
 | 
			
		||||
    var ScoreboardBaseCriteria = bukkit.nmsCls('ScoreboardBaseCriteria');
 | 
			
		||||
    var PacketPlayOutScoreboardScore = bukkit.nmsCls('PacketPlayOutScoreboardScore');
 | 
			
		||||
    var PacketPlayOutScoreboardObjective = bukkit.nmsCls('PacketPlayOutScoreboardObjective');
 | 
			
		||||
    var PacketPlayOutScoreboardDisplayObjective = bukkit.nmsCls('PacketPlayOutScoreboardDisplayObjective');
 | 
			
		||||
    
 | 
			
		||||
    var scoreboardManager = bukkit.$.scoreboardManager;
 | 
			
		||||
    var mainScoreboard = scoreboardManager.mainScoreboard.handle;
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        // 注册tag对象
 | 
			
		||||
        mainScoreboard.registerObjective(name, new ScoreboardBaseCriteria(name));
 | 
			
		||||
    } catch (ex){
 | 
			
		||||
        // ignore 忽略创建错误 eg: java.lang.IllegalArgumentException: An objective with the name 'xxxxx' already exists!
 | 
			
		||||
    }
 | 
			
		||||
    var objective = mainScoreboard.getObjective(name);
 | 
			
		||||
 | 
			
		||||
    // 缓存虚拟的tag包
 | 
			
		||||
    var cache = {
 | 
			
		||||
        objective: new PacketPlayOutScoreboardObjective(objective, 0),
 | 
			
		||||
        display: new PacketPlayOutScoreboardDisplayObjective(2, objective)
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    function sendPacket(player, p) {
 | 
			
		||||
        player.handle.playerConnection.sendPacket(p);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this.set = function (player) {
 | 
			
		||||
        sendPacket(player, cache.objective);
 | 
			
		||||
        sendPacket(player, cache.display);
 | 
			
		||||
        this.update(player);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    this.update = function (player){
 | 
			
		||||
        var score = mainScoreboard.getPlayerScoreForObjective(player.name, objective);
 | 
			
		||||
        score.setScore(player.getHealth());
 | 
			
		||||
        var scorePack = new PacketPlayOutScoreboardScore(score);
 | 
			
		||||
        //把其他玩家缓存的包发给这个玩家
 | 
			
		||||
        bukkit.players(function (t) {
 | 
			
		||||
            sendPacket(t, scorePack);
 | 
			
		||||
            if (t.name != player.name) {
 | 
			
		||||
                var outher = mainScoreboard.getPlayerScoreForObjective(t.name, objective);
 | 
			
		||||
                outher.setScore(t.getHealth());
 | 
			
		||||
                sendPacket(player, new PacketPlayOutScoreboardScore(outher));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    this.disable = function (){
 | 
			
		||||
        // 注销tag对象
 | 
			
		||||
        mainScoreboard.unregisterObjective(objective);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    description: description,
 | 
			
		||||
    load: load,
 | 
			
		||||
    enable: enable,
 | 
			
		||||
    disable: disable
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user