You can make lots of projectile effects in Minecraft in addition to exploding. Rather than rebuild all the projectile code from scratch, you can abstract the projectile state-machine (make it a projectile state-machine for any effect, not just exploding) and create a library that helps you build a lot of different projectiles.
A library is a mod that lets you call functions from other mods.
To set up the projectile mod, copy a projectile mod. Follow these steps:
Click the Actions tab and then choose the Copy command.
You see two versions of the projectile mod.
Open one mod and rename it Projectile_Library.
Open the other mod and rename it Exploding_Projectile.
Here are the two renamed mods.
Open the mod that you renamed Projectile_Library. It looks like the code example shown here.
Making the Projectile_Library mod is a little tricky because it has to handle the following information:
All state information, like launching
The actual launching
Destruction of the projectile
But the Explosion_Projectile mod needs to handle
The type of block to launch
The actions that should happen when the block lands
Make one more copy of your mod and name it Original_Explosion_Projectile, just in case you mess up and need to get back to a working mod.
The following directions tell you how to actually make the changes in your code.
To make changes to the main function, follow these steps:
Rename the main function init.
Because this mod is now a library, the functions in it are called from other mods — though the library itself isn’t run in Minecraft.
Export this function (make it accessible from other mods) so that it can be accessed from the Explosion_Projectile mod. This is how the function should look.
You can find the export block under the Misc category.
Add a parameter named launch_block to the init function that defines the type of block to be launched, and save the parameter in a variable named block_type.
Delete the event.
This step is handled by the Exploding_Projectile mod. The library doesn’t handle it, because you may want other things to trigger the effects later on.
To name the exploding variable landing, click the drop-down arrow next to the word exploding and choose Rename Variable from the menu.
Renaming the variable is shown here.
Your init should look like this one.
After you complete this step list, you may want to make a minor change to the launch function. The image below shows how to change the bedrock block to the block_type variable instead; that way, the type of block is decided in the Explosion_Projectile mod.
The landing variable updates by itself when you rename it.
To make changes to the explode function, you first need to change it to a function that has a return value. The purpose of this function, in the library, is to let the other mod know whether the block has already been launched. Follow these steps:
Rename the function to check_if_launched.
This shows the new function block you have.
Make a new variable named launched and initialize (give it the value) it to false.
This variable should be returned to let the other mod know whether the block has been launched.
You need the if statement from the original explode function, but instead of sending a message and exploding and destroying anything around the block, just set the launched variable to true.
Export this function too so that the other mod can check to see whether the block has been launched.
You also need to update the destroy function to look like this. Follow these steps:
Add a parameter named block, which is the block that needs to be destroyed.
Change the info’s block to the parameter block.
Export the destroy function.
Congratulations! You have completed the Projectile_Library mod. The entire mod should look like the one shown here.
Figure 7-51After the library is written, you can define what happens in the Explosion_Projectile mod. First, open the Explosion_Projectile mod and import the Projectile_Library. Then follow these steps:
Grab an import block from the Misc category.
Type your LearnToMod nickname and then Projectile_Library. For example, if your LearnToMod nickname was sarah, your import block should look this.
Now you should make changes to the main function because you have the library that you just created and you need to call those new functions.
Remove everything from the main function, and delete all of the blocks except the event blocks. The two event blocks should go into a new function named on_land_event, but the function that should be called is a new function named on_land that has a parameter named info.
Look in the Functions category and you should see three new functions from the library you just made.
Add a call to the Projectile_Library.init function, with a parameter of Bedrock, and a call to the on_land_event function.
Finally, after you have fixed the main function, fill in the on_land function. Follow these steps:
Add an if-statement that calls the Projectile_Library.check_if_launched function.
Put the explosion code from the old explode function into the if-statement.
Make a call to the Projectile_Library.destroy function.
Congratulations! You have completed the code for the Explosion_Projectile mod! Everything else in the mod can be deleted, so your entire mod should look like this.
If you’ve followed along with all the steps in this project, you should now have two mods:
Projectile_Library
Explosion_Projectile: When you test the Explosion_Projectile mod, you see a scene like this one.
Congratulations! You have successfully created a projectile library and used it to make an exploding projectile.