1
0
Fork 0

Merge pull request '`mcl_item_entity` fixes' (#2936) from mcl-item-entity-fixes into master

Reviewed-on: MineClone2/MineClone2#2936
Reviewed-by: iliekprogrammar <iliekprogrammar@gmail.com>
This commit is contained in:
iliekprogrammar 2022-12-30 18:39:34 +00:00
commit 5237eca31e
1 changed files with 120 additions and 102 deletions

View File

@ -14,24 +14,17 @@ local function mcl_log (message)
end
end
minetest.register_on_joinplayer(function(player)
local name
name = player:get_player_name()
pool[name] = 0
pool[player:get_player_name()] = 0
end)
minetest.register_on_leaveplayer(function(player)
local name
name = player:get_player_name()
pool[name] = nil
pool[player:get_player_name()] = nil
end)
local has_awards = minetest.get_modpath("awards")
local mcl_item_entity = {}
mcl_item_entity = {}
--basic settings
local item_drop_settings = {} --settings table
@ -52,16 +45,23 @@ local function get_gravity()
return tonumber(minetest.settings:get("movement_gravity")) or 9.81
end
local registered_pickup_achievement = {}
mcl_item_entity.registered_pickup_achievement = {}
--TODO: remove limitation of 1 award per itemname
---Register an achievement that will be unlocked on pickup.
---
---TODO: remove limitation of 1 award per itemname
---@param itemname string
---@param award string
function mcl_item_entity.register_pickup_achievement(itemname, award)
if not has_awards then
minetest.log("warning", "[mcl_item_entity] Trying to register pickup achievement ["..award.."] for ["..itemname.."] while awards missing")
elseif registered_pickup_achievement[itemname] then
minetest.log("error", "[mcl_item_entity] Trying to register already existing pickup achievement ["..award.."] for ["..itemname.."]")
minetest.log("warning",
"[mcl_item_entity] Trying to register pickup achievement [" .. award .. "] for [" ..
itemname .. "] while awards missing")
elseif mcl_item_entity.registered_pickup_achievement[itemname] then
minetest.log("error",
"[mcl_item_entity] Trying to register already existing pickup achievement [" .. award .. "] for [" .. itemname .. "]")
else
registered_pickup_achievement[itemname] = award
mcl_item_entity.registered_pickup_achievement[itemname] = award
end
end
@ -74,11 +74,13 @@ mcl_item_entity.register_pickup_achievement("mcl_nether:ancient_debris", "mcl:hi
mcl_item_entity.register_pickup_achievement("mcl_end:dragon_egg", "mcl:PickUpDragonEgg")
mcl_item_entity.register_pickup_achievement("mcl_armor:elytra", "mcl:skysTheLimit")
---@param object ObjectRef
---@param player ObjectRef
local function check_pickup_achievements(object, player)
if has_awards then
local itemname = ItemStack(object:get_luaentity().itemstring):get_name()
local playername = player:get_player_name()
for name,award in pairs(registered_pickup_achievement) do
for name, award in pairs(mcl_item_entity.registered_pickup_achievement) do
if itemname == name or minetest.get_item_group(itemname, name) ~= 0 then
awards.unlock(playername, award)
end
@ -86,16 +88,23 @@ local function check_pickup_achievements(object, player)
end
end
---@param object ObjectRef
---@param luaentity Luaentity
---@param ignore_check? boolean
local function enable_physics(object, luaentity, ignore_check)
if luaentity.physical_state == false or ignore_check == true then
luaentity.physical_state = true
object:set_properties({
physical = true
})
object:set_acceleration({x=0,y=-get_gravity(),z=0})
object:set_acceleration(vector.new(0, -get_gravity(), 0))
end
end
---@param object ObjectRef
---@param luaentity Luaentity
---@param ignore_check? boolean
---@param reset_movement? boolean
local function disable_physics(object, luaentity, ignore_check, reset_movement)
if luaentity.physical_state == true or ignore_check == true then
luaentity.physical_state = false
@ -103,14 +112,13 @@ local function disable_physics(object, luaentity, ignore_check, reset_movement)
physical = false
})
if reset_movement ~= false then
object:set_velocity({x=0,y=0,z=0})
object:set_acceleration({x=0,y=0,z=0})
object:set_velocity(vector.zero())
object:set_acceleration(vector.zero())
end
end
end
minetest.register_globalstep(function(dtime)
minetest.register_globalstep(function(_)
tick = not tick
for _, player in pairs(minetest.get_connected_players()) do
@ -135,15 +143,18 @@ minetest.register_globalstep(function(dtime)
end
local inv = player:get_inventory()
local checkpos = {x=pos.x,y=pos.y + item_drop_settings.player_collect_height,z=pos.z}
local checkpos = vector.offset(pos, 0, item_drop_settings.player_collect_height, 0)
--magnet and collection
for _, object in pairs(minetest.get_objects_inside_radius(checkpos, item_drop_settings.xp_radius_magnet)) do
if not object:is_player() and vector.distance(checkpos, object:get_pos()) < item_drop_settings.radius_magnet and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" and object:get_luaentity()._magnet_timer and (object:get_luaentity()._insta_collect or (object:get_luaentity().age > item_drop_settings.age)) then
if not object:is_player() and vector.distance(checkpos, object:get_pos()) < item_drop_settings.radius_magnet and
object:get_luaentity() and object:get_luaentity().name == "__builtin:item" and object:get_luaentity()._magnet_timer
and (object:get_luaentity()._insta_collect or (object:get_luaentity().age > item_drop_settings.age)) then
if object:get_luaentity()._magnet_timer >= 0 and object:get_luaentity()._magnet_timer < item_drop_settings.magnet_time and inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
if object:get_luaentity()._magnet_timer >= 0 and
object:get_luaentity()._magnet_timer < item_drop_settings.magnet_time and inv and
inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
-- Collection
if not object:get_luaentity()._removed then
@ -158,8 +169,8 @@ minetest.register_globalstep(function(dtime)
object:get_luaentity().target = checkpos
object:get_luaentity()._removed = true
object:set_velocity({x=0,y=0,z=0})
object:set_acceleration({x=0,y=0,z=0})
object:set_velocity(vector.zero())
object:set_acceleration(vector.zero())
object:move_to(checkpos)
@ -179,7 +190,6 @@ minetest.register_globalstep(function(dtime)
local entity = object:get_luaentity()
entity.collector = player:get_player_name()
entity.collected = true
end
end
@ -194,6 +204,11 @@ end)
local tmp_id = 0
---@param drop string|drop_definition
---@param toolname string
---@param param2 integer
---@param paramtype2 paramtype2
---@return string[]
local function get_drops(drop, toolname, param2, paramtype2)
tmp_id = tmp_id + 1
local tmp_node_name = "mcl_item_entity:" .. tmp_id
@ -265,7 +280,7 @@ function minetest.handle_node_drops(pos, drops, digger)
* table: Drop every itemstring in this table when dug by shears _mcl_silk_touch_drop
]]
local enchantments = tool and mcl_enchanting.get_enchantments(tool, "silk_touch")
local enchantments = tool and mcl_enchanting.get_enchantments(tool)
local silk_touch_drop = false
local nodedef = minetest.registered_nodes[dug_node.name]
@ -294,7 +309,8 @@ function minetest.handle_node_drops(pos, drops, digger)
local max_count = fortune_drop.max_count + fortune_level * (fortune_drop.factor or 1)
local chance = fortune_drop.chance or fortune_drop.get_chance and fortune_drop.get_chance(fortune_level)
if not chance or math.random() < chance then
drops = discrete_uniform_distribution(fortune_drop.multiply and drops or fortune_drop.items, min_count, max_count, fortune_drop.cap)
drops = discrete_uniform_distribution(fortune_drop.multiply and drops or fortune_drop.items, min_count, max_count,
fortune_drop.cap)
elseif fortune_drop.override then
drops = {}
end
@ -348,7 +364,7 @@ end
function minetest.item_drop(itemstack, dropper, pos)
if dropper and dropper:is_player() then
local v = dropper:get_look_dir()
local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
local p = vector.offset(pos, 0, 1.2, 0)
local cs = itemstack:get_count()
if dropper:get_player_control().sneak then
cs = 1
@ -548,7 +564,7 @@ minetest.register_entity(":__builtin:item", {
local z = math.random(5, 10) / 10 * v
if math.random(0, 10) < 5 then z = -z end
local y = math.random(2, 4)
self.object:set_velocity({x=x, y=y, z=z})
self.object:set_velocity(vector.new(x, y, z))
end
self.random_velocity = 0
end,
@ -696,8 +712,8 @@ minetest.register_entity(":__builtin:item", {
self._forcetimer = 0
self.object:set_armor_groups({ immortal = 1 })
-- self.object:set_velocity({x = 0, y = 2, z = 0})
self.object:set_acceleration({x = 0, y = -get_gravity(), z = 0})
-- self.object:set_velocity(vector.new(0, 2, 0))
self.object:set_acceleration(vector.new(0, -get_gravity(), 0))
self:set_item(self.itemstring)
end,
@ -745,8 +761,8 @@ minetest.register_entity(":__builtin:item", {
self.object:set_properties({
physical = false
})
self.object:set_velocity({x=0,y=0,z=0})
self.object:set_acceleration({x=0,y=0,z=0})
self.object:set_velocity(vector.zero())
self.object:set_acceleration(vector.zero())
return
end
self.age = self.age + dtime
@ -761,21 +777,22 @@ minetest.register_entity(":__builtin:item", {
-- Delete corrupted item entities. The itemstring MUST be non-empty on its first step,
-- otherwise there might have some data corruption.
if self.itemstring == "" then
minetest.log("warning", "Item entity with empty itemstring found at "..minetest.pos_to_string(self.object:get_pos()).. "! Deleting it now.")
minetest.log("warning",
"Item entity with empty itemstring found at " .. minetest.pos_to_string(self.object:get_pos()) ..
"! Deleting it now.")
self._removed = true
self.object:remove()
return
end
local p = self.object:get_pos()
-- If hopper has taken item, it has gone, and no operations should be conducted on this item
if hopper_take_item(self, p) then
return
end
local node = minetest.get_node_or_nil(p)
local in_unloaded = (node == nil)
local node = minetest.get_node(p)
local in_unloaded = node.name == "ignore"
if in_unloaded then
-- Don't infinetly fall into unloaded map
@ -791,7 +808,7 @@ minetest.register_entity(":__builtin:item", {
local nn = node.name
local is_in_water = (minetest.get_item_group(nn, "liquid") ~= 0)
local nn_above = minetest.get_node({x=p.x, y=p.y+0.1, z=p.z}).name
local nn_above = minetest.get_node(vector.offset(p, 0, 0.1, 0)).name
-- make sure it's more or less stationary and is at water level
local sleep_threshold = 0.3
local is_floating = false
@ -804,8 +821,8 @@ minetest.register_entity(":__builtin:item", {
end
if is_floating and self.physical_state == true then
self.object:set_velocity({x = 0, y = 0, z = 0})
self.object:set_acceleration({x = 0, y = 0, z = 0})
self.object:set_velocity(vector.zero())
self.object:set_acceleration(vector.zero())
disable_physics(self.object, self)
end
-- If no collector was found for a long enough time, declare the magnet as disabled
@ -875,7 +892,7 @@ minetest.register_entity(":__builtin:item", {
end
-- If none of the 4 sides is free, shoot upwards
if shootdir == nil then
shootdir = { x=0, y=1, z=0 }
shootdir = vector.new(0, 1, 0)
local nn = minetest.get_node(vector.add(p, shootdir)).name
if nn == "ignore" then
-- Do not push into ignore
@ -885,7 +902,7 @@ minetest.register_entity(":__builtin:item", {
-- Set new item moving speed accordingly
local newv = vector.multiply(shootdir, 3)
self.object:set_acceleration({x = 0, y = 0, z = 0})
self.object:set_acceleration(vector.zero())
self.object:set_velocity(newv)
disable_physics(self.object, self, false, false)
@ -941,7 +958,7 @@ minetest.register_entity(":__builtin:item", {
-- Set new item moving speed into the direciton of the liquid
local newv = vector.multiply(vec, f)
-- Swap to acceleration instead of a static speed to better mimic MC mechanics.
self.object:set_acceleration({x = newv.x, y = -0.22, z = newv.z})
self.object:set_acceleration(vector.new(newv.x, -0.22, newv.z))
self.physical_state = true
self._flowing = true
@ -956,7 +973,8 @@ minetest.register_entity(":__builtin:item", {
local vec = {
x = 0 - cur_vec.x * 0.9,
y = 3 - cur_vec.y * 0.9,
z = 0 -cur_vec.z*0.9}
z = 0 - cur_vec.z * 0.9
}
self.object:set_acceleration(vec)
-- slow down the item in water
local vel = self.object:get_velocity()
@ -980,7 +998,7 @@ minetest.register_entity(":__builtin:item", {
end
-- If node is not registered or node is walkably solid and resting on nodebox
local nn = minetest.get_node({x=p.x, y=p.y-0.5, z=p.z}).name
local nn = minetest.get_node(vector.offset(p, 0, -0.5, 0)).name
local def = minetest.registered_nodes[nn]
local v = self.object:get_velocity()
local is_on_floor = def and (def.walkable