aboutsummaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorPerttu Ahola <celeron55@gmail.com>2011-11-29 21:30:22 +0200
committerPerttu Ahola <celeron55@gmail.com>2011-11-29 21:30:22 +0200
commit103d4793f00b2dd592739f686e90370c2d8953a3 (patch)
tree7b07b413fa646314cde37bd3f2342859761a24ef /data
parent2a610b011ac2f289ebde692ab3dcf10dd4f0244f (diff)
downloadminetest-103d4793f00b2dd592739f686e90370c2d8953a3.tar.gz
minetest-103d4793f00b2dd592739f686e90370c2d8953a3.tar.bz2
minetest-103d4793f00b2dd592739f686e90370c2d8953a3.zip
Create the necessary API for /giveme and /give and implement those commands; also sort out the scripts a bit
Diffstat (limited to 'data')
-rw-r--r--data/builtin.lua149
-rw-r--r--data/mods/default/init.lua61
-rw-r--r--data/mods/experimental/init.lua146
3 files changed, 212 insertions, 144 deletions
diff --git a/data/builtin.lua b/data/builtin.lua
index 5f5d40f67..dc41caf69 100644
--- a/data/builtin.lua
+++ b/data/builtin.lua
@@ -148,6 +148,155 @@ minetest.register_node("ignore", {
})
--
+-- stackstring manipulation functions
+-- example stackstring: 'CraftItem "apple" 4'
+-- example item: {type="CraftItem", name="apple"}
+-- example item: {type="ToolItem", name="SteelPick", wear="23272"}
+--
+
+function stackstring_take_item(stackstring)
+ if stackstring == nil then
+ return '', nil
+ end
+ local stacktype = nil
+ stacktype = string.match(stackstring,
+ '([%a%d]+Item[%a%d]*)')
+ if stacktype == "NodeItem" or stacktype == "CraftItem" then
+ local itemtype = nil
+ local itemname = nil
+ local itemcount = nil
+ itemtype, itemname, itemcount = string.match(stackstring,
+ '([%a%d]+Item[%a%d]*) "([^"]*)" (%d+)')
+ itemcount = tonumber(itemcount)
+ if itemcount == 0 then
+ return '', nil
+ elseif itemcount == 1 then
+ return '', {type=itemtype, name=itemname}
+ else
+ return itemtype.." \""..itemname.."\" "..(itemcount-1),
+ {type=itemtype, name=itemname}
+ end
+ elseif stacktype == "ToolItem" then
+ local itemtype = nil
+ local itemname = nil
+ local itemwear = nil
+ itemtype, itemname, itemwear = string.match(stackstring,
+ '([%a%d]+Item[%a%d]*) "([^"]*)" (%d+)')
+ itemwear = tonumber(itemwear)
+ return '', {type=itemtype, name=itemname, wear=itemwear}
+ end
+end
+
+function stackstring_put_item(stackstring, item)
+ if item == nil then
+ return stackstring, false
+ end
+ stackstring = stackstring or ''
+ local stacktype = nil
+ stacktype = string.match(stackstring,
+ '([%a%d]+Item[%a%d]*)')
+ stacktype = stacktype or ''
+ if stacktype ~= '' and stacktype ~= item.type then
+ return stackstring, false
+ end
+ if item.type == "NodeItem" or item.type == "CraftItem" then
+ local itemtype = nil
+ local itemname = nil
+ local itemcount = nil
+ itemtype, itemname, itemcount = string.match(stackstring,
+ '([%a%d]+Item[%a%d]*) "([^"]*)" (%d+)')
+ itemtype = itemtype or item.type
+ itemname = itemname or item.name
+ if itemcount == nil then
+ itemcount = 0
+ end
+ itemcount = itemcount + 1
+ return itemtype.." \""..itemname.."\" "..itemcount, true
+ elseif item.type == "ToolItem" then
+ if stacktype ~= nil then
+ return stackstring, false
+ end
+ local itemtype = nil
+ local itemname = nil
+ local itemwear = nil
+ itemtype, itemname, itemwear = string.match(stackstring,
+ '([%a%d]+Item[%a%d]*) "([^"]*)" (%d+)')
+ itemwear = tonumber(itemwear)
+ return itemtype.." \""..itemname.."\" "..itemwear, true
+ end
+ return stackstring, false
+end
+
+function stackstring_put_stackstring(stackstring, src)
+ while src ~= '' do
+ --print("src="..dump(src))
+ src, item = stackstring_take_item(src)
+ --print("src="..dump(src).." item="..dump(item))
+ local success
+ stackstring, success = stackstring_put_item(stackstring, item)
+ if not success then
+ return stackstring, false
+ end
+ end
+ return stackstring, true
+end
+
+function test_stackstring()
+ local stack
+ local item
+ local success
+
+ stack, item = stackstring_take_item('NodeItem "TNT" 3')
+ assert(stack == 'NodeItem "TNT" 2')
+ assert(item.type == 'NodeItem')
+ assert(item.name == 'TNT')
+
+ stack, item = stackstring_take_item('CraftItem "with spaces" 2')
+ assert(stack == 'CraftItem "with spaces" 1')
+ assert(item.type == 'CraftItem')
+ assert(item.name == 'with spaces')
+
+ stack, item = stackstring_take_item('CraftItem "with spaces" 1')
+ assert(stack == '')
+ assert(item.type == 'CraftItem')
+ assert(item.name == 'with spaces')
+
+ stack, item = stackstring_take_item('CraftItem "s8df2kj3" 0')
+ assert(stack == '')
+ assert(item == nil)
+
+ stack, item = stackstring_take_item('ToolItem "With Spaces" 32487')
+ assert(stack == '')
+ assert(item.type == 'ToolItem')
+ assert(item.name == 'With Spaces')
+ assert(item.wear == 32487)
+
+ stack, success = stackstring_put_item('NodeItem "With Spaces" 40',
+ {type='NodeItem', name='With Spaces'})
+ assert(stack == 'NodeItem "With Spaces" 41')
+ assert(success == true)
+
+ stack, success = stackstring_put_item('CraftItem "With Spaces" 40',
+ {type='CraftItem', name='With Spaces'})
+ assert(stack == 'CraftItem "With Spaces" 41')
+ assert(success == true)
+
+ stack, success = stackstring_put_item('ToolItem "With Spaces" 32487',
+ {type='ToolItem', name='With Spaces'})
+ assert(stack == 'ToolItem "With Spaces" 32487')
+ assert(success == false)
+
+ stack, success = stackstring_put_item('NodeItem "With Spaces" 40',
+ {type='ToolItem', name='With Spaces'})
+ assert(stack == 'NodeItem "With Spaces" 40')
+ assert(success == false)
+
+ assert(stackstring_put_stackstring('NodeItem "With Spaces" 2',
+ 'NodeItem "With Spaces" 1') == 'NodeItem "With Spaces" 3')
+end
+test_stackstring()
+
+--
-- Callback registration
--
diff --git a/data/mods/default/init.lua b/data/mods/default/init.lua
index 395e4a64c..f9c4c8231 100644
--- a/data/mods/default/init.lua
+++ b/data/mods/default/init.lua
@@ -26,6 +26,7 @@
-- minetest.setting_getbool(name)
-- minetest.chat_send_all(text)
-- minetest.chat_send_player(name, text)
+-- minetest.get_player_privs(name)
--
-- Global objects:
-- minetest.env - environment reference
@@ -52,6 +53,7 @@
-- - add_rat(pos)
-- - add_firefly(pos)
-- - get_meta(pos) -- Get a NodeMetaRef at that position
+-- - get_player_by_name(name) -- Get an ObjectRef to a player
--
-- NodeMetaRef
-- - get_type()
@@ -1322,6 +1324,65 @@ function on_punchnode(p, node)
end
minetest.register_on_punchnode(on_punchnode)
+minetest.register_on_chat_message(function(name, message)
+ print("default on_chat_message: name="..dump(name).." message="..dump(message))
+ local cmd = "/giveme"
+ if message:sub(0, #cmd) == cmd then
+ if not minetest.get_player_privs(name)["give"] then
+ minetest.chat_send_player(name, "you don't have permission to give")
+ return true -- Handled chat message
+ end
+ stackstring = string.match(message, cmd.." (.*)")
+ if stackstring == nil then
+ minetest.chat_send_player(name, 'usage: '..cmd..' stackstring')
+ return true -- Handled chat message
+ end
+ print(cmd..' invoked, stackstring="'..stackstring..'"')
+ player = minetest.env:get_player_by_name(name)
+ added, error_msg = player:add_to_inventory(stackstring)
+ if added then
+ minetest.chat_send_player(name, '"'..stackstring
+ ..'" added to inventory.');
+ else
+ minetest.chat_send_player(name, 'Could not give "'..stackstring
+ ..'": '..error_msg);
+ end
+ return true -- Handled chat message
+ end
+ local cmd = "/give"
+ if message:sub(0, #cmd) == cmd then
+ print("minetest.get_player_privs(name)="
+ ..dump(minetest.get_player_privs(name)))
+ if not minetest.get_player_privs(name)["give"] then
+ minetest.chat_send_player(name, "you don't have permission to give")
+ return true -- Handled chat message
+ end
+ name2, stackstring = string.match(message, cmd.." ([%a%d_-]+) (.*)")
+ if name == nil or stackstring == nil then
+ minetest.chat_send_player(name, 'usage: '..cmd..' name stackstring')
+ return true -- Handled chat message
+ end
+ print(cmd..' invoked, name2="'..name2
+ ..'" stackstring="'..stackstring..'"')
+ player = minetest.env:get_player_by_name(name2)
+ if player == nil then
+ minetest.chat_send_player(name, name2..' is not a known player')
+ return true -- Handled chat message
+ end
+ added, error_msg = player:add_to_inventory(stackstring)
+ if added then
+ minetest.chat_send_player(name, '"'..stackstring
+ ..'" added to '..name2..'\'s inventory.');
+ minetest.chat_send_player(name2, '"'..stackstring
+ ..'" added to inventory.');
+ else
+ minetest.chat_send_player(name, 'Could not give "'..stackstring
+ ..'": '..error_msg);
+ end
+ return true -- Handled chat message
+ end
+end)
+
--
-- Done, print some random stuff
--
diff --git a/data/mods/experimental/init.lua b/data/mods/experimental/init.lua
index 45f16738f..fdfc8a780 100644
--- a/data/mods/experimental/init.lua
+++ b/data/mods/experimental/init.lua
@@ -41,148 +41,6 @@ minetest.register_on_placenode(function(pos, newnode, placer)
end
end)
-function stackstring_take_item(stackstring)
- if stackstring == nil then
- return '', nil
- end
- local stacktype = nil
- stacktype = string.match(stackstring,
- '([%a%d]+Item[%a%d]*)')
- if stacktype == "NodeItem" or stacktype == "CraftItem" then
- local itemtype = nil
- local itemname = nil
- local itemcount = nil
- itemtype, itemname, itemcount = string.match(stackstring,
- '([%a%d]+Item[%a%d]*) "([^"]*)" (%d+)')
- itemcount = tonumber(itemcount)
- if itemcount == 0 then
- return '', nil
- elseif itemcount == 1 then
- return '', {type=itemtype, name=itemname}
- else
- return itemtype.." \""..itemname.."\" "..(itemcount-1),
- {type=itemtype, name=itemname}
- end
- elseif stacktype == "ToolItem" then
- local itemtype = nil
- local itemname = nil
- local itemwear = nil
- itemtype, itemname, itemwear = string.match(stackstring,
- '([%a%d]+Item[%a%d]*) "([^"]*)" (%d+)')
- itemwear = tonumber(itemwear)
- return '', {type=itemtype, name=itemname, wear=itemwear}
- end
-end
-
-function stackstring_put_item(stackstring, item)
- if item == nil then
- return stackstring, false
- end
- stackstring = stackstring or ''
- local stacktype = nil
- stacktype = string.match(stackstring,
- '([%a%d]+Item[%a%d]*)')
- stacktype = stacktype or ''
- if stacktype ~= '' and stacktype ~= item.type then
- return stackstring, false
- end
- if item.type == "NodeItem" or item.type == "CraftItem" then
- local itemtype = nil
- local itemname = nil
- local itemcount = nil
- itemtype, itemname, itemcount = string.match(stackstring,
- '([%a%d]+Item[%a%d]*) "([^"]*)" (%d+)')
- itemtype = itemtype or item.type
- itemname = itemname or item.name
- if itemcount == nil then
- itemcount = 0
- end
- itemcount = itemcount + 1
- return itemtype.." \""..itemname.."\" "..itemcount, true
- elseif item.type == "ToolItem" then
- if stacktype ~= nil then
- return stackstring, false
- end
- local itemtype = nil
- local itemname = nil
- local itemwear = nil
- itemtype, itemname, itemwear = string.match(stackstring,
- '([%a%d]+Item[%a%d]*) "([^"]*)" (%d+)')
- itemwear = tonumber(itemwear)
- return itemtype.." \""..itemname.."\" "..itemwear, true
- end
- return stackstring, false
-end
-
-function stackstring_put_stackstring(stackstring, src)
- while src ~= '' do
- --print("src="..dump(src))
- src, item = stackstring_take_item(src)
- --print("src="..dump(src).." item="..dump(item))
- local success
- stackstring, success = stackstring_put_item(stackstring, item)
- if not success then
- return stackstring, false
- end
- end
- return stackstring, true
-end
-
-function test_stack()
- local stack
- local item
- local success
-
- stack, item = stackstring_take_item('NodeItem "TNT" 3')
- assert(stack == 'NodeItem "TNT" 2')
- assert(item.type == 'NodeItem')
- assert(item.name == 'TNT')
-
- stack, item = stackstring_take_item('CraftItem "with spaces" 2')
- assert(stack == 'CraftItem "with spaces" 1')
- assert(item.type == 'CraftItem')
- assert(item.name == 'with spaces')
-
- stack, item = stackstring_take_item('CraftItem "with spaces" 1')
- assert(stack == '')
- assert(item.type == 'CraftItem')
- assert(item.name == 'with spaces')
-
- stack, item = stackstring_take_item('CraftItem "s8df2kj3" 0')
- assert(stack == '')
- assert(item == nil)
-
- stack, item = stackstring_take_item('ToolItem "With Spaces" 32487')
- assert(stack == '')
- assert(item.type == 'ToolItem')
- assert(item.name == 'With Spaces')
- assert(item.wear == 32487)
-
- stack, success = stackstring_put_item('NodeItem "With Spaces" 40',
- {type='NodeItem', name='With Spaces'})
- assert(stack == 'NodeItem "With Spaces" 41')
- assert(success == true)
-
- stack, success = stackstring_put_item('CraftItem "With Spaces" 40',
- {type='CraftItem', name='With Spaces'})
- assert(stack == 'CraftItem "With Spaces" 41')
- assert(success == true)
-
- stack, success = stackstring_put_item('ToolItem "With Spaces" 32487',
- {type='ToolItem', name='With Spaces'})
- assert(stack == 'ToolItem "With Spaces" 32487')
- assert(success == false)
-
- stack, success = stackstring_put_item('NodeItem "With Spaces" 40',
- {type='ToolItem', name='With Spaces'})
- assert(stack == 'NodeItem "With Spaces" 40')
- assert(success == false)
-
- assert(stackstring_put_stackstring('NodeItem "With Spaces" 2',
- 'NodeItem "With Spaces" 1') == 'NodeItem "With Spaces" 3')
-end
-test_stack()
-
minetest.register_abm({
nodenames = {"luafurnace"},
interval = 1.0,
@@ -426,7 +284,7 @@ print("setting max_users = " .. dump(minetest.setting_get("max_users")))
print("setting asdf = " .. dump(minetest.setting_get("asdf")))
minetest.register_on_chat_message(function(name, message)
- print("on_chat_message: name="..dump(name).." message="..dump(message))
+ --[[print("on_chat_message: name="..dump(name).." message="..dump(message))
local cmd = "/testcommand"
if message:sub(0, #cmd) == cmd then
print(cmd.." invoked")
@@ -437,7 +295,7 @@ minetest.register_on_chat_message(function(name, message)
print("script-overridden help command")
minetest.chat_send_all("script-overridden help command")
return true
- end
+ end]]
end)
-- Grow papyrus on TNT every 10 seconds