Minecraft Modding: How to Make a Way to Win and Lose in Spleef - For Dummies

After your Minecraft mod has an arena to play in, you need to make a way for the player to win and lose. The easiest version of the goal for Spleef is this:

  • Win: You win if you stay on the diamond platform.

  • Lose: You lose if you fall through the platform.

It’s impossible to lose now because players don’t make blocks below them disappear. However, you can still code the logic for what happens if they fall through the platform.

Now you add in some basic logic to make sure that players start in the right place and know what to do when they respawn.

To set up the winning and losing conditions for Iteration 1 of Spleef, follow these steps:

  1. Add a call to the ArenaBuilder’s SetArenaCenter function at the end of the Arena function.

    The SetArenaCenter function will be in the Functions category of code because you imported the ArenaBuilder mod, and the SetArenaCenter function was exported so that you could use it.

    This step finds the center of the arena so that the player can be moved to the center for the start of the game. This shows the added call to SetArenaCenter in the Arena function.

    image0.jpg

    The SetArenaCenter not only finds the center of the arena but also creates a melon block in the arena. You use this melon block to start the game. The melon block is on the platform. Make sure you see it before moving on.

    image1.jpg
  2. Make a new function named StartGame that takes info as a parameter. It’s time to start the game when the player breaks the melon block.

    image2.jpg

    The special function StartGame is used for events. You call it as shown below.

    As you can see, the StartGame function is called only when a player breaks a block. So, every single time you break a block, this function runs.

    image3.jpg

    However, you should put players into Survival mode only when the block they break is the melon block. Luckily, the info parameter represents the block you broke.

    Variables are represented by a box consisting of two sections: name and data.

    image4.jpg

    Some variables are simple. As evidence, this shows a variable named num with the data 5.

    image5.jpg

    Here is a variable named name with the data Sarah.

    image6.jpg

    Some variables, on the other hand, are more complicated. For example, the image below shows the info parameter for an event function associated with a block (that is, the block_break event). You can see that the info parameter has a block for its data. The block has a type for its data, and the type’s data is Melon Block.

    The first two lines of code in the StartGame function follow the boxes seen below to find the type of melon block that was broken. Then you check to see whether the type that was broken was a melon block; if it was, you set the player’s gamemode to Survival so that they have to step on the blocks and, possibly, lose all health if they fall through the platform.

    image7.jpg
  3. Run the mod in Minecraft. When you break the melon block, you, the player, are put into Survival mode. Make sure the mod works before you continue.

  4. Set up the respawn event.

    A player who dies respawns to the center of the arena. This step is tricky: This shows the SetupPlayer function, which is called when the player respawns.

    image8.jpg

    This shows a way to call the SetupPlayer function when the player respawns.

    image9.jpg

    Take another look above. The JavaScript block is used to call the event PlayerRespawnEvent, which requires these three parameters:

    • The function to call when the player respawns.

    • The player who is respawning.

    • A true or false value that indicates whether this is a BedSpawn. (Use the false value because you don’t want to teleport back to your bed.)

    The SetupPlayer function gets data from the info parameter except that this time the info parameter is a player, not a block.

    This image shows how this data is retrieved.

    image10.jpg

    This is another way to get the same data. After you have the name of the player, you can set up the event to teleport the player to the center of the arena, but only two seconds (2,000 milliseconds) after the player has respawned.

    image11.jpg

    Events can be tricky. But you can always review badges you have already earned or ask questions on the LearnToMod forums if you need help.

  5. To test the mod, run it and break the melon block. When you do that, the gamemode should switch to Survival mode. Then break a diamond block and fall. Two seconds after you respawn, you’re placed into the arena again.

You now have a game! The problem is that unless you actually break a diamond block, you never lose.