Merge pull request #91 from tacigar/tacigar/nametag

Add nametag
This commit is contained in:
tacigar 2016-12-21 20:17:19 +09:00 committed by GitHub
commit 89db49bcec
6 changed files with 72 additions and 31 deletions

View File

@ -156,6 +156,11 @@ function maidroid.maidroid.move_main_to_wield(self, itemname)
return false
end
-- maidroid.maidroid.is_named reports the maidroid is still named.
function maidroid.maidroid.is_named(self)
return self.nametag ~= ""
end
---------------------------------------------------------------------
-- maidroid.manufacturing_data represents a table that contains manufacturing data.
@ -179,32 +184,6 @@ maidroid.manufacturing_data = (function()
return {}
end) ()
-- formspec_opened_selves represents a table that contains player names and
-- maidroids whose formspec is opened.
local formspec_opened_maidroids = {}
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "maidroid:gui" then
return
end
local self = formspec_opened_maidroids[player]
if not self then -- if the maidroid is dead now.
return
end
if fields.name then
if fields.name == "" then
self.nametag = self.inventory_name
else
self.nametag = fields.name
end
self.object:set_nametag_attributes{
text = self.nametag
}
end
end)
--------------------------------------------------------------------
-- register empty item entity definition.
@ -372,7 +351,6 @@ function maidroid.register_maidroid(product_name, def)
-- create_formspec_string returns a string that represents a formspec definition.
local function create_formspec_string(self)
local nametag = self.object:get_nametag_attributes().text
return "size[8,9]"
.. default.gui_bg
.. default.gui_bg_img
@ -382,8 +360,6 @@ function maidroid.register_maidroid(product_name, def)
.. "list[detached:"..self.inventory_name..";core;4.5,1.5;1,1;]"
.. "list[current_player;main;0,5;8,1;]"
.. "list[current_player;main;0,6.2;8,3;8]"
.. "button[7,0.25;1,0.875;apply_name;Apply]"
.. "field[4.5,0.5;2.75,1;name;name;" .. nametag .. "]"
.. "label[5.5,1;wield]"
.. "list[detached:"..self.inventory_name..";wield_item;5.5,1.5;1,1;]"
end
@ -396,7 +372,6 @@ function maidroid.register_maidroid(product_name, def)
self.manufacturing_number = maidroid.manufacturing_data[product_name]
maidroid.manufacturing_data[product_name] = maidroid.manufacturing_data[product_name] + 1
create_inventory(self)
self.nametag = self.inventory_name
-- attach dummy item to new maidroid.
minetest.add_entity(self.object:getpos(), "maidroid:dummy_item")
@ -503,7 +478,6 @@ function maidroid.register_maidroid(product_name, def)
-- on_rightclick is a callback function that is called when a player right-click them.
local function on_rightclick(self, clicker)
formspec_opened_maidroids[clicker] = self
minetest.show_formspec(
clicker:get_player_name(),
"maidroid:gui",
@ -571,6 +545,7 @@ function maidroid.register_maidroid(product_name, def)
set_yaw_by_direction = maidroid.maidroid.set_yaw_by_direction,
get_wield_item_stack = maidroid.maidroid.get_wield_item_stack,
move_main_to_wield = maidroid.maidroid.move_main_to_wield,
is_named = maidroid.maidroid.is_named,
})
-- register maidroid egg.

View File

@ -20,3 +20,12 @@ minetest.register_craft{
{"default:steel_ingot", "default:cobble", "default:steel_ingot"},
},
}
minetest.register_craft{
output = "maidroid_tool:nametag",
recipe = {
{ "", "farming:cotton", ""},
{ "default:paper", "default:paper", "default:paper"},
{"default:steel_ingot", "dye:black", "default:steel_ingot"},
},
}

View File

@ -12,3 +12,4 @@ dofile(maidroid_tool.modpath .. "/api.lua")
dofile(maidroid_tool.modpath .. "/core_writer.lua")
dofile(maidroid_tool.modpath .. "/egg_writer.lua")
dofile(maidroid_tool.modpath .. "/crafting.lua")
dofile(maidroid_tool.modpath .. "/nametag.lua")

56
maidroid_tool/nametag.lua Normal file
View File

@ -0,0 +1,56 @@
------------------------------------------------------------
-- Copyright (c) 2016 tacigar. All rights reserved.
-- https://github.com/tacigar/maidroid
------------------------------------------------------------
local formspec = "size[4,1.25]"
.. default.gui_bg
.. default.gui_bg_img
.. default.gui_slots
.. "button_exit[3,0.25;1,0.875;apply_name;Apply]"
.. "field[0.5,0.5;2.75,1;name;name;]"
local maidroid_buf = {} -- for buffer of target maidroids.
minetest.register_craftitem("maidroid_tool:nametag", {
description = "maidroid tool : nametag",
inventory_image = "maidroid_tool_nametag.png",
stack_max = 1,
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "object" then
return nil
end
local obj = pointed_thing.ref
local luaentity = obj:get_luaentity()
if not obj:is_player() and luaentity then
local name = luaentity.name
if maidroid.registered_maidroids[name] and not luaentity:is_named() then
local player_name = user:get_player_name()
minetest.show_formspec(player_name, "maidroid_tool:nametag", formspec)
maidroid_buf[player_name] = luaentity
return itemstack
end
end
return nil
end,
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "maidroid_tool:nametag" then
return
end
if fields.name then
local luaentity = maidroid_buf[player:get_player_name()]
luaentity.nametag = fields.name
luaentity.object:set_nametag_attributes{
text = fields.name,
}
end
end)

Binary file not shown.

After

Width:  |  Height:  |  Size: 419 B

Binary file not shown.