drawers/lua/visual.lua

256 lines
7.3 KiB
Lua
Raw Normal View History

--[[
Minetest Mod Storage Drawers - A Mod adding storage drawers
Copyright (C) 2017 LNJ <git@lnj.li>
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
core.register_entity("drawers:visual", {
initial_properties = {
hp_max = 1,
physical = false,
collide_with_objects = false,
collisionbox = {-0.4374, -0.4374, 0, 0.4374, 0.4374, 0}, -- for param2 0, 2
visual = "upright_sprite", -- "wielditem" for items without inv img?
visual_size = {x = 0.6, y = 0.6},
textures = {"drawers_empty.png"},
spritediv = {x = 1, y = 1},
initial_sprite_basepos = {x = 0, y = 0},
is_visible = true,
},
get_staticdata = function(self)
return core.serialize({
drawer_posx = self.drawer_pos.x,
drawer_posy = self.drawer_pos.y,
drawer_posz = self.drawer_pos.z,
texture = self.texture
})
end,
on_activate = function(self, staticdata, dtime_s)
-- Restore data
data = core.deserialize(staticdata)
if data then
self.drawer_pos = {
x = data.drawer_posx,
y = data.drawer_posy,
z = data.drawer_posz,
}
self.texture = data.texture
else
self.drawer_pos = drawers.last_drawer_pos
self.texture = drawers.last_texture or "drawers_empty.png"
end
2017-04-01 19:13:09 +02:00
-- add self to public drawer visuals
-- this is needed because there is no other way to get this class
-- only the underlying ObjectRef
-- PLEASE contact me, if this is wrong
drawers.drawer_visuals[core.serialize(self.drawer_pos)] = self
local node = core.get_node(self.drawer_pos)
-- collisionbox
local colbox = {-0.4374, -0.4374, 0, 0.4374, 0.4374, 0} -- for param2 = 0 or 2
if node.param2 == 1 or node.param2 == 3 then
colbox = {0, -0.4374, -0.4374, 0, 0.4374, 0.4374}
end
2017-04-01 20:04:27 +02:00
-- drawer values
local meta = core.get_meta(self.drawer_pos)
self.count = meta:get_int("count")
self.itemName = meta:get_string("name")
self.maxCount = meta:get_int("max_count")
self.itemStackMax = meta:get_int("base_stack_max")
self.stackMaxFactor = meta:get_int("stack_max_factor")
-- infotext
local infotext = meta:get_string("entity_infotext") .. "\n\n\n\n\n"
self.object:set_properties({
collisionbox = colbox,
infotext = infotext,
textures = {self.texture}
})
-- make entity undestroyable
self.object:set_armor_groups({immortal = 1})
end,
on_rightclick = function(self, clicker)
2017-04-01 19:13:09 +02:00
local leftover = self.try_insert_stack(self, clicker:get_wielded_item(),
not clicker:get_player_control().sneak)
2017-04-01 19:13:09 +02:00
clicker:set_wielded_item(leftover)
end,
on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
local meta = core.get_meta(self.drawer_pos)
2017-04-01 20:04:27 +02:00
if self.count <= 0 then
return
end
2017-04-01 20:04:27 +02:00
local removeCount = 1
if not puncher:get_player_control().sneak then
2017-04-01 20:04:27 +02:00
removeCount = ItemStack(self.itemName):get_stack_max()
end
2017-04-01 20:04:27 +02:00
if removeCount > self.count then removeCount = self.count end
2017-04-01 20:04:27 +02:00
local stack = ItemStack(self.itemName)
stack:set_count(removeCount)
local inv = puncher:get_inventory()
if not inv:room_for_item("main", stack) then
return
end
inv:add_item("main", stack)
2017-04-01 20:04:27 +02:00
self.count = self.count - removeCount
meta:set_int("count", self.count)
-- update infotext
2017-04-01 20:04:27 +02:00
local itemDescription = ""
if core.registered_items[self.itemName] then
itemDescription = core.registered_items[self.itemName].description
end
2017-04-01 20:04:27 +02:00
if self.count <= 0 then
2017-04-01 22:41:35 +02:00
self.itemName = ""
meta:set_string("name", self.itemName)
self.texture = "drawers_empty.png"
2017-04-01 20:04:27 +02:00
itemDescription = "Empty"
end
2017-04-01 20:04:27 +02:00
local infotext = drawers.gen_info_text(itemDescription,
self.count, self.stackMaxFactor, self.itemStackMax)
meta:set_string("entity_infotext", infotext)
self.object:set_properties({
infotext = infotext .. "\n\n\n\n\n",
textures = {self.texture}
})
2017-04-01 19:13:09 +02:00
end,
try_insert_stack = function(self, itemstack, insert_stack)
local stackCount = itemstack:get_count()
local stackName = itemstack:get_name()
-- if nothing to be added, return
if stackCount <= 0 then return itemstack end
-- if no itemstring, return
if stackName == "" then return itemstack end
-- only add one, if player holding sneak key
if not insert_stack then
stackCount = 1
end
-- if current itemstring is not empty
2017-04-01 20:04:27 +02:00
if self.itemName ~= "" then
2017-04-01 19:13:09 +02:00
-- check if same item
2017-04-01 20:04:27 +02:00
if stackName ~= self.itemName then return itemstack end
2017-04-01 19:13:09 +02:00
else -- is empty
2017-04-01 20:04:27 +02:00
self.itemName = stackName
self.count = 0
2017-04-01 19:13:09 +02:00
-- get new stack max
2017-04-01 20:04:27 +02:00
self.itemStackMax = ItemStack(self.itemName):get_stack_max()
self.maxCount = self.itemStackMax * self.stackMaxFactor
2017-04-01 19:13:09 +02:00
-- Don't add items stackable only to 1
2017-04-01 20:04:27 +02:00
if self.itemStackMax == 1 then
2017-04-01 19:13:09 +02:00
return itemstack
end
end
-- set new counts:
-- if new count is more than max_count
2017-04-01 20:04:27 +02:00
if (self.count + stackCount) > self.maxCount then
itemstack:set_count(self.count + stackCount - self.maxCount)
2017-04-01 20:04:27 +02:00
self.count = self.maxCount
2017-04-01 19:13:09 +02:00
else -- new count fits
2017-04-01 20:04:27 +02:00
self.count = self.count + stackCount
2017-04-01 19:13:09 +02:00
-- this is for only removing one
itemstack:set_count(itemstack:get_count() - stackCount)
end
2017-04-01 20:04:27 +02:00
-- get meta
local meta = core.get_meta(self.drawer_pos)
2017-04-01 19:13:09 +02:00
-- update infotext
2017-04-01 20:04:27 +02:00
local itemDescription
if core.registered_items[self.itemName] then
itemDescription = core.registered_items[self.itemName].description
else
itemDescription = "Empty"
end
local infotext = drawers.gen_info_text(itemDescription,
self.count, self.stackMaxFactor, self.itemStackMax)
2017-04-01 19:13:09 +02:00
meta:set_string("entity_infotext", infotext)
-- texture
2017-04-01 20:04:27 +02:00
self.texture = drawers.get_inv_image(self.itemName)
2017-04-01 19:13:09 +02:00
self.object:set_properties({
infotext = infotext .. "\n\n\n\n\n",
textures = {self.texture}
})
2017-04-01 20:04:27 +02:00
self.saveMetaData(self, meta)
2017-04-01 19:13:09 +02:00
if itemstack:get_count() == 0 then itemstack = ItemStack("") end
return itemstack
2017-04-01 20:04:27 +02:00
end,
saveMetaData = function(self, meta)
meta:set_int("count", self.count)
meta:set_string("name", self.itemName)
meta:set_int("max_count", self.maxCount)
meta:set_int("base_stack_max", self.itemStackMax)
meta:set_int("stack_max_factor", self.stackMaxFactor)
end
})
core.register_lbm({
name = "drawers:restore_visual",
nodenames = {"group:drawer"},
run_at_every_load = true,
action = function(pos, node)
local objs = core.get_objects_inside_radius(pos, 0.5)
if objs then
for _, obj in pairs(objs) do
if obj and obj:get_luaentity() and
obj:get_luaentity().name == "drawers:visual" then
return
end
end
end
-- no visual found, create a new one
drawers.spawn_visual(pos)
end
})