mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
move hud.get_player() out from util.lua & add error returning
This commit is contained in:
parent
73541e4ef6
commit
ebcbc017ac
2 changed files with 28 additions and 20 deletions
|
|
@ -1,20 +1,21 @@
|
||||||
local util = {}
|
local util = {}
|
||||||
|
|
||||||
local DROP_FORCE = 8
|
local DROP_FORCE = 8
|
||||||
local DROP_INIT_VEL = {0, 3, 0}
|
local DROP_INIT_VEL = { 0, 3, 0 }
|
||||||
|
|
||||||
|
--- Create a drop from slot, set it velocity, and return it
|
||||||
---@param mode integer 0 - whole stack, 1 - single item, 2 - dupe whole stack
|
---@param mode integer 0 - whole stack, 1 - single item, 2 - dupe whole stack
|
||||||
function util.drop_from_slot(invid, slot, mode)
|
---@return table? entity, string? error
|
||||||
|
function util.drop_from_slot(pid, invid, slot, mode)
|
||||||
if invid == 0 then
|
if invid == 0 then
|
||||||
return
|
return nil, "invid cannot be 0"
|
||||||
end
|
end
|
||||||
local pid = hud.get_player()
|
|
||||||
if mode == 2 and not player.is_infinite_items(pid) then
|
if mode == 2 and not player.is_infinite_items(pid) then
|
||||||
return
|
return nil, "no permission to dupe items"
|
||||||
end
|
end
|
||||||
local itemid, itemcount = inventory.get(invid, slot)
|
local itemid, itemcount = inventory.get(invid, slot)
|
||||||
if itemid == 0 then
|
if itemid == 0 then
|
||||||
return
|
return nil, "no item in slot"
|
||||||
end
|
end
|
||||||
|
|
||||||
local drop_itemcount = 1
|
local drop_itemcount = 1
|
||||||
|
|
@ -31,24 +32,28 @@ function util.drop_from_slot(invid, slot, mode)
|
||||||
local pvel = { player.get_vel(pid) }
|
local pvel = { player.get_vel(pid) }
|
||||||
local ppos = vec3.add({ player.get_pos(pid) }, { 0, 0.7, 0 })
|
local ppos = vec3.add({ player.get_pos(pid) }, { 0, 0.7, 0 })
|
||||||
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
|
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
|
||||||
local drop = util.drop(ppos, itemid, drop_itemcount, data, 1.5)
|
local drop, err = util.drop(ppos, itemid, drop_itemcount, data, 1.5)
|
||||||
if not drop then
|
if not drop then
|
||||||
return
|
return nil, err
|
||||||
end
|
end
|
||||||
local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))
|
local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))
|
||||||
drop.rigidbody:set_vel(velocity)
|
drop.rigidbody:set_vel(velocity)
|
||||||
|
return drop
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@return table? entity, string? error
|
||||||
function util.drop(ppos, itemid, count, data, pickup_delay)
|
function util.drop(ppos, itemid, count, data, pickup_delay)
|
||||||
if itemid == 0 or not itemid then
|
if itemid == 0 or not itemid then
|
||||||
return nil
|
return nil, "item is empty"
|
||||||
end
|
end
|
||||||
return entities.spawn("base:drop", ppos, {base__drop={
|
return entities.spawn("base:drop", ppos, {
|
||||||
id=itemid,
|
base__drop = {
|
||||||
count=count,
|
id = itemid,
|
||||||
data=data,
|
count = count,
|
||||||
pickup_delay=pickup_delay
|
data = data,
|
||||||
}})
|
pickup_delay = pickup_delay
|
||||||
|
}
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function util.calc_loot(loot_table)
|
function util.calc_loot(loot_table)
|
||||||
|
|
@ -66,7 +71,9 @@ function util.calc_loot(loot_table)
|
||||||
if count == 0 then
|
if count == 0 then
|
||||||
goto continue
|
goto continue
|
||||||
end
|
end
|
||||||
table.insert(results, {item=item.index(loot.item), count=count})
|
table.insert(results, {
|
||||||
|
item = item.index(loot.item), count = count
|
||||||
|
})
|
||||||
end
|
end
|
||||||
::continue::
|
::continue::
|
||||||
end
|
end
|
||||||
|
|
@ -78,7 +85,7 @@ function util.block_loot(blockid)
|
||||||
if lootscheme then
|
if lootscheme then
|
||||||
return util.calc_loot(lootscheme)
|
return util.calc_loot(lootscheme)
|
||||||
end
|
end
|
||||||
return {{item=block.get_picking_item(blockid), count=1}}
|
return { { item = block.get_picking_item(blockid), count = 1 } }
|
||||||
end
|
end
|
||||||
|
|
||||||
return util
|
return util
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,9 @@ local base_util = require "util"
|
||||||
|
|
||||||
function on_hud_open()
|
function on_hud_open()
|
||||||
events.on("core:drop_outside_inventory", function(mode)
|
events.on("core:drop_outside_inventory", function(mode)
|
||||||
|
local pid = hud.get_player()
|
||||||
local invid = hud.get_exchange_inventory()
|
local invid = hud.get_exchange_inventory()
|
||||||
base_util.drop_from_slot(invid, 0, mode)
|
base_util.drop_from_slot(pid, invid, 0, mode)
|
||||||
end)
|
end)
|
||||||
input.add_callback("player.drop", function()
|
input.add_callback("player.drop", function()
|
||||||
if hud.is_paused() or hud.is_inventory_open() then
|
if hud.is_paused() or hud.is_inventory_open() then
|
||||||
|
|
@ -13,9 +14,9 @@ function on_hud_open()
|
||||||
local invid, slot = player.get_inventory(pid)
|
local invid, slot = player.get_inventory(pid)
|
||||||
local mode = 1
|
local mode = 1
|
||||||
if input.is_pressed("key:left-ctrl") or input.is_pressed("key:right-ctrl") then
|
if input.is_pressed("key:left-ctrl") or input.is_pressed("key:right-ctrl") then
|
||||||
mode = 0
|
mode = 0
|
||||||
end
|
end
|
||||||
base_util.drop_from_slot(invid, slot, mode)
|
base_util.drop_from_slot(pid, invid, slot, mode)
|
||||||
end)
|
end)
|
||||||
rules.create("do-loot-non-player", true)
|
rules.create("do-loot-non-player", true)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue