2017-02-14 05:23:06 +03:00
--[[ This mod registers 3 nodes:
- One node for the horizontal - facing dispensers ( mcl_dispensers : dispenser )
- One node for the upwards - facing dispensers ( mcl_dispenser : dispenser_up )
- One node for the downwards - facing dispensers ( mcl_dispenser : dispenser_down )
3 node definitions are needed because of the way the textures are defined .
All node definitions share a lot of code , so this is the reason why there
are so many weird tables below .
] ]
2021-05-29 17:12:33 +03:00
local S = minetest.get_translator ( minetest.get_current_modname ( ) )
2017-02-14 05:23:06 +03:00
-- For after_place_node
2021-05-29 17:12:33 +03:00
local function setup_dispenser ( pos )
2017-02-14 05:23:06 +03:00
-- Set formspec and inventory
local form = " size[9,8.75] " ..
2021-04-19 15:44:20 +03:00
" label[0,4.0; " .. minetest.formspec_escape ( minetest.colorize ( " #313131 " , S ( " Inventory " ) ) ) .. " ] " ..
2017-02-14 05:23:06 +03:00
" list[current_player;main;0,4.5;9,3;9] " ..
2020-03-29 15:15:00 +03:00
mcl_formspec.get_itemslot_bg ( 0 , 4.5 , 9 , 3 ) ..
2017-02-14 05:23:06 +03:00
" list[current_player;main;0,7.74;9,1;] " ..
2020-03-29 15:15:00 +03:00
mcl_formspec.get_itemslot_bg ( 0 , 7.74 , 9 , 1 ) ..
2021-04-19 15:44:20 +03:00
" label[3,0; " .. minetest.formspec_escape ( minetest.colorize ( " #313131 " , S ( " Dispenser " ) ) ) .. " ] " ..
2021-06-12 01:09:39 +03:00
" list[context;main;3,0.5;3,3;] " ..
2020-03-29 15:15:00 +03:00
mcl_formspec.get_itemslot_bg ( 3 , 0.5 , 3 , 3 ) ..
2021-06-12 01:09:39 +03:00
" listring[context;main] " ..
2017-02-14 05:23:06 +03:00
" listring[current_player;main] "
local meta = minetest.get_meta ( pos )
meta : set_string ( " formspec " , form )
local inv = meta : get_inventory ( )
inv : set_size ( " main " , 9 )
end
2021-05-29 17:12:33 +03:00
local function orientate_dispenser ( pos , placer )
2018-01-11 03:28:36 +03:00
-- Not placed by player
if not placer then return end
-- Pitch in degrees
local pitch = placer : get_look_vertical ( ) * ( 180 / math.pi )
local node = minetest.get_node ( pos )
if pitch > 55 then
2018-01-13 05:15:32 +03:00
minetest.swap_node ( pos , { name = " mcl_dispensers:dispenser_up " , param2 = node.param2 } )
2018-01-11 03:28:36 +03:00
elseif pitch < - 55 then
2018-01-13 05:15:32 +03:00
minetest.swap_node ( pos , { name = " mcl_dispensers:dispenser_down " , param2 = node.param2 } )
2018-01-11 03:28:36 +03:00
end
end
2017-12-05 16:09:39 +03:00
local on_rotate
if minetest.get_modpath ( " screwdriver " ) then
on_rotate = screwdriver.rotate_simple
end
2017-02-14 05:23:06 +03:00
-- Shared core definition table
local dispenserdef = {
is_ground_content = false ,
sounds = mcl_sounds.node_sound_stone_defaults ( ) ,
2019-02-08 23:59:01 +03:00
allow_metadata_inventory_move = function ( pos , from_list , from_index , to_list , to_index , count , player )
local name = player : get_player_name ( )
if minetest.is_protected ( pos , name ) then
minetest.record_protection_violation ( pos , name )
return 0
else
return count
end
end ,
allow_metadata_inventory_take = function ( pos , listname , index , stack , player )
local name = player : get_player_name ( )
if minetest.is_protected ( pos , name ) then
minetest.record_protection_violation ( pos , name )
return 0
else
return stack : get_count ( )
end
end ,
allow_metadata_inventory_put = function ( pos , listname , index , stack , player )
local name = player : get_player_name ( )
if minetest.is_protected ( pos , name ) then
minetest.record_protection_violation ( pos , name )
return 0
else
return stack : get_count ( )
end
end ,
2017-02-14 05:23:06 +03:00
after_dig_node = function ( pos , oldnode , oldmetadata , digger )
local meta = minetest.get_meta ( pos )
2021-11-29 21:12:49 +03:00
local meta2 = meta : to_table ( )
2017-02-14 05:23:06 +03:00
meta : from_table ( oldmetadata )
local inv = meta : get_inventory ( )
for i = 1 , inv : get_size ( " main " ) do
local stack = inv : get_stack ( " main " , i )
if not stack : is_empty ( ) then
local p = { x = pos.x + math.random ( 0 , 10 ) / 10 - 0.5 , y = pos.y , z = pos.z + math.random ( 0 , 10 ) / 10 - 0.5 }
minetest.add_item ( p , stack )
end
end
2021-11-29 21:12:49 +03:00
meta : from_table ( meta2 )
2017-02-14 05:23:06 +03:00
end ,
2020-04-17 22:40:13 +03:00
_mcl_blast_resistance = 3.5 ,
2017-02-27 03:52:24 +03:00
_mcl_hardness = 3.5 ,
2021-05-23 12:50:16 +03:00
mesecons = {
effector = {
-- Dispense random item when triggered
2021-05-29 17:12:33 +03:00
action_on = function ( pos , node )
2021-05-23 12:50:16 +03:00
local meta = minetest.get_meta ( pos )
local inv = meta : get_inventory ( )
local droppos , dropdir
if node.name == " mcl_dispensers:dispenser " then
dropdir = vector.multiply ( minetest.facedir_to_dir ( node.param2 ) , - 1 )
droppos = vector.add ( pos , dropdir )
elseif node.name == " mcl_dispensers:dispenser_up " then
dropdir = { x = 0 , y = 1 , z = 0 }
droppos = { x = pos.x , y = pos.y + 1 , z = pos.z }
elseif node.name == " mcl_dispensers:dispenser_down " then
dropdir = { x = 0 , y =- 1 , z = 0 }
droppos = { x = pos.x , y = pos.y - 1 , z = pos.z }
2017-02-14 05:23:06 +03:00
end
2021-05-23 12:50:16 +03:00
local dropnode = minetest.get_node ( droppos )
local dropnodedef = minetest.registered_nodes [ dropnode.name ]
local stacks = { }
for i = 1 , inv : get_size ( " main " ) do
local stack = inv : get_stack ( " main " , i )
if not stack : is_empty ( ) then
table.insert ( stacks , { stack = stack , stackpos = i } )
end
end
if # stacks >= 1 then
local r = math.random ( 1 , # stacks )
local stack = stacks [ r ] . stack
local dropitem = ItemStack ( stack )
dropitem : set_count ( 1 )
local stack_id = stacks [ r ] . stackpos
local stackdef = stack : get_definition ( )
2021-07-08 16:31:27 +03:00
if not stackdef then
return
end
2022-05-26 08:29:28 +03:00
2021-05-23 12:50:16 +03:00
local iname = stack : get_name ( )
2021-07-08 16:31:27 +03:00
local igroups = stackdef.groups
2021-05-23 12:50:16 +03:00
--[===[ Dispense item ]===]
-- Hardcoded dispensions --
-- Armor, mob heads and pumpkins
if igroups.armor then
local droppos_below = { x = droppos.x , y = droppos.y - 1 , z = droppos.z }
for _ , objs in ipairs ( { minetest.get_objects_inside_radius ( droppos , 1 ) , minetest.get_objects_inside_radius ( droppos_below , 1 ) } ) do
for _ , obj in ipairs ( objs ) do
stack = mcl_armor.equip ( stack , obj )
if stack : is_empty ( ) then
break
end
end
2021-04-22 14:51:36 +03:00
if stack : is_empty ( ) then
2017-02-14 21:45:02 +03:00
break
end
end
2017-06-11 23:53:12 +03:00
2021-05-23 12:50:16 +03:00
-- Place head or pumpkin as node, if equipping it as armor has failed
if not stack : is_empty ( ) then
if igroups.head or iname == " mcl_farming:pumpkin_face " then
if dropnodedef.buildable_to then
minetest.set_node ( droppos , { name = iname , param2 = node.param2 } )
stack : take_item ( )
end
2017-06-11 23:53:12 +03:00
end
end
2017-02-15 02:31:00 +03:00
inv : set_stack ( " main " , stack_id , stack )
2021-09-06 16:30:08 +03:00
-- Use shears on sheeps
elseif igroups.shears then
for _ , obj in pairs ( minetest.get_objects_inside_radius ( droppos , 1 ) ) do
local entity = obj : get_luaentity ( )
if entity and not entity.child and not entity.gotten then
local entname = entity.name
local pos = obj : get_pos ( )
local used , texture = false
if entname == " mobs_mc:sheep " then
2023-06-19 19:49:20 +03:00
if entity.drops [ 2 ] then
minetest.add_item ( pos , entity.drops [ 2 ] . name .. " " .. math.random ( 1 , 3 ) )
end
2021-09-06 16:30:08 +03:00
if not entity.color then
entity.color = " unicolor_white "
end
entity.base_texture = { " blank.png " , " mobs_mc_sheep.png " }
texture = entity.base_texture
entity.drops = {
2022-05-26 00:25:15 +03:00
{ name = " mcl_mobitems:mutton " , chance = 1 , min = 1 , max = 2 } ,
2021-09-06 16:30:08 +03:00
}
used = true
elseif entname == " mobs_mc:snowman " then
texture = {
" mobs_mc_snowman.png " ,
" blank.png " , " blank.png " ,
" blank.png " , " blank.png " ,
" blank.png " , " blank.png " ,
}
used = true
elseif entname == " mobs_mc:mooshroom " then
local droppos = vector.offset ( pos , 0 , 1.4 , 0 )
if entity.base_texture [ 1 ] == " mobs_mc_mooshroom_brown.png " then
2022-05-26 00:25:15 +03:00
minetest.add_item ( droppos , " mcl_mushrooms:mushroom_brown 5 " )
2021-09-06 16:30:08 +03:00
else
2022-05-26 00:25:15 +03:00
minetest.add_item ( droppos , " mcl_mushrooms:mushroom_red 5 " )
2021-09-06 16:30:08 +03:00
end
2021-09-09 13:07:45 +03:00
obj = mcl_util.replace_mob ( obj , " mobs_mc:cow " )
entity = obj : get_luaentity ( )
2021-09-06 16:30:08 +03:00
used = true
end
if used then
obj : set_properties ( { textures = texture } )
entity.gotten = true
minetest.sound_play ( " mcl_tools_shears_cut " , { pos = pos } , true )
stack : add_wear ( 65535 / stackdef._mcl_diggroups . shearsy.uses )
inv : set_stack ( " main " , stack_id , stack )
2021-09-08 17:22:53 +03:00
break
2021-09-06 16:30:08 +03:00
end
end
end
2021-05-23 12:50:16 +03:00
-- Spawn Egg
elseif igroups.spawn_egg then
-- Spawn mob
if not dropnodedef.walkable then
--pointed_thing = { above = droppos, under = { x=droppos.x, y=droppos.y-1, z=droppos.z } }
minetest.add_entity ( droppos , stack : get_name ( ) )
2017-02-15 02:31:00 +03:00
2021-05-23 12:50:16 +03:00
stack : take_item ( )
2018-02-02 00:45:19 +03:00
inv : set_stack ( " main " , stack_id , stack )
2021-05-23 12:50:16 +03:00
end
-- Generalized dispension
elseif ( not dropnodedef.walkable or stackdef._dispense_into_walkable ) then
--[[ _on_dispense(stack, pos, droppos, dropnode, dropdir)
* stack : Itemstack which is dispense
* pos : Position of dispenser
* droppos : Position to which to dispense item
* dropnode : Node of droppos
* dropdir : Drop direction
_dispense_into_walkable : If true , can dispense into walkable nodes
] ]
if stackdef._on_dispense then
-- Item-specific dispension (if defined)
local od_ret = stackdef._on_dispense ( dropitem , pos , droppos , dropnode , dropdir )
if od_ret then
local newcount = stack : get_count ( ) - 1
stack : set_count ( newcount )
inv : set_stack ( " main " , stack_id , stack )
if newcount == 0 then
inv : set_stack ( " main " , stack_id , od_ret )
elseif inv : room_for_item ( " main " , od_ret ) then
inv : add_item ( " main " , od_ret )
else
2022-07-22 15:38:36 +03:00
local pos_variation = 100
droppos = {
x = droppos.x + math.random ( - pos_variation , pos_variation ) / 1000 ,
y = droppos.y + math.random ( - pos_variation , pos_variation ) / 1000 ,
z = droppos.z + math.random ( - pos_variation , pos_variation ) / 1000 ,
}
local item_entity = minetest.add_item ( droppos , dropitem )
local drop_vel = vector.subtract ( droppos , pos )
local speed = 3
2022-08-09 22:47:11 +03:00
item_entity : set_velocity ( vector.multiply ( drop_vel , speed ) )
2021-05-23 12:50:16 +03:00
end
2018-02-02 00:45:19 +03:00
else
2021-05-23 12:50:16 +03:00
stack : take_item ( )
inv : set_stack ( " main " , stack_id , stack )
2018-02-02 00:45:19 +03:00
end
else
2021-05-23 12:50:16 +03:00
-- Drop item otherwise
2022-07-22 15:38:36 +03:00
local pos_variation = 100
droppos = {
x = droppos.x + math.random ( - pos_variation , pos_variation ) / 1000 ,
y = droppos.y + math.random ( - pos_variation , pos_variation ) / 1000 ,
z = droppos.z + math.random ( - pos_variation , pos_variation ) / 1000 ,
}
local item_entity = minetest.add_item ( droppos , dropitem )
local drop_vel = vector.subtract ( droppos , pos )
local speed = 3
2022-08-09 22:47:11 +03:00
item_entity : set_velocity ( vector.multiply ( drop_vel , speed ) )
2018-02-02 00:45:19 +03:00
stack : take_item ( )
inv : set_stack ( " main " , stack_id , stack )
end
end
2021-05-23 12:50:16 +03:00
end
end ,
rules = mesecon.rules . alldirs ,
} ,
} ,
2017-12-05 16:09:39 +03:00
on_rotate = on_rotate ,
2017-02-14 05:23:06 +03:00
}
-- Horizontal dispenser
local horizontal_def = table.copy ( dispenserdef )
2019-03-08 03:07:41 +03:00
horizontal_def.description = S ( " Dispenser " )
2020-03-12 03:35:11 +03:00
horizontal_def._tt_help = S ( " 9 inventory slots " ) .. " \n " .. S ( " Launches item when powered by redstone power " )
2019-03-08 03:07:41 +03:00
horizontal_def._doc_items_longdesc = S ( " A dispenser is a block which acts as a redstone component which, when powered with redstone power, dispenses an item. It has a container with 9 inventory slots. " )
2019-03-16 04:00:48 +03:00
horizontal_def._doc_items_usagehelp = S ( " Place the dispenser in one of 6 possible directions. The “hole” is where items will fly out of the dispenser. Use the dispenser to access its inventory. Insert the items you wish to dispense. Supply the dispenser with redstone energy once to dispense a random item. " ) .. " \n \n " ..
2017-03-11 04:50:12 +03:00
2019-03-08 03:07:41 +03:00
S ( " The dispenser will do different things, depending on the dispensed item: " ) .. " \n \n " ..
2017-03-11 04:50:12 +03:00
2019-03-16 04:00:48 +03:00
S ( " • Arrows: Are launched " ) .. " \n " ..
S ( " • Eggs and snowballs: Are thrown " ) .. " \n " ..
S ( " • Fire charges: Are fired in a straight line " ) .. " \n " ..
S ( " • Armor: Will be equipped to players and armor stands " ) .. " \n " ..
S ( " • Boats: Are placed on water or are dropped " ) .. " \n " ..
S ( " • Minecart: Are placed on rails or are dropped " ) .. " \n " ..
S ( " • Bone meal: Is applied on the block it is facing " ) .. " \n " ..
S ( " • Empty buckets: Are used to collect a liquid source " ) .. " \n " ..
S ( " • Filled buckets: Are used to place a liquid source " ) .. " \n " ..
S ( " • Heads, pumpkins: Equipped to players and armor stands, or placed as a block " ) .. " \n " ..
S ( " • Shulker boxes: Are placed as a block " ) .. " \n " ..
S ( " • TNT: Is placed and ignited " ) .. " \n " ..
S ( " • Flint and steel: Is used to ignite a fire in air and to ignite TNT " ) .. " \n " ..
S ( " • Spawn eggs: Will summon the mob they contain " ) .. " \n " ..
S ( " • Other items: Are simply dropped " )
2017-03-11 04:50:12 +03:00
2021-05-29 17:12:33 +03:00
function horizontal_def . after_place_node ( pos , placer , itemstack , pointed_thing )
2017-02-14 05:23:06 +03:00
setup_dispenser ( pos )
2018-01-11 03:28:36 +03:00
orientate_dispenser ( pos , placer )
2017-02-14 05:23:06 +03:00
end
2021-05-29 17:12:33 +03:00
2017-02-14 05:23:06 +03:00
horizontal_def.tiles = {
" default_furnace_top.png " , " default_furnace_bottom.png " ,
" default_furnace_side.png " , " default_furnace_side.png " ,
" default_furnace_side.png " , " mcl_dispensers_dispenser_front_horizontal.png "
}
horizontal_def.paramtype2 = " facedir "
2017-03-11 07:34:58 +03:00
horizontal_def.groups = { pickaxey = 1 , container = 2 , material_stone = 1 }
2017-02-14 05:23:06 +03:00
minetest.register_node ( " mcl_dispensers:dispenser " , horizontal_def )
-- Down dispenser
local down_def = table.copy ( dispenserdef )
2019-03-08 03:07:41 +03:00
down_def.description = S ( " Downwards-Facing Dispenser " )
2017-02-14 05:23:06 +03:00
down_def.after_place_node = setup_dispenser
down_def.tiles = {
" default_furnace_top.png " , " mcl_dispensers_dispenser_front_vertical.png " ,
" default_furnace_side.png " , " default_furnace_side.png " ,
" default_furnace_side.png " , " default_furnace_side.png "
}
2017-03-11 07:34:58 +03:00
down_def.groups = { pickaxey = 1 , container = 2 , not_in_creative_inventory = 1 , material_stone = 1 }
2017-03-02 21:53:53 +03:00
down_def._doc_items_create_entry = false
2017-02-14 05:23:06 +03:00
down_def.drop = " mcl_dispensers:dispenser "
minetest.register_node ( " mcl_dispensers:dispenser_down " , down_def )
-- Up dispenser
-- The up dispenser is almost identical to the down dispenser , it only differs in textures
2017-08-02 02:34:12 +03:00
local up_def = table.copy ( down_def )
2019-03-08 03:07:41 +03:00
up_def.description = S ( " Upwards-Facing Dispenser " )
2017-02-14 05:23:06 +03:00
up_def.tiles = {
" mcl_dispensers_dispenser_front_vertical.png " , " default_furnace_bottom.png " ,
" default_furnace_side.png " , " default_furnace_side.png " ,
" default_furnace_side.png " , " default_furnace_side.png "
}
minetest.register_node ( " mcl_dispensers:dispenser_up " , up_def )
minetest.register_craft ( {
2021-05-29 17:12:33 +03:00
output = " mcl_dispensers:dispenser " ,
2017-02-14 05:23:06 +03:00
recipe = {
{ " mcl_core:cobble " , " mcl_core:cobble " , " mcl_core:cobble " , } ,
2018-05-08 00:10:49 +03:00
{ " mcl_core:cobble " , " mcl_bows:bow " , " mcl_core:cobble " , } ,
2017-02-14 05:23:06 +03:00
{ " mcl_core:cobble " , " mesecons:redstone " , " mcl_core:cobble " , } ,
}
} )
2017-02-14 06:39:37 +03:00
2017-03-21 06:27:50 +03:00
-- Add entry aliases for the Help
if minetest.get_modpath ( " doc " ) then
doc.add_entry_alias ( " nodes " , " mcl_dispensers:dispenser " , " nodes " , " mcl_dispensers:dispenser_down " )
doc.add_entry_alias ( " nodes " , " mcl_dispensers:dispenser " , " nodes " , " mcl_dispensers:dispenser_up " )
end
2019-03-21 18:08:29 +03:00
2020-03-29 15:35:01 +03:00
-- Legacy
2019-03-21 18:08:29 +03:00
minetest.register_lbm ( {
2020-03-29 15:35:01 +03:00
label = " Update dispenser formspecs (0.60.0) " ,
name = " mcl_dispensers:update_formspecs_0_60_0 " ,
2019-03-21 18:08:29 +03:00
nodenames = { " mcl_dispensers:dispenser " , " mcl_dispensers:dispenser_down " , " mcl_dispensers:dispenser_up " } ,
action = function ( pos , node )
setup_dispenser ( pos )
minetest.log ( " action " , " [mcl_dispenser] Node formspec updated at " .. minetest.pos_to_string ( pos ) )
end ,
} )