Skip to content

Commit 08891f8

Browse files
authored
Add inventory command (#1554)
* Add inventory command
1 parent a948e00 commit 08891f8

3 files changed

Lines changed: 173 additions & 0 deletions

File tree

features/admin_commands.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local DiscordChannels = require 'resources.discord'.channel_names
44
local Event = require 'utils.event'
55
local Game = require 'utils.game'
66
local Global = require 'utils.global'
7+
local PlayerInventory = require 'features.gui.player_inventory'
78
local Rank = require 'features.rank_system'
89
local Ranks = require 'resources.ranks'
910
local Report = require 'features.report'
@@ -470,6 +471,14 @@ local function destroy_selected(_, player)
470471
end
471472
end
472473

474+
--- Shows all/target player inventory
475+
local function inventory(args, player)
476+
local target_ident = args.player
477+
local target = Utils.validate_player(target_ident)
478+
479+
PlayerInventory.get_main_frame(player, target)
480+
end
481+
473482
-- Event registrations
474483

475484
Event.add(defines.events.on_built_entity, built_entity)
@@ -663,6 +672,17 @@ Command.add(
663672
destroy_selected
664673
)
665674

675+
Command.add(
676+
'inventory',
677+
{
678+
description = {'command_description.inventory'},
679+
arguments = { 'player' },
680+
default_values = { player = false },
681+
required_rank = Ranks.moderator,
682+
},
683+
inventory
684+
)
685+
666686
return {
667687
create_pool = pool,
668688
destroy_selected = destroy_selected,

features/gui/player_inventory.lua

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
local Gui = require 'utils.gui'
2+
local Rank = require 'features.rank_system'
3+
local ENTITIES = prototypes.entity
4+
local ITEMS = prototypes.item
5+
local TILES = prototypes.tile
6+
7+
local Public = {
8+
main_frame_name = Gui.uid_name(),
9+
close_button_name = Gui.uid_name(),
10+
}
11+
12+
local function get_sprite_from_name(name)
13+
if ITEMS[name] then
14+
return 'item.'..name
15+
elseif TILES[name] then
16+
return 'tile.'..name
17+
elseif ENTITIES[name] then
18+
return 'entity.'..name
19+
end
20+
return 'virtual-signal.signal-deny'
21+
end
22+
23+
local function get_tooltip_from_name(name)
24+
if ITEMS[name] then
25+
return ITEMS[name].localised_name
26+
elseif TILES[name] then
27+
return TILES[name].localised_name
28+
elseif ENTITIES[name] then
29+
return ENTITIES[name].localised_name
30+
end
31+
return name
32+
end
33+
34+
Public.get_player_inventory = function(player)
35+
local results = {}
36+
37+
for k = 1, player.get_max_inventory_index() do
38+
local inventory = player.get_inventory(k)
39+
if (inventory and inventory.valid) then
40+
for _, stack in pairs(inventory.get_contents()) do
41+
results[stack.name] = (results[stack.name] or 0) + stack.count
42+
end
43+
end
44+
end
45+
46+
return results
47+
end
48+
49+
Public.get_main_frame = function(player, selected)
50+
local frame = player.gui.screen[Public.main_frame_name]
51+
if frame and frame.valid then
52+
Gui.destroy(frame)
53+
end
54+
55+
frame = player.gui.screen.add {
56+
type = 'frame',
57+
name = Public.main_frame_name,
58+
direction = 'vertical',
59+
}
60+
Gui.set_style(frame, { maximal_height = 930 })
61+
62+
do -- Header
63+
local header = frame.add { type = 'flow', direction = 'horizontal' }
64+
Gui.set_style(header, { horizontal_spacing = 8, vertical_align = 'center', bottom_padding = 4 })
65+
66+
local label = header.add { type = 'label', caption = 'Player inventory', style = 'frame_title' }
67+
label.drag_target = frame
68+
69+
local dragger = header.add { type = 'empty-widget', style = 'draggable_space_header' }
70+
dragger.drag_target = frame
71+
Gui.set_style(dragger, { height = 24, horizontally_stretchable = true })
72+
73+
local button = header.add {
74+
type = 'sprite-button',
75+
name = Public.close_button_name,
76+
sprite = 'utility/close',
77+
clicked_sprite = 'utility/close_black',
78+
style = 'close_button',
79+
tooltip = {'gui.close-instruction'}
80+
}
81+
Gui.set_data(button, frame)
82+
end
83+
84+
do -- Content
85+
local inner = frame.add { type = 'frame', style = 'inside_deep_frame', direction = 'vertical' }
86+
local online = inner
87+
.add { type = 'frame', style = 'subheader_frame'}
88+
.add { type = 'label', caption = ('Online: %d/%d'):format(#game.connected_players, #game.players), style = 'subheader_label' }
89+
Gui.set_style(online.parent, { horizontally_stretchable = true })
90+
91+
local grid = inner
92+
.add { type = 'scroll-pane', style = 'naked_scroll_pane' }
93+
.add { type = 'table', style = 'table_with_selection', column_count = 4 }
94+
95+
local function make_rank(name)
96+
local label = grid.add { type = 'label', caption = Rank.get_player_rank_name(name) }
97+
Gui.set_style(label, { font_color = Rank.get_player_rank_color(name) })
98+
end
99+
100+
local function make_status(target)
101+
grid.add {
102+
type = 'label',
103+
caption = target.connected and 'Online' or 'Offline',
104+
style = target.connected and 'bold_green_label' or 'bold_red_label',
105+
}
106+
end
107+
108+
local function make_inventory(target)
109+
local list = grid
110+
.add { type = 'scroll-pane', style = 'naked_scroll_pane' }
111+
.add { type = 'table', column_count = 10, style = 'filter_slot_table' }
112+
list.parent.horizontal_scroll_policy = 'never'
113+
114+
for name, count in pairs(Public.get_player_inventory(target)) do
115+
list.add{
116+
type = 'sprite-button',
117+
style = 'slot_button',
118+
sprite = get_sprite_from_name(name),
119+
tooltip = get_tooltip_from_name(name),
120+
number = count,
121+
}
122+
end
123+
end
124+
125+
grid.add { type = 'label', caption = 'Name' }
126+
grid.add { type = 'label', caption = 'Rank' }
127+
grid.add { type = 'label', caption = 'Status' }
128+
grid.add { type = 'label', caption = 'Inventory' }
129+
130+
local player_list = selected and { selected } or game.players
131+
for _, p in pairs(player_list) do
132+
grid.add { type = 'label', caption = p.name }
133+
make_rank(p.name)
134+
make_status(p)
135+
make_inventory(p)
136+
end
137+
end
138+
139+
frame.force_auto_center()
140+
player.opened = frame
141+
return frame
142+
end
143+
144+
Gui.on_click(Public.close_button_name, function(event)
145+
Gui.destroy(Gui.get_data(event.element))
146+
end)
147+
148+
Gui.on_custom_close(Public.main_frame_name, function(event)
149+
Gui.destroy(event.element)
150+
end)
151+
152+
return Public

locale/en/redmew_command_text.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ moderator=Gives a player the moderator rank
135135
moderator_remove=Demotes a player from moderator to the next lowest rank
136136
regular_all=Gives everyone online the regular rank, if not in probation
137137
blueprint_tools=Open the Blueprint Tools manager GUI
138+
inventory=Show target player inventory. If no player is provided, all inventories are shown.
138139

139140
[command_custom_help]
140141
tp=<blank|mode|player> 3 different uses: "/tp" to tp to selected entity. "/tp mode" to toggle tp mode. "/tp Newcott" to tp to Newcott.

0 commit comments

Comments
 (0)