Minecraft Modding: How to Make a Way to Win and Lose in Spleef
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:
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.
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.
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.
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.
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.
Some variables are simple. As evidence, this shows a variable named num with the data 5.
Here is a variable named name with the data Sarah.
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.
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.
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.
This shows a way to call the SetupPlayer function when the player respawns.
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.
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.
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.
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.