Skip to content

Instantly share code, notes, and snippets.

@z3t0
Created April 13, 2015 00:19
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 z3t0/f2fe3f291d1ec49bbab1 to your computer and use it in GitHub Desktop.
Save z3t0/f2fe3f291d1ec49bbab1 to your computer and use it in GitHub Desktop.
Trying to get Events working
inherits = require('inherits')
EventEmitter = require('events').EventEmitter
module.exports = (game, opts) ->
return new Gamemode(game, opts)
module.exports.pluginInfo =
loadAfter: ['voxel-mine', 'voxel-fly', 'voxel-registry', 'voxel-harvest', 'voxel-commands', 'voxel-keys']
class Gamemode extends EventEmitter
constructor: (@game, opts) ->
return if not @game.isClient # TODO
@keys = @game.plugins.get('voxel-keys') ? throw new Error('voxel-gamemode requires voxel-keys plugin')
@mode = opts.startMode ? 'survival'
@registry = @game.plugins?.get('voxel-registry') ? throw new Error('voxel-gamemode requires "voxel-registry" plugin')
@enable()
inherits(Gamemode, EventEmitter)
enable: () ->
@game.plugins?.get('voxel-commands')?.registerCommand 'creative', @enterCreative.bind(@), '', 'enters creative mode'
@game.plugins?.get('voxel-commands')?.registerCommand 'survival', @enterSurvival.bind(@), '', 'enters survival mode'
if @game.plugins?.isEnabled('voxel-fly') and @mode == 'survival'
@game.plugins.disable('voxel-fly')
@keys.registerKey 'inventory', 'E'
@keys.down.on 'inventory', @onInventory = () =>
if @mode == 'creative' and @game.plugins.isEnabled('voxel-inventory-creative')
@game.plugins.get('voxel-inventory-creative')?.open()
else
@game.plugins.get('voxel-inventory-crafting')?.open()
enterCreative: () ->
@mode = 'creative'
@game.plugins.enable('voxel-fly')
@game.plugins.get('voxel-mine')?.instaMine = true
@game.plugins.get('voxel-harvest')?.enableToolDamage = false
console.log 'Entered creative mode'
@game.plugins?.get('voxel-console')?.log?('Entered creative mode')
@emit 'enterCreative'
@on 'enterCreative', =>
console.log "Has entered Creative"
enterSurvival: () ->
@mode = 'survival'
@game.plugins.disable 'voxel-fly'
@game.plugins.get('voxel-mine')?.instaMine = false
@game.plugins.get('voxel-harvest')?.enableToolDamage = true
console.log 'Entered survival mode'
@game.plugins?.get('voxel-console')?.log?('Entered survival mode')
@emit 'enterSurvival'
@on 'enterSurival', =>
console.log "Has Entered Survival"
disable: () ->
@keys.down.removeListener 'inventory', @onInventory
@keys.unregisterKey 'inventory'
# TODO: un-registerCommand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment