Files
LonaRPG-Online-Games/ModScripts/multiplayer/11_chat.rb
T
2026-10-29 21:59:06 +04:00

1138 lines
27 KiB
Ruby

# encoding: UTF-8
#==============================================================================
# Multiplayer Chat (T / E) + server-backed history (last 20)
#------------------------------------------------------------------------------
# Protocol (TCP lines):
# client -> chat:<text>
# client -> chat_last:<n>
# server -> chat_msg:<msg_id>:<ts_ms>:<player_id>:<nick>:<text>
# Notes:
# - <text> can contain ':' (client splits with limit)
# - nick uses WebSocket.esc/unesc (':' -> ';')
#==============================================================================
module WebSocket
# -------------------------
# Chat state
# -------------------------
@chat_messages = [] # [id, ts_ms, pid, nick, text]
@chat_ids = {}
def self.chat_messages
@chat_messages ||= []
end
def self.chat_clear
@chat_messages = []
@chat_ids = {}
end
def self.chat_add(msg_id, ts_ms, pid, nick, text)
@chat_messages ||= []
@chat_ids ||= {}
mid = msg_id.to_i
return if mid <= 0
return if @chat_ids[mid]
n = nick.to_s
t = text.to_s
@chat_messages << [mid, ts_ms.to_i, pid.to_i, n, t]
@chat_ids[mid] = true
# keep last 20 by msg_id
@chat_messages.sort_by! { |m| m[0] }
max_log = 20 # Print (задаем макс строк логирования)
if @chat_messages.size > max_log # Print 20
removed = @chat_messages[0, @chat_messages.size - max_log] # Print 20
removed.each { |m| @chat_ids.delete(m[0]) }
@chat_messages = @chat_messages.last(max_log) # Print 20
end
end
def self.chat_time_text(ts_ms)
raw = ts_ms.to_i
return "" if raw <= 0
Time.at(raw / 1000.0).strftime("%d.%m %H:%M")
rescue
""
end
def self.request_chat_last(n = 20) # Print 20
return unless @connected
max_log = 20 # Print (добавили макс значение)
n = n.to_i
n = max_log if n <= 0 # Print 20
n = max_log if n > max_log # Print 20
send_line("chat_last:#{n}")
end
def self.send_chat(text)
return unless @connected
t = text.to_s
t = t.gsub(/\r\n?/, " ").gsub("\n", " ")
t = t.gsub("\0", "")
t = t.strip
t = t[0, 200]
return if t.empty?
send_line("chat:#{t}")
end
# -------------------------
# Extend parser without editing core file
# -------------------------
class << self
alias mp_parse_without_chat parse
end
def self.parse(msg)
if msg.start_with?("chat_msg:")
parts = msg.split(":", 6)
if parts.size >= 6
msg_id = parts[1].to_i
ts_ms = parts[2].to_i
pid = parts[3].to_i
nick = unesc(parts[4].to_s)
text = parts[5].to_s
chat_add(msg_id, ts_ms, pid, nick, text)
end
return
end
mp_parse_without_chat(msg)
end
end
#==============================================================================
# Ruby 3 / mkxp-z keyboard helpers
# - avoid direct user32 calls
# - prefer mkxp-z Input.UTF8String for layout-aware text input
#==============================================================================
module MPKeyboard
VK_TO_SYMBOL = {
0x08 => :BACK,
0x09 => :TAB,
0x0D => :RETURN,
0x10 => :SHIFT,
0x11 => :CTRL,
0x12 => :ALT,
0x1B => :ESCAPE,
0x20 => :SPACE,
0x70 => :F1,
0x71 => :F2,
0x72 => :F3,
0x73 => :F4,
0x74 => :F5,
0x75 => :F6,
0x76 => :F7,
0x77 => :F8,
0x78 => :F9,
0x79 => :F10,
0x7A => :F11,
0x7B => :F12
}
LETTER_SYMBOLS = {}
('A'..'Z').each { |ch| LETTER_SYMBOLS[ch.ord] = ch.to_sym }
DIGIT_SYMBOLS = {
0x30 => :'0', 0x31 => :'1', 0x32 => :'2', 0x33 => :'3', 0x34 => :'4',
0x35 => :'5', 0x36 => :'6', 0x37 => :'7', 0x38 => :'8', 0x39 => :'9'
}
OEM_SYMBOLS = {
0xBA => :OEM_1,
0xBB => :OEM_PLUS,
0xBC => :OEM_COMMA,
0xBD => :OEM_MINUS,
0xBE => :OEM_PERIOD,
0xBF => :OEM_2,
0xC0 => :OEM_3,
0xDB => :OEM_4,
0xDC => :OEM_5,
0xDD => :OEM_6,
0xDE => :OEM_7
}
def self.symbol_for(vk)
VK_TO_SYMBOL[vk] || LETTER_SYMBOLS[vk] || DIGIT_SYMBOLS[vk] || OEM_SYMBOLS[vk]
end
def self.query(method_name, vk)
result = false
begin
result = Input.send(method_name, vk)
return !!result
rescue
end
sym = symbol_for(vk)
if !sym.nil?
begin
result = Input.send(method_name, sym)
return !!result
rescue
end
end
false
end
def self.trigger?(vk)
query(:trigger?, vk.to_i)
end
def self.press?(vk)
query(:press?, vk.to_i)
end
end
module MPTextInput
VK_BACK = 0x08
VK_RETURN = 0x0D
VK_ESCAPE = 0x1B
VK_CTRL = 0x11
VK_SHIFT = 0x10
VK_ALT = 0x12
INITIAL_REPEAT_DELAY = 22
REPEAT_INTERVAL = 3
# punctuation
VK_OEM_1 = 0xBA
VK_OEM_PLUS = 0xBB
VK_OEM_COMMA = 0xBC
VK_OEM_MINUS = 0xBD
VK_OEM_PERIOD = 0xBE
VK_OEM_2 = 0xBF
VK_OEM_3 = 0xC0
VK_OEM_4 = 0xDB
VK_OEM_5 = 0xDC
VK_OEM_6 = 0xDD
VK_OEM_7 = 0xDE
KEYS = (
(0x30..0x39).to_a +
(0x41..0x5A).to_a +
[0x20, VK_OEM_1, VK_OEM_PLUS, VK_OEM_COMMA, VK_OEM_MINUS, VK_OEM_PERIOD,
VK_OEM_2, VK_OEM_3, VK_OEM_4, VK_OEM_5, VK_OEM_6, VK_OEM_7]
)
ASCII_SHIFT_MAP = {
'1' => '!', '2' => '@', '3' => '#', '4' => '$', '5' => '%',
'6' => '^', '7' => '&', '8' => '*', '9' => '(', '0' => ')',
'-' => '_', '=' => '+', '[' => '{', ']' => '}', '\\' => '|',
';' => ':', "'" => '"', ',' => '<', '.' => '>', '/' => '?', '`' => '~'
}
ASCII_BASE_MAP = {
VK_OEM_1 => ';',
VK_OEM_PLUS => '=',
VK_OEM_COMMA => ',',
VK_OEM_MINUS => '-',
VK_OEM_PERIOD => '.',
VK_OEM_2 => '/',
VK_OEM_3 => '`',
VK_OEM_4 => '[',
VK_OEM_5 => '\\',
VK_OEM_6 => ']',
VK_OEM_7 => "'",
0x20 => ' '
}
def self.native_available?
Input.respond_to?(:UTF8String)
rescue
false
end
def self.sanitize_text(text)
value = text.to_s
begin
value = value.encode('UTF-8', 'UTF-8', :invalid => :replace, :undef => :replace, :replace => '')
rescue
value = value.to_s
end
value = value.gsub(/
|
|/, '')
value.gsub(/[-]/, '')
end
def self.reset!
@repeat_frames = {}
end
def self.utf8_text
return '' unless native_available?
sanitize_text(Input.UTF8String.to_s)
rescue
''
end
def self.shift_pressed?
MPKeyboard.press?(VK_SHIFT)
rescue
false
end
def self.to_char(vk)
key = vk.to_i
shift = shift_pressed?
if key >= 0x41 && key <= 0x5A
base = key.chr
return shift ? base : base.downcase
end
if key >= 0x30 && key <= 0x39
base = key.chr
return shift ? (ASCII_SHIFT_MAP[base] || base) : base
end
base = ASCII_BASE_MAP[key]
return '' if base.nil?
shift ? (ASCII_SHIFT_MAP[base] || base) : base
rescue
''
end
def self.triggered_text
text = utf8_text
return text unless text.empty?
out = ''
KEYS.each do |vk|
next unless MPKeyboard.trigger?(vk)
ch = to_char(vk).to_s
next if ch.empty?
out << ch
end
sanitize_text(out)
rescue
''
end
def self.repeated_text
text = utf8_text
return text unless text.empty?
triggered_text
rescue
''
end
end
#==============================================================================
# Chat formatting: Minecraft color codes (&0-&f / §0-§f) + server new lines
#==============================================================================
module MPChatFormat
COLOR_TABLE = {
'0' => [0, 0, 0],
'1' => [0, 0, 170],
'2' => [0, 170, 0],
'3' => [0, 170, 170],
'4' => [170, 0, 0],
'5' => [170, 0, 170],
'6' => [255, 170, 0],
'7' => [170, 170, 170],
'8' => [85, 85, 85],
'9' => [85, 85, 255],
'a' => [85, 255, 85],
'b' => [85, 255, 255],
'c' => [255, 85, 85],
'd' => [255, 85, 255],
'e' => [255, 255, 85],
'f' => [255, 255, 255]
}
COLOR_CODE_MARKERS = ['&', '§']
def self.color_for_code(code)
rgb = COLOR_TABLE[code.to_s.downcase]
return nil if rgb.nil?
Color.new(rgb[0], rgb[1], rgb[2])
rescue
nil
end
def self.expand_escaped_section_signs(value)
# Supports MOTD-style text that arrives as literal "\u00A7a".
value.to_s.gsub(/\\u00a7/i, '§')
rescue
value.to_s
end
def self.normalize_text(value, keep_newlines = false)
s = expand_escaped_section_signs(value)
begin
s = s.encode('UTF-8', 'UTF-8', :invalid => :replace, :undef => :replace, :replace => '')
rescue
s = s.to_s
end
if keep_newlines
s = s.gsub(/\r\n?/, "\n").gsub("\0", '')
# Preserve only LF; remove all other control symbols.
s.gsub(/[\u0000-\u0009\u000B-\u001F\u007F]/, '')
else
s.gsub(/\r|\n|\0/, '').gsub(/[\u0000-\u001F\u007F]/, '')
end
end
# TCP protocol is line based, so the server can send literal \n / \r\n in text.
# Example server text: "&cSERVER: first line\n&esecond line".
def self.decode_server_newlines(value)
s = expand_escaped_section_signs(value)
s = s.gsub(/\\r\\n|\\n|\\r/i, "\n")
s = s.gsub(/\\u000d\\u000a|\\u000a|\\u000d/i, "\n")
normalize_text(s, true)
rescue
normalize_text(value, true)
end
def self.color_code_marker?(ch)
COLOR_CODE_MARKERS.include?(ch.to_s)
rescue
false
end
def self.color_or_reset_code?(code)
c = code.to_s.downcase
COLOR_TABLE.has_key?(c) || c == 'r'
rescue
false
end
def self.strip_color_codes(value)
s = normalize_text(value, true)
begin
chars = s.each_char.to_a
rescue
chars = s.to_s.split(//)
end
out = ''
i = 0
while i < chars.size
ch = chars[i]
if color_code_marker?(ch) && i + 1 < chars.size
code = chars[i + 1].to_s.downcase
if color_or_reset_code?(code)
i += 2
next
end
end
out << ch.to_s
i += 1
end
out
end
# Returns [visible_char, color] items; color codes are consumed and not drawn.
# Supports chat codes &0-&f and MOTD/unicode codes §0-§f / \u00A70-\u00A7f.
# &r / §r are accepted as reset to the default message color.
def self.items(value, default_color, keep_newlines = false)
s = normalize_text(value, keep_newlines)
begin
chars = s.each_char.to_a
rescue
chars = s.to_s.split(//)
end
out = []
current_color = default_color
i = 0
while i < chars.size
ch = chars[i]
if color_code_marker?(ch) && i + 1 < chars.size
code = chars[i + 1].to_s.downcase
if code == 'r'
current_color = default_color
i += 2
next
end
new_color = color_for_code(code)
if new_color
current_color = new_color
i += 2
next
end
end
out << [ch, current_color]
i += 1
end
out
end
def self.color_key(color)
begin
[color.red.to_i, color.green.to_i, color.blue.to_i, color.alpha.to_i]
rescue
[color.object_id]
end
end
end
#==============================================================================
# Chat overlay (Scene_Map)
#==============================================================================
module MPChat
VK_T = 0x54 # T (русская Е на той же клавише)
# VK_E = 0x45 # E (на всякий) # Print (хотелось бы выбирать клавишу)
VK_E = 0x52 # R (на всякий) # Print (хотелось бы выбирать клавишу)
VK_ESCAPE = MPTextInput::VK_ESCAPE
VK_RETURN = MPTextInput::VK_RETURN
VK_BACK = MPTextInput::VK_BACK
def self.active?
!!@active
end
def self.input_passthrough?
!!@input_passthrough
end
def self.recently_closed?
begin
return (@last_close_frame == Graphics.frame_count)
rescue
return false
end
end
def self.with_input_passthrough
@input_passthrough = true
yield
ensure
@input_passthrough = false
end
def self.open_key_triggered?
false
end
def self.cancel_key_triggered?
Input.trigger?(:ESCAPE)
rescue
false
end
def self.submit_key_triggered?
Input.trigger?(:RETURN)
rescue
false
end
def self.backspace_pressed?
Input.repeat?(:BACK) || Input.repeat?(:DELETE)
rescue
false
end
def self.typed_text
native_text = false
begin
native_text = MPTextInput.native_available?
rescue
native_text = false
end
begin
return "" if !native_text && (Input.press?(:CTRL) || Input.press?(:ALT))
rescue
end
text = ""
begin
text = MPTextInput.repeated_text.to_s
rescue
text = ""
end
begin
text = text.to_s.encode("UTF-8", "UTF-8", :invalid => :replace, :undef => :replace, :replace => "")
rescue
text = text.to_s
end
text.gsub(/\r|\n|\0/, "").gsub(/[\u0000-\u001F\u007F]/, "")
end
def self.ui_font_name
begin
return MPSafeFont.chat_name if defined?(MPSafeFont) && MPSafeFont.respond_to?(:chat_name)
return MPSafeFont.name if defined?(MPSafeFont) && MPSafeFont.respond_to?(:name)
rescue
end
begin
if defined?(FONT_MOD) && defined?(FM_ST) && FONT_MOD.const_defined?(:LNG) && FONT_MOD::LNG.to_s == (DataManager.get_lang_constant.to_s rescue "")
return FONT_MOD::DIALOG if FONT_MOD.const_defined?(:DIALOG)
return FONT_MOD::MENU_M if FONT_MOD.const_defined?(:MENU_M)
return FM_ST::MW_FNT if FM_ST.const_defined?(:MW_FNT)
end
rescue
end
begin
if defined?(System_Settings) && System_Settings.const_defined?(:MESSAGE_WINDOW_FONT_NAME)
value = System_Settings::MESSAGE_WINDOW_FONT_NAME
value = value[0] if value.is_a?(Array)
return value
end
rescue
end
"VL Gothic"
end
def self.open
return if @active
@active = true
MPTextInput.reset! rescue nil
WebSocket.request_chat_last(21) # Print 20
create_windows
end
def self.close
return unless @active
@active = false
@input_passthrough = false
MPTextInput.reset! rescue nil
begin
@last_close_frame = Graphics.frame_count
rescue
@last_close_frame = nil
end
dispose_windows
end
def self.toggle
active? ? close : open
end
def self.create_windows
dispose_windows
w = Graphics.width
h = Graphics.height
input_h = 43 # Print (высота окна ввода) 33
log_h = h-input_h # Print (высота лога - всё свободное место)
@log_window = Window_MPChatLog.new(0, 0, w, log_h) # Print (лог с самого верха)
@input_window = Window_MPChatInput.new(0, log_h, w, input_h) # Print (ввод с самого низа)
# Always on top of map/UI
@log_window.z = 9999 rescue nil
@input_window.z = 9999 rescue nil
end
def self.dispose_windows
if @log_window
@log_window.dispose rescue nil
@log_window = nil
end
if @input_window
@input_window.dispose rescue nil
@input_window = nil
end
end
def self.update
if !active? && open_key_triggered?
open
return
end
return unless active?
close_requested = false
with_input_passthrough do
close_requested = cancel_key_triggered?
@input_window.update if @input_window && !close_requested
end
if close_requested
close
return
end
@log_window.update if @log_window
if @input_window && @input_window.consume_enter?
text = @input_window.text
WebSocket.send_chat(text)
@input_window.clear
end
@log_window.refresh_if_needed if @log_window
end
end
class Window_MPChatLog < Window_Base
CHAT_PAD_X = 4
def initialize(x, y, w, h)
super(x, y, w, h)
self.opacity = 200
self.z = 9999
@last_sig = nil
refresh
end
def refresh_if_needed
apply_chat_font
msgs = WebSocket.chat_messages
sig = [msgs.size, (msgs.empty? ? 0 : msgs[-1][0]), contents.width, contents.height]
refresh if sig != @last_sig
end
def apply_chat_font
contents.font.name = MPChat.ui_font_name
contents.font.size = 20
contents.font.shadow = false
contents.font.outline = false
rescue
end
def normalize_chat_text(value, keep_newlines = false)
MPChatFormat.normalize_text(value, keep_newlines)
rescue
value.to_s
end
def server_message?(pid, nick)
return true if pid.to_i < 0
n = nick.to_s.upcase
n == 'SERVER' || n == '[SERVER]'
rescue
false
end
def chat_text_width(value)
begin
return contents.text_size(value.to_s).width
rescue
begin
return value.to_s.size * (contents.font.size / 2)
rescue
return value.to_s.size * 10
end
end
end
def append_colored_char(segments, ch, color)
key = MPChatFormat.color_key(color)
if !segments.empty? && segments[-1][2] == key
segments[-1][0] << ch.to_s
else
segments << [ch.to_s, color, key]
end
rescue
segments << [ch.to_s, color, key]
end
def wrap_chat_line(value, max_width, default_color, keep_newlines = false)
max_width = [max_width.to_i, 1].max
items = MPChatFormat.items(value, default_color, keep_newlines)
return [[]] if items.empty?
rows = []
row = []
line_text = ''
ended_with_break = false
items.each do |pair|
ch = pair[0].to_s
color = pair[1]
ch = ' ' if ch == "\t"
if ch == "\n"
rows << row
row = []
line_text = ''
ended_with_break = true
next
end
candidate = line_text + ch
if line_text.empty? || chat_text_width(candidate) <= max_width
append_colored_char(row, ch, color)
line_text = candidate
else
rows << row
row = []
line_text = ''
next if ch == ' '
append_colored_char(row, ch, color)
line_text = ch
end
ended_with_break = false
end
if !row.empty? || rows.empty?
rows << row
elsif ended_with_break
rows << []
end
rows
end
def color_for_message(pid, nick)
color = nil
begin
if nick.to_s == NicknameInput.nickname.to_s
color = Color.new(183, 164, 255)
else
color = Color.new(255, 213, 128)
end
rescue
color = Color.new(255, 213, 128)
end
begin
color = Color.new(255, 92, 92) if pid.to_i < 0
rescue
end
color
end
def format_chat_time(ts_ms)
text = ""
begin
text = WebSocket.chat_time_text(ts_ms) if defined?(WebSocket) && WebSocket.respond_to?(:chat_time_text)
rescue
text = ""
end
text = Time.now.strftime("%d.%m %H:%M") if text.to_s.empty?
text
rescue
Time.now.strftime("%d.%m %H:%M")
end
def draw_colored_segments(x, y, max_width, lh, segments)
dx = x
segments.each do |seg|
text = seg[0].to_s
next if text.empty?
color = seg[1]
begin
contents.font.color = color
rescue
end
w = chat_text_width(text)
draw_text(dx, y, [w + 8, 1].max, lh, text)
dx += w
break if dx >= x + max_width
end
end
def refresh
contents.clear
apply_chat_font
msgs = WebSocket.chat_messages
@last_sig = [msgs.size, (msgs.empty? ? 0 : msgs[-1][0]), contents.width, contents.height]
lh = contents.font.size + 4
row_step = [lh - 2, 1].max
max_visible = [contents.height / row_step, 1].max
draw_width = [contents.width - (CHAT_PAD_X * 2), 1].max
rows = []
msgs.each do |m|
_id, ts_ms, pid, nick, text = m
color = color_for_message(pid, nick)
is_server = server_message?(pid, nick)
time_text = format_chat_time(ts_ms)
nick_text = normalize_chat_text(nick)
body_text = is_server ? MPChatFormat.decode_server_newlines(text) : normalize_chat_text(text)
line = "#{time_text} #{nick_text}: #{body_text}"
wrap_chat_line(line, draw_width, color, is_server).each do |wrapped|
rows << wrapped
end
end
y = 0
rows.last(max_visible).each do |segments|
draw_colored_segments(CHAT_PAD_X, y, draw_width, lh, segments)
y += row_step
end
end
end
class Window_MPChatInput < Window_Base
attr_reader :text
def initialize(x, y, w, h)
super(x, y, w, h)
self.opacity = 220
self.z = 9999
@text = ""
@enter = false
@last_input_frame = nil
refresh
end
def clear
@text = ""
refresh
end
def consume_enter?
v = @enter
@enter = false
v
end
def update
super
begin
frame = Graphics.frame_count
return if @last_input_frame == frame
@last_input_frame = frame
rescue
end
changed = false
if MPChat.backspace_pressed?
if @text && @text.size > 0
@text.chop!
changed = true
end
end
if MPChat.submit_key_triggered?
@enter = true
changed = true
end
typed = MPChat.typed_text
if typed != ""
typed.each_char do |ch|
next if ch.nil? || ch == ""
break if @text.size >= 200
@text << ch
changed = true
end
end
refresh if changed
end
def chat_text_width(value)
begin
return contents.text_size(value.to_s).width
rescue
begin
return value.to_s.size * (contents.font.size / 2)
rescue
return value.to_s.size * 10
end
end
end
def draw_colored_input(x, y, max_width, lh, prefix, value, default_color)
contents.font.color = default_color
draw_text(x, y, max_width, lh, prefix)
dx = x + chat_text_width(prefix)
items = MPChatFormat.items(value, default_color, false)
items.each do |pair|
ch = pair[0].to_s
color = pair[1]
next if ch == "\n" || ch == "\r"
begin
contents.font.color = color
rescue
end
w = chat_text_width(ch)
break if dx + w > x + max_width
draw_text(dx, y, [w + 8, 1].max, lh, ch)
dx += w
end
rescue
contents.font.color = default_color
draw_text(x, y, max_width, lh, prefix + MPChatFormat.strip_color_codes(value))
end
def refresh
contents.clear
contents.font.name = MPChat.ui_font_name
contents.font.size = 20
default_color = Color.new(255,125,25)
contents.font.color = default_color
contents.font.shadow = false
contents.font.outline = false
prefix = "Чат: "
visible = @text.to_s.dup
draw_width = [contents.width - 8, 1].max
begin
while visible.size > 0 && contents.text_size(prefix + MPChatFormat.strip_color_codes(visible)).width > draw_width
visible = visible[1..-1] || ""
end
visible = "…" + visible if visible != @text.to_s
rescue
end
draw_colored_input(4, 0, draw_width, line_height, prefix, visible, default_color)
end
end
#==============================================================================
# Block gameplay hotkeys while chat is open
#==============================================================================
module WebSocket
class << self
begin
alias mp_chat_check_interaction check_interaction
def check_interaction
return if defined?(MPChat) && MPChat.active?
mp_chat_check_interaction
end
rescue
end
begin
alias mp_chat_check_pickup_or_interaction check_pickup_or_interaction
def check_pickup_or_interaction
return if defined?(MPChat) && MPChat.active?
mp_chat_check_pickup_or_interaction
end
rescue
end
begin
alias mp_chat_check_attack check_attack
def check_attack
return if defined?(MPChat) && MPChat.active?
mp_chat_check_attack
end
rescue
end
end
end
#==============================================================================
# Disable movement while typing
#==============================================================================
class Game_Player < Game_Character
alias mp_chat_move_by_input move_by_input
def move_by_input
return if defined?(MPChat) && MPChat.active?
mp_chat_move_by_input
end
# Block the default "action" button (Enter/Z/Space etc.) while typing
begin
alias mp_chat_trigger_action trigger_action
def trigger_action
return false if defined?(MPChat) && MPChat.active?
mp_chat_trigger_action
end
rescue
end
end
#==============================================================================
# Scene_Map hook
#==============================================================================
class Scene_Map < Scene_Base
alias mp_chat_update update
def update
# IMPORTANT: update chat BEFORE the normal update so that
# the "open chat" key (especially E) doesn't trigger interaction that frame.
MPChat.update if defined?(MPChat) && defined?(WebSocket) && WebSocket.connected?
mp_chat_update
end
alias mp_chat_terminate terminate
def terminate
MPChat.close if defined?(MPChat)
mp_chat_terminate
end
end
#=============================================================================
# Also block Map menu/debug calls while chat is active (ESC is :B in RGSS)
#=============================================================================
class Scene_Map < Scene_Base
begin
alias mp_chat_update_call_menu update_call_menu
def update_call_menu
return false if defined?(MPChat) && MPChat.active?
mp_chat_update_call_menu
end
rescue
end
begin
alias mp_chat_update_call_debug update_call_debug
def update_call_debug
return false if defined?(MPChat) && MPChat.active?
mp_chat_update_call_debug
end
rescue
end
end
#==============================================================================
# HARD BLOCK of RGSS Input while chat is open
#------------------------------------------------------------------------------
# Some scripts check Input.trigger?(:C) directly (Enter / Space / Z) and will still
# perform "action" even if Game_Player hooks are blocked. We mask Input at source.
# Chat uses a narrow Input passthrough while its own window is reading keys.
#==============================================================================
module Input
class << self
unless method_defined?(:mp_chat__orig_trigger)
alias mp_chat__orig_trigger trigger?
alias mp_chat__orig_press press?
alias mp_chat__orig_repeat repeat?
alias mp_chat__orig_dir4 dir4
alias mp_chat__orig_dir8 dir8
end
def mp_chat_input_masked?
return false unless defined?(MPChat)
return false if MPChat.respond_to?(:input_passthrough?) && MPChat.input_passthrough?
MPChat.active? || MPChat.recently_closed?
end
def trigger?(sym)
return false if mp_chat_input_masked?
mp_chat__orig_trigger(sym)
end
def press?(sym)
return false if mp_chat_input_masked?
mp_chat__orig_press(sym)
end
def repeat?(sym)
return false if mp_chat_input_masked?
mp_chat__orig_repeat(sym)
end
def dir4
return 0 if mp_chat_input_masked?
mp_chat__orig_dir4
end
def dir8
return 0 if mp_chat_input_masked?
mp_chat__orig_dir8
end
end
end
module WebSocket
class << self
begin
alias mp_chat_history_disconnect disconnect
def disconnect
chat_clear
mp_chat_history_disconnect
end
rescue
end
end
end