How to Make a Projectile Library for Minecraft Modding - For Dummies

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.

Set up the projectile mods

To set up the projectile mod, copy a projectile mod. Follow these steps:

  1. Click the Actions tab and then choose the Copy command.

    image0.jpg

    You see two versions of the projectile mod.

    image1.jpg
  2. Open one mod and rename it Projectile_Library.

    image2.jpg
  3. Open the other mod and rename it Exploding_Projectile.

    image3.jpg

    Here are the two renamed mods.

    image4.jpg

Outline the projectile library and explosion projectile mods

Open the mod that you renamed Projectile_Library. It looks like the code example shown here.

image5.jpg

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.

Change the main and launch functions

The following directions tell you how to actually make the changes in your code.

To make changes to the main function, follow these steps:

  1. 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.

  2. 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.

    image6.jpg

    You can find the export block under the Misc category.

    image7.jpg
  3. 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.

    image8.jpg
  4. 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.

    image9.jpg
  5. To name the exploding variable landing, click the drop-down arrow next to the word exploding and choose Rename Variable from the menu.

    image10.jpg

Renaming the variable is shown here.

image11.jpg

Your init should look like this one.

image12.jpg

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.

image13.jpg

The landing variable updates by itself when you rename it.

Change the explode function

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:

  1. Rename the function to check_if_launched.

    This shows the new function block you have.

    image14.jpg
  2. Make a new variable named launched and initialize (give it the value) it to false.

    image15.jpg

    This variable should be returned to let the other mod know whether the block has been launched.

  3. 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.

    image16.jpg
  4. Export this function too so that the other mod can check to see whether the block has been launched.

    image17.jpg

Change the destroy function

You also need to update the destroy function to look like this. Follow these steps:

image18.jpg
  1. Add a parameter named block, which is the block that needs to be destroyed.

  2. Change the info’s block to the parameter block.

  3. Export the destroy function.

Congratulations! You have completed the Projectile_Library mod. The entire mod should look like the one shown here.

Figure 7-51Figure 7-51

Change the Explosion_Projectile mod

After 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:

  1. Grab an import block from the Misc category.

    image20.jpg
  2. Type your LearnToMod nickname and then Projectile_Library. For example, if your LearnToMod nickname was sarah, your import block should look this.

    image21.jpg

    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.

  3. 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.

    image22.jpg
  4. Look in the Functions category and you should see three new functions from the library you just made.

    image23.jpg
  5. Add a call to the Projectile_Library.init function, with a parameter of Bedrock, and a call to the on_land_event function.

    image24.jpg

Finally, after you have fixed the main function, fill in the on_land function. Follow these steps:

  1. Add an if-statement that calls the Projectile_Library.check_if_launched function.

    image25.jpg
  2. Put the explosion code from the old explode function into the if-statement.

    image26.jpg
  3. Make a call to the Projectile_Library.destroy function.

    image27.jpg

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.

image28.jpg

Test your new library

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.

    image29.jpg

Congratulations! You have successfully created a projectile library and used it to make an exploding projectile.