Skip to content

Instantly share code, notes, and snippets.

@AlinaNova21
Last active November 12, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlinaNova21/1690177425e43bae4a85857d28084a0b to your computer and use it in GitHub Desktop.
Save AlinaNova21/1690177425e43bae4a85857d28084a0b to your computer and use it in GitHub Desktop.
module.exports = function(config) {
if(config.cli){
/** Current Way:
let onPlayerSandbox = config.engine.onPlayerSandbox;
config.engine.onPlayerSandbox = function(sandbox, userId) {
sandbox.test = function(){ return 'Hello World!' }
return onPlayerSandbox(sandbox,userId)
}
**/
// Proposed way
config.cli.on('createSandbox',(event,sandbox)=>{
sandbox.test = function(){ return 'Hello World!' }
})
}
if(config.engine){
// Normal way: (Not multimod firendly)
/**
config.engine.onProcessObject = function(object) {
if(object.type == 'keeperLair') {
return false; // This object will not be processed
}
}**/
// Event based way: (More multimod friendly)
config.engine.on('processObject',(event, object, roomObjects, roomTerrain, gameTime, roomInfo, objectsUpdate, usersUpdate)=>{
if(object.type == 'keeperLair'){
event.preventDefault() // Prevents default handler from running
event.returnValue = false
}
})
}
}
const { EventEmitter } = require('events')
module.exports = function(config) {
if(config.__eventmod_hooked) return
config.__eventmod_hooked = true
for (let modName in config) {
let mod = config[modName]
let ee = new EventEmitter()
config[modName] = new Proxy(mod,{
get: (target,name)=>ee[name] || target[name]
})
let hooked = {}
ee.on('newListener',(eventName,listener)=>{
// This allows us to hook the event on first listener
if(hooked[eventName]) return
hooked[eventName] = true
console.log('Hooking event',eventName)
let name = `on${eventName.slice(0,1).toUpperCase}${eventName.slice(1)}`
let orig = mod[name]
mod[name] = function(...args) {
let def = true
let event = { returnValue: null, preventDefault: ()=>def=false }
ee.emit(event,eventName, ...args)
if(def) orig(...args)
return event.returnValue
}
})
}
}
/*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment