2018-08-07 12:33:46 +02:00
|
|
|
local Event = require 'utils.event'
|
2018-09-23 00:25:13 +02:00
|
|
|
local Game = require 'utils.game'
|
2019-01-02 15:42:18 +02:00
|
|
|
local Command = require 'utils.command'
|
|
|
|
local Server = require 'features.server'
|
|
|
|
local Token = require 'utils.token'
|
|
|
|
local Utils = require 'utils.core'
|
|
|
|
|
|
|
|
local serialize = serpent.line
|
|
|
|
|
|
|
|
local Public = {}
|
|
|
|
|
|
|
|
local color_callback =
|
|
|
|
Token.register(
|
|
|
|
function(data)
|
|
|
|
local key = data.key
|
|
|
|
local value = data.value
|
|
|
|
if not value then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local player = game.players[key]
|
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
player.chat_color = value.chat_color
|
|
|
|
player.color = value.color
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
--- Attempts to retrieve and get the saved color of a LuaPlayer
|
|
|
|
function Public.recall_player_color(player)
|
|
|
|
Server.try_get_data('colors', player.name, color_callback)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Assigns LuaPlayer random RGB values for color and player_color and returns the RGB table.
|
|
|
|
function Public.set_random_color(player)
|
|
|
|
return {
|
|
|
|
chat_color = Utils.set_and_return(player, 'chat_color', Utils.random_RGB()),
|
|
|
|
color = Utils.set_and_return(player, 'color', Utils.random_RGB())
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
Command.add(
|
2019-01-27 21:37:27 +02:00
|
|
|
'redmew-color',
|
2019-01-02 15:42:18 +02:00
|
|
|
{
|
|
|
|
description = 'Set will save your current color for future maps. Reset will erase your saved color. Random will give you a random color.',
|
|
|
|
arguments = {'set-reset-random'},
|
|
|
|
admin_only = false,
|
|
|
|
regular_only = true,
|
|
|
|
allowed_by_server = false,
|
|
|
|
allowed_by_player = true
|
|
|
|
},
|
|
|
|
function(args, player)
|
|
|
|
local player_name = player.name
|
|
|
|
if args['set-reset-random'] == 'set' then
|
|
|
|
local data = {
|
|
|
|
color = player.color,
|
|
|
|
chat_color = player.chat_color,
|
|
|
|
}
|
2019-01-21 17:42:38 +02:00
|
|
|
Server.set_data('colors', player_name, data)
|
2019-01-02 15:42:18 +02:00
|
|
|
player.print('Your color has been saved. Any time you join a redmew server your color will automatically be set.')
|
2019-01-21 17:39:15 +02:00
|
|
|
Utils.print_except(player_name .. ' has saved their color server-side for future maps. You can do the same! Check out /help color-redmew', player)
|
2019-01-02 15:42:18 +02:00
|
|
|
elseif args['set-reset-random'] == 'reset' then
|
2019-01-21 17:42:38 +02:00
|
|
|
Server.set_data('colors', player_name, nil)
|
2019-01-02 15:42:18 +02:00
|
|
|
player.print('Your saved color (if you had one) has been removed.')
|
|
|
|
elseif args['set-reset-random'] == 'random' then
|
|
|
|
local color_data = Public.set_random_color(player)
|
|
|
|
player.print('Your color has been changed to: ' .. serialize(color_data))
|
|
|
|
else
|
|
|
|
player.print('Only set, reset, and random are accepted arguments')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
2018-08-07 12:33:46 +02:00
|
|
|
|
|
|
|
Event.add(
|
2019-01-02 15:42:18 +02:00
|
|
|
defines.events.on_player_joined_game,
|
2018-08-07 12:33:46 +02:00
|
|
|
function(event)
|
2018-09-23 12:46:58 +02:00
|
|
|
local player = Game.get_player_by_index(event.player_index)
|
2018-08-07 12:33:46 +02:00
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-01-02 15:42:18 +02:00
|
|
|
Public.recall_player_color(player)
|
2018-08-07 12:33:46 +02:00
|
|
|
end
|
2018-11-20 12:46:19 +02:00
|
|
|
)
|
2019-01-02 15:42:18 +02:00
|
|
|
|
|
|
|
return Public
|