Class: McBlocky::DSL::Commands

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

Direct Known Subclasses

CommandBlock, 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(kind, *args) ⇒ Commands

Returns a new instance of Commands.



6
7
8
9
10
11
12
13
14
# File 'lib/mcblocky/dsl/commands.rb', line 6

def initialize(kind, *args)
  @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.



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

def commands
  @commands
end

#kindObject (readonly)

Returns the value of attribute kind.



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

def kind
  @kind
end

Instance Method Details

#blockdata(*args) ⇒ Object



26
27
28
29
# File 'lib/mcblocky/dsl/commands.rb', line 26

def blockdata(*args)
  args[-1] = to_nbt(args[-1]) if Hash === args[-1]
  command :blockdata, *args
end

#command(*args) ⇒ Object



16
17
18
# File 'lib/mcblocky/dsl/commands.rb', line 16

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

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mcblocky/dsl/commands.rb', line 31

def detect(selector, *args, &block)
  if block
    chain = Commands.new(: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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mcblocky/dsl/commands.rb', line 43

def execute(selector, *args, &block)
  if args.empty?
    args = ['~ ~ ~']
  end
  if block
    chain = Commands.new(: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



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mcblocky/dsl/commands.rb', line 58

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



70
71
72
73
# File 'lib/mcblocky/dsl/commands.rb', line 70

def replaceitem(*args)
  args[-1] = to_nbt(args[-1]) if Hash === args[-1]
  command :replaceitem, *args
end

#scoreboard(*args, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mcblocky/dsl/commands.rb', line 75

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



94
95
96
97
# File 'lib/mcblocky/dsl/commands.rb', line 94

def setblock(*args)
  args[-1] = to_nbt(args[-1]) if Hash === args[-1]
  command :setblock, *args
end

#tellraw(player, *args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mcblocky/dsl/commands.rb', line 99

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mcblocky/dsl/commands.rb', line 114

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



20
21
22
# File 'lib/mcblocky/dsl/commands.rb', line 20

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