#Randomized permutations

1 messages · Page 1 of 1 (latest)

tulip dragon
#

All my blocks with randomized permutations are broken. How would I recreate this in scripts? Haven't really messed with blocks in scripts before.

slate turret
# tulip dragon All my blocks with randomized permutations are broken. How would I recreate this...
// random numbers [0,1,2,3]
const randomNumber = Math.floor(Math.random()*4);

// random boolean [false, true]
const randomBoolean = Math.random() < 0.5 ? true : false;

// random strings
const values = ["a", "b", "c"]
const randomString = values[Math.floor(Math.random()*values.length)];

// Then change the permutation of the block with the value obtained
const state = block.permutation.withState("example:state", randomNumber);
block.setPermutation(state);
#

You can use this in your custom component

tulip dragon
#

How would I set up the custom component?

slate turret
#
import { world } from '@minecraft/server';

// Register your component with an identifier
world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('example:identifier', {
        
        // Choose which event you will use
        onPlace: e => {
            
            // Get the block
            const { block } = e;
            
            // Get a random value between 0 and 3
            const randomNumber = Math.floor(Math.random()*4);
            
            // Set the block state based on this value
            const state = block.permutation.withState("example:state", randomNumber);
            block.setPermutation(state);
        }
    });
});
#

Then add the custom component to your block's json file using the identifier you chose.

"minecraft:custom_components": [
  "example:identifier"
]
tulip dragon
slate turret
tulip dragon
slate turret
#

script

tulip dragon
#


// Register your component with an identifier
world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('myth:beer_random', {

    // Choose which event you will use
    onPlace: e => {

      // Get the block
      const { block } = e;

      // Get a random value between 0 and 3
      const randomNumber = Math.floor(Math.random() * 2);

      // Set the block state based on this value
      const state = block.permutation.withState("myth:beer_variant", randomNumber);
      block.setPermutation(state);
    }
  });
});```
slate turret
#

Can you send the block too?

#

The code seems correct, if there was an error setting the permutation it may be that it does not exist in the block or the value is not accepted.

slate turret
#

And in your script you used Math.floor(Math.random()*2), this gives you a random number between 0 and 2 but without including 2, use Math.floor(Math.random()*3) instead

tulip dragon
slate turret
tulip dragon
smoky nest
#

@slate turret how this works can I use it on custom logs

slate turret