Support all minecart types in dispenser

This commit is contained in:
Wuzzy 2017-08-29 02:36:50 +02:00
parent 0795a97dd7
commit 3a18f4044d
2 changed files with 73 additions and 32 deletions

View File

@ -5,6 +5,8 @@ mcl_minecarts.speed_max = 10
dofile(mcl_minecarts.modpath.."/functions.lua") dofile(mcl_minecarts.modpath.."/functions.lua")
dofile(mcl_minecarts.modpath.."/rails.lua") dofile(mcl_minecarts.modpath.."/rails.lua")
-- Table for item-to-entity mapping. Keys: itemstring, Values: Corresponding entity ID
local entity_mapping = {}
local function register_entity(entity_id, mesh, textures, drop, on_rightclick) local function register_entity(entity_id, mesh, textures, drop, on_rightclick)
local cart = { local cart = {
@ -290,7 +292,45 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick)
minetest.register_entity(entity_id, cart) minetest.register_entity(entity_id, cart)
end end
-- Place a minecart at pointed_thing
mcl_minecarts.place_minecart = function(itemstack, pointed_thing)
if not pointed_thing.type == "node" then
return
end
local railpos, node
if mcl_minecarts:is_rail(pointed_thing.under) then
railpos = pointed_thing.under
node = minetest.get_node(pointed_thing.under)
elseif mcl_minecarts:is_rail(pointed_thing.above) then
railpos = pointed_thing.above
node = minetest.get_node(pointed_thing.above)
else
return
end
-- Activate detector rail
if node.name == "mcl_minecarts:detector_rail" then
local newnode = {name="mcl_minecarts:detector_rail_on", param2 = node.param2}
minetest.swap_node(railpos, newnode)
mesecon.receptor_on(railpos)
end
local entity_id = entity_mapping[itemstack:get_name()]
local cart = minetest.add_entity(railpos, entity_id)
local railtype = minetest.get_item_group(node.name, "connect_to_raillike")
local cart_dir = mcl_minecarts:get_rail_direction(railpos, {x=1, y=0, z=0}, nil, nil, railtype)
cart:setyaw(minetest.dir_to_yaw(cart_dir))
if not minetest.settings:get_bool("creative_mode") then
itemstack:take_item()
end
return itemstack
end
local register_craftitem = function(itemstring, entity_id, description, longdesc, usagehelp, icon) local register_craftitem = function(itemstring, entity_id, description, longdesc, usagehelp, icon)
entity_mapping[itemstring] = entity_id
local def = { local def = {
stack_max = 1, stack_max = 1,
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
@ -306,32 +346,7 @@ local register_craftitem = function(itemstring, entity_id, description, longdesc
end end
end end
local railpos return mcl_minecarts.place_minecart(itemstack, pointed_thing)
if mcl_minecarts:is_rail(pointed_thing.under) then
railpos = pointed_thing.under
elseif mcl_minecarts:is_rail(pointed_thing.above) then
railpos = pointed_thing.above
node = minetest.get_node(pointed_thing.above)
else
return
end
-- Activate detector rail
if node.name == "mcl_minecarts:detector_rail" then
local newnode = {name="mcl_minecarts:detector_rail_on", param2 = node.param2}
minetest.swap_node(railpos, newnode)
mesecon.receptor_on(railpos)
end
local cart = minetest.add_entity(railpos, entity_id)
local railtype = minetest.get_item_group(node.name, "connect_to_raillike")
local cart_dir = mcl_minecarts:get_rail_direction(railpos, {x=1, y=0, z=0}, nil, nil, railtype)
cart:setyaw(minetest.dir_to_yaw(cart_dir))
if not minetest.settings:get_bool("creative_mode") then
itemstack:take_item()
end
return itemstack
end, end,
groups = { minecart = 1, transport = 1}, groups = { minecart = 1, transport = 1},
} }
@ -410,7 +425,30 @@ register_minecart(
"mcl_minecarts_minecart.png", "mcl_minecarts_minecart.png",
}, },
"mcl_minecarts_minecart_furnace.png", "mcl_minecarts_minecart_furnace.png",
{"mcl_minecarts:minecart", "mcl_furnaces:furnace"} {"mcl_minecarts:minecart", "mcl_furnaces:furnace"},
-- Feed furnace with coal
function(self, clicker)
if not clicker or not clicker:is_player() then
return
end
if not self._fueltime then
self._fueltime = 0
end
local held = clicker:get_wielded_item()
if minetest.get_item_group(held:get_name(), "coal") == 1 then
self._fueltime = self._fueltime + 180
if not minetest.settings:get_bool("creative_mode") then
held:take_item()
local index = clicker:get_wielded_index()
local inv = clicker:get_inventory()
inv:set_stack("main", index, held)
end
-- DEBUG
minetest.chat_send_player(clicker:get_player_name(), "Fuel: " .. tostring(self._fueltime))
end
end
) )
-- Minecart with Command Block -- Minecart with Command Block

View File

@ -191,6 +191,7 @@ local dispenserdef = {
elseif iname == "mcl_dye:white" then elseif iname == "mcl_dye:white" then
-- Apply bone meal, if possible -- Apply bone meal, if possible
local pointed_thing
if dropnode.name == "air" then if dropnode.name == "air" then
pointed_thing = { above = droppos, under = { x=droppos.x, y=droppos.y-1, z=droppos.z } } pointed_thing = { above = droppos, under = { x=droppos.x, y=droppos.y-1, z=droppos.z } }
else else
@ -202,15 +203,17 @@ local dispenserdef = {
inv:set_stack("main", stack_id, stack) inv:set_stack("main", stack_id, stack)
end end
elseif iname == "mcl_minecarts:minecart" then elseif minetest.get_item_group(iname, "minecart") == 1 then
-- Place minecart as entity on rail -- Place minecart as entity on rail
local placed
if dropnodedef.groups.rail then if dropnodedef.groups.rail then
minetest.add_entity(droppos, "mcl_minecarts:minecart") -- FIXME: This places minecarts even if the spot is already occupied
local pointed_thing = { under = droppos, above = { x=droppos.x, y=droppos.y+1, z=droppos.z } }
else placed = mcl_minecarts.place_minecart(stack, pointed_thing)
end
if placed == nil then
-- Drop item -- Drop item
minetest.add_item(droppos, dropitem) minetest.add_item(droppos, dropitem)
end end
stack:take_item() stack:take_item()