feat: add papi hook

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-09-27 19:39:10 +08:00
parent c31eaeb8f6
commit 94840ae59d

View File

@ -51,10 +51,18 @@ class QRCodeRender {
interface PlaceholderAPI { interface PlaceholderAPI {
registerPlaceholderHook: (key: string, onPlaceholderRequest: (player, s) => string) => void registerPlaceholderHook: (key: string, onPlaceholderRequest: (player, s) => string) => void
unregisterPlaceholderHook: (key: string) => void
setPlaceholders: (player: any, str: string) => string setPlaceholders: (player: any, str: string) => string
} }
@JSPlugin({ prefix: 'MRD', version: '1.3.1', author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], source: __filename }) interface UserInfo {
balance: number
sign: string
video: string
box: string
}
@JSPlugin({ prefix: 'MRD', version: '1.3.2', author: 'MiaoWoo', servers: [constants.ServerType.Bukkit], source: __filename })
export class MiaoReward extends interfaces.Plugin { export class MiaoReward extends interfaces.Plugin {
private serverInfo: any private serverInfo: any
private cacheBindUuid = '' private cacheBindUuid = ''
@ -66,6 +74,7 @@ export class MiaoReward extends interfaces.Plugin {
private bindingLeftTime = 45 private bindingLeftTime = 45
private bindingNotify = new java.util.HashSet<org.bukkit.entity.Player>() private bindingNotify = new java.util.HashSet<org.bukkit.entity.Player>()
private drawCooldown = new Map<string, number>() private drawCooldown = new Map<string, number>()
private playerInfoCache = new Map<string, UserInfo>()
private downgrade = false private downgrade = false
@ -105,6 +114,7 @@ export class MiaoReward extends interfaces.Plugin {
this.logger.prefix = this.config.prefix this.logger.prefix = this.config.prefix
this.downgrade = Bukkit.getServer().class.name.split('.')[3] == "v1_7_R4" this.downgrade = Bukkit.getServer().class.name.split('.')[3] == "v1_7_R4"
this.updateServerInfo() this.updateServerInfo()
this.updatePlayerInfo()
} }
private updateServerInfo(player?: any) { private updateServerInfo(player?: any) {
@ -122,6 +132,17 @@ export class MiaoReward extends interfaces.Plugin {
}).async().submit() }).async().submit()
} }
private updatePlayerInfo(player?: any) {
this.taskManager.create(() => {
Java.from(this.server.getOnlinePlayers()).forEach(p => {
let info = this.queryUser(p)
if (info.code == 200) {
this.playerInfoCache.set(p.getName(), info.data)
}
})
}).async().submit()
}
@JSClass('com.google.common.io.ByteStreams') @JSClass('com.google.common.io.ByteStreams')
private ByteStreams: any private ByteStreams: any
@ -135,7 +156,6 @@ export class MiaoReward extends interfaces.Plugin {
this.channelOff = this.channel?.listen(this, 'BungeeCord', (data) => { this.channelOff = this.channel?.listen(this, 'BungeeCord', (data) => {
if (!this.isBungeeCord) { if (!this.isBungeeCord) {
this.isBungeeCord = true this.isBungeeCord = true
this.PlayerJoinEvent['off']()
} }
let input = this.ByteStreams.newDataInput(data) let input = this.ByteStreams.newDataInput(data)
let subChannel = input.readUTF() let subChannel = input.readUTF()
@ -153,15 +173,6 @@ export class MiaoReward extends interfaces.Plugin {
if (players.length) this.bungeeCordDetect(players[0]) if (players.length) this.bungeeCordDetect(players[0])
} }
cmdrun(sender: any, ...args: any[]) {
try {
let tfunc = new Function(`return '§a返回结果: §r'+ eval(${JSON.stringify(args.join(' '))});`)
this.logger.sender(sender, tfunc.apply(this) + '')
} catch (ex) {
this.logger.sender(sender, this.logger.stack(ex))
}
}
private readForward(input) { private readForward(input) {
let message = JSON.parse(input.readUTF()) let message = JSON.parse(input.readUTF())
console.log(message) console.log(message)
@ -179,12 +190,20 @@ export class MiaoReward extends interfaces.Plugin {
} else { } else {
this.PlaceholderAPI.registerPlaceholderHook("mrd", new this.PlaceholderHook({ this.PlaceholderAPI.registerPlaceholderHook("mrd", new this.PlaceholderHook({
onPlaceholderRequest: (player: any, s: string) => { onPlaceholderRequest: (player: any, s: string) => {
if (!this.playerInfoCache.has(player.getName())) {
return '用户未绑定'
}
let data = this.playerInfoCache.get(player.getName())
switch (s.toLowerCase()) { switch (s.toLowerCase()) {
case "server": case "balance":
case "bserver": return data.balance
case "sign":
return data.sign
case "box":
return data.box
default: default:
} }
return "未知的参数" return "未知的参数: " + s
} }
})) }))
} }
@ -201,6 +220,7 @@ export class MiaoReward extends interfaces.Plugin {
} }
disable() { disable() {
this.PlaceholderAPI.unregisterPlaceholderHook("mrd")
Java.from(this.server.getOnlinePlayers()).forEach(p => this.checkAndClear(p)) Java.from(this.server.getOnlinePlayers()).forEach(p => this.checkAndClear(p))
this.channelOff?.off() this.channelOff?.off()
} }
@ -267,7 +287,7 @@ export class MiaoReward extends interfaces.Plugin {
private bindCheck(sender: org.bukkit.entity.Player, cooldown: number) { private bindCheck(sender: org.bukkit.entity.Player, cooldown: number) {
if (this.isBinding) { if (this.isBinding) {
let bindUser = Bukkit.getPlayerExact(this.bindingUser) let bindUser = Bukkit.getPlayerExact(this.bindingUser)
if (bindUser && bindUser.isOnline() && this.isQrCodeItem(bindUser.getItemInHand())[0]) { if (bindUser && bindUser.isOnline() && this.isHoldQrCodeItem(bindUser)) {
this.bindingNotify.add(sender) this.bindingNotify.add(sender)
this.logger.sender(sender, [ this.logger.sender(sender, [
"§c当前 §a" + this.bindingUser + " §c玩家正在扫码", "§c当前 §a" + this.bindingUser + " §c玩家正在扫码",
@ -580,14 +600,15 @@ export class MiaoReward extends interfaces.Plugin {
} }
cmdquery(sender: org.bukkit.entity.Player) { cmdquery(sender: org.bukkit.entity.Player) {
let check = this.queryUser(sender) let info = this.queryUser(sender)
if (check.code !== 200) { if (info.code !== 200) {
return this.logger.sender(sender, '§4查询异常! §cError: ' + check.msg) return this.logger.sender(sender, '§4查询异常! §cError: ' + info.msg)
} }
this.sendResult(sender, '查询结果', check.data) this.sendResult(sender, '查询结果', info.data)
} }
private sendResult(sender: any, title: string, data: any) { private sendResult(sender: any, title: string, data: any) {
this.playerInfoCache.set(sender.getName(), data)
this.logger.sender(sender, [ this.logger.sender(sender, [
`§6====== ${this.config.prefix} §a${title} §6======`, `§6====== ${this.config.prefix} §a${title} §6======`,
`§6用 户 名: §a${sender.getName()}`, `§6用 户 名: §a${sender.getName()}`,
@ -612,47 +633,6 @@ CAST TIME : ${Date.now() - startTime}`)
return result return result
} }
// var server = container.get(api.server.Server)
// var console = server.getDedicatedServer()
// var mainWorld = reflect.on(console).get('field_71305_c').get()[0]
// 设置新的Data
// mapdata = new MapData(s);
// mapdata.field_76197_d = (byte)3;
// mapdata.func_176054_a((double)worldIn.func_72912_H().func_76079_c(), (double)worldIn.func_72912_H().func_76074_e(), (int)mapdata.field_76197_d);
// mapdata.field_76200_c = ((WorldServer)worldIn).dimension;
// mapdata.func_76185_a();
// worldMain.func_72823_a(s, (WorldSavedData)mapdata);
// s = "map_" + stack.getMetadata();
// mapdata = new MapData(s);
// mapdata.scale = 3;
// mapdata.calculateMapCenter((double)worldIn.getWorldInfo().getSpawnX(), (double)worldIn.getWorldInfo().getSpawnZ(), mapdata.scale);
// // mapdata.dimension = worldIn.provider.getDimension();
// mapdata.dimension = ((WorldServer) worldIn).dimension; // CraftBukkit - fixes Bukkit multiworld maps
// mapdata.markDirty();
// worldMain.setData(s, mapdata);
// var mapStorage = mainWorld.field_72988_C
// var loadedDataMap = reflect.on(mapStorage).get('field_75749_b').get()
// var loadedDataList = reflect.on(mapStorage).get('field_75750_c').get()
// var idCounts = reflect.on(mapStorage).get('field_75748_d').get()
// @Override
// public CraftMapView createMap(World world) {
// Validate.notNull((Object)world, (String)"World cannot be null", (Object[])new Object[0]);
// net.minecraft.item.ItemStack stack = new net.minecraft.item.ItemStack((Item)Items.field_151148_bJ, 1, -1);
// MapData worldmap = Items.field_151098_aY.func_77873_a(stack, (net.minecraft.world.World)((CraftWorld)world).getHandle());
// return worldmap.mapView;
// }
//net.minecraft.item.ItemMap
// @Override
// public CraftMapView createMap(World world) {
// Validate.notNull(world, "World cannot be null");
// net.minecraft.item.ItemStack stack = new net.minecraft.item.ItemStack(Items.MAP, 1, -1, true); // CatServer
// MapData worldmap = Items.FILLED_MAP.getMapData(stack, ((CraftWorld) world).getHandle());
// return worldmap.mapView;
// }
private createQrCodeMapItem(content: string) { private createQrCodeMapItem(content: string) {
let item: org.bukkit.inventory.ItemStack let item: org.bukkit.inventory.ItemStack
item = new ItemStack(Material.FILLED_MAP || Material.MAP) item = new ItemStack(Material.FILLED_MAP || Material.MAP)
@ -679,15 +659,6 @@ CAST TIME : ${Date.now() - startTime}`)
let qrcode = this.js2qr(content) let qrcode = this.js2qr(content)
let startPoint = Math.round((bufferedImage.getWidth() - qrcode.getWidth()) / 2) let startPoint = Math.round((bufferedImage.getWidth() - qrcode.getWidth()) / 2)
graphics2D.drawImage(qrcode, startPoint, startPoint, null) graphics2D.drawImage(qrcode, startPoint, startPoint, null)
// let font = new Font("DejaVuSans", Font.PLAIN, 10)
// graphics2D.setFont(font)
// let fm = graphics2D.getFontMetrics(font)
// let text = "Use QQ Scan Bind!"
// let textWidth = fm.stringWidth(text)
// let widthX = (128 - textWidth) / 2
// graphics2D.setColor(Color.BLACK)
// graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB)
// graphics2D['drawString(java.lang.String, int, int)'](text, Math.round(widthX), 122)
graphics2D.dispose() graphics2D.dispose()
return bufferedImage return bufferedImage
} }
@ -714,13 +685,17 @@ CAST TIME : ${Date.now() - startTime}`)
@Listener() @Listener()
PlayerJoinEvent(event: org.bukkit.event.player.PlayerJoinEvent) { PlayerJoinEvent(event: org.bukkit.event.player.PlayerJoinEvent) {
this.bungeeCordDetect(event.getPlayer()) const player = event.getPlayer()
this.bungeeCordDetect(player)
let info = this.queryUser(player)
if (info.code == 200) {
this.playerInfoCache.set(player.getName(), info.data)
}
} }
@Listener() @Listener()
PlayerDropItemEvent(event: org.bukkit.event.player.PlayerDropItemEvent) { PlayerDropItemEvent(event: org.bukkit.event.player.PlayerDropItemEvent) {
let [cancelled, id] = this.isQrCodeItem(event.getItemDrop().getItemStack()) if (this.isQrCodeItem(event.getItemDrop().getItemStack())) {
if (id != null && id != undefined && cancelled) {
event.getItemDrop().remove() event.getItemDrop().remove()
} }
} }
@ -728,8 +703,7 @@ CAST TIME : ${Date.now() - startTime}`)
@Listener() @Listener()
PlayerItemHeldEvent(event: org.bukkit.event.player.PlayerItemHeldEvent) { PlayerItemHeldEvent(event: org.bukkit.event.player.PlayerItemHeldEvent) {
let inv = event.getPlayer().getInventory() let inv = event.getPlayer().getInventory()
let [cancelled, id] = this.isQrCodeItem(inv.getItem(event.getPreviousSlot() as any)) if (this.isQrCodeItem(inv.getItem(event.getPreviousSlot() as any))) {
if (id != null && id != undefined && cancelled) {
inv.setItem(event.getPreviousSlot(), null) inv.setItem(event.getPreviousSlot(), null)
} }
} }
@ -737,8 +711,7 @@ CAST TIME : ${Date.now() - startTime}`)
@Listener() @Listener()
InventoryClickEvent(event: org.bukkit.event.inventory.InventoryClickEvent) { InventoryClickEvent(event: org.bukkit.event.inventory.InventoryClickEvent) {
let item = event.getCurrentItem() let item = event.getCurrentItem()
let [cancelled, id] = this.isQrCodeItem(item) if (this.isQrCodeItem(item)) {
if (id != null && id != undefined && cancelled) {
event.getInventory().setItem(event.getSlot(), null) event.getInventory().setItem(event.getSlot(), null)
event.setCancelled(true) event.setCancelled(true)
} }
@ -767,7 +740,7 @@ CAST TIME : ${Date.now() - startTime}`)
} }
private isHoldQrCodeItem(player: org.bukkit.entity.Player) { private isHoldQrCodeItem(player: org.bukkit.entity.Player) {
return this.isQrCodeItem(player.getItemInHand())[0] return this.isQrCodeItem(player.getItemInHand())
} }
private checkAndClear(player: org.bukkit.entity.Player) { private checkAndClear(player: org.bukkit.entity.Player) {
@ -776,13 +749,12 @@ CAST TIME : ${Date.now() - startTime}`)
} }
} }
private isQrCodeItem(item: org.bukkit.inventory.ItemStack): [boolean, number?] { private isQrCodeItem(item: org.bukkit.inventory.ItemStack): boolean {
if (!item) { return [false] } if (!item) { return false }
if ((item?.getType() == Material.MAP || item?.getType() == Material.FILLED_MAP) && item.hasItemMeta()) { if ((item.getType() == Material.MAP || item.getType() == Material.FILLED_MAP) && item.hasItemMeta()) {
let meta = item.getItemMeta() return Java.from(item.getItemMeta().getLore()).indexOf('QRCODE') != -1
return [!!(Java.from(meta.getLore()).indexOf('QRCODE') != -1), item.getDurability()]
} }
return [false] return false
} }
cmdhelp(sender: any) { cmdhelp(sender: any) {