From b6a452aa109a0de25228acbc016644571805c5cd Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 3 Nov 2017 03:15:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E5=8A=A0=E8=BD=BD=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../java/pw/yumc/MiaoScript/ScriptEngine.java | 6 +- src/main/resources/bios.js | 2 +- src/main/resources/plugins/Lottery.js | 72 ++++++++++--------- 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java index d83a1a4..e538c6c 100644 --- a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java @@ -8,6 +8,7 @@ import java.nio.file.Paths; import javax.script.ScriptEngineManager; import lombok.SneakyThrows; +import lombok.val; import pw.yumc.YumCore.engine.MiaoScriptEngine; /** @@ -32,6 +33,8 @@ public class ScriptEngine { @SneakyThrows public void enableEngine(ClassLoader loader) { + val origin = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(loader); ScriptEngineManager manager = new ScriptEngineManager(null); this.engine = new MiaoScriptEngine(manager, "nashorn"); this.engine.put("base", new Base()); @@ -40,9 +43,10 @@ public class ScriptEngine { if (Files.exists(bios)) { this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')"); } else { - this.engine.eval(new InputStreamReader(loader.getResourceAsStream("bios.js"))); + this.engine.eval(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bios.js"))); } engine.invokeFunction("boot", root, logger); + Thread.currentThread().setContextClassLoader(origin); } @SneakyThrows diff --git a/src/main/resources/bios.js b/src/main/resources/bios.js index b95344d..c8f0ab7 100644 --- a/src/main/resources/bios.js +++ b/src/main/resources/bios.js @@ -12,7 +12,7 @@ var disable; // 开发环境下初始化 root = root || "src/main/resources"; // 解压文件到根目录 - release(root, "[core|modules]/.*", true); + release(root, "[api|core|internal|modules]/.*", true); release(root, "plugins/.*"); load(root + '/core/init.js'); if (__FILE__ !== "") { diff --git a/src/main/resources/plugins/Lottery.js b/src/main/resources/plugins/Lottery.js index 1ccd406..b96eb7e 100644 --- a/src/main/resources/plugins/Lottery.js +++ b/src/main/resources/plugins/Lottery.js @@ -8,7 +8,6 @@ var event = require('api/event'); var command = require('api/command'); var bukkit = require('api/server'); var item = require('api/item'); -var fs = require('fs'); var Arrays = Java.type('java.util.Arrays'); var ItemStackArray = Java.type('org.bukkit.inventory.ItemStack[]'); @@ -26,7 +25,7 @@ var description = { panel: '160:13', list: [ { - box:{ + box: { id: 160, damage: 1, name: '§a箱子', @@ -34,7 +33,7 @@ var description = { '这是箱子的Lore' ] }, - key:{ + key: { id: 160, damage: 2, name: '§b钥匙', @@ -42,16 +41,28 @@ var description = { '这是钥匙的Lore' ] }, - result:[ + result: [ { percent: 10, - command: 'money give %player% 200', + command: 'money give %player% 100', item: { id: 160, damage: 3, - name: '§c奖品', + name: '§c奖品1', lore: [ - '这是奖品子的Lore' + '这是奖品1的Lore' + ] + } + }, + { + percent: 20, + command: 'money give %player% 200', + item: { + id: 160, + damage: 4, + name: '§c奖品2', + lore: [ + '这是奖品2的Lore' ] } } @@ -62,12 +73,11 @@ var description = { }; var panel = item.create(160, 1, 13); -var air = item.create(0); var config; var items; -var lmap=[]; -var cmap=[]; +var lmap = []; +var cmap = []; function load() { config = this.config; @@ -95,21 +105,21 @@ function load() { Arrays.fill(items, 41, 54, panel); } -function isTargetItem(item, config){ - return item.typeId == config.id && - item.itemMeta && - item.itemMeta.displayName == config.name && - item.itemMeta.lore && Java.from(item.itemMeta.lore).toJson() == config.lore.toJson() +function isTargetItem(item, config) { + return item.typeId === config.id && + item.itemMeta && + item.itemMeta.displayName === config.name && + item.itemMeta.lore && Java.from(item.itemMeta.lore).toJson() === config.lore.toJson() } function newItem(name, sub) { return item.create(name, 1, sub || 0); } -function newItemFromConfig(config){ +function newItemFromConfig(config) { var i = newItem(config.id, config.damage); - if (config.name) item.setName(i, config.name) - if (config.lore) item.setLore(i, config.lore) + if (config.name) item.setName(i, config.name); + if (config.lore) item.setLore(i, config.lore); return i; } @@ -124,16 +134,14 @@ function enable() { }); event.on(this, 'InventoryClick', function click(event) { var inv = event.inventory; - if (inv.title != config.title) return; + if (inv.title !== config.title) return; var player = event.whoClicked; var slot = event.rawSlot; if (slot > 53 || slot < 0) { return; } event.cancelled = true; - var click = event.currentItem; - var cursor = event.cursor; - switch(slot){ + switch (slot) { case 10: case 16: case 40: @@ -145,17 +153,17 @@ function enable() { break; case 33: var temp = inv.getItem(40); - if (temp && temp.typeId != 0) { - console.sender(player, '§c请先取走奖品!') + if (temp && temp.typeId !== 0) { + console.sender(player, '§c请先取走奖品!'); return; } var litem; var box = inv.getItem(10); var key = inv.getItem(16); - if (box && box.typeId != 0 && key && key.typeId != 0) { - for (var i = 0; i < config.list.length; i++){ + if (box && box.typeId !== 0 && key && key.typeId !== 0) { + for (var i = 0; i < config.list.length; i++) { var r = config.list[i]; - if (isTargetItem(box, r.box)){ + if (isTargetItem(box, r.box)) { litem = r; break; } @@ -163,16 +171,16 @@ function enable() { } // TODO 抽奖 if (!litem) { - console.sender(player, '§c请先放入抽奖物品和钥匙!') + console.sender(player, '§c请先放入抽奖物品和钥匙!'); return; } if (!isTargetItem(key, litem.key)) { - console.sender(player, '§c抽奖物品和钥匙不匹配!') + console.sender(player, '§c抽奖物品和钥匙不匹配!'); return; } var resultlist = []; - litem.result.forEach(function(t){ - for (var i=0; i < t.percent; i++){ + litem.result.forEach(function (t) { + for (var i = 0; i < t.percent; i++) { resultlist.push(t); } }); @@ -191,7 +199,7 @@ function enable() { delete cmap[player.name]; break; default: - event.cancelled = true; + event.cancelled = true; } }); }