-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
119 lines (104 loc) · 6.08 KB
/
control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-- Listener when mod gets initialized
script.on_init(function ()
serverLog("TransparentInventory initialized.") -- Show it in the server-side log
end)
-- Listener when someone presses the hotkey
script.on_event("tinv-open", function(event) -- When defined shortcut was called...
-- Only do something when we're in a multiplayer
if game.is_multiplayer() then
stalking_player_inventory(event) -- ...then we call our function just right below.
end
end)
-- Main function
function stalking_player_inventory(event)
-- Notes:
-- "selected player" is the player who's inventory is opened
-- simply "player" is actually the player who's doing that
if settings.global["tinv-is-enabled"].value then -- Check if TINV is globally enabled. If so, proceed.
local player = game.get_player(event.player_index) -- Get LuaPlayer class from current player who triggered the shortcut
local selected = player.selected -- Get the selected LuaEntity of said player
local colors = { -- A few colors we use later on
red = {r = 1};
green = {g = 1};
blue = {r = 30, g = 144, b = 255};
orange = {r = 255, g = 99, b = 71};
}
-- selected.player might be nil when being in "god mode" (where the character is detached from LuaPlayer)
-- This check must be as the last one to not crash when trying to open inventory of any other entity than a player (e.g. building)
-- Check that target is not empty and is actually a player
-- Notice: player.type was renamed from 'player' to 'character' in 0.17.35 (https://wiki.factorio.com/Version_history/0.17.0)
if selected ~= nil and selected.type == "character" and selected.player ~= nil then
-- Everything is fine... so proceed...
local selected_player = selected.player -- "selected" is just LuaEntity, therefore we use player property to get LuaPlayer class
-- Check if player is allowed to open inventory of someone based on admin-side settings
if tinv_is_allowed_to_stalk(player) then
-- Check if selected player allowed access to his inventory, and if the setting should be honored based on the admin-setting
if not settings.global["tinv-user-inv-democracy"].value or (settings.global["tinv-user-inv-democracy"].value and selected_player.mod_settings["tinv-user-control-access"].value) then
serverLog(player.name .. " is looking in the inventory of player " .. selected_player.name)
if settings.global["tinv-tell-stalker"].value then -- Check if we tell selected player that player is stalking him
selected_player.print({'message.target-is-stalking-player', player.name}, colors.orange) -- Write selected player that someone is stalking him now
end
player.print({'message.player-stalking-now', selected_player.name}, colors.green) -- Write current player
player.opened = selected_player -- Force the player to open LuaPlayer (which causes the inventory to open)
else -- User disallowed access to his inventory due to user-settings
serverLog(player.name .. " tried looking in the inventory of player " .. selected_player.name .. " but was denied by target user settings.")
player.print({'message.player-stalking-by-target-not-allowed', selected_player.name}, colors.blue) -- Tell the player that the target player does not allow that
end
else -- Apparently not...
serverLog(player.name .. " tried looking in the inventory of player " .. selected_player.name .. " but was denied due to server-side permissions.")
player.print({'message.player-stalking-not-allowed'}, colors.red) -- Tell the player that he's not allowed to stalk
end
else -- ...or not...
player.print({'message.target-must-be-player'}, colors.red) -- Tell the player that the target must be a player
end
end
end
function tinv_is_allowed_to_stalk(player)
local mode = settings.global["tinv-users-mode"].value
local users = settings.global["tinv-users-list"].value
-- Lets check if username matches
for user in string.gmatch(users, '([^,]+)') do -- Go through all defined users, separated by comma
if tinv_playername_matches(player.name, user) then -- If current user matches...
if mode == "only-listed" then -- If only-listed and user is matching, then the user has permission
return true
elseif mode == "excluding-listed" then -- If excluding-listed and user is matching, the user is not allowed to
return false
end
end
end
-- Default if no matching user was found
if mode == "only-listed" then -- If only-listed, all other user are not granted access
return false
elseif mode == "excluding-listed" then -- If excluding-listed, by default everyone has access, except listed
return true
end
return false -- If anything strange happens, we simply deny as a fallback
end
-- Function to check if username matches and taking wildcard into account
function tinv_playername_matches(player_name, compare_name)
local regex_enabled = settings.global["tinv-users-list-regex"].value
-- Check if username contains a wildcard sign, so we know we check it a different way
if not regex_enabled and string.find(compare_name, "%*") then -- "%" is the escape char in Lua... whyever.
-- Note: It might be possible directly using string.match() on it, but I would like to use simply "*" as the wildcard char
-- instead of the official Lua ".*" wildcard for the match function. So this is basically a workaround for that.
compare_name = string.gsub(compare_name, "%*", ".*")
if string.match(player_name:lower(), "^" .. compare_name:lower() .. "$") then -- Check is case-insensitive.
return true
end
elseif regex_enabled then -- If regex support is enabled... Fingers crossed the user knows what to do.
-- Here we now valdiate the player_name after all specified regex strings, exactly as user provided.
-- Notice: This is case-sensitive!
if string.match(player_name, "^" .. compare_name .. "$") then
return true
end
else -- Fallback. No wildcard, going the boring way...
if player_name:lower() == compare_name:lower() then -- Check is case-insensitive.
return true
end
end
return false -- By default it does not match
end
-- Function to log something in the server log
function serverLog(text)
log(text) -- Logs to server output
end