Diese Seite auf Deutsch

Rewards and teleporters ​

Three programs that create things and move them: the guessing game pays out diamonds – the fewer tries, the more. An animal spawner fills the pen, but never too much. And a teleporter hub sends you to the farm, the mine or the top of the tower, depending on the pressure plate you step on.

Functions: SPAWN_ITEM, SPAWN_ENTITY, TELEPORT, and FIND_ENTITIES, GET_PRESSURE_PLATE, SAY

Needs an operator

These functions make things out of nothing or move them without a player, so they only work if the owner of the trigger block is an operator – in the world of the tutorials, you are one. On a server, the administrator can allow SPAWN_ITEM and SPAWN_ENTITY for everybody with allowSpawning=true, and TELEPORT of animals and items with allowTeleporting=true in config/c0de.properties. Players can only be teleported by programs of an operator; no setting allows that. Without the right, the program crashes with PERMISSION_DENIED. For everyone, a dispenser full of diamonds or spawn eggs does it: PRESS_BUTTON presses its button (exercise 1).

What you need

  • an empty sheet for each program
  • for the reward dispenser: a trigger block with a button, and your laptop
  • for the animal spawner: a second trigger block with a button, and a fenced pen, like in Watching the surroundings
  • for the teleporter hub: a timer block, three pressure plates with a sign each, and the positions of three places you want to reach
  • for the exercises: a dispenser full of diamonds with a stone button on top

The idea ​

SPAWN_ITEM lets 1 to 64 items appear on the ground at a position, SPAWN_ENTITY one entity, such as a sheep. Both return the UUID of what they created: a text that names this one entity in the whole world. TELEPORT moves an entity, given by its UUID, to a position – the UUIDs also come from FIND_ENTITIES.

How many diamonds a game is worth is a decision that needs no world. It gets a function of its own, which the program runner tests.

The prize ​

c0de
PROGRAM prizes
NOOP
FUNCTION prizeFor
    PARAMETER tries INTEGER
    RESULT prize INTEGER
BEGIN
    IF tries ≤ 10 ;
    BEGIN
        ASSIGN prize 1 ;
    END IF
    IF tries ≤ 7 ;
    BEGIN
        ASSIGN prize 3 ;
    END IF
    IF tries ≤ 5 ;
    BEGIN
        ASSIGN prize 5 ;
    END IF
END FUNCTION

Five tries or fewer give 5 diamonds, up to seven 3, up to ten 1, and more than ten nothing. The runner checks 6 tries (3), 4 tries (5) and 12 tries (0). The IFs go from the worst to the best result, so the last one that fits wins.

The reward dispenser ​

The guessing game of the lesson Guess the number, shorter, with the prize at the end.

c0de
PROGRAM rewardgame
NOOP
INCLUDE c0de/math
INCLUDE c0de/minecraft
NOOP
FUNCTION prizeFor
    PARAMETER tries INTEGER
    RESULT prize INTEGER
BEGIN
    IF tries ≤ 10 ;
    BEGIN
        ASSIGN prize 1 ;
    END IF
    IF tries ≤ 7 ;
    BEGIN
        ASSIGN prize 3 ;
    END IF
    IF tries ≤ 5 ;
    BEGIN
        ASSIGN prize 5 ;
    END IF
END FUNCTION
NOOP
FUNCTION play
BEGIN
    VARIABLE chance FLOAT
    VARIABLE secret INTEGER
    VARIABLE guess INTEGER
    VARIABLE tries INTEGER
    VARIABLE prize INTEGER
    VARIABLE spot STRUCT c0de/minecraft/POSITION
    CALL c0de/math/RANDOM
        RESULT result chance
    CALL c0de/math/INT
        PARAMETER value chance * 100 + 1 ;
        RESULT result secret
    CALL CLS
    CALL PRINT
        PARAMETER value "I am thinking of a number from 1 to 100." ;
    REPEAT
    BEGIN
        CALL INPUT
            PARAMETER variable guess ;
        ASSIGN tries tries + 1 ;
        IF guess < secret ;
        BEGIN
            CALL PRINT
                PARAMETER value guess ⊕ " is too low." ;
        END IF
        IF guess > secret ;
        BEGIN
            CALL PRINT
                PARAMETER value guess ⊕ " is too high." ;
        END IF
    END REPEAT guess ≠ secret ;
    CALL prizeFor
        PARAMETER tries tries ;
        RESULT prize prize
    CALL PRINT
        PARAMETER value "Right after " ⊕ tries ⊕ " tries! Prize: " ⊕ prize ⊕ " diamonds." ;
    IF prize = 0 ;
    BEGIN
        RETURN
    END IF
    ASSIGN spot.x PID.x ;
    ASSIGN spot.y PID.y + 1 ;
    ASSIGN spot.z PID.z ;
    CALL c0de/minecraft/SPAWN_ITEM
        PARAMETER position spot ;
        PARAMETER id "minecraft:diamond" ;
        PARAMETER count prize ;
END FUNCTION

Set the laptop and the trigger block to rewardgame and play, as in the lesson, and press the button. Guess the number on the laptop: when you find it, the laptop says what you've won, and the diamonds pop out on top of the trigger block.

  • SPAWN_ITEM needs a position, the item id and the count. count must be 1 to 64, so a prize of 0 returns before SPAWN_ITEM is called.
  • The UUID isn't needed here, so the CALL has no RESULT line, and the UUID is thrown away.

The animal spawner ​

c0de
PROGRAM animalspawner
NOOP
INCLUDE c0de/minecraft
NOOP
FUNCTION spawn
    PARAMETER animal STRING "minecraft:sheep"
    PARAMETER most INTEGER 8
BEGIN
    VARIABLE pen STRUCT c0de/minecraft/POSITION
    VARIABLE found ARRAY STRUCT c0de/minecraft/ENTITY
    VARIABLE count INTEGER
    ASSIGN pen.x PID.x ;
    ASSIGN pen.y PID.y ;
    ASSIGN pen.z PID.z + 5 ;
    CALL c0de/minecraft/FIND_ENTITIES
        PARAMETER position pen ;
        PARAMETER radius 4 ;
        PARAMETER type animal ;
        RESULT result found
    CALL LEN
        PARAMETER value found ;
        RESULT result count
    IF count ≥ most ;
    BEGIN
        CALL c0de/minecraft/SAY
            PARAMETER message "The pen is full: " ⊕ count ⊕ " animals." ;
        RETURN
    END IF
    CALL c0de/minecraft/SPAWN_ENTITY
        PARAMETER position pen ;
        PARAMETER type animal ;
    CALL c0de/minecraft/SAY
        PARAMETER message "Animal number " ⊕ ( count + 1 ) ⊕ " is in the pen." ;
END FUNCTION

Leave the trigger block of the reward dispenser where it is – exercise 1 needs it. Place a second trigger block with a button 5 blocks north of the center of the pen, like the timer block of the animal counter, set it to animalspawner and spawn, and press the button: a sheep appears in the middle of the pen. For pigs, put a STRING variable item animal with minecraft:pig into the trigger block.

the fenced pen with several sheep, one of them just appeared in the middle, the trigger block with its button north of the pen and the chat line "Animal number 4 is in the pen."
  • First count, then spawn. FIND_ENTITIES counts the animals of this type in the pen. At 8 or more, the program says that the pen is full, and spawns nothing.
  • SPAWN_ENTITY raises INVALID_ARGUMENT for a type that doesn't exist, such as minecraft:unicorn.

The teleporter hub ​

c0de
PROGRAM teleporterhub
NOOP
INCLUDE c0de/minecraft
NOOP
VARIABLE farm STRUCT c0de/minecraft/POSITION
BEGIN
    x 120
    y 64
    z 240
END VARIABLE
VARIABLE mine STRUCT c0de/minecraft/POSITION
BEGIN
    x -80
    y 12
    z 150
END VARIABLE
VARIABLE tower STRUCT c0de/minecraft/POSITION
BEGIN
    x 100
    y 90
    z 180
END VARIABLE
NOOP
FUNCTION send
    PARAMETER dx INTEGER
    PARAMETER place STRUCT c0de/minecraft/POSITION
BEGIN
    VARIABLE plate STRUCT c0de/minecraft/POSITION
    VARIABLE strength INTEGER
    VARIABLE players ARRAY STRUCT c0de/minecraft/ENTITY
    ASSIGN plate.x PID.x + dx ;
    ASSIGN plate.y PID.y ;
    ASSIGN plate.z PID.z ;
    CALL c0de/minecraft/GET_PRESSURE_PLATE
        PARAMETER position plate ;
        RESULT result strength
    IF strength = 0 ;
    BEGIN
        RETURN
    END IF
    CALL c0de/minecraft/FIND_ENTITIES
        PARAMETER position plate ;
        PARAMETER radius 1 ;
        PARAMETER type "minecraft:player" ;
        RESULT result players
    EACH player players
    BEGIN
        IF player.position.x = plate.x AND player.position.y = plate.y AND player.position.z = plate.z ;
        BEGIN
            CALL c0de/minecraft/TELEPORT
                PARAMETER uuid player.uuid ;
                PARAMETER position place ;
        END IF
    END EACH
END FUNCTION
NOOP
FUNCTION hub
BEGIN
    CALL send
        PARAMETER dx 2 ;
        PARAMETER place farm ;
    CALL send
        PARAMETER dx 4 ;
        PARAMETER place mine ;
    CALL send
        PARAMETER dx 6 ;
        PARAMETER place tower ;
END FUNCTION

Building and running it ​

  1. Go to each place you want to reach – the farm, the mine, the top of the tower –, press F3 and write down x, y and z of the block you stand on. Put them into the start values of farm, mine and tower, instead of the numbers of the example. Take the block where your feet are, and make sure it is air, with air above.
  2. Place the timer block, and three pressure plates on the ground 2, 4 and 6 blocks east of it. The plates keep a block away from the timer block and from each other.
  3. Set the timer block to teleporterhub and hub, with an interval of 10 ticks. Put a sign next to each plate with its destination.

Step on the first plate: half a second later, you stand on the farm.

the teleporter hub: three pressure plates in a row with signs "Farm", "Mine" and "Tower" behind them, the timer block at the west end
  • send handles one plate and its destination; hub calls it three times. A fourth plate is one more CALL and one more variable.
  • Who is on the plate? GET_PRESSURE_PLATE tells if anybody is; FIND_ENTITIES with radius 1 finds the players near the plate. Only those whose position is the plate's go: a player on the plate next to it is also within 1 block.
  • TELEPORT takes the uuid from the ENTITY and the destination. Players can only be teleported by an operator's program, see the box at the top.

Try it yourself ​

  1. Pay out the reward for everyone, without allowSpawning: from a dispenser full of diamonds, with a stone button on top.
Solution

Put the dispenser 2 blocks north of the trigger block, facing south, and the button on top of it. The program presses the button once per diamond. A stone button stays pressed for 20 ticks, so SLEEP waits 30 ticks before the next press.

c0de
PROGRAM dispensergame
NOOP
INCLUDE c0de/math
INCLUDE c0de/minecraft
INCLUDE c0de/time
NOOP
FUNCTION prizeFor
    PARAMETER tries INTEGER
    RESULT prize INTEGER
BEGIN
    IF tries ≤ 10 ;
    BEGIN
        ASSIGN prize 1 ;
    END IF
    IF tries ≤ 7 ;
    BEGIN
        ASSIGN prize 3 ;
    END IF
    IF tries ≤ 5 ;
    BEGIN
        ASSIGN prize 5 ;
    END IF
END FUNCTION
NOOP
FUNCTION play
BEGIN
    VARIABLE chance FLOAT
    VARIABLE secret INTEGER
    VARIABLE guess INTEGER
    VARIABLE tries INTEGER
    VARIABLE prize INTEGER
    VARIABLE spot STRUCT c0de/minecraft/POSITION
    CALL c0de/math/RANDOM
        RESULT result chance
    CALL c0de/math/INT
        PARAMETER value chance * 100 + 1 ;
        RESULT result secret
    CALL CLS
    CALL PRINT
        PARAMETER value "I am thinking of a number from 1 to 100." ;
    REPEAT
    BEGIN
        CALL INPUT
            PARAMETER variable guess ;
        ASSIGN tries tries + 1 ;
        IF guess < secret ;
        BEGIN
            CALL PRINT
                PARAMETER value guess ⊕ " is too low." ;
        END IF
        IF guess > secret ;
        BEGIN
            CALL PRINT
                PARAMETER value guess ⊕ " is too high." ;
        END IF
    END REPEAT guess ≠ secret ;
    CALL prizeFor
        PARAMETER tries tries ;
        RESULT prize prize
    CALL PRINT
        PARAMETER value "Right after " ⊕ tries ⊕ " tries! Prize: " ⊕ prize ⊕ " diamonds." ;
    IF prize = 0 ;
    BEGIN
        RETURN
    END IF
    ASSIGN spot.x PID.x ;
    ASSIGN spot.y PID.y + 1 ;
    ASSIGN spot.z PID.z - 2 ;
    FOR i INTEGER 1 ; prize ;
    BEGIN
        CALL c0de/minecraft/PRESS_BUTTON
            PARAMETER position spot ;
        CALL c0de/time/SLEEP
            PARAMETER ticks 30 ;
    END FOR
END FUNCTION
  1. Send animals through the teleporter too, for example to bring a cow to the farm.
Solution

Leave out PARAMETER type "minecraft:player" ; in send: then FIND_ENTITIES finds every entity near the plate. Stone pressure plates react to players and mobs, so a cow on the plate starts the transport. Without the type, items lying on the plate would be sent too – that's no harm.

Read more ​