32 lines
805 B
GDScript
32 lines
805 B
GDScript
extends PacketHandler
|
|
|
|
signal message_received(player: Player, message: String)
|
|
|
|
func send_message(message: String):
|
|
var buffer := StreamPeerBuffer.new()
|
|
buffer.put_u8(Packet.CHAT)
|
|
buffer.put_data(message.to_utf8_buffer())
|
|
%Client.send(buffer.data_array)
|
|
|
|
func handle_packet(data: PackedByteArray):
|
|
var buffer := StreamPeerBuffer.new()
|
|
buffer.data_array = data
|
|
|
|
var player_id := buffer.get_string()
|
|
var player := Global.players.get_player(player_id)
|
|
|
|
if !player:
|
|
return
|
|
|
|
var message := buffer.get_utf8_string()
|
|
message = escape_bbcode(message)
|
|
|
|
Log.info("%s: %s" % [player.name, message])
|
|
emit.call_deferred(player, message)
|
|
|
|
func emit(player: Player, message: String):
|
|
message_received.emit(player, message)
|
|
|
|
func escape_bbcode(text: String) -> String:
|
|
return text.replace("[", "[lb]")
|
|
|