-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
213 lines (174 loc) · 8.86 KB
/
Copy pathinit.lua
File metadata and controls
213 lines (174 loc) · 8.86 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
--mods/recipe_maker/init.lua
recipe_maker = {}
-- Luanti標準の翻訳システム(i18n)を初期化
local S = core.get_translator("recipe_maker")
-- 【新設】アイテムの全グループを逆引きし、等幅フォント用に綺麗に一覧テキスト化する関数
local function get_item_group_report(item_name)
if not item_name or item_name == "" then
-- return "【" .. S("Error") .. "】\n" .. "調査枠(Target)にアイテムを配置してください!"
return "【" .. S("Error") .. "】\n" .. S("Please place an item into the Target slot!")
end
local def = core.registered_items[item_name]
if not def or not def.groups then
-- return "Item ID: " .. item_name .. "\n" .. "このアイテムにはグループ(groups)が登録されていません。"
return "Item ID: " .. item_name .. "\n" .. S("This item has no groups registered.")
end
local lines = {}
table.insert(lines, "===============================================")
table.insert(lines, S("MATERIAL HIDDEN GROUP DISCOVERY REPORT"))
table.insert(lines, "===============================================")
table.insert(lines, "Target Item ID: " .. item_name)
table.insert(lines, "-----------------------------------------------")
table.insert(lines, string.format(" %-30s | %s", "Internal Group Name", "Value"))
table.insert(lines, "-----------------------------------------------")
-- アルファベット順に並べるためキーをソート
local sorted_keys = {}
for g_name, _ in pairs(def.groups) do
table.insert(sorted_keys, g_name)
end
table.sort(sorted_keys)
local count = 0
for _, g_name in ipairs(sorted_keys) do
local g_val = def.groups[g_name]
-- 有効な数値、かつクリエイティブ非表示等の特殊内部フラグを除外
if g_val > 0 and not string.find(g_name, "not_in_") then
-- 等幅フォントで縦のラインがビシッと揃うようにフォーマット
table.insert(lines, string.format(" group:%-24s | %d", g_name, g_val))
count = count + 1
end
end
if count == 0 then
-- table.insert(lines, S(" 有効な主要グループは見つかりませんでした。"))
table.insert(lines, S("No groups found."))
end
table.insert(lines, "===============================================")
return table.concat(lines, "\n")
end
-- 1. 専用の画面(Formspec)を動的に生成する関数
local function get_recipe_maker_formspec(pos, output_code)
if not output_code then
output_code = "【" .. S("Usage") .. "】\n" ..
"1. " .. S("Place ingredients into the 3x3 grid on the left.") .. "\n" ..
"2. " .. S("Place the outcome into the single slot on the right.") .. "\n" ..
"3. " .. S("Click the [Generate Code] button.")
end
output_code = core.formspec_escape(output_code)
-- 【追加】40ピクセル=1.0マスとする、ピクセル変換ヘルパー
local scale = 1.0
local function px(pixel)
return (pixel / 40) * scale
end
-- ★バージョンの「5」を明示指定!
local formspec = "formspec_version[5]" ..
string.format("size[%f,%f]", px(432), px(396)) ..
-- スロットサイズは極小「0.6」(=24px), 隙間 0.1 (=4px)
string.format("style_type[list;size=%f,%f;spacing=%f,%f]", px(24), px(24), px(4), px(4)) ..
-- 【仕様5準拠】box命令の区切りをカンマ「,」に統一して完全固定
string.format("box[%f,%f;%f,%f;#202020aa]", px(32), px(28), px(88), px(88)) ..
string.format("box[%f,%f;%f,%f;#202020aa]", px(184), px(48), px(32), px(32)) ..
string.format("box[%f,%f;%f,%f;#202020aa]", px(248+64), px(48), px(32), px(32)) .. -- 調査枠用の黒座布団を追加
-- 【1】素材入力枠 (3x3)
string.format("label[%f,%f;%s]", px(16), px(16), S("Materials (3x3)")) ..
string.format("list[nodemeta:%d,%d,%d;craft_input;%f,%f;3,3;]", pos.x, pos.y, pos.z, px(36), px(32)) ..
-- 【2】完成品枠
string.format("label[%f,%f;%s]", px(176), px(36), S("Outcome")) ..
string.format("list[nodemeta:%d,%d,%d;craft_output;%f,%f;1,1;]", pos.x, pos.y, pos.z, px(188), px(52)) ..
-- 【3】コード生成ボタン
string.format("button[%f,%f;%f,%f;generate_code;%s]", px(160), px(88), px(80), px(28), S("Generate Code")) ..
-- 【2B】調査枠
string.format("label[%f,%f;%s]", px(240+64), px(36), S("Groups")) ..
string.format("list[nodemeta:%d,%d,%d;group_output;%f,%f;1,1;]", pos.x, pos.y, pos.z, px(252+64), px(52)) ..
-- 【3B】調査ボタン
string.format("button[%f,%f;%f,%f;generate_grouplist;%s]", px(224+64), px(88), px(80), px(28), S("Find Groups")) ..
-- 【4】出力テキストエリア (等幅フォント「font=mono」ハックを適用し、縦ラインを美しく整列)
"style_type[textarea;font=mono;textcolor=#00AF00]" ..
string.format("label[%f,%f;%s]", px(16), px(136), S("Generated Lua Code / JSON Array")) ..
string.format("textarea[%f,%f;%f,%f;code_box;;" .. output_code .. "]", px(16), px(148), px(400), px(88)) ..
"style_type[textarea;font=normal;textcolor=#FFFFFF]" .. -- 他への干渉を防ぐリセット
-- 【5】プレイヤーインベントリ
string.format("label[%f,%f;%s]", px(16), px(254), S("Inventory")) ..
string.format("box[%f,%f;%f,%f;#202020aa]", px(16), px(264), px(256), px(116)) ..
string.format("list[current_player;main;%f,%f;9,4;]", px(20), px(268)) ..
string.format("listring[nodemeta:%d,%d,%d;craft_input]", pos.x, pos.y, pos.z) ..
"listring[current_player;main]"
return formspec
end
-- 2. ブロック(ノード)の登録
core.register_node("recipe_maker:generator", {
description = S("Craft Recipe Maker (Code Generator)"),
tiles = {"recipe_maker_block.png"},
groups = {choppy = 2, oddly_breakable_by_hand = 2, creative_breakable = 1},
stack_max = 1,
light_source = 5,
on_construct = function(pos)
local meta = core.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("craft_input", 9)
inv:set_size("craft_output", 1)
inv:set_size("group_output", 1) -- 【重要バグ修正】調査枠スロットのサイズ(1面)を内部インフラに安全確保!
meta:set_string("formspec", get_recipe_maker_formspec(pos))
end,
on_receive_fields = function(pos, formname, fields, player)
local meta = core.get_meta(pos)
local inv = meta:get_inventory()
-- 処理A:[Generate Code] コード生成ボタンが押された場合
if fields.generate_code then
local output_stack = inv:get_stack("craft_output", 1)
local output_name = output_stack:get_name()
local output_count = output_stack:get_count()
if not output_name or output_name == "" then
meta:set_string("formspec", get_recipe_maker_formspec(pos, S("[Error] Please place the outcome item into the slot on the right!")))
return
end
local items = {}
for i = 1, 9 do
local name = inv:get_stack("craft_input", i):get_name()
items[i] = name
end
local lua_code = "core.register_craft({\n" ..
"\toutput = \"" .. output_name .. " " .. output_count .. "\",\n" ..
"\trecipe = {\n" ..
"\t\t{\"" .. items[1] .. "\", \"" .. items[2] .. "\", \"" .. items[3] .. "\"},\n" ..
"\t\t{\"" .. items[4] .. "\", \"" .. items[5] .. "\", \"" .. items[6] .. "\"},\n" ..
"\t\t{\"" .. items[7] .. "\", \"" .. items[8] .. "\", \"" .. items[9] .. "\"}\n" ..
"\t}\n" ..
"})"
local json_code = "\n\n--- JSON用配列フォーマット ---\n" ..
"\"recipe\": [\n" ..
" [\"" .. items[1] .. "\", \"" .. items[2] .. "\", \"" .. items[3] .. "\"],\n" ..
" [\"" .. items[4] .. "\", \"" .. items[5] .. "\", \"" .. items[6] .. "\"],\n" ..
" [\"" .. items[7] .. "\", \"" .. items[8] .. "\", \"" .. items[9] .. "\"]\n" ..
"]"
meta:set_string("formspec", get_recipe_maker_formspec(pos, lua_code .. json_code))
end
-- 処理B:【新規実装】[グループ調査] ボタンが押された場合の処理
if fields.generate_grouplist then
local scan_stack = inv:get_stack("group_output", 1)
local scan_item_name = scan_stack:get_name()
-- アイテムの非公開グループ一覧レポートを動的生成
local report_text = get_item_group_report(scan_item_name)
-- 出力コンソール画面を書き換えてプレイヤーにダイレクト表示
meta:set_string("formspec", get_recipe_maker_formspec(pos, report_text))
end
end,
on_dig = function(pos, node, digger)
local meta = core.get_meta(pos)
local inv = meta:get_inventory()
for i = 1, 9 do
local stack = inv:get_stack("craft_input", i)
if not stack:is_empty() then
core.add_item(pos, stack)
end
end
local out_stack = inv:get_stack("craft_output", 1)
if not out_stack:is_empty() then
core.add_item(pos, out_stack)
end
-- 撤去時に調査枠の中身も安全にポコンとドロップさせる処理を補填
local scan_stack = inv:get_stack("group_output", 1)
if scan_stack and not scan_stack:is_empty() then
core.add_item(pos, scan_stack)
end
return core.node_dig(pos, node, digger)
end,
})