Skip to content

Commit

Permalink
Refactoring commands
Browse files Browse the repository at this point in the history
  • Loading branch information
enebo committed May 14, 2011
1 parent 88dd1dd commit 374eb4f
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions examples/commands.rb
Expand Up @@ -3,12 +3,10 @@
class CommandsPlugin
include Purugin::Plugin, Purugin::Colors
description 'Commands', 0.1

attr_reader :commands

module Command
class CMD < Struct.new(:permission, :description, :plugin_name, :code)
end
class CMD < Struct.new(:permission, :description, :plugin_name, :code); end

def basename
@basename ||= "purugin.#{getDescription.name.downcase}"
Expand All @@ -28,7 +26,7 @@ def command(name, desc, perm="#{basename}.#{name[1..-1]}", &code)

include Command

def initialize(*a)
def initialize(*)
super
@commands = {}
end
Expand All @@ -39,33 +37,29 @@ def on_enable
puts "No permissions...free for all" unless @permissions

command('/help', 'help on all commands', nil) do |e, *args|
player = e.player
@commands.sort.each do |command, entry|
permission = has_permission(player, entry.permission) ? green('*') : red('*')
e.player.send_message "#{command} - #{entry.description} [#{entry.plugin_name}]#{permission}"
@commands.sort.each do |name, cmd|
allowed = permitted?(e.player, cmd.permission) ? green('*') : red('*')
e.player.msg "#{name} - #{cmd.description} [#{cmd.plugin_name}]#{allowed}"
end
end

event(:player_command_preprocess) do |e|
player = e.player
command, *args = e.message.split(/\s+/)
entry = @commands[command]
name, *args = e.message.split(/\s+/)
cmd = @commands[name]

if entry
if has_permission(e.player, entry.permission)
entry.code.call(e, command, *args)
if cmd
if permitted? e.player, cmd.permission
cmd.code.call(e, name, *args)
else
e.player.send_message "Insufficient privelege for: #{command}"
e.player.msg "Insufficient privelege for: #{name}"
end
else
e.player.send_message "Uknown command: #{command}"
e.player.msg "Uknown command: #{name}"
end
end
end

def has_permission(player, permission)
!@permissions || !permission ||
@permissions.handler.has(player, permission) ||
@permissions.handler.has(player, basename)
def permitted?(player, permission)
!@permissions || !permission || @permissions.handler.has(player, permission)
end
end

0 comments on commit 374eb4f

Please sign in to comment.