Forum


Notifications
Clear all

MCPE/Bedrock CPU Command Block Addon v0.0.1 Beta (Script)

McBedrock
(@mcbedrock1)
Illustrious Member Admin

CPU Command Block Addon v0.0.1 Beta (Script)

This addon is intended for developers, map makers, players and those willing to use Minecraft as an educational tool. It adds Numbered Blocks and a new kind of command block packed with many enhancements to the game. This new command block is called CPU and is capable of running multiple commands per line, parsing and executing mathematical expressions, reading and placing sequences of Numbered Blocks in the world, among other things. This addon requires the experimental Scripting API (currently only available for Windows 10).

Developer: Kodexky.
Twitter:@kodexky

How It Works:

Numbered Blocks:

These are orientation aware and will rotate themselves once placed in the world according to the player’s facing direction. They can be crafted by putting a Cobblestone block in a Stone-cutter. They can also be obtained using this command:

    /give @p cpu:number_0_west

Just replace the 0 with your desired number. If you need to rotate a number once placed just touch it (right click or short touch) with a Wooden Stick or with any Sword.

CPU Command Block:

CPU blocks can only be obtained using this command:

    /give @p cpu:cpu

CPUs have many more functions than the standard Command block. Once placed in the world the CPU will generate a name for itself. This name can be used later for relaying commands remotely to the CPU. When there is no other CPU in the world the 1st one will name itself “foo”. Every CPU after that will take an unique random name in the form “c-????”, where ???? will be replaced by hexadecimal characters (0-9, a-f). This initial name can be changed from the CPU interface.

CPUs can be activated and manipulated remotely or from their own User Interface (UI). You can enter the CPU UI the same way that you would use to rotate a number (touching it with a stick or a sword). To manipulate a CPU remotely you’ll have to use this command from either the Chat or another command block:

    /tag @e[type=cpu:cpu,name=CPUNAME] add “COMMAND”

You’ll have to replace CPUNAME and COMMAND as necessary.

The most basic enhancement offered by the CPU is the ability to sequentially execute multiple commands written on the same line. This is accomplished by separating all commands using two or more consecutive semi-colons (;;, ;;;, etc…). For example:

    /tag @e[type=cpu:cpu,name=foo] add “/say Hi! I’m a working CPU.;; /say Let me give you a present!;; /give @a minecraft:gold_block”

When issuing commands to a CPU from the chat or another normal command block, if you want to include a double-quote (“) as part of your command and not as a delimiter you’ll have to escape it. In other words, you must precede it with a backslash like this \”. This is not necessary when entering commands on the CPU UI.
    

CPU Commands Storage and Custom CPU Commands:

The CPU can store single commands and command sequences in multiple Command Slots. This system allows reusing them and the development of complex programs. Each Command Slot has its own name and can contain command sequences of up to 16384 characters. There is also a list of special commands that only the CPUs can understand.

To store commands in a slot from the chat or from other command blocks you should use this command:

    /tag @e[type=cpu:cpu,name=CPUNAME] add “/store SLOTNAME COMMANDSEQUENCE”

To delete a slot use this command:

    /tag @e[type=cpu:cpu,name=CPUNAME] add “/remove SLOTNAME”

To delete all the currently stored slots use this command:

    /tag @e[type=cpu:cpu,name=CPUNAME] add “/removeall”

To list all currently stored Slots use this command:

    /tag @e[type=cpu:cpu,name=CPUNAME] add “/list”

To execute the command sequence stored in a Slot use this command:

    /tag @e[type=cpu:cpu,name=CPUNAME] add “/exec SLOTNAME”

The ‘/exec’ command can execute any number of slots sequentially in this way: ‘/exec SLOTNAME0 SLOTNAME1 SLOTNAME2’.

The CPU names and Command Slots names share the same naming conventions. Names can contain up to 6 letters, numbers, hyphens (-) or underscores (_). If you wish to rename a CPU remotely use this command:

    /tag @e[type=cpu:cpu,name=CPUNAME] add “/rename NEWCPUNAME”

One extra improvement over standard command blocks is that CPUs cannot be destroyed by hitting them. To destroy a CPU you’ll have to enter its UI an click the button labeled “Kill me”. To avoid accidental destruction, this button will have to be clicked 3 times. If you want to destroy a CPU remotely you can use this command:

    /tag @e[type=cpu:cpu,name=CPUNAME] add “/killme”

Math Parser:

The CPU includes a math parser provided by the mathjs javascript library ( https://mathjs.org ) which allows it to execute complex operations. To perform an operation you’ll have to precede it with 2 slashes (//), and assign its result to a variable. To perform multiple operations on the same // command you’ll have to separate them using a single semi-colon. After that, you can use any variable value with other commands by preceding the variable name with this sequence of characters @=. Here is an example:

    /tag @e[type=cpu:cpu,name=foo] add “// a=2; b=3; c=a*b ;; /say The result of multiplying @=a by @=b is @=c”

The previous command will print this line to the chat:

    [foo]: The result of multiplying 2 by 3 is 6

All variable values are persistent and will remain declared between world loads. For this purpose the CPU uses a special Command Slot called ‘_scope_’. This Slot is not accessible from the UI, but can be read using the ‘/list’ command.

It’s important to mention that this addon uses a patched version of the mathjs library. In this version variables don’t need to be initialized before use. Any undeclared variable will have a default value of 0. This allows expression like ‘//a= a + 1’ to work without throwing an error. Keep in mind that this patch may cause some strange behaviors in the parser.

If you want to remotely clear all currently assigned variables you can use the command ‘/reset’:

    /tag @e[type=cpu:cpu,name=foo] add “/reset”

The ‘/removeall’ command also clears all currently assigned variables.

For more information about the operations syntax and available functions on mathjs please refer to “ https://mathjs.org/docs/expressions/syntax.html ” and “ https://mathjs.org/docs/reference/functions.html “.

Custom Math Parser Functions:

This addon also expands the mathjs parser with 4 custom functions that can be used in operations to enable interaction between the parser and the world. Let’s check them out.

pos(POSITION)
This function returns minecraft xyz location in an array ([x,y,z]). POSITION can be empty, in which case it will return the CPU location. POSITION can also be an string with a location in standard minecraft syntax (ie: “pos(‘~~~’)”, “pos(‘~ ~ ~’)”, “pos(‘~1~-1~1’)”, “pos(‘~1 0 0’)”, etc). For convenience POSITION can also be an array of either numbers or strings (ie: “pos([0,0,0])”, “pos([‘~’,’~1′,0])” ).

get(POSITION)
This function will return the value of a continuous Numbered Blocks sequence located at POSITION. It uses the same POSITION values available for the pos() function. The POSITION location should be the least significant number of the sequence (To read 2019 its POSITION should be the location of the 9 block). The orientation of the sequence will be taken from the block at POSITION, it doesn’t matter if the orientation of the other blocks in the sequence is different. This function will consider a sequence finished after finding the first non Numbered Block.

set(POSITION, VALUE, STYLE)
This function will place VALUE as a sequence of Numbered Blocks at POSITION. There must already be a Numbered Block at POSITION, for the orientation of the sequence will be the same of that block. The STYLE parameter is an optional string that allows to control decimals precision, whether the VALUE decimals will be rounded or truncated, and whether the new Numbered Blocks sequence length will be restricted to the one already existing in the world or if it will be unbounded.

The syntax of STYLE is as follows:

  • A numeric value from 0 to 16 for the decimals precision. If omitted defaults to showing as many decimals as the original VALUE.
  • The letter ‘r’ indicates the decimals will be (r)ounded to the precision amount. If omitted decimals will be truncated instead of rounded.
  • The letter ‘u’ indicates the Numbered Blocks sequence is (u)nbound and will be as long a needed to express the VALUE . If omitted the sequence length will be restricted to the length of the sequence already in place. When ‘u’ is omitted and VALUE is too big for the available space it will fill the sequence with the ‘-‘ character. When omitted and VALUE is smaller than the available space it will fill the remainder blocks with 0.

    //a=10.0005; set(‘~~1~’, a)        
    //a=10.0005; set(‘~~1~’, a)        
    //a=10.0005; set(‘~~1~’, a, ‘u’)   
    //a=10.0005; set(‘~~1~’, a, ‘u’)   
    //a=10.0005; set(‘~~1~’, a, ‘3r’)  
    //a=10.0005; set(‘~~1~’, a, 3)     
    //a=10.0005; set(‘~~1~’, a, 0)     

setblock(POSITION, BLOCKNAME, DATAVALUE)
This function works like the regular /setblock command. The only difference is that it can be used directly inside operations. There is a good example of the utility of this function in the Demo World provided in the downloads section of this article.

    //setblock(‘~~1~’,’emerald_block’);
    //loc=pos(‘~~1~’); block=’dirt’; setblock(loc, block);

 

Variable Substitution:

The CPU allows to dynamically replace variables for their values in commands using this syntax ‘@=VARNAME’. If VARNAME is an array with 3 values it will be recognized as a position and replaced with its 3 values separated by spaces.

    //a=’emerald_block’; b=pos(‘~~1~’);; /setblock @=b @=a;;  
    //a=[0,1,2];; /say Position @=a;;  

Inside operations, all variable naming conventions of the mathjs library are valid. At this point the substitutions parser can only recognize simple variables. Objects (ie: ‘a.x’), matrices/arrays with specific indexes (ie: ‘a[1]’) are not recognized. The substitution parser can recognize variable names made with these characters only: Any alphanumeric (a-z, A-Z, 0-9) or underscores (_). Any invalid character will make the substitution parser truncate the variable name at the last valid character and ignore the rest of it.

Redstone Signal Awareness:

CPUs are sensible to redstone signals in their immediate surroundings.

When a CPU receives a redstone signal it will execute the commands stored in the ‘_def’ slot. This default slot cannot be completely deleted. If you try to remove it, its contents will be erased but the slot will remain.

The redstone signal strength will be stored in a variable named ‘RS’ and can be used in operations.

There are some peculiarities for the way a CPU reacts to redstone. Right now CPU Blocks can only react to signals in direct contact with them (x+-1,y+-1,z+-1), they are sensible to all normal signal sources at those positions except for powered blocks (ie: a dirt block powered by a button), and Jukeboxes playing a disc.

CPUs can only read real signal strength in Redstone Wire, Pressure Plates and Daylight Detectors, all other sources will be read as a full signal (15), even for directly attached Comparators. The only way to read the real Comparator output signal is to place a redstone wire between the Comparator and the CPU.

CPU User Interface:

The CPU UI is pretty much self-explanatory. Due to the lack of full physical keyboard support in the addons experimental UI system, this addon features an On Screen Keyboard (OSK). When you click/touch any text field on the UI an editor dialog window showing the OSK will open. The OSK allows key auto-repeat by holding the click/touch on the key like a physical keyboard.

Currently there is a very limited physical keyboard support.
The ‘Esc’ key will exit from the editor window to the main UI window without saving any changes to the content. This key also closes the main CPU UI window.
The ‘Enter’ key will save the editor content and exit to the main CPU UI window.
The ‘Tab’ key will change the focus between the available text edit boxes.
The ‘Backspace’ key and the left and right arrow keys, work normally within the text boxes but lack the auto-repeat function.


    

Demo CPU Program Function:

This addon provides a Demo Program that can be used to create spheres. To test this CPU program you’ll have to enter this command to the chat:

    /function cpu/sphere

This command will setup the default CPU (‘foo’) with the necessary commands while deleting all previous Command Slots stored in that CPU. You can also test this program in the attached Demo World.

Future Improvements:

Please keep in mind that this is an early beta and a proof of concept. It’s my intention to keep improving this addon in the future. Here is a list of currently planned features and improvements:

  • Add a conditional variant of the /exec command capable of reacting to variables changes.
  • Add a /clone command to allow CPUs replication.
  • Add wildcards support to the /exec command.
  • Add a mobility feature to the CPU.
  • Add Repeat/Impulse functionality like the regular command block.
  • Add a ‘Delay in ticks’ control to the UI like the regular the command block.
  • Add in game help and documentation to the UI.
  • Make the CPU execution log persistent and active while outside the UI.
  • Tweak and enhance the UI activation method.
  • Add extra keys to the UI on screen keyboard.
  • Add full physical keyboard support to the UI.
  • Add extra math symbols as blocks.
  • Add debugging options.
  • Add extra parser commands to allow extended interaction with the world (read blocks and block states).
  • Enhance the variable substitution parser to allow object properties and array indexes.
  • Add the sphere demo program as a custom parser function.
  • Add extra WorldEdit functionality as custom parser functions.

Feedback and Support:

If you have a bug report, want to make a suggestion, have a feature request, or just want to provide feedback, please contact me at my twitter: @kodexky

Also, if you want to keep informed about changes and new releases of this addon please follow me there.

Changelog
  • Just fixed some typos in the description article. Nothing new in the addon itself.

Installation

  • Download the mcaddon file and open it.
  • Apply the pack to a world.
  • Enable “Experimental Gameplay” in the world settings.
  • Create the world.

CREATOR: KODEXKY (TWITTER)

Downloads

Quote
Topic starter Posted : 15/11/2019 10:37 pm
Share: