simplify creation functions

This commit is contained in:
OgelGames 2024-05-19 01:17:15 +10:00
parent 2be3b91f38
commit 799c4b706d
3 changed files with 34 additions and 40 deletions

View File

@ -15,18 +15,10 @@ function fakelib.is_inventory(x)
return false return false
end end
function fakelib.create_inventory(inv) function fakelib.create_inventory(sizes)
local lists = {} local lists = {}
if inv and fakelib.is_inventory(inv) then if type(sizes) == "table" then
lists = inv:get_lists() for listname, size in pairs(sizes) do
for listname in pairs(lists) do
local width = inv:get_width(listname)
if width > 0 then
lists[listname].width = width
end
end
elseif type(inv) == "table" then
for listname, size in pairs(inv) do
if type(listname) == "string" and type(size) == "number" then if type(listname) == "string" and type(size) == "number" then
local list = {} local list = {}
for i=1, size do for i=1, size do

View File

@ -15,10 +15,14 @@ function fakelib.is_metadata(x)
return false return false
end end
function fakelib.create_metadata(meta) function fakelib.create_metadata(data)
local fields = {} local fields = {}
if meta and fakelib.is_metadata(meta) then if type(data) == "table" then
fields = meta:to_table().fields for k,v in pairs(data) do
if type(k) == "string" and type(v) == "string" then
fields[k] = v
end
end
end end
return secure_table({fields = fields}, fake_metadata, identifier) return secure_table({fields = fields}, fake_metadata, identifier)
end end

View File

@ -20,50 +20,48 @@ function fakelib.is_player(x)
return false return false
end end
function fakelib.create_player(player) function fakelib.create_player(options)
local data = {} local data = {}
if type(player) == "string" then if type(options) == "table" then
data.name = player if type(options.name) == "string" then
elseif fakelib.is_player(player) then data.name = options.name
data.name = player:get_player_name()
elseif type(player) == "table" then
if type(player.name) == "string" then
data.name = player.name
end end
if type(player.position) == "table" then if type(options.position) == "table" then
data.position = vector.copy(player.position) data.position = vector.copy(options.position)
end end
if type(player.direction) == "table" then if type(options.direction) == "table" then
local dir = vector.normalize(player.direction) local dir = vector.normalize(options.direction)
data.pitch = -math.asin(dir.y) data.pitch = -math.asin(dir.y)
data.yaw = math.atan2(-dir.x, dir.z) % (math.pi * 2) data.yaw = math.atan2(-dir.x, dir.z) % (math.pi * 2)
end end
if type(player.controls) == "table" then if type(options.controls) == "table" then
data.controls = {} data.controls = {}
player.controls.dig = player.controls.dig or player.controls.LMB
player.controls.place = player.controls.place or player.controls.RMB
for name in pairs(player_controls) do for name in pairs(player_controls) do
data.controls[name] = player.controls[name] == true data.controls[name] = options.controls[name] == true
end end
data.controls.dig = data.controls.dig or options.controls.LMB
data.controls.place = data.controls.place or options.controls.RMB
end end
if fakelib.is_metadata(player.metadata) then if fakelib.is_metadata(options.metadata) then
data.metadata = fakelib.create_metadata(player.metadata) data.metadata = options.metadata
end end
if fakelib.is_inventory(player.inventory) then if fakelib.is_inventory(options.inventory) then
data.inventory = player.inventory data.inventory = options.inventory
end end
local size = 32 local size = 32
if data.inventory and type(player.wield_list) == "string" then if data.inventory and type(options.wield_list) == "string" then
size = data.inventory:get_size(player.wield_list) size = data.inventory:get_size(options.wield_list)
if size > 0 then if size > 0 then
data.wield_list = player.wield_list data.wield_list = options.wield_list
end end
end end
if type(player.wield_index) == "number" then if type(options.wield_index) == "number" then
if player.wield_index > 0 and player.wield_index <= size then if options.wield_index > 0 and options.wield_index <= size then
data.wield_index = player.wield_index data.wield_index = options.wield_index
end end
end end
elseif type(options) == "string" then
data.name = options
end end
return secure_table({data = data}, fake_player, identifier) return secure_table({data = data}, fake_player, identifier)
end end