Improve Formspec

This commit is contained in:
Olive 2024-10-22 05:05:22 +01:00
parent 7264ebd6ad
commit 2bf00221fa
1 changed files with 25 additions and 17 deletions

View File

@ -1,23 +1,24 @@
local function get_moth_formspec() local function get_send_formspec()
return "size[10,10]" return "formspec_version[8] size[8,7.75]"
.. "field[1,1;8,1;target;Recipent: ;]" .. "field[0.5,0.75;7,1;target;Recipent: ;]"
.. "textarea[1,3;8,5;message;Message: ;]" .. "textarea[0.5,2.25;7,4;message;Message: ;]"
.. "button_exit[1,8;5,1;send;Fly Away...]" .. "button_exit[2,6.5;4,1;send;Fly Away...]"
end end
local function get_message_formspec(from, msg) local function get_message_formspec(from, msg)
return "size[10,10]" return "formspec_version[8] size[8,7.75]"
.. "label[0.5,0.5;A moth whispers to you...]" .. "label[0.5,0.5;A moth whispers to you...]"
.. "label[0.5,1;(From "..core.formspec_escape(from)..")".."]" .. "label[0.5,1;(from "..core.formspec_escape(from)..")".."]"
.. "textarea[0.5,2.5;7.5,7;;" ..core.formspec_escape(msg) .. ";]" .. "textarea[1,1.5;6.5,5.75;;" ..core.formspec_escape(msg) .. ";]"
end end
local function get_error_formspec(msg) local function get_error_formspec(msg)
return "size[5,0.5]" return "formspec_version[8] size[8,1]"
.. "label[0,0;"..core.formspec_escape(msg).."]" .. "label[0.5,0.5;"..core.formspec_escape(msg).."]"
end end
core.register_node("moth:moth", { core.register_node("moth:moth", {
description = "Moth", description = "Moth",
inventory_image = "moth_img.png", inventory_image = "moth_img.png",
@ -33,14 +34,20 @@ core.register_node("moth:moth", {
groups = { oddly_breakable_by_hand=2 }, groups = { oddly_breakable_by_hand=2 },
on_use = function(itemstack, player, pointed_thing) on_use = function(itemstack, player, pointed_thing)
core.show_formspec(player:get_player_name(), "moth_send", get_moth_formspec()) core.show_formspec(player:get_player_name(), "moth_send", get_send_formspec())
end, end,
}) })
local form_handlers = {}
core.register_on_player_receive_fields(function(player, formname, fields) core.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "moth_send" then return end local h = form_handlers[formname]
if h then return h(player, formname, fields) end
end)
function form_handlers.moth_send(player, formname, fields)
if not fields.send then return end if not fields.send then return end
local success = false
local name = player:get_player_name() local name = player:get_player_name()
local target = core.get_player_by_name(fields.target) local target = core.get_player_by_name(fields.target)
@ -65,8 +72,9 @@ core.register_on_player_receive_fields(function(player, formname, fields)
local player_inv = player:get_inventory() local player_inv = player:get_inventory()
player_inv:remove_item("main", "moth:moth") player_inv:remove_item("main", "moth:moth")
core.show_formspec(target:get_player_name(), "moth_show", get_message_formspec(name, fields.message)) core.show_formspec(target:get_player_name(), "moth_message", get_message_formspec(name, fields.message))
end) end
if core.get_modpath("flowers") then if core.get_modpath("flowers") then
core.override_item("flowers:dandelion_white", { core.override_item("flowers:dandelion_white", {