Skip to content

Instantly share code, notes, and snippets.

@piperswe
Last active August 29, 2015 14:16
Show Gist options
  • Save piperswe/7ba8275387d9de416925 to your computer and use it in GitHub Desktop.
Save piperswe/7ba8275387d9de416925 to your computer and use it in GitHub Desktop.
InteractionPE

Example

ipe.registerMod('modName', function(mod) {
  var redstonemod = mod.require('redstonemod')
  mod.api = {
    doRedstoneModThing: function() {
      redstonemod.explodeTheWorld()
    }
  }
})

API

ipe - Global

ipe.registerMod - Function

Takes parameters name and init, name is the name of the mod when referenced by others, init is called with the parameter mod when ipe.js is loading.

Parameters:

  • name - String
  • init - Function (function(mod))

ipe.load - Function

Called at the end of ipe.js, loads mods.

mod - Passed as parameter to init (second parameter of ipe.registerMod)

mod.require - Function

Takes parameter name: the name of the mod to load the API of.

Parameters:

  • name - String

mod.api - Object

What is passed to other mods when they require your mod.

mod.api.<hook> - Function (userdefined)

Called on ModPE events (hooks)

See https://github.com/Connor4898/ModPE-Scripts/wiki/ModPE-Scripts-Functions-List#hook-functions-1

/*
Define a mod like follows:
ipe.registerMod('modName', function(mod) {
var redstonemod = mod.require('redstonemod')
mod.api = {
doRedstoneModThing: function() {
redstonemod.explodeTheWorld()
}
}
})
ipe.registerMod('otherModName', function(mod) {
var modName = mod.require('modName')
modName.doRedstoneModThing()
})
Just paste the mod at the end of ipe.js BEFORE ipe.load()
*/
var useItem, attackHook, modTick, procCmd, newLevel, leaveGame, entityAddedHook, entityRemovedHook, deathHook, levelEventHook, blockEventHook
var ipe = (function() {
var mods = []
function forEveryMod(onMod) {
for (var i = 0; i < mods.length; i++) {
onMod(mods[i])
}
}
useItem = function(x, y, z, itemId, blockId, side) {
forEveryMod(function(mod) {
if (mod.mod.api.useItem instanceof Function) {
mod.mod.api.useItem(x, y, z, itemId, blockId, side)
}
})
}
attackHook = function(attacker, victim) {
forEveryMod(function(mod) {
if (mod.mod.api.attackHook instanceof Function) {
mod.mod.api.attackHook(attacker, victim)
}
})
}
modTick = function() {
forEveryMod(function(mod) {
if (mod.mod.api.modTick instanceof Function) {
mod.mod.api.modTick()
}
})
}
procCmd = function(cmd) {
forEveryMod(function(mod) {
if (mod.mod.api.procCmd instanceof Function) {
mod.mod.api.procCmd(cmd)
}
})
}
newLevel = function() {
forEveryMod(function(mod) {
if (mod.mod.api.newLevel instanceof Function) {
mod.mod.api.newLevel()
}
})
}
leaveGame = function() {
forEveryMod(function(mod) {
if (mod.mod.api.leaveGame instanceof Function) {
mod.mod.api.leaveGame()
}
})
}
entityAddedHook = function(entity) {
forEveryMod(function(mod) {
if (mod.mod.api.entityAddedHook instanceof Function) {
mod.mod.api.entityAddedHook(entity)
}
})
}
entityRemovedHook = function(entity) {
forEveryMod(function(mod) {
if (mod.mod.api.entityRemovedHook instanceof Function) {
mod.mod.api.entityRemovedHook(entity)
}
})
}
deathHook = function(murderer, victim) {
forEveryMod(function(mod) {
if (mod.mod.api.deathHook instanceof Function) {
mod.mod.api.deathHook(murderer, victim)
}
})
}
levelEventHook = function(entity, eventType, x, y, z, data) {
forEveryMod(function(mod) {
if (mod.mod.api.levelEventHook instanceof Function) {
mod.mod.api.levelEventHook(entity, eventType, x, y, z, data)
}
})
}
blockEventHook = function(x, y, z, eventType, data) {
forEveryMod(function(mod) {
if (mod.mod.api.blockEventHook instanceof Function) {
mod.mod.api.blockEventHook(x, y, z, eventType, data)
}
})
}
return {
registerMod: function(name, init) {
mods.push({
name: name,
loaded: false,
mod: {
init: init,
loaded: false,
api: {},
require: function(name) {
for (var i = 0; i < mods.length; i++) {
if (mods[i].name === name) {
var mod = mods[i]
if (!mod.loaded) {
mod.mod.init(mod.mod)
mod.loaded = true
}
return mod.mod.api
}
}
throw new ReferenceError(name)
}
}
})
},
load: function() {
for (var i = 0; i < mods.length; i++) {
if (mods[i].loaded) continue
mods[i].mod.init(mods[i].mod)
}
}
}
})()
// Place mods here
ipe.load()
The MIT License (MIT)
Copyright (c) 2015 Zebulon McCorkle
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment