feat: 使用插件的类加载器

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-11-03 03:15:37 +08:00
parent abe5569c4a
commit b6a452aa10
3 changed files with 46 additions and 34 deletions

View File

@ -8,6 +8,7 @@ import java.nio.file.Paths;
import javax.script.ScriptEngineManager; import javax.script.ScriptEngineManager;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.val;
import pw.yumc.YumCore.engine.MiaoScriptEngine; import pw.yumc.YumCore.engine.MiaoScriptEngine;
/** /**
@ -32,6 +33,8 @@ public class ScriptEngine {
@SneakyThrows @SneakyThrows
public void enableEngine(ClassLoader loader) { public void enableEngine(ClassLoader loader) {
val origin = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(loader);
ScriptEngineManager manager = new ScriptEngineManager(null); ScriptEngineManager manager = new ScriptEngineManager(null);
this.engine = new MiaoScriptEngine(manager, "nashorn"); this.engine = new MiaoScriptEngine(manager, "nashorn");
this.engine.put("base", new Base()); this.engine.put("base", new Base());
@ -40,9 +43,10 @@ public class ScriptEngine {
if (Files.exists(bios)) { if (Files.exists(bios)) {
this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')"); this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')");
} else { } 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); engine.invokeFunction("boot", root, logger);
Thread.currentThread().setContextClassLoader(origin);
} }
@SneakyThrows @SneakyThrows

View File

@ -12,7 +12,7 @@ var disable;
// 开发环境下初始化 // 开发环境下初始化
root = root || "src/main/resources"; root = root || "src/main/resources";
// 解压文件到根目录 // 解压文件到根目录
release(root, "[core|modules]/.*", true); release(root, "[api|core|internal|modules]/.*", true);
release(root, "plugins/.*"); release(root, "plugins/.*");
load(root + '/core/init.js'); load(root + '/core/init.js');
if (__FILE__ !== "<eval>") { if (__FILE__ !== "<eval>") {

View File

@ -8,7 +8,6 @@ var event = require('api/event');
var command = require('api/command'); var command = require('api/command');
var bukkit = require('api/server'); var bukkit = require('api/server');
var item = require('api/item'); var item = require('api/item');
var fs = require('fs');
var Arrays = Java.type('java.util.Arrays'); var Arrays = Java.type('java.util.Arrays');
var ItemStackArray = Java.type('org.bukkit.inventory.ItemStack[]'); var ItemStackArray = Java.type('org.bukkit.inventory.ItemStack[]');
@ -26,7 +25,7 @@ var description = {
panel: '160:13', panel: '160:13',
list: [ list: [
{ {
box:{ box: {
id: 160, id: 160,
damage: 1, damage: 1,
name: '§a箱子', name: '§a箱子',
@ -34,7 +33,7 @@ var description = {
'这是箱子的Lore' '这是箱子的Lore'
] ]
}, },
key:{ key: {
id: 160, id: 160,
damage: 2, damage: 2,
name: '§b钥匙', name: '§b钥匙',
@ -42,16 +41,28 @@ var description = {
'这是钥匙的Lore' '这是钥匙的Lore'
] ]
}, },
result:[ result: [
{ {
percent: 10, percent: 10,
command: 'money give %player% 200', command: 'money give %player% 100',
item: { item: {
id: 160, id: 160,
damage: 3, damage: 3,
name: '§c奖品', name: '§c奖品1',
lore: [ 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 panel = item.create(160, 1, 13);
var air = item.create(0);
var config; var config;
var items; var items;
var lmap=[]; var lmap = [];
var cmap=[]; var cmap = [];
function load() { function load() {
config = this.config; config = this.config;
@ -95,21 +105,21 @@ function load() {
Arrays.fill(items, 41, 54, panel); Arrays.fill(items, 41, 54, panel);
} }
function isTargetItem(item, config){ function isTargetItem(item, config) {
return item.typeId == config.id && return item.typeId === config.id &&
item.itemMeta && item.itemMeta &&
item.itemMeta.displayName == config.name && item.itemMeta.displayName === config.name &&
item.itemMeta.lore && Java.from(item.itemMeta.lore).toJson() == config.lore.toJson() item.itemMeta.lore && Java.from(item.itemMeta.lore).toJson() === config.lore.toJson()
} }
function newItem(name, sub) { function newItem(name, sub) {
return item.create(name, 1, sub || 0); return item.create(name, 1, sub || 0);
} }
function newItemFromConfig(config){ function newItemFromConfig(config) {
var i = newItem(config.id, config.damage); var i = newItem(config.id, config.damage);
if (config.name) item.setName(i, config.name) if (config.name) item.setName(i, config.name);
if (config.lore) item.setLore(i, config.lore) if (config.lore) item.setLore(i, config.lore);
return i; return i;
} }
@ -124,16 +134,14 @@ function enable() {
}); });
event.on(this, 'InventoryClick', function click(event) { event.on(this, 'InventoryClick', function click(event) {
var inv = event.inventory; var inv = event.inventory;
if (inv.title != config.title) return; if (inv.title !== config.title) return;
var player = event.whoClicked; var player = event.whoClicked;
var slot = event.rawSlot; var slot = event.rawSlot;
if (slot > 53 || slot < 0) { if (slot > 53 || slot < 0) {
return; return;
} }
event.cancelled = true; event.cancelled = true;
var click = event.currentItem; switch (slot) {
var cursor = event.cursor;
switch(slot){
case 10: case 10:
case 16: case 16:
case 40: case 40:
@ -145,17 +153,17 @@ function enable() {
break; break;
case 33: case 33:
var temp = inv.getItem(40); var temp = inv.getItem(40);
if (temp && temp.typeId != 0) { if (temp && temp.typeId !== 0) {
console.sender(player, '§c请先取走奖品!') console.sender(player, '§c请先取走奖品!');
return; return;
} }
var litem; var litem;
var box = inv.getItem(10); var box = inv.getItem(10);
var key = inv.getItem(16); var key = inv.getItem(16);
if (box && box.typeId != 0 && key && key.typeId != 0) { if (box && box.typeId !== 0 && key && key.typeId !== 0) {
for (var i = 0; i < config.list.length; i++){ for (var i = 0; i < config.list.length; i++) {
var r = config.list[i]; var r = config.list[i];
if (isTargetItem(box, r.box)){ if (isTargetItem(box, r.box)) {
litem = r; litem = r;
break; break;
} }
@ -163,16 +171,16 @@ function enable() {
} }
// TODO 抽奖 // TODO 抽奖
if (!litem) { if (!litem) {
console.sender(player, '§c请先放入抽奖物品和钥匙!') console.sender(player, '§c请先放入抽奖物品和钥匙!');
return; return;
} }
if (!isTargetItem(key, litem.key)) { if (!isTargetItem(key, litem.key)) {
console.sender(player, '§c抽奖物品和钥匙不匹配!') console.sender(player, '§c抽奖物品和钥匙不匹配!');
return; return;
} }
var resultlist = []; var resultlist = [];
litem.result.forEach(function(t){ litem.result.forEach(function (t) {
for (var i=0; i < t.percent; i++){ for (var i = 0; i < t.percent; i++) {
resultlist.push(t); resultlist.push(t);
} }
}); });
@ -191,7 +199,7 @@ function enable() {
delete cmap[player.name]; delete cmap[player.name];
break; break;
default: default:
event.cancelled = true; event.cancelled = true;
} }
}); });
} }