feat: 更新基础类库 新增 bukkit 的物品类
This commit is contained in:
		
							
								
								
									
										2
									
								
								src/main/resources/api/item.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								src/main/resources/api/item.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
/*global Java, base, module, exports, require*/
 | 
			
		||||
module.exports = require('./mserver').item;
 | 
			
		||||
@@ -9,5 +9,6 @@ module.exports = {
 | 
			
		||||
    permission: impl('permission'),
 | 
			
		||||
    plugin: impl('plugin'),
 | 
			
		||||
    server: impl('server'),
 | 
			
		||||
    task: impl('task')
 | 
			
		||||
    task: impl('task'),
 | 
			
		||||
    item: impl('item')
 | 
			
		||||
};
 | 
			
		||||
@@ -4,8 +4,8 @@ var global = this;
 | 
			
		||||
 | 
			
		||||
// noinspection JSUnusedLocalSymbols
 | 
			
		||||
function init(root) {
 | 
			
		||||
    log.info('Init MiaoScript System...');
 | 
			
		||||
    global.root = root;
 | 
			
		||||
    global.noop = function () {};
 | 
			
		||||
    loadCore();
 | 
			
		||||
    loadRequire();
 | 
			
		||||
    loadPatch();
 | 
			
		||||
@@ -17,8 +17,6 @@ function init(root) {
 | 
			
		||||
 * 初始化核心
 | 
			
		||||
 */
 | 
			
		||||
function loadCore() {
 | 
			
		||||
    global.noop = function () {
 | 
			
		||||
    };
 | 
			
		||||
    // 加载基础模块
 | 
			
		||||
    load(root + '/core/ext.js');
 | 
			
		||||
    // 探测服务器类型
 | 
			
		||||
 
 | 
			
		||||
@@ -19,4 +19,29 @@
 | 
			
		||||
    Object.prototype.toYaml = function () {
 | 
			
		||||
        return yaml.safeDump(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 日期格式化
 | 
			
		||||
     * 例: new Date().format('yyyy-MM-dd hh:mm:ss.s') => "2017-08-24 16:15:40.693"
 | 
			
		||||
     * @param fmt 格式化字符串
 | 
			
		||||
     * @returns {*}
 | 
			
		||||
     */
 | 
			
		||||
    Date.prototype.format = function (fmt) { //author: meizz
 | 
			
		||||
        var o = {
 | 
			
		||||
            "M+": this.getMonth() + 1, //月份
 | 
			
		||||
            "d+": this.getDate(), //日
 | 
			
		||||
            "h+": this.getHours(), //小时
 | 
			
		||||
            "m+": this.getMinutes(), //分
 | 
			
		||||
            "s+": this.getSeconds(), //秒
 | 
			
		||||
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
 | 
			
		||||
            "S": this.getMilliseconds() //毫秒
 | 
			
		||||
        };
 | 
			
		||||
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")  .substr(4 - RegExp.$1.length));
 | 
			
		||||
        for (var k in o) {
 | 
			
		||||
            if (new RegExp("(" + k + ")").test(fmt)) {
 | 
			
		||||
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? o[k] : (("00"   + o[k]).substr(("" + o[k]).length)));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return fmt;
 | 
			
		||||
    };
 | 
			
		||||
})();
 | 
			
		||||
 
 | 
			
		||||
@@ -148,6 +148,10 @@
 | 
			
		||||
        return module;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function _isFile(file) {
 | 
			
		||||
        return file.isFile && file.isFile();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获得文件规范路径
 | 
			
		||||
     * @param file
 | 
			
		||||
@@ -172,7 +176,8 @@
 | 
			
		||||
     * @private
 | 
			
		||||
     */
 | 
			
		||||
    function _require(name, path, optional) {
 | 
			
		||||
        var file = name.isFile && name.isFile() ? name : resolve(name, path);
 | 
			
		||||
        var file = new File(name);
 | 
			
		||||
        file = _isFile(file) ? file : resolve(name, path);
 | 
			
		||||
        if (file === undefined) {
 | 
			
		||||
            console.console("§c模块 §a%s §c加载失败! §4未找到该模块!".format(name));
 | 
			
		||||
            return {exports: {}};
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										97
									
								
								src/main/resources/internal/bukkit/item.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								src/main/resources/internal/bukkit/item.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,97 @@
 | 
			
		||||
'use strict';
 | 
			
		||||
/**
 | 
			
		||||
 * 物品快速生成类
 | 
			
		||||
 * Created by 蒋天蓓 on 2017/2/9 0009.
 | 
			
		||||
 */
 | 
			
		||||
/*global Java, base, module, exports, require, __FILE__*/
 | 
			
		||||
var Bukkit = MServer;
 | 
			
		||||
var item = {};
 | 
			
		||||
var ItemStack = Java.type("org.bukkit.inventory.ItemStack");
 | 
			
		||||
var Material = Java.type('org.bukkit.Material');
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 创建一个物品
 | 
			
		||||
 * @constructor (ID)
 | 
			
		||||
 * @constructor (ID,数量)
 | 
			
		||||
 * @constructor (ID,数量,子ID)
 | 
			
		||||
 */
 | 
			
		||||
item.create = function () {
 | 
			
		||||
    var idOrType = arguments[0];
 | 
			
		||||
    if (isNaN(new Number(idOrType))) {
 | 
			
		||||
       idOrType = Material[idOrType];
 | 
			
		||||
    }
 | 
			
		||||
    switch (arguments.length) {
 | 
			
		||||
        case 1:
 | 
			
		||||
            return new ItemStack(idOrType);
 | 
			
		||||
        case 2:
 | 
			
		||||
            return new ItemStack(idOrType, arguments[1]);
 | 
			
		||||
        case 3:
 | 
			
		||||
            return new ItemStack(idOrType, arguments[1], arguments[2]);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
/**
 | 
			
		||||
 * 创建一个头颅
 | 
			
		||||
 * @constructor (玩家名称)
 | 
			
		||||
 */
 | 
			
		||||
item.head = function (name) {
 | 
			
		||||
    var head = item.create(397, 1, 3);
 | 
			
		||||
    var skullMeta = head.getItemMeta();
 | 
			
		||||
    skullMeta.setOwner(name);
 | 
			
		||||
    head.setItemMeta(skullMeta);
 | 
			
		||||
    return head;
 | 
			
		||||
};
 | 
			
		||||
/**
 | 
			
		||||
 * 给玩家添加物品
 | 
			
		||||
 * @param player 玩家
 | 
			
		||||
 * @param items 物品数组
 | 
			
		||||
 * @param drop 满背包是否掉落
 | 
			
		||||
 */
 | 
			
		||||
item.add = function (player, items, drop) {
 | 
			
		||||
    var drops = player.getInventory().addItem(items).values();
 | 
			
		||||
    if (drops.size() !== 0 && drop) {
 | 
			
		||||
        drops.forEach(function (itemStack) {
 | 
			
		||||
            item.drop(player.getLocation(), itemStack);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
/**
 | 
			
		||||
 * 指定地点掉落物品
 | 
			
		||||
 * @param loc 地点
 | 
			
		||||
 * @param item 物品
 | 
			
		||||
 */
 | 
			
		||||
item.drop = function (loc, item) {
 | 
			
		||||
    setTimeout(function () {
 | 
			
		||||
        loc.getWorld().dropItem(loc, item);
 | 
			
		||||
    }, 1);
 | 
			
		||||
};
 | 
			
		||||
/**
 | 
			
		||||
 * 设置物品名称
 | 
			
		||||
 * @param item 物品
 | 
			
		||||
 * @param name
 | 
			
		||||
 * @returns {*}
 | 
			
		||||
 */
 | 
			
		||||
item.setName = function (item, name) {
 | 
			
		||||
    if (item.getType().name() !== "AIR") {
 | 
			
		||||
        var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType());
 | 
			
		||||
        meta.setDisplayName(name);
 | 
			
		||||
        item.setItemMeta(meta);
 | 
			
		||||
    }
 | 
			
		||||
    return item;
 | 
			
		||||
};
 | 
			
		||||
/**
 | 
			
		||||
 * 设置物品Lore
 | 
			
		||||
 * @param item 物品
 | 
			
		||||
 * @param lores Lore
 | 
			
		||||
 * @returns {*} 物品
 | 
			
		||||
 */
 | 
			
		||||
item.setLore = item.setLores = function (item, lores) {
 | 
			
		||||
    if (item.getType().name() !== "AIR") {
 | 
			
		||||
        var meta = item.hasItemMeta() ? item.getItemMeta() : Bukkit.getItemFactory().getItemMeta(item.getType());
 | 
			
		||||
        if (typeof(lores) === 'string') { lores = lores.split("\n") };
 | 
			
		||||
        meta.setLore(lores);
 | 
			
		||||
        item.setItemMeta(meta);
 | 
			
		||||
    }
 | 
			
		||||
    return item;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = item;
 | 
			
		||||
@@ -19,9 +19,6 @@ exports.nmsVersion = Bukkit.server.class.name.split('.')[3];
 | 
			
		||||
exports.nmsCls = function (name) {
 | 
			
		||||
    return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.'));
 | 
			
		||||
};
 | 
			
		||||
exports.command = function (name) {
 | 
			
		||||
    return Server.getPluginCommand(name);
 | 
			
		||||
};
 | 
			
		||||
/**
 | 
			
		||||
 * 获取玩家
 | 
			
		||||
 */
 | 
			
		||||
 
 | 
			
		||||
@@ -10,11 +10,33 @@ var plugin = server.plugin.self;
 | 
			
		||||
var CommandManager = server.CommandManager;
 | 
			
		||||
 | 
			
		||||
var CommandSpec = Java.type('org.spongepowered.api.command.spec.CommandSpec');
 | 
			
		||||
var CommandCallable = Java.type('org.spongepowered.api.command.CommandCallable');
 | 
			
		||||
 | 
			
		||||
var Text = Java.type('org.spongepowered.api.text.Text');
 | 
			
		||||
 | 
			
		||||
var Optional = Java.type('java.util.Optional');
 | 
			
		||||
 | 
			
		||||
var ArrayList = Java.type('java.util.ArrayList');
 | 
			
		||||
var Arrays = Java.type('java.util.Arrays');
 | 
			
		||||
 | 
			
		||||
var SimpleCommandCallable = function () {
 | 
			
		||||
    this.process = function (source, arguments) {
 | 
			
		||||
        
 | 
			
		||||
    },
 | 
			
		||||
    this.getSuggestions = function (source, arguments, targetPosition) {
 | 
			
		||||
        return Arrays.asList('');
 | 
			
		||||
    },
 | 
			
		||||
    this.testPermission = function (source) {
 | 
			
		||||
        return true;
 | 
			
		||||
    },
 | 
			
		||||
    this.getShortDescription = function (source) {
 | 
			
		||||
        return Optional.ofNullable('');
 | 
			
		||||
    },
 | 
			
		||||
    this.getHelp = function (source) {
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function enable(jsp) {
 | 
			
		||||
    var commands = jsp.description.commands;
 | 
			
		||||
    if (commands) {
 | 
			
		||||
@@ -22,9 +44,9 @@ function enable(jsp) {
 | 
			
		||||
        for (var name in commands) {
 | 
			
		||||
            var command = commands[name];
 | 
			
		||||
            if (typeof command !== 'object') continue;
 | 
			
		||||
            var newCmd = create(jsp, name);
 | 
			
		||||
            if (command.description) newCmd.setDescription(command.description);
 | 
			
		||||
            if (command.usage) newCmd.setUsage(command.usage);
 | 
			
		||||
            var newCmd = CommandSpec.builder();
 | 
			
		||||
            if (command.description) newCmd.description(Text.of(command.description));
 | 
			
		||||
            // if (command.usage) newCmd.setUsage(command.usage);
 | 
			
		||||
            if (command.aliases) newCmd.setAliases(Arrays.asList(command.aliases));
 | 
			
		||||
            if (command.permission) newCmd.setPermission(command.permission);
 | 
			
		||||
            if (command['permission-message']) newCmd.setPermissionMessage(command['permission-message']);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user