Class: McBlocky::DSL::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/mcblocky/dsl/commands.rb

Direct Known Subclasses

CommandBlock, ImpulseChain, RepeatChain, Executor

Constant Summary collapse

COMMANDS =
[:achievement, :ban, :ban_ip, :banlist, :blockdata, :clear, :clone, :debug, :defaultgamemode, :deop, :difficulty, :effect, :enchant, :entitydata, :execute, :fill, :gamemode, :gamerule, :give, :help, :kick, :kill, :list, :me, :op, :pardon, :pardon_ip, :particle, :playsound, :replaceitem, :save_all, :save_off, :save_on, :say, :scoreboard, :seed, :setblock, :setidletimeout, :setworldspawn, :spawnpoint, :spreadplayers, :stats, :stop, :summon, :tell, :tellraw, :testfor, :testforblock, :testforblocks, :time, :title, :toggledownfall, :tp, :trigger, :weather, :whitelist, :worldborder, :xp]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, kind, *args) ⇒ Commands

Returns a new instance of Commands.



7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



5
6
7
# File 'lib/mcblocky/dsl/commands.rb', line 5

def commands
  @commands
end

#contextObject

Returns the value of attribute context.



3
4
5
# File 'lib/mcblocky/dsl/commands.rb', line 3

def context
  @context
end

#kindObject (readonly)

Returns the value of attribute kind.



4
5
6
# File 'lib/mcblocky/dsl/commands.rb', line 4

def kind
  @kind
end

Instance Method Details

#activate(name) ⇒ Object

Raises:

  • (NameError)


26
27
28
29
30
31
# 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) ⇒ Object



49
50
51
52
# 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) ⇒ Object



18
19
20
# File 'lib/mcblocky/dsl/commands.rb', line 18

def command(*args)
  commands << args.map(&:to_s).join(' ')
end

#detect(selector, *args, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object

Raises:

  • (NameError)


40
41
42
43
44
45
# 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) ⇒ Object

Raises:

  • (NameError)


33
34
35
36
37
38
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



93
94
95
96
# 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) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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) ⇒ Object



117
118
119
120
# 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) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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) ⇒ Object



22
23
24
# File 'lib/mcblocky/dsl/commands.rb', line 22

def to_nbt(obj)
  McBlocky::DSL.to_nbt(obj)
end