Improve the API by using definition tables.

This commit totally breaks retro-compatibility.
This commit is contained in:
upsilon
2017-01-17 17:43:10 +01:00
parent ba3838842b
commit c3ac46697b
3 changed files with 207 additions and 143 deletions

View File

@ -27,41 +27,44 @@ throwing.allow_arrow_placing = false
There are two available functions in the mod API:
```lua
function throwing.register_bow(name, itemcraft, description, texture[, groups])
function throwing.register_bow(name, definition)
--[[
Name: Bow name (in second part of the itemstring).
Itemcraft: item used to craft the bow (nil if uncraftable).
Description: Description of the bow.
Texture: Texture of the bow, shown in inventory.
Groups: optional groups.
Definition: definition table, containing:
* itemcraft (optional, you may want to register your own craft or to make the bow uncraftable): item used to craft the bow.
* description (highly recommended): description of the bow.
* texture (essential): texture of the bow, shown in inventory.
* groups (optional): groups of the item.
]]
-- Example:
throwing.register_bow("bow_stone", "default:cobble", "Stone Bow", "throwing_bow_stone.png")
function throwing.register_arrow(name, itemcraft, craft_quantity, description, tiles, on_hit_sound, on_hit[, on_throw[, groups]])
itemcraft, craft_quantity, description, tiles, on_hit_sound, on_hit[, on_throw[, groups]]
function throwing.register_arrow(name, definition table)
--[[
Name: Arrow name (in second part of the itemstring).
Itemcraft: item used to craft the arrow (nil if uncraftable).
Craft_quantity: quantity of arrows in the craft output.
Tiles: tiles of the arrow.
On_hit_sound: sound played when the arrow hits a node or an object (nil if no sound).
On_hit: callback function: on_hit(pos, last_pos, node, object, hitter, self) where:
* Pos: the position of the hitted node or object
* Last_pos: the last air node where the arrow was (used by the build_arrow, for example)
* Node and object: hit node or object. Either node or object is nil, depending
whether the arrow hit a node or an object (you should always check for that).
An object can be a player or a luaentity.
* Hitter: the ObjectRef of the player who threw the arrow.
* Self: the arrow entity table (it allows you to hack a lot!)
* When it fails, it should return:
false[, reason]
On_throw: option callback function: on_throw(pos, thrower, self) where:
* Pos: the position from where the arrow is throw (which a bit higher than the hitter position)
* Thrower: the ObjectRef of the thrower
* Self: the arrow entity table
* Should return false if the arrow should not be throw
Definition: definition table, containing:
* itemcraft (optional, you may want to register your own craft or to make the arrow uncraftable): item used to craft the arrow.
* craft_quantity (optional, defaulting to 1 if itemcraft is non-nil, pointless otherwise): quantity of arrows in the craft output.
* tiles (essential): tiles of the arrow.
* target (optional, defaulting to throwing.target_both): what the arrow is able to hit (throwing.target_node, throwing.target_object, throwing.target_both).
* on_hit_sound (optional): sound played when the arrow hits a node or an object.
* on_hit (must exist, will crash if nil): callback function: on_hit(pos, last_pos, node, object, hitter, self):
- pos: the position of the hit node or object.
- last_pos: the last air node where the arrow was
- node and object: hit node or object. Either node or object is nil, depending
whether the arrow hit a node or an object.
- hitter: an ObjectRef to the thrower player.
- self: the arrow entity table (it allows you to hack a lot!)
- If it fails, it should return:
false[, reason]
* on_throw (optional): callback function: on_throw(pos, thrower, next_itemstack, self):
- pos: the position from where the arrow is throw (which a bit higher than the hitter position)
- thrower: an ObjectRef to the thrower player
- next_itemstack: the ItemStack next to the arrow in the "main" inventory
- If the arrow shouldn't be throw, it should return false.
* on_throw_sound (optional, there is a default sound, specify "" for no sound): sound to be played when the arrow is throw
]]
-- Examples: