Skip to content

Instantly share code, notes, and snippets.

@z3t0
Created April 14, 2015 19:28
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/8d7a6eca218f35355476 to your computer and use it in GitHub Desktop.
Save z3t0/8d7a6eca218f35355476 to your computer and use it in GitHub Desktop.
Gamemode.coffee Updated
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
constructor: (@game, opts) ->
return if not @game.isClient # TODO
super @game, opts
@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()
@on 'enterCreative', =>
console.log "Has entered Creative"
@on 'enterSurival', =>
console.log "Has entered Survival"
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')
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'
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