Skip to content

Commit

Permalink
Rework base_classes.ObjectState & use in lord_damage. Relates to #…
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 authored and Doloment committed Aug 20, 2024
1 parent 2b0f9f5 commit a0417db
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
46 changes: 34 additions & 12 deletions mods/lord/Core/base_classes/src/base_classes/ObjectState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,51 @@ local Storage = require("base_classes.ObjectState.Storage")

--- @class base_classes.ObjectState
local ObjectState = {
--- @type table
state = nil,
--- @private
--- @type table<string,any>
state_table = nil,
}

--- @param state_table table Table of all state entries
--- @param object table Table of all state entries
--- @return base_classes.ObjectState
function ObjectState:new(state_table)
function ObjectState:new(object)
local class = self
self = {}
self.state = state_table or {}

return setmetatable(self, {__index = class})
end
self = setmetatable({}, { __index = class })
self:load(object)

function ObjectState:add_state_entry(entry_name, value)
self.state[entry_name] = value
return self
end

function ObjectState:remove_state_entry(entry_name)
self.state[entry_name] = nil
--- @param name string
--- @return any
function ObjectState:get_entry(name)
return self.state_table[name]
end

--- @param name string
--- @param value any
--- @return base_classes.ObjectState
function ObjectState:set_entry(name, value)
self.state_table[name] = value

return self
end
--- @param name string
--- @return base_classes.ObjectState
function ObjectState:del_entry(name)
return self:set_entry(name, nil)
end

--- @param object ObjectRef
--- @return boolean
function ObjectState:save(object)
return Storage.set_state_of(object, self.state_table)
end

--- @param object Player|Entity
function ObjectState:load(object)
self.state_table = Storage.get_state_of(object)
end

return ObjectState
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ function Storage.get_state_of(object)
end

return object:is_player()
and minetest.deserialize(object:get_meta():get("object_state"))
and minetest.deserialize(object:get_meta():get("object_state") or "return {}")
or object:get_luaentity().object_state
end

--- Obtains `ObjectState` from meta or object properties depending on whether `object` is a player or not
--- @param object ObjectRef a player or an entity to apply `ObjectState` to
---
--- @param object Player|Entity a player or an entity to apply `ObjectState` to
--- @param state_table table
---
--- @return boolean success or not
function Storage.set_state_of(object, state_table)
if not object then
return
return false
end

local state_string = minetest.serialize(state_table)
Expand All @@ -29,9 +32,14 @@ function Storage.set_state_of(object, state_table)
local meta = object:get_meta()
meta:set_string("object_state", state_string)
else
local entity = object:get_luaentity()
local entity = object:get_luaentity()
if not entity then
return false
end
entity.object_state = state_table
end

return true
end


Expand Down
15 changes: 5 additions & 10 deletions mods/lord/_experimental/lord_damage/src/damage/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@ local damage = {
}

local function set_source(object, source, value)
local state = minetest.get_object_state(object)
minetest.chat_send_all(source)
state:add_state_entry(source, value)
minetest.set_object_state(object, state)

--print(minetest.serialize(state))
local state = base_classes.ObjectState:new(object)
state:set_entry(source, value)
state:save(object)
end

local function get_source_status(object, source)
local state = minetest.get_object_state(object)
--print(minetest.serialize(state))
local state = base_classes.ObjectState:new(object)

--minetest.chat_send_all(state.state[source])
return state.state[source]
return state:get_entry(source)
end

--- @param damage_type string damage type name
Expand Down
1 change: 0 additions & 1 deletion util/mt-ide-helper/classes/MetaDataRef.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function MetaDataRef:get(key) end

---@param key string
---@param value string value "" will delete the key
---@return number
function MetaDataRef:set_string(key, value) end

---@param key string
Expand Down
1 change: 1 addition & 0 deletions util/mt-ide-helper/classes/ObjectRef/Player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function Player:set_attribute(attribute, value) end
--- * Returns `nil` if no attribute found.
function Player:get_attribute(attribute) end
--- Returns a PlayerMetaRef.
--- @return PlayerMetaRef
function Player:get_meta() end
--- * Redefine player's inventory form
--- * Should usually be called in `on_joinplayer`
Expand Down

0 comments on commit a0417db

Please sign in to comment.