2019-03-08 00:07:41 +00:00
local S = minetest.get_translator ( " mesecons_commandblock " )
2019-03-15 05:48:12 +00:00
local F = minetest.formspec_escape
2017-01-07 01:52:27 +00:00
2021-03-25 20:57:07 +00:00
local color_red = mcl_colors.RED
2021-03-25 21:07:38 +00:00
local command_blocks_activated = minetest.settings : get_bool ( " mcl_enable_commandblocks " , true )
2021-03-26 08:59:58 +00:00
local msg_not_activated = S ( " Command blocks are not enabled on this server " )
2021-03-25 21:07:38 +00:00
2017-01-07 01:52:27 +00:00
local function construct ( pos )
local meta = minetest.get_meta ( pos )
2017-01-07 02:11:17 +00:00
meta : set_string ( " commands " , " " )
2018-05-16 14:55:52 +00:00
meta : set_string ( " commander " , " " )
2017-01-07 01:52:27 +00:00
end
local function after_place ( pos , placer )
if placer then
local meta = minetest.get_meta ( pos )
2018-05-16 14:55:52 +00:00
meta : set_string ( " commander " , placer : get_player_name ( ) )
2017-01-07 01:52:27 +00:00
end
end
local function resolve_commands ( commands , pos )
local players = minetest.get_connected_players ( )
2018-05-16 15:07:11 +00:00
local meta = minetest.get_meta ( pos )
local commander = meta : get_string ( " commander " )
2018-05-16 15:31:01 +00:00
-- A non-printable character used while replacing “@@”.
local SUBSTITUTE_CHARACTER = ' \26 ' -- ASCII SUB
2017-01-07 01:52:27 +00:00
-- No players online: remove all commands containing
2018-05-16 15:31:01 +00:00
-- problematic placeholders.
2017-01-07 01:52:27 +00:00
if # players == 0 then
commands = commands : gsub ( " [^ \r \n ]+ " , function ( line )
2018-05-16 15:31:01 +00:00
line = line : gsub ( " @@ " , SUBSTITUTE_CHARACTER )
2018-05-16 15:58:32 +00:00
if line : find ( " @n " ) then return " " end
if line : find ( " @p " ) then return " " end
if line : find ( " @f " ) then return " " end
if line : find ( " @r " ) then return " " end
line = line : gsub ( " @c " , commander )
2018-05-16 15:31:01 +00:00
line = line : gsub ( SUBSTITUTE_CHARACTER , " @ " )
2017-01-07 01:52:27 +00:00
return line
end )
return commands
end
local nearest , farthest = nil , nil
local min_distance , max_distance = math.huge , - 1
for index , player in pairs ( players ) do
2019-02-01 05:33:07 +00:00
local distance = vector.distance ( pos , player : get_pos ( ) )
2017-01-07 01:52:27 +00:00
if distance < min_distance then
min_distance = distance
nearest = player : get_player_name ( )
end
if distance > max_distance then
max_distance = distance
farthest = player : get_player_name ( )
end
end
local random = players [ math.random ( # players ) ] : get_player_name ( )
2018-05-16 15:31:01 +00:00
commands = commands : gsub ( " @@ " , SUBSTITUTE_CHARACTER )
2018-05-16 15:58:32 +00:00
commands = commands : gsub ( " @p " , nearest )
commands = commands : gsub ( " @n " , nearest )
commands = commands : gsub ( " @f " , farthest )
commands = commands : gsub ( " @r " , random )
commands = commands : gsub ( " @c " , commander )
2018-05-16 15:31:01 +00:00
commands = commands : gsub ( SUBSTITUTE_CHARACTER , " @ " )
2017-01-07 01:52:27 +00:00
return commands
end
2017-12-13 19:16:32 +00:00
local function check_commands ( commands , player_name )
2017-05-23 22:20:25 +00:00
for _ , command in pairs ( commands : split ( " \n " ) ) do
local pos = command : find ( " " )
local cmd , param = command , " "
if pos then
cmd = command : sub ( 1 , pos - 1 )
end
local cmddef = minetest.chatcommands [ cmd ]
if not cmddef then
-- Invalid chat command
2019-03-16 01:00:48 +00:00
local msg = S ( " Error: The command “@1” does not exist; your command block has not been changed. Use the “help” chat command for a list of available commands. " , cmd )
2017-12-13 19:16:32 +00:00
if string.sub ( cmd , 1 , 1 ) == " / " then
2019-03-16 01:00:48 +00:00
msg = S ( " Error: The command “@1” does not exist; your command block has not been changed. Use the “help” chat command for a list of available commands. Hint: Try to remove the leading slash. " , cmd )
2017-12-13 19:16:32 +00:00
end
2021-03-25 20:57:07 +00:00
return false , minetest.colorize ( color_red , msg )
2017-12-13 19:16:32 +00:00
end
if player_name then
local player_privs = minetest.get_player_privs ( player_name )
for cmd_priv , _ in pairs ( cmddef.privs ) do
if player_privs [ cmd_priv ] ~= true then
2019-03-16 01:00:48 +00:00
local msg = S ( " Error: You have insufficient privileges to use the command “@1” (missing privilege: @2)! The command block has not been changed. " , cmd , cmd_priv )
2021-03-25 20:57:07 +00:00
return false , minetest.colorize ( color_red , msg )
2017-12-13 19:16:32 +00:00
end
end
2017-05-23 22:20:25 +00:00
end
end
return true
end
2017-01-07 01:52:27 +00:00
local function commandblock_action_on ( pos , node )
if node.name ~= " mesecons_commandblock:commandblock_off " then
return
end
2021-03-25 21:13:37 +00:00
2017-01-07 01:52:27 +00:00
local meta = minetest.get_meta ( pos )
2021-03-25 21:13:37 +00:00
local commander = meta : get_string ( " commander " )
if not command_blocks_activated then
2021-03-26 09:00:48 +00:00
--minetest.chat_send_player(commander, msg_not_activated)
2021-03-25 21:13:37 +00:00
return
end
minetest.swap_node ( pos , { name = " mesecons_commandblock:commandblock_on " } )
2017-01-07 01:52:27 +00:00
local commands = resolve_commands ( meta : get_string ( " commands " ) , pos )
for _ , command in pairs ( commands : split ( " \n " ) ) do
2018-05-16 14:55:52 +00:00
local cpos = command : find ( " " )
2017-01-07 01:52:27 +00:00
local cmd , param = command , " "
2018-05-16 14:55:52 +00:00
if cpos then
cmd = command : sub ( 1 , cpos - 1 )
param = command : sub ( cpos + 1 )
2017-01-07 01:52:27 +00:00
end
local cmddef = minetest.chatcommands [ cmd ]
if not cmddef then
2017-05-23 20:57:42 +00:00
-- Invalid chat command
2017-01-07 01:52:27 +00:00
return
end
2018-05-16 14:55:52 +00:00
-- Execute command in the name of commander
cmddef.func ( commander , param )
2017-01-07 01:52:27 +00:00
end
end
local function commandblock_action_off ( pos , node )
if node.name == " mesecons_commandblock:commandblock_on " then
minetest.swap_node ( pos , { name = " mesecons_commandblock:commandblock_off " } )
end
end
2017-05-23 20:57:42 +00:00
local on_rightclick = function ( pos , node , player , itemstack , pointed_thing )
2021-03-25 21:13:37 +00:00
if not command_blocks_activated then
minetest.chat_send_player ( player : get_player_name ( ) , msg_not_activated )
return
end
2019-03-15 05:48:12 +00:00
local can_edit = true
-- Only allow write access in Creative Mode
2020-07-10 14:08:40 +00:00
if not minetest.is_creative_enabled ( player : get_player_name ( ) ) then
2019-03-15 05:48:12 +00:00
can_edit = false
2017-05-23 20:57:42 +00:00
end
2019-02-08 20:59:01 +00:00
local pname = player : get_player_name ( )
if minetest.is_protected ( pos , pname ) then
2019-03-15 05:48:12 +00:00
can_edit = false
2019-02-08 20:59:01 +00:00
end
local privs = minetest.get_player_privs ( pname )
2017-12-12 23:30:23 +00:00
if not privs.maphack then
2019-03-15 05:48:12 +00:00
can_edit = false
2017-12-12 23:30:23 +00:00
end
2017-05-23 20:57:42 +00:00
2017-01-07 01:52:27 +00:00
local meta = minetest.get_meta ( pos )
2017-05-23 20:57:42 +00:00
local commands = meta : get_string ( " commands " )
2019-03-16 02:09:51 +00:00
if not commands then
commands = " "
end
2018-05-16 14:55:52 +00:00
local commander = meta : get_string ( " commander " )
local commanderstr
if commander == " " or commander == nil then
2019-03-15 05:48:12 +00:00
commanderstr = S ( " Error: No commander! Block must be replaced. " )
else
commanderstr = S ( " Commander: @1 " , commander )
end
2020-03-10 23:54:06 +00:00
local textarea_name , submit , textarea
2019-03-15 05:48:12 +00:00
-- If editing is not allowed, only allow read-only access.
-- Player can still view the contents of the command block.
if can_edit then
textarea_name = " commands "
2020-03-10 04:17:53 +00:00
submit = " button_exit[3.3,4.4;2,1;submit; " .. F ( S ( " Submit " ) ) .. " ] "
2019-03-15 05:48:12 +00:00
else
textarea_name = " "
submit = " "
end
2019-03-16 02:19:54 +00:00
if not can_edit and commands == " " then
2019-03-15 05:48:12 +00:00
textarea = " label[0.5,0.5; " .. F ( S ( " No commands. " ) ) .. " ] "
2018-05-16 14:55:52 +00:00
else
2019-03-15 05:48:12 +00:00
textarea = " textarea[0.5,0.5;8.5,4; " .. textarea_name .. " ; " .. F ( S ( " Commands: " ) ) .. " ; " .. F ( commands ) .. " ] "
2018-05-16 14:55:52 +00:00
end
2020-03-10 04:01:49 +00:00
local formspec = " size[9,5;] " ..
2019-03-15 05:48:12 +00:00
textarea ..
submit ..
2020-03-10 04:17:53 +00:00
" image_button[8,4.4;1,1;doc_button_icon_lores.png;doc;] " ..
2019-03-15 05:48:12 +00:00
" tooltip[doc; " .. F ( S ( " Help " ) ) .. " ] " ..
" label[0,4; " .. F ( commanderstr ) .. " ] "
2019-02-08 20:59:01 +00:00
minetest.show_formspec ( pname , " commandblock_ " .. pos.x .. " _ " .. pos.y .. " _ " .. pos.z , formspec )
2017-01-07 01:52:27 +00:00
end
2017-12-12 23:30:23 +00:00
local on_place = function ( itemstack , placer , pointed_thing )
if pointed_thing.type ~= " node " then
return itemstack
end
-- Use pointed node's on_rightclick function first, if present
local node = minetest.get_node ( pointed_thing.under )
if placer and not placer : get_player_control ( ) . sneak then
if minetest.registered_nodes [ node.name ] and minetest.registered_nodes [ node.name ] . on_rightclick then
return minetest.registered_nodes [ node.name ] . on_rightclick ( pointed_thing.under , node , placer , itemstack ) or itemstack
end
end
local privs = minetest.get_player_privs ( placer : get_player_name ( ) )
if not privs.maphack then
2019-03-15 05:48:12 +00:00
minetest.chat_send_player ( placer : get_player_name ( ) , S ( " Placement denied. You need the “maphack” privilege to place command blocks. " ) )
2017-12-12 23:30:23 +00:00
return itemstack
end
return minetest.item_place_node ( itemstack , placer , pointed_thing )
end
2017-01-07 01:52:27 +00:00
minetest.register_node ( " mesecons_commandblock:commandblock_off " , {
2019-03-08 00:07:41 +00:00
description = S ( " Command Block " ) ,
2017-03-11 02:38:08 +00:00
2020-02-19 03:54:17 +00:00
_tt_help = S ( " Executes server commands when powered by redstone power " ) ,
2017-03-11 02:38:08 +00:00
_doc_items_longdesc =
2019-03-08 00:07:41 +00:00
S ( " Command blocks are mighty redstone components which are able to alter reality itself. In other words, they cause the server to execute server commands when they are supplied with redstone power. " ) ,
2017-03-11 02:38:08 +00:00
_doc_items_usagehelp =
2019-03-15 05:48:12 +00:00
S ( " Everyone can activate a command block and look at its commands, but not everyone can edit and place them. " ) .. " \n \n " ..
S ( " To view the commands in a command block, use it. To activate the command block, just supply it with redstone power. This will execute the commands once. To execute the commands again, turn the redstone power off and on again. " ) ..
" \n \n " ..
2017-05-23 21:59:08 +00:00
2020-07-23 20:43:16 +00:00
S ( " To be able to place a command block and change the commands, you need to be in Creative Mode and must have the “maphack” privilege. A new command block does not have any commands and does nothing. Use the command block (in Creative Mode!) to edit its commands. Read the help entry “Advanced usage > Server Commands” to understand how commands work. Each line contains a single command. You enter them like you would in the console, but without the leading slash. The commands will be executed from top to bottom. " ) .. " \n \n " ..
2017-03-11 02:38:08 +00:00
2019-03-08 00:07:41 +00:00
S ( " All commands will be executed on behalf of the player who placed the command block, as if the player typed in the commands. This player is said to be the “commander” of the block. " ) .. " \n \n " ..
2018-05-16 14:55:52 +00:00
2019-03-16 01:00:48 +00:00
S ( " Command blocks support placeholders, insert one of these placeholders and they will be replaced by some other text: " ) .. " \n " ..
2019-03-25 11:05:57 +00:00
S ( " • “@@c”: commander of this command block " ) .. " \n " ..
S ( " • “@@n” or “@@p”: nearest player from the command block " ) .. " \n " ..
S ( " • “@@f” farthest player from the command block " ) .. " \n " ..
S ( " • “@@r”: random player currently in the world " ) .. " \n " ..
S ( " • “@@@@”: literal “@@” sign " ) .. " \n \n " ..
2018-01-02 13:25:31 +00:00
2019-03-08 00:07:41 +00:00
S ( " Example 1: \n time 12000 \n Sets the game clock to 12:00 " ) .. " \n \n " ..
2018-01-02 13:25:31 +00:00
2019-03-25 11:32:59 +00:00
S ( " Example 2: \n give @@n mcl_core:apple 5 \n Gives the nearest player 5 apples " ) ,
2017-03-11 02:38:08 +00:00
2017-02-10 17:30:04 +00:00
tiles = { { name = " jeija_commandblock_off.png " , animation = { type = " vertical_frames " , aspect_w = 32 , aspect_h = 32 , length = 2 } } } ,
2017-12-12 23:30:23 +00:00
groups = { creative_breakable = 1 , mesecon_effector_off = 1 } ,
2017-01-07 02:11:17 +00:00
drop = " " ,
on_blast = function ( ) end ,
2017-01-07 01:52:27 +00:00
on_construct = construct ,
2017-01-07 02:11:17 +00:00
is_ground_content = false ,
2017-12-12 23:30:23 +00:00
on_place = on_place ,
2017-01-07 01:52:27 +00:00
after_place_node = after_place ,
2017-05-23 20:57:42 +00:00
on_rightclick = on_rightclick ,
2017-02-11 17:46:23 +00:00
sounds = mcl_sounds.node_sound_stone_defaults ( ) ,
2017-01-07 01:52:27 +00:00
mesecons = { effector = {
2017-09-14 00:20:47 +00:00
action_on = commandblock_action_on ,
rules = mesecon.rules . alldirs ,
2017-01-07 01:52:27 +00:00
} } ,
2020-04-17 19:40:13 +00:00
_mcl_blast_resistance = 3600000 ,
2017-05-23 21:10:32 +00:00
_mcl_hardness = - 1 ,
2017-01-07 01:52:27 +00:00
} )
minetest.register_node ( " mesecons_commandblock:commandblock_on " , {
2017-02-10 17:30:04 +00:00
tiles = { { name = " jeija_commandblock_off.png " , animation = { type = " vertical_frames " , aspect_w = 32 , aspect_h = 32 , length = 2 } } } ,
2017-02-27 17:32:35 +00:00
groups = { creative_breakable = 1 , mesecon_effector_on = 1 , not_in_creative_inventory = 1 } ,
2017-01-07 02:11:17 +00:00
drop = " " ,
on_blast = function ( ) end ,
2017-01-07 01:52:27 +00:00
on_construct = construct ,
2017-01-07 02:11:17 +00:00
is_ground_content = false ,
2017-12-12 23:30:23 +00:00
on_place = on_place ,
2017-01-07 01:52:27 +00:00
after_place_node = after_place ,
2017-05-23 20:57:42 +00:00
on_rightclick = on_rightclick ,
2017-02-11 17:46:23 +00:00
sounds = mcl_sounds.node_sound_stone_defaults ( ) ,
2017-01-07 01:52:27 +00:00
mesecons = { effector = {
2017-09-14 00:20:47 +00:00
action_off = commandblock_action_off ,
rules = mesecon.rules . alldirs ,
2017-02-22 15:22:28 +00:00
} } ,
2020-04-17 19:40:13 +00:00
_mcl_blast_resistance = 3600000 ,
2017-05-23 21:10:32 +00:00
_mcl_hardness = - 1 ,
2017-01-07 01:52:27 +00:00
} )
2017-03-21 03:27:50 +00:00
2017-05-23 20:57:42 +00:00
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
if string.sub ( formname , 1 , 13 ) == " commandblock_ " then
2020-03-10 04:13:32 +00:00
if fields.doc and minetest.get_modpath ( " doc " ) then
doc.show_entry ( player : get_player_name ( ) , " nodes " , " mesecons_commandblock:commandblock_off " , true )
2017-07-25 03:14:50 +00:00
return
end
2020-03-10 04:13:32 +00:00
if ( not fields.submit and not fields.key_enter ) or ( not fields.commands ) then
return
end
2017-12-12 23:30:23 +00:00
local privs = minetest.get_player_privs ( player : get_player_name ( ) )
if not privs.maphack then
2019-03-15 05:48:12 +00:00
minetest.chat_send_player ( player : get_player_name ( ) , S ( " Access denied. You need the “maphack” privilege to edit command blocks. " ) )
2017-12-12 23:30:23 +00:00
return
end
2017-05-23 20:57:42 +00:00
local index , _ , x , y , z = string.find ( formname , " commandblock_(-?%d+)_(-?%d+)_(-?%d+) " )
if index ~= nil and x ~= nil and y ~= nil and z ~= nil then
local pos = { x = tonumber ( x ) , y = tonumber ( y ) , z = tonumber ( z ) }
local meta = minetest.get_meta ( pos )
2020-07-10 14:08:40 +00:00
if not minetest.is_creative_enabled ( player : get_player_name ( ) ) then
2019-03-15 05:48:12 +00:00
minetest.chat_send_player ( player : get_player_name ( ) , S ( " Editing the command block has failed! You can only change the command block in Creative Mode! " ) )
2017-05-23 20:57:42 +00:00
return
end
2017-12-13 19:16:32 +00:00
local check , error_message = check_commands ( fields.commands , player : get_player_name ( ) )
2017-05-23 22:20:25 +00:00
if check == false then
2017-12-13 19:16:32 +00:00
-- Command block rejected
minetest.chat_send_player ( player : get_player_name ( ) , error_message )
return
else
meta : set_string ( " commands " , fields.commands )
2017-05-23 22:20:25 +00:00
end
2017-05-23 20:57:42 +00:00
else
2019-03-15 05:48:12 +00:00
minetest.chat_send_player ( player : get_player_name ( ) , S ( " Editing the command block has failed! The command block is gone. " ) )
2017-05-23 20:57:42 +00:00
end
end
end )
2017-03-21 03:27:50 +00:00
-- Add entry alias for the Help
if minetest.get_modpath ( " doc " ) then
doc.add_entry_alias ( " nodes " , " mesecons_commandblock:commandblock_off " , " nodes " , " mesecons_commandblock:commandblock_on " )
end