feat: update ms system
This commit is contained in:
		@@ -9,7 +9,7 @@ const Messenger = Bukkit.getMessenger()
 | 
			
		||||
export class BukkitChannel extends channel.Channel {
 | 
			
		||||
    @inject(plugin.PluginInstance)
 | 
			
		||||
    private pluginInstance: any
 | 
			
		||||
 | 
			
		||||
    private cacheChannel = new Map<string, any[]>()
 | 
			
		||||
    /**
 | 
			
		||||
     * 给玩家发送通道消息
 | 
			
		||||
     * @param player 接受消息的玩家
 | 
			
		||||
@@ -26,15 +26,29 @@ export class BukkitChannel extends channel.Channel {
 | 
			
		||||
     * @param listener 监听器
 | 
			
		||||
     */
 | 
			
		||||
    register(channel: string, listener: channel.ChannelListener) {
 | 
			
		||||
        Messenger.registerIncomingPluginChannel(this.pluginInstance, channel, new PluginMessageListener({
 | 
			
		||||
        if (!this.cacheChannel.has(channel)) this.cacheChannel.set(channel, [])
 | 
			
		||||
        this.cacheChannel.get(channel).push(listener)
 | 
			
		||||
        let pluginMessageListener = new PluginMessageListener({
 | 
			
		||||
            onPluginMessageReceived: (/**String */ channel, /**Player */ player, /**byte[] */data) => {
 | 
			
		||||
                listener(data, { channel, player, data })
 | 
			
		||||
                try {
 | 
			
		||||
                    listener(data, { channel, player, data })
 | 
			
		||||
                } catch (error) {
 | 
			
		||||
                    console.ex(error)
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }))
 | 
			
		||||
        })
 | 
			
		||||
        Messenger.registerIncomingPluginChannel(this.pluginInstance, channel, pluginMessageListener)
 | 
			
		||||
        Messenger.registerOutgoingPluginChannel(this.pluginInstance, channel)
 | 
			
		||||
        return pluginMessageListener
 | 
			
		||||
    }
 | 
			
		||||
    unregister(channel: string, listener: any) {
 | 
			
		||||
        Messenger.unregisterIncomingPluginChannel(this.pluginInstance, channel)
 | 
			
		||||
        Messenger.unregisterOutgoingPluginChannel(this.pluginInstance, channel)
 | 
			
		||||
        if (!this.cacheChannel.has(channel)) return
 | 
			
		||||
        let cacheListener = this.cacheChannel.get(channel)
 | 
			
		||||
        cacheListener = cacheListener.filter(l => l != listener)
 | 
			
		||||
        Messenger.unregisterIncomingPluginChannel(this.pluginInstance, channel, listener)
 | 
			
		||||
        if (cacheListener.length == 0) {
 | 
			
		||||
            this.cacheChannel.delete(channel)
 | 
			
		||||
            Messenger.unregisterOutgoingPluginChannel(this.pluginInstance, channel)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -5,12 +5,12 @@ import bukkitChat from './enhance/chat'
 | 
			
		||||
@provideSingleton(chat.Chat)
 | 
			
		||||
export class BukkitChat extends chat.Chat {
 | 
			
		||||
    sendMessage(sender: any, message: string) {
 | 
			
		||||
        bukkitChat.send(sender, { text: message }, 0)
 | 
			
		||||
        bukkitChat.send(sender, JSON.stringify({ text: message }), 0)
 | 
			
		||||
    }
 | 
			
		||||
    sendActionBar(sender: any, message: string) {
 | 
			
		||||
        bukkitChat.send(sender, { text: message }, 2)
 | 
			
		||||
        bukkitChat.send(sender, JSON.stringify({ text: message }), 2)
 | 
			
		||||
    }
 | 
			
		||||
    sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 1, time: number = 5, fadeOut: number = 1) {
 | 
			
		||||
    sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 20, time: number = 100, fadeOut: number = 20) {
 | 
			
		||||
        sender.sendTitle(title, subtitle, fadeIn, time, fadeOut)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,7 @@ let RemapUtils: any
 | 
			
		||||
let playerConnectionFieldName: string
 | 
			
		||||
let sendPacketMethodName: string
 | 
			
		||||
 | 
			
		||||
let above_1_16 = false
 | 
			
		||||
let downgrade = false
 | 
			
		||||
/**
 | 
			
		||||
 * 获取NMS版本
 | 
			
		||||
@@ -59,12 +60,18 @@ function init() {
 | 
			
		||||
    let packetTypeClass = nmsCls("PacketPlayOutChat")
 | 
			
		||||
    PacketPlayOutChat = Java.type(packetTypeClass.getName())
 | 
			
		||||
    let packetTypeConstructor: { parameterTypes: any[] }
 | 
			
		||||
    Java.from(packetTypeClass.constructors).forEach(function (c) {
 | 
			
		||||
    let constructors = packetTypeClass.constructors
 | 
			
		||||
    Java.from(constructors).forEach(function (c) {
 | 
			
		||||
        if (c.parameterTypes.length === 2) {
 | 
			
		||||
            packetTypeConstructor = c
 | 
			
		||||
        }
 | 
			
		||||
        if (c.parameterTypes.length === 3) {
 | 
			
		||||
            packetTypeConstructor = c
 | 
			
		||||
            above_1_16 = true
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
    let nmsChatMessageTypeClass = packetTypeConstructor.parameterTypes[1]
 | 
			
		||||
    let parameterTypes = packetTypeConstructor.parameterTypes
 | 
			
		||||
    let nmsChatMessageTypeClass = parameterTypes[1]
 | 
			
		||||
    if (nmsChatMessageTypeClass.isEnum()) {
 | 
			
		||||
        chatMessageTypes = nmsChatMessageTypeClass.getEnumConstants()
 | 
			
		||||
    }
 | 
			
		||||
@@ -83,7 +90,13 @@ function json(sender: { name: string }, json: string) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function send(sender: any, json: any, type: number) {
 | 
			
		||||
    sendPacket(sender, new PacketPlayOutChat(ChatSerializer[nmsChatSerializerMethodName](json), chatMessageTypes == null ? type : chatMessageTypes[type]))
 | 
			
		||||
    let packet
 | 
			
		||||
    if (above_1_16) {
 | 
			
		||||
        packet = new PacketPlayOutChat(ChatSerializer[nmsChatSerializerMethodName](json), chatMessageTypes == null ? type : chatMessageTypes[type], sender.getUniqueId())
 | 
			
		||||
    } else {
 | 
			
		||||
        packet = new PacketPlayOutChat(ChatSerializer[nmsChatSerializerMethodName](json), chatMessageTypes == null ? type : chatMessageTypes[type])
 | 
			
		||||
    }
 | 
			
		||||
    sendPacket(sender, packet)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function sendPacket(player: { handle: { [x: string]: { [x: string]: (arg0: any) => void } } }, p: any) {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,13 +3,14 @@
 | 
			
		||||
import { server } from '@ccms/api'
 | 
			
		||||
import { Container } from '@ccms/container'
 | 
			
		||||
 | 
			
		||||
import { BukkitConsole } from './console';
 | 
			
		||||
import './event';
 | 
			
		||||
import './server';
 | 
			
		||||
import './command';
 | 
			
		||||
import './channel';
 | 
			
		||||
import './task';
 | 
			
		||||
import { BukkitConsole } from './console'
 | 
			
		||||
import './chat'
 | 
			
		||||
import './task'
 | 
			
		||||
import './event'
 | 
			
		||||
import './server'
 | 
			
		||||
import './command'
 | 
			
		||||
import './channel'
 | 
			
		||||
 | 
			
		||||
export default function BukkitImpl(container: Container) {
 | 
			
		||||
    container.bind(server.Console).toConstantValue(BukkitConsole);
 | 
			
		||||
    container.bind(server.Console).toConstantValue(BukkitConsole)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -63,4 +63,7 @@ export class BukkitServer extends server.ReflectServer {
 | 
			
		||||
            this.dispatchConsoleCommand(result)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    tabComplete?(sender: any, input: string, index?: number): string[] {
 | 
			
		||||
        throw new Error("Method not implemented.")
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user