- Modding Minecraft
- Preparing to Create Mods
- Your First Minecraft Mod
- Howling Blocks Mod
- Lightning Arrows Mod
- Next Steps
Lightning Arrows Mod
Let's step things up a notch and create a more exciting modone that will add "lightning strike" arrows to the game. When a player shoots an arrow, a bolt of lightning will strike where the arrow lands. (The lightning will strike even if the arrow drops to the ground.) Create a new file in the scriptcraft/plugins directory called lightning-arrows.js, and type the following code in the file:
var cmArrow = Packages.net.canarymod.api.entity.Arrow;
var cmPlayer = Packages.net.canarymod.api.entity.living.humanoid.Player; function strikeLightning(event){ var projectile = event.projectile; var shooter = event.owner; var location = projectile.location; if (projectile instanceof cmArrow && shooter instanceof cmPlayer){ location.world.makeLightningBolt(location); } } events.projectileHit(strikeLightning);
Save and close the file and then restart CanaryMod. Next, launch Minecraft and choose Multiplayer to connect to your CanaryMod server. Switch to Creative mode by typing gamemode creative at the in-game command prompt. Now select a bow from your inventory and shoot some arrows. A bolt of lightning should strike where each arrow lands.
The strikeLightning function only gets called when a projectile (which could be an arrow, snowball, or any other in-game item that can be thrown) strikes something. In the strikeLightning function, we check to see if the projectile is actually an arrow, and if the projectile's ownerthe shooteris a player; if so, lightning is summoned. This is important because skeletons can also shoot arrows, and giving such powers to mobs would be dangerous.
This mod makes heavy use of CanaryMod's API. For example, lightning is summoned using the world.makeLightningBolt() function (documented here). The CanaryMod API is a collection of functions that mod creators can call using either Java or JavaScript. Hundreds of different function calls can be made. You should browse around the CanaryMod API to learn more about it.