Namespace: Hooks

Hooks

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
attacker long

the attacker's native entity ID

victim long

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
x int

the chest's x coordiante

y int

the chest's y coordiante

z int

the chest's z coordiante

type int

the event type (seems to always be 1)

data int

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
str string

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
str string

the message text

sender string

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
attacker int

the attacker's native entity ID

victim int

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
x int

the x coordinate of the block

y int

the y coordinate of the block

z int

the z coordinate of the block

side int

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
foodPoints int

the number of half-drumsticks restored by the food item

saturationRatio float

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
entity long

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
entity long

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
entity long

the native entity id of the entity that exploded

x float

the x coordinate of the explosion

y float

the y coordinate of the explosion

z float

the z coordinate of the explosion

radius float

the radius of the explosion

onFire boolean

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
player int

the player entity that activated the door

eventType int

the event type (seems to always be 1)

x int

the door's x coordiante

y int

the door's y coordiante

z int

the door's z coordiante

data int

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
cmd string

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
x int

the x coordinate of the block

y int

the y coordinate of the block

z int

the z coordinate of the block

newCurrent int

???

someBooleanIDontKnow boolean

???

blockId int

the ID of the block

blockData int

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.

serverMessageReceiveHook(message)

Callback function that is called when the server sends a chat message.

Name Type Description
message string

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
x int

the x coordinate of the block

y int

the y coordinate of the block

z int

the z coordinate of the block

side int

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
x int

the x coordinate of the block

y int

the y coordinate of the block

z int

the z coordinate of the block

itemid int

the ID of the item used

blockid int

the ID of the block

blockSide int

the side of the block that was tapped

itemData int

the damage/data for the item used

blockData int

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();
}