Howling Blocks Mod
One of the really cool things about Minecraft modding is the ability to "listen" for "events" in the game and change the normal rules of the game. An event is anything that happens in the gamefor example, a player breaks a block, an animal moves, lava flows, an arrow strikes something. It's possible for mod creators to write code that listens for these events and changes what happens when the events occur. In programming terms, this is called event-driven programming.
This next mod will change the game behavior so that whenever a block is broken it howls like a wolf. Create a new file called howling-blocks.js in the scriptcraft/plugins directory, and type the following code in the howling-blocks.js file:
var sounds = require('sounds'); function howl(event){ var block = event.block; var location = block.location; sounds.wolfHowl( location ); } events.blockDestroy( howl );
Save and close the file and then restart CanaryMod. Next, launch Minecraft and choose Multiplayer to connect to your CanaryMod server. You'll need to add a new server with the IP address localhost (that's a special address that refers to your own computer). Join the server and then break some blocks. When the blocks are broken, they should emit the sound of a wolf howling.
This simple mod illustrates how event-driven programming works. In the "howling blocks" code, we created a new function (a set of statements) that will be executed whenever the blockDestroy event occurs. We gave the function the name howl() and connected that function to the blockDestroy() event by using the following code:
events.blockDestroy( howl );
This instruction ensures that the howl() function will be executed whenever a block is destroyed. When called, the howl() function receives an event parameter from which it gets the location of the destroyed block, and then it plays a sound at that location. This mod makes use of ScriptCraft's API. The sounds module is just one of the many modules provided by ScriptCraft's API, which you can browse in the ScriptCraft API Reference.