ItemsGround
The server's list of the items lying on the ground: create, find, move and remove them; players' drops and picks.
The server keeps the list of the items lying on the ground - the global table ItemsGround, each item an ItemGround. Every player sees the items near them as the game's own items: they look, burn (torches) and are picked up like the game's items, in the same place for everyone (client scripts read them with the client-side ItemsGround).
How it works
- A player throws or drops an item (drops it from the inventory, throws a torch away, or grabs a winch with a torch in hand) - the item goes on the list and
onPlayerDropItemis called;cancelEvent()gives it back to the player. The other players near it see it fly and land. - A player picks an item up -
onPlayerTakeItemis called and the item leaves the list. The first player wins: a pick that comes too late (or that a handler cancelled withcancelEvent()) is undone in that player's game - the item leaves their inventory again (a cancelled one lies on the ground again). - Scripts create items (
ItemsGround.create), move them, change their amount and remove them; the players near them see it at once. - Streaming - a player has the items within the
<streaming radius>of config.xml, like characters; walking away and back brings them again. - The list lives until the server stops; items do not disappear by themselves. At most
<groundItems limit>(config.xml, default 5000) lie on the ground at once - a drop over the limit is refused and the item stays in the player's inventory. - Items placed in the map by the game itself (plants, ore, loot lying in the world) are not on the list.
-- no throwing ore away, and the server console tells who took what
addEventHandler("onPlayerDropItem", function(pid, item)
if item.instance == "ItMi_Orenugget" then
sendMessage(pid, 0xFF8080, "Ore cannot be thrown away here.")
cancelEvent()
end
end)
addEventHandler("onPlayerTakeItem", function(pid, item)
print(getPlayerName(pid) .. " took " .. item.instance .. " x" .. item.amount)
end)Fields of ItemGround
In the examples below item is any ItemGround.
The object stays usable after the item was picked up or destroyed - then its fields are nil and
isValid() returns false. Two objects of the same item are equal (==). Its fields can be read, not set
(use the methods):
| Field | Type | Description |
|---|---|---|
id | integer | Number of the item on the server's list - never reused while the server runs, the same in every player's game. |
instance | ItemClass | Item class, e.g. "ItMw_1H_Torch"; nil once the item is gone. |
amount | integer | How many pieces lie there (a stack of ore is one item with an amount); nil once gone. |
droppedBy | integer | Id of the player who dropped it, -1 for an item created by a script (or when that player left); nil once gone. |
ItemsGround
The global table: put items on the ground, find them, remove them.
create
ItemsGround.create(item)Puts an item on the ground. It appears for every player near it at once, exactly at the given place (it does
not fall - give a position on the ground, e.g. a player's position from getPlayerPosition lowered to their feet).
registerCommand("gold", function(pid, args)
local x, y, z = getPlayerPosition(pid)
ItemsGround.create({ instance = "ItMi_Orenugget", amount = tonumber(args) or 10, x = x, y = y, z = z - 90 })
end)| Parameter | Type | Description |
|---|---|---|
item | ItemGroundSpec | instance and the position are required; amount defaults to 1, the rotation to 0. |
Returns: ItemGround | nil - The new item, or nil when <groundItems limit> is reached.
getById
ItemsGround.getById(id)Returns the item with this number.
| Parameter | Type | Description |
|---|---|---|
id | integer | Number of the item (item.id). |
Returns: ItemGround | nil - nil when no such item lies on the ground.
destroy
ItemsGround.destroy(id)Removes an item from the ground (for every player). Same as item:destroy().
| Parameter | Type | Description |
|---|---|---|
id | integer | Number of the item. |
Returns: boolean - false when no such item lies on the ground.
getAll
ItemsGround.getAll()Returns every item lying on the ground, in the order they were put there.
Returns: ItemGround[]
getNear
ItemsGround.getNear(x, y, z, radius)Returns the items within radius of a place, the nearest first.
registerCommand("cleanup", function(pid)
local x, y, z = getPlayerPosition(pid)
for _, item in ipairs(ItemsGround.getNear(x, y, z, 1000)) do item:destroy() end
end)| Parameter | Type | Description |
|---|---|---|
x | number | X coordinate (cm). |
y | number | Y coordinate (cm). |
z | number | Z coordinate (cm). |
radius | number | Distance in cm. |
Returns: ItemGround[]
ItemGround
One item on the ground (item below): its place, amount and removal. Objects come from ItemsGround and from the events onPlayerDropItem / onPlayerTakeItem.
getPosition
item:getPosition()Returns the position of the item. A thrown item has its landing place once the thrower's game reported it (a moment after the throw); until then the place it was thrown from.
Returns:
| Value | Type | Description |
|---|---|---|
x | number | nil | X coordinate (cm), or nil when the item is gone. |
y | number | nil | Y coordinate (cm). |
z | number | nil | Z coordinate (cm). |
setPosition
item:setPosition(x, y, z)Moves the item; every player near it sees it there at once (it does not fall - put it on the ground).
| Parameter | Type | Description |
|---|---|---|
x | number | X coordinate (cm). |
y | number | Y coordinate (cm). |
z | number | Z coordinate (cm). |
Returns: boolean - false when the item is gone.
getRotation
item:getRotation()Returns the rotation of the item.
Returns:
| Value | Type | Description |
|---|---|---|
pitch | number | nil | Pitch in degrees, or nil when the item is gone. |
yaw | number | nil | Yaw in degrees. |
roll | number | nil | Roll in degrees. |
setRotation
item:setRotation(pitch, yaw, roll)Turns the item.
| Parameter | Type | Description |
|---|---|---|
pitch | number | Pitch in degrees. |
yaw | number | Yaw in degrees. |
roll | number | Roll in degrees. |
Returns: boolean - false when the item is gone.
setAmount
item:setAmount(amount)Changes how many pieces lie there (the player who picks it up gets this many).
| Parameter | Type | Description |
|---|---|---|
amount | integer | 1 to 1000000. |
Returns: boolean - false when the item is gone.
isValid
item:isValid()Tells whether the item still lies on the ground.
Returns: boolean - false after it was picked up or destroyed.
destroy
item:destroy()Removes the item from the ground (for every player). Same as ItemsGround.destroy(item.id).
Returns: boolean - false when it was already gone.
Types
ItemGroundSpec
The table given to ItemsGround.create.
| Field | Type | Description |
|---|---|---|
instance | ItemClass | Item class, e.g. "ItMi_Orenugget" (required). |
amount | integer | nil | How many pieces, 1 to 1000000 (default 1). |
x | number | X coordinate in cm (required). |
y | number | Y coordinate in cm (required). |
z | number | Z coordinate in cm (required) - the item does not fall, give a height on the ground. |
pitch | number | nil | Pitch in degrees (default 0). |
yaw | number | nil | Yaw in degrees (default 0). |
roll | number | nil | Roll in degrees (default 0). |
