Callback functions for the ModPE API. These functions are used to execute other code when certain game events occur.
Methods
-
attackHook(
attacker, victim) -
Callback function that is called when an entity is attacked.
Name Type Description attackerlong the attacker's native entity ID
victimlong the victim's native entity ID
Example
Credit: Connor4898
// who is attacking who? function attackHook(attacker, victim) { if (attacker == Player.getEntity()) { clientMessage("Sir, you shouldn't attack others."); } else { clientMessage("Sir, you are under attack!"); } } -
blockEventHook(
x, y, z, type, data) -
Callback function that is called when a chest is opened or closed.
Name Type Description xint the chest's x coordiante
yint the chest's y coordiante
zint the chest's z coordiante
typeint the event type (seems to always be 1)
dataint the open/close data (0 = closing, 1 = open)
Example
// Announce the opening of a chest function blockEventHook(x, y, z, eventType, data) { if (data === 1) { clientMessage("A chest was opened at location " + x + " : " + y + " : " + z); } else if (data === 0) { clientMessage("The chest has been closed"); } } -
chatHook(
str) -
Callback function that is called when a chat message is sent.
Name Type Description strstring the message text
Example
// repeat after me function chatHook(str) { clientMessage(ChatColor.RED + str); } -
chatReceiveHook(
str, sender) -
Callback function that is called when a chat message is received.
Name Type Description strstring the message text
senderstring the sender of the message
Example
// who said that? function chatReceiveHook(str, sender) { clientMessage(ChatColor.RED + sender + " says: " + str); } -
deathHook(
attacker, victim) -
Callback function that is called when an entity dies.
Name Type Description attackerint the attacker's native entity ID
victimint the victim's native entity ID
Example
Credit: Connor4898
// shame on you! function deathHook(attacker, victim) { if (Player.getEntity() == attacker) { clientMessage("How could you?!"); } } -
destroyBlock(
x, y, z, side) -
Callback function that is called when a block is destroyed.
Name Type Description xint the x coordinate of the block
yint the y coordinate of the block
zint the z coordinate of the block
sideint the side of the block that was targeted
Example
// let the world know a block was destroyed function destroyBlock(x, y, z, side) { clientMessage("A block was destroyed at: " + x + ":" + y + ":" + z); } -
eatHook(
foodPoints, saturationRatio) -
Callback function that is called when the player eats.
Name Type Description foodPointsint the number of half-drumsticks restored by the food item
saturationRatiofloat the level of saturation provided per food point
Example
// yummy food is yummy function eatHook(foodPoints, saturationRatio) { clientMessage( ChatColor.DARK_PURPLE + "That yummy snack replenished " + foodPoints + " half-drumsticks\nwith a saturation ratio of: " + saturationRatio ); } -
entityAddedHook(
entity) -
Callback function that is called when an entity is added to the world. This includes arrows and falling blocks.
Name Type Description entitylong the native entity id
Example
// enderman warning function entityAddedHook(entity) { var entityType = Entity.getEntityTypeId(entity); if (entityType === 38) { clientMessage(ChatColor.RED + "Enderman in da house!"); } } -
entityRemovedHook(
entity) -
Callback function that is called when an entity is despawns or dies.
Name Type Description entitylong the native entity id
Example
// what a tragedy! function entityRemovedHook(entity) { if (Entity.getEntityTypeId(entity) === 10) { clientMessage("Another brave chicken has passed from this world."); } } -
explodeHook(
entity, x, y, z, radius, onFire) -
Callback function that is called when an explosion occurs.
Name Type Description entitylong the native entity id of the entity that exploded
xfloat the x coordinate of the explosion
yfloat the y coordinate of the explosion
zfloat the z coordinate of the explosion
radiusfloat the radius of the explosion
onFireboolean whether or not the explosion caused a fire
Example
// explosion notification function explodeHook(entity, x, y, z, power, onFire) { clientMessage(ChatColor.RED + "An explosion occured at " + x + " : " + y + " : " + z); } -
leaveGame(
) -
Callback function that is called when the player exits a world (i.e., Quit to Title).
Example
// say goodbye on exit function leaveGame() { print("Goodbye"); } -
levelEventHook(
player, eventType, x, y, z, data) -
Callback function that is called when a door is opened or closed.
Name Type Description playerint the player entity that activated the door
eventTypeint the event type (seems to always be 1)
xint the door's x coordiante
yint the door's y coordiante
zint the door's z coordiante
dataint the open/close data (0 = closing, 1 = opening, 2 = open)
- better example once this is working; broken in BL 1.11??
Example
// Announce the opening of a door function levelEventHook(player, eventType, x, y, z, data) { if (data === 1) { clientMessage("A door was opened at location " + x + " : " + y + " : " + z); } else if (data === 0) { clientMessage("The door has been closed"); } } -
modTick(
) -
Callback function that is called every game tick. A game tick is 1/20 of a second, therefore there are 20 ticks in a second.
Example
// keep an eye on hunger level function modTick() { if (Player.getHunger() < 5) { clientMessage(ChatColor.RED + "Warning! You are getting very hungry!"); } } -
newLevel(
) -
Callback function that is called when a game level is loaded
Example
// friendly welcome message function newLevel() { clientMessage("Welcome to Minecraft PE!!"); } -
procCmd(
cmd) -
Callback function that is called when a chat command is issued.
Name Type Description cmdstring the command without the leading slash (/)
Example
// quick heal function procCmd(cmd) { if (cmd === "heal") { Entity.setHealth(Player.getEntity(), 20); } } -
redstoneUpdateHook(
x, y, z, newCurrent, someBooleanIDontKnow, blockId, blockData) -
Callback function that is called when ???
Name Type Description xint the x coordinate of the block
yint the y coordinate of the block
zint the z coordinate of the block
newCurrentint ???
someBooleanIDontKnowboolean ???
blockIdint the ID of the block
blockDataint the damage/data for the block
- test this and try to figure it out
Example
// code here -
selectLevelHook(
) -
Callback function that is called when a game world is selected. This allows you to run initialization code before the selected world is fully loaded.
- See the example at Block.defineBlock
-
serverMessageReceiveHook(
message) -
Callback function that is called when the server sends a chat message.
Name Type Description messagestring the message
Example
// announce a server message function serverMessageReceiveHook(message) { clientMessage("Message from server: " + message); } -
startDestroyBlock(
x, y, z, side) -
Callback function function that is called when a block starts being destroyed.
Name Type Description xint the x coordinate of the block
yint the y coordinate of the block
zint the z coordinate of the block
sideint the side of the block that was targeted
- confirm this - it doesn't seem to work in BL 1.11
Example
// let the world know a block is being destroyed function startDestroyBlock(x, y, z, side) { clientMessage("A block is being destroyed at: " + x + ":" + y + ":" + z); } -
useItem(
x, y, z, itemid, blockid, blockSide, itemData, blockData) -
Callback function function that is called when an item is used on a block.
Name Type Description xint the x coordinate of the block
yint the y coordinate of the block
zint the z coordinate of the block
itemidint the ID of the item used
blockidint the ID of the block
blockSideint the side of the block that was tapped
itemDataint the damage/data for the item used
blockDataint the damage/data for the block
Example
credit: Zhuowei Zhang
// Super pickaxe: tap on a block with a diamond pickaxe to mine // a 11x11x11 square around/above it function useItem(x, y, z, itemId, blockId) { if (itemId != 278) return; // if not diamond pickaxe exit the method for (var xx = x - 5; xx <= x + 5; xx++) { for (var zz = z - 5; zz <= z + 5; zz++) { for (var yy = y; yy < y + 11; yy++) { var tile = getTile(xx, yy, zz); if (tile != 0) { setTile(xx, yy, zz, 0); if (tile != 1 && tile != 2 && tile != 3) { // not stone, grass, dirt addItemInventory(tile, 1); } } } } } preventDefault(); }