class McBlocky::DSL::Commands

Constants

COMMANDS

Attributes

commands[RW]
context[RW]
kind[R]

Public Class Methods

new(context, kind, *args) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 7
def initialize(context, kind, *args)
  @context = context
  @kind = kind
  @args = args
  @commands = []
  @a = Selector.new '@a'
  @p = Selector.new '@p'
  @r = Selector.new '@r'
  @e = Selector.new '@e'
end

Public Instance Methods

activate(name) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 26
def activate(name)
  raise NameError, "No chain named #{name}" unless context.named_chains.has_key? name
  raise ArgumentError, "#{name} is not an activateable chain" unless context.named_chains[name].kind == :impulse_chain
  chain = context.named_chains[name]
  command :blockdata, chain.rect.x1, chain.rect.y1, chain.rect.z1, '{auto:1}'
end
blockdata(*args) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 49
def blockdata(*args)
  args[-1] = to_nbt(args[-1]) if Hash === args[-1]
  command :blockdata, *args
end
command(*args) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 18
def command(*args)
  commands << args.map(&:to_s).join(' ')
end
detect(selector, *args, &block) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 54
def detect(selector, *args, &block)
  if block
    chain = Commands.new(context, :detect)
    chain.instance_exec &block
    chain.commands.each do |c|
      command :execute, selector, '~ ~ ~', :detect, *args, c
    end
  else
    command :execute, selector, '~ ~ ~', :detect, *args
  end
end
disable(name) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 40
def disable(name)
  raise NameError, "No chain named #{name}" unless context.named_chains.has_key? name
  raise ArgumentError, "#{name} is not a repeat chain" unless context.named_chains[name].kind == :repeat
  chain = context.named_chains[name]
  command :blockdata, chain.rect.x1, chain.rect.y1, chain.rect.z1, '{auto:0}'
end
enable(name) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 33
def enable(name)
  raise NameError, "No chain named #{name}" unless context.named_chains.has_key? name
  raise ArgumentError, "#{name} is not a repeat chain" unless context.named_chains[name].kind == :repeat
  chain = context.named_chains[name]
  command :blockdata, chain.rect.x1, chain.rect.y1, chain.rect.z1, '{auto:1}'
end
execute(selector, *args, &block) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 66
def execute(selector, *args, &block)
  if args.empty?
    args = ['~ ~ ~']
  end
  if block
    chain = Commands.new(context, :execute)
    chain.instance_exec &block
    chain.commands.each do |c|
      command :execute, selector, *args, c
    end
  else
    command :execute, selector, *args
  end
end
gamerule(rule=nil, value=nil, &block) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 81
def gamerule(rule=nil, value=nil, &block)
  if (rule and block) or (rule and value.nil?)
    raise ArgumentError
  end
  unless block
    command :gamerule, rule, value
  else
    o = PartialCommand.new(self, :gamerule)
    o.instance_exec &block
  end
end
replaceitem(*args) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 93
def replaceitem(*args)
  args[-1] = to_nbt(args[-1]) if Hash === args[-1]
  command :replaceitem, *args
end
scoreboard(*args, &block) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 98
def scoreboard(*args, &block)
  if block
    d = SimpleDelegator.new(self)
    d.instance_variable_set :@a, @a
    d.instance_variable_set :@p, @p
    d.instance_variable_set :@r, @r
    d.instance_variable_set :@e, @e
    d.instance_variable_set :@args, args
    def d.method_missing(m, *a)
      super
    rescue NoMethodError
      command :scoreboard, *@args, m, *a
    end
    d.instance_exec(&block)
  else
    command :scoreboard, *args
  end
end
setblock(*args) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 117
def setblock(*args)
  args[-1] = to_nbt(args[-1]) if Hash === args[-1]
  command :setblock, *args
end
tellraw(player, *args) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 122
def tellraw(player, *args)
  if args.length < 1
    raise ArgumentError, "No message given in tellraw"
  end
  obj = []
  args.each do |arg|
    if Array === arg
      obj += arg
    else
      obj << arg
    end
  end
  command :tellraw, player, JSON.dump(obj)
end
title(selector, subcommand, *args) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 137
def title(selector, subcommand, *args)
  if args.length < 1
    raise ArgumentError, "No message given in title"
  end
  obj = []
  args.each do |arg|
    if Array === arg
      obj += arg
    else
      obj << arg
    end
  end
  command :title, selector, subcommand, JSON.dump(obj)
end
to_nbt(obj) click to toggle source
# File lib/mcblocky/dsl/commands.rb, line 22
def to_nbt(obj)
  McBlocky::DSL.to_nbt(obj)
end