Skip to content

Commit

Permalink
Core/base_classes: add DetachedInventory base class. Closes #1683
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 committed Sep 19, 2024
1 parent 4206a38 commit 7287d63
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
15 changes: 9 additions & 6 deletions mods/lord/Core/base_classes/src/base_classes.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
local Event = require("base_classes.Event")
local Form = require("base_classes.Form")
local ObjectState = require("base_classes.ObjectState")
local Event = require("base_classes.Event")
local Form = require("base_classes.Form")
local DetachedInventory = require("base_classes.DetachedInventory")
local ObjectState = require("base_classes.ObjectState")


base_classes = {} -- luacheck: ignore unused global variable base_classes

local function register_api()
_G.base_classes = {
--- @type base_classes.Event
Event = Event,
Event = Event,
--- @type base_classes.Form
Form = Form,
Form = Form,
--- @type base_classes.DetachedInventory
DetachedInventory = DetachedInventory,
--- @type base_classes.ObjectState
ObjectState = ObjectState,
ObjectState = ObjectState,
}
end

Expand Down
82 changes: 82 additions & 0 deletions mods/lord/Core/base_classes/src/base_classes/DetachedInventory.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@


--- @class base_classes.DetachedInventory
local DetachedInventory = {
--- @protected
--- @type string
player_name = nil,
--- @protected
--- @type string
id = nil,
--- @overridable
--- @static
--- @protected
--- @type DetachedInventoryCallbacksDef
callbacks = nil,
}

--- @protected
--- @generic GenericDetachedInventory: base_classes.DetachedInventory
--- @param child_class GenericDetachedInventory
--- @return GenericDetachedInventory
function DetachedInventory:extended(child_class)
return setmetatable(child_class or {}, { __index = self })
end

--- Constructor
--- @public
--- @generic GenericDetachedInventory: base_classes.DetachedInventory
--- @param player Player
--- @return GenericDetachedInventory
function DetachedInventory:new(player)
local class = self
self = {}

self.player_name = player:get_player_name()

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

--- @protected
--- @abstract
--- @return string
function DetachedInventory:generate_id()
return error('You should override method `DetachedInventory:generate_id()` in your inventory class')
end

--- @protected
--- @abstract
--- @param detached InvRef
--- @generic GenericDetachedInventory: base_classes.DetachedInventory
--- @return GenericDetachedInventory
function DetachedInventory:build(detached)
return error('You should override method `DetachedInventory:build()` in your inventory class')
end

--- @protected
--- @generic GenericDetachedInventory: base_classes.DetachedInventory
--- @return GenericDetachedInventory
function DetachedInventory:create()
local detached = minetest.create_detached_inventory(self.id, self.callbacks, self.player_name)
self:build(detached)

return self
end

--- @generic GenericDetachedInventory: base_classes.DetachedInventory
--- @return GenericDetachedInventory
function DetachedInventory:get_or_create()
self.id = self:generate_id()
local exists = minetest.get_inventory({ type = "detached", name = self.id })

return exists and self or self:create()
end

--- @public
--- @return string
function DetachedInventory:get_id()
return self.id or self:get_or_create().id
end


return DetachedInventory

0 comments on commit 7287d63

Please sign in to comment.