The Video
explodeo.js
/*global require, server, Packages*/
var items = require("items"),
recipes = require("recipes"),
events = require("events");
var bow = items.bow(1),
tnt = items.tnt(1),
explodeBow = items.bow(1),
nuclearBow = items.bow(1),
cmArrow = Packages.net.canarymod.api.entity.Arrow,
EXPLOSIVE_YIELD = 2.5,
NUCLEAR_YIELD = 10;
explodeBow.setDisplayName("Bow of Exploding");
explodeBow.setLore("Excite. Very boom.");
var explodeBowRecipe = recipes.create({
result: explodeBow,
ingredients: {
B: bow,
T: tnt
},
shape: [
" ",
"BT ",
" "
]
});
server.addRecipe(explodeBowRecipe);
nuclearBow.setDisplayName("Nuclear Bow");
nuclearBow.setLore("The only way to win is to not play the game.");
var nuclearBowRecipe = recipes.create({
result: nuclearBow,
ingredients: {
B: bow,
T: tnt
},
shape: [
" ",
"TBT",
" "
]
});
server.addRecipe(nuclearBowRecipe);
function onArrowHit(event) {
var projectile = event.projectile,
world = projectile.world,
loc = projectile.location,
shooter = projectile.owner;
if (projectile instanceof cmArrow) {
if (shooter.inventory.itemInHand.getDisplayName() === "Bow of Exploding") {
projectile.destroy();
world.makeExplosion(shooter, loc, EXPLOSIVE_YIELD, true);
} else if (shooter.inventory.itemInHand.getDisplayName() === "Nuclear Bow") {
projectile.destroy();
world.makeExplosion(shooter, loc, NUCLEAR_YIELD, true);
}
}
}
events.projectileHit(onArrowHit);
The Documentation
In the video, I spoke about (and used!) some web pages in order to find out how to make my customized items. An “API”, as used below, is an “application programming interface” (wikipedia link) which is the set of modules and functions that computer programs give to programmers.
- The ScriptCraft API Documentation
- This has information about the modules, functions and objects that ScriptCraft makes available to you.
- There’s an incredible amount of stuff in there. Check it out!
- The CanaryMod documentation
- ScriptCraft provides an easy-to-use JavaScript API on top of CanaryMod
- Sometimes, you’ll find yourself interacting with CanaryMod objects.
- For example,
self
in ScriptCraft is a Human object- A Human object also gets capabilities from LivingBase
- LivingBase, in turn, gets more capabilities from Entity
- When you see methods (which is just the name of a function that is part of an object) like
getInventory
orsetInventory
, you can just useinventory
when referring to it. So, you can typeself.inventory
to make changes to your inventory. self.inventory
is the same as typingself.getInventory()
- The Young Person’s Guide to Programming in Minecraft is a tutorial about using ScriptCraft that covers many of the topics covered in the Merry Squid. Go here if you want another view on things covered in this class, or a refresher on something I’ve covered.
Docs, Copy/Paste and just playing around will get you far
I didn’t magically know how to make arrows explode. The scriptcraft/plugins/arrows.js
file gave me the key points and I used the ScriptCraft API documentation to learn about the recipes
modules so that I could create the Bow of Exploding. Don’t be afraid of copying code from one place and changing it around somewhere else. Also don’t be afraid of breaking stuff. Because computer software is so changeable experimentation is quick and the quickest way to learn!
Where To Go From Here
Try making custom recipes (how about a way to turn iron into gold?) or learning how to use other events to make the world react to things that happen within it.