[ShellVent] Functions & commandblocks

Discussion in 'General Minecraft Discussion' started by ShelLuser, Jun 29, 2017.

  1. Hi gang!

    One of the reasons why I'm (still) quite excited about Minecraft 1.12 is the massive improvement in customization which Mojang gave us. In this case I'm specifically talking about functions and how those can seriously enhance your singleplayer worlds (and servers alike of course).

    Now... Several people online, both on the Minecraft forum as well on Youtube, seem to have gained this weird idea that the introduction of these functions is surely going to spell the end for the command blocks. An opinion which I personally consider to be ridiculous and every time I come across such arguments I can't help wonder if the author was simply looking for a catchy headline (to attract more viewers (and thus potentially more advertisement income)) or... if they simply don't know as much about the topic they're addressing as they like to pretend.

    I vented my opinion here and there, but because EMC is my Minecraft home I'm obviously saving the best example for the best place :cool:

    What is a function?

    A function is a text file which contains one or more Minecraft commands. You can make these yourself and place them in your world save folder after which they will become available within the game. Executing a function is done with the /function command.

    The main advantage is that you can put multiple commands into one single function, and then execute all of those commands with one /function command. This is a big change between command blocks because a command block can basically contain 1 command per block.

    And there's more... A Minecraft world consists of chunks; a chunk is a 16x16 area and it's basically the smallest part of a world. Press F3 and look in the upper left corner: you'll notice mentioning of so called chunk updates. The problem though is that not every chunk of a world gets loaded. That would be virtually impossible considering the gigantic size of a regular Minecraft world. Instead Minecraft will load and unload chunks when you move through the world. And here lies a problem... Because as soon as a chunk which contains several command blocks unloads then those command blocks will also stop being active.

    This is quite in contrary to a function... A function gets loaded into memory and it'll stay there. So no matter where you are in the world you'll always have access to those function(s).

    The borders of a chunk

    So, in all fairness, even though I consider those ideas about "The end of the commandblock as we know it" to be utter nonsense it is (a bit) understandable where it came from. After all: there are several advantages... Heck, you can even run a function as if it was a repeating command block, all you need is to set /gamerule gameLoopFunction.

    But the thing is... If you messed with command blocks yourself then you should know better.

    So let's set up a demonstration and test this out for ourselves!

    Taking apart a woodland mansion


    One of the things which I really like about Mojang is that they don't only give us several gizmo's to play with (such as functions, structure blocks, advancements, etc.). No: they're also using those themselves in their own game. That has always made these things special to me: it's not just a thing for us players, it's a thing to enhance on the entire game by everyone (including Mojang).

    And a good example of that is shown above. Here I'm setting up an experiment and I'm loading in all the different parts of a woodland mansion. What you see here are basically several parts which, when formed together, can make up for one room. Some rooms are even known as secret rooms within the mansion.

    To keep things clear for myself I've placed a concrete ring around each part. Gray rings indicate a 1x1_aX structure (X being 1 to 5), a light gray ring indicates a 1x1_asX structure (X being 1 to 4) and right now (time of writing) I just started working on the 1x1_bX section (X being 1 to 5 again) surrounded by a brown ring.


    Now, this is pretty easy if you're only placing 1 or 3 structure. But I've been working with 12 structures so far and I got 61 more to examine :eek:

    Enter the functions...

    So I did something different... Instead of digging out a ring every time myself I made a function to do that for me, called data\functions\catslair\mansion\1x1b.mcfunction (I use multiple).

    It places a separation line (made of iron blocks), then places the concrete ring:

    Code:
    ## Creates a 9x9 concrete ring & adds iron separator
    # Stand on the strucure block to make this work! => x3, y0, z0
    fill ~2 ~-2 ~-1 ~10 ~-2 ~-1 minecraft:concrete 12
    fill ~10 ~-2 ~-1 ~10 ~-2 ~7 minecraft:concrete 12
    fill ~10 ~-2 ~7 ~2 ~-2 ~7 minecraft:concrete 12
    fill ~2 ~-2 ~7 ~2 ~-2 ~-1 minecraft:concrete 12
    # Add iron separator
    fill ~2 ~-2 ~-2 ~10 ~-2 ~-2 minecraft:iron_block
    
    I know I could have shortened this to only 3 fill commands (first fill the whole area with concrete, then fill the middle square with sandstone again) but I'm not doing that because I plan on using these functions again later in combination with other existing floor designs (to build an actual customized woodland mansion).

    These 4 commands ensure that only required changes are made, and nothing else.


    All good, right? Well, no... ;)

    See: first I place a structure block, then I open its interface and paste in the structure name (and change it), then I change the rest of the values (as shown above: 3 for x and 0 for y). I click load, check the size and when it's all good I open the interface again so that I can click load once more to add the structure.

    The function gets started by pressing t, cursor up (brings up my last command) and then I run the function. But what if I had to perform some other commands in the mean time? What if a friend comes online and I start chatting? /function catslair:mansion/1x1b gets tedious to type after a while.

    Or... The biggest issue with functions: what if a friend comes over to help you and you don't want to give them OP status on your game or server? Then they don't get to use the /function command themselves...

    If only we had a block of some sort which could execute these commands for us.... :confused:

    Now wait a second! :D

    Command block advantages...


    Here I have 2 repeating command blocks which are only active when powered by redstone. And to make it easier on me to see if this setup is active or not I also added a lamp ;)

    The bottom command block does most of the important work, it checks if a nearby player is standing on a structure (load) block and if so it'll start the function I mentioned earlier:

    Code:
    execute @p[tag=!done] ~ ~ ~ detect ~ ~-1 ~ minecraft:structure_block 1 function catslair:mansion/1x1b
    
    The chain command block behind it is set to optional, meaning that it will only get executed once the main command block is actually triggered. That one contains this command:

    Code:
    scoreboard players tag @p add done
    
    What this does is to ensure that my function will only get called once. It gives me the 'done' tag, and the first command block only executes the function when that tag is not present. This is important because otherwise this routine would simply continue filling in blocks as long as I'm standing on a structure block.

    So how to reset this? Elementary my dear reader, that's what the top command block does:

    Code:
    execute @p[tag=done] ~ ~ ~ detect ~ ~-1 ~ minecraft:sandstone 0 scoreboard players tag @s remove done
    
    Here I detect if a nearby player who has the done tag is standing on sandstone. Considering that my whole redstone world is build of sandstone that is a very safe assumption to make ;) When that happens it'll remove the done tag. So as soon as I step on a new structure block the function gets called once more. This is what it all looks like:


    Now, on a server I would probably have added a few more fail saves (keep that in mind if you're going to try this out yourself!) but this works perfectly in single player.

    While still having function advantages

    See... In my setup I'll have to head back to my command block section as soon as I want to make it place a different type of ring. That was by choice. But I could also set it up so that it would call one specific function ("addring" for example) and then edit this function whenever I'm working on a new section. Then all I have to do is use /reload to reload all functions, advancements and loot tables and then I'd be ready for the next batch.

    Concluding...

    Functions are not going to make command blocks obsolete. Far from it: they're going to seriously enhance on what we can do with command blocks instead.