148 lines
4.0 KiB
Lua
Raw Normal View History

2021-05-05 04:45:32 -07:00
--[[ LICENSE HEADER
The MIT License (MIT)
Copyright © 2021 Jordan Irwin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
2021-05-05 05:05:02 -07:00
--- API
--
-- @module api.lua
2021-05-05 04:45:32 -07:00
2021-05-05 05:05:02 -07:00
2021-08-03 15:29:43 -07:00
local S = core.get_translator(asm.modname)
local registered_eggs = {}
--- Retrieves egg that spawns specified entity.
--
-- @function asm.getEgg
-- @tparam string entity Entity name spawned by egg.
-- @treturn string Egg name or `nil`.
function asm.getEgg(entity)
return registered_eggs[entity]
end
2021-05-05 05:05:02 -07:00
--- Adds a craft recipe for an egg.
--
-- Alias: *asm.addEggRecipe*
--
-- @function asm.registerEggRecipe
2021-05-05 05:05:02 -07:00
-- @param name Name of spawnegg that will be created from recipe.
-- @param ingredients Items used for recipe in addition to `spawneggs:egg`. Can be string or list.
asm.registerEggRecipe = function(name, ingredients)
2021-05-05 04:45:32 -07:00
if type(ingredients) == "string" then
ingredients = {ingredients,}
end
table.insert(ingredients, 1, "spawneggs:egg")
core.register_craft({
output = "spawneggs:" .. name,
type = "shapeless",
recipe = ingredients,
})
end
-- Alias for `asm.registerEggRecipe`.
asm.addEggRecipe = asm.registerEggRecipe
2021-05-05 04:45:32 -07:00
2021-05-05 10:21:03 -07:00
local function formatTitle(s)
s = s:gsub("_", " ")
local t = {}
for m in (s .. " "):gmatch("(.-) ") do
local v = m:gsub("^%l", string.upper)
table.insert(t, v)
end
2021-08-03 15:29:43 -07:00
return S("@1 Spawn Egg", table.concat(t, " "))
2021-05-05 10:21:03 -07:00
end
2021-05-05 05:05:02 -07:00
--- Registers new egg in game.
--
-- Alias: *asm.addEgg*
--
2021-05-05 05:05:02 -07:00
-- @function asm.registerEgg
2021-05-05 05:40:49 -07:00
-- @param def `EggDef` table.
asm.registerEgg = function(def)
local img = "spawneggs_" .. def.name .. ".png"
if def.inventory_image then
img = def.inventory_image
2021-05-05 04:45:32 -07:00
end
2021-05-05 15:12:39 -07:00
local title = def.title
if not title then
title = formatTitle(def.name)
end
local egg_name = "spawneggs:" .. def.name:lower()
core.register_craftitem(":" .. egg_name, {
2021-05-05 15:12:39 -07:00
description = title,
2021-05-05 04:45:32 -07:00
inventory_image = img,
on_place = function(itemstack, placer, target)
if target.type == "node" then
local pos = target.above
pos.y = pos.y + 1
2021-05-05 05:40:49 -07:00
local ref = core.add_entity(pos, def.spawn)
2021-05-05 04:45:32 -07:00
if ref and placer:is_player() then
local entity = ref:get_luaentity()
-- set owner
if entity.ownable then entity.owner = placer:get_player_name() end
end
if not core.settings:get_bool("creative_mode") then
itemstack:take_item()
end
return itemstack
end
end
})
-- store registration
registered_eggs[def.spawn] = egg_name
2021-05-05 06:39:10 -07:00
if def.ingredients then
asm.registerEggRecipe(def.name:lower(), def.ingredients)
2021-05-05 06:39:10 -07:00
end
2021-05-05 04:45:32 -07:00
-- DEBUG
2021-05-05 06:39:10 -07:00
asm.log("action", "Registered spawnegg for " .. def.spawn)
2021-05-05 04:45:32 -07:00
end
2021-05-05 05:05:02 -07:00
-- Alias for `asm.registerEgg`.
2021-05-05 05:05:02 -07:00
asm.addEgg = asm.registerEgg
--- Egg definition table.
--
-- @table EggDef
2021-05-05 15:12:39 -07:00
-- @tfield string name Name of the egg. Will be appended to "spawneggs:".
-- @tfield[opt] string title Description displayed for item.
-- @tfield string inventory_image Image displayed in inventory.
-- @tfield string spawn Entity that will be spawned from egg.
-- @tfield[opt] table ingredients Ingredients to use, in addition to `spawneggs:egg`, to register craft recipe. Can be a `table` or `string`.