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 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

View File

@ -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__ !== "<eval>") {

View File

@ -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[]');
@ -45,13 +44,25 @@ var description = {
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,7 +73,6 @@ var description = {
};
var panel = item.create(160, 1, 13);
var air = item.create(0);
var config;
var items;
@ -96,10 +106,10 @@ function load() {
}
function isTargetItem(item, config) {
return item.typeId == config.id &&
return item.typeId === config.id &&
item.itemMeta &&
item.itemMeta.displayName == config.name &&
item.itemMeta.lore && Java.from(item.itemMeta.lore).toJson() == config.lore.toJson()
item.itemMeta.displayName === config.name &&
item.itemMeta.lore && Java.from(item.itemMeta.lore).toJson() === config.lore.toJson()
}
function newItem(name, sub) {
@ -108,8 +118,8 @@ function newItem(name, sub) {
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,15 +134,13 @@ 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) {
case 10:
case 16:
@ -145,14 +153,14 @@ 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) {
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)) {
@ -163,11 +171,11 @@ 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 = [];