Skip to content

Commit c979e1b

Browse files
committed
Add documentation
1 parent 1a71842 commit c979e1b

File tree

11 files changed

+475
-18
lines changed

11 files changed

+475
-18
lines changed

cmd_ir/core.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def _create_func(self, name):
251251
return IRFunction(name, self)
252252

253253
def create_global(self, namehint, vartype):
254+
from .instructions import DefineGlobal
254255
def create(name):
255256
return self.preamble.add(DefineGlobal(vartype))
256257
return self.uniq(namehint, create)
@@ -470,6 +471,7 @@ def create_block(self, namehint):
470471
return block
471472

472473
def create_var(self, namehint, vartype):
474+
from .instructions import DefineVariable
473475
def create(name):
474476
return self.preamble.add(DefineVariable(vartype))
475477
return self.uniq(namehint, create)
@@ -535,6 +537,7 @@ def variables_finalized(self):
535537
self._varsfinalized = True
536538

537539
def add_entry_exit(self):
540+
from .instructions import PushNewStackFrame, PopStack, Branch
538541
# sorted from head to tail of stack
539542
vars = sorted([var.var for var in self.scope.values() \
540543
if isinstance(var, LocalVariable) \
@@ -554,6 +557,7 @@ def add_entry_exit(self):
554557
return vars
555558

556559
def add_advancement_revoke(self, event):
560+
from instructions import RevokeEventAdvancement
557561
self._entryblock.add(RevokeEventAdvancement(self))
558562

559563
def serialize(self):
@@ -609,6 +613,7 @@ def is_terminated(self):
609613
return self.insns[-1].terminator()
610614

611615
def add(self, insn):
616+
from .instructions import Branch, Return
612617
if not self.force and self.insns and self.insns[-1].terminator():
613618
assert False, "Block %s is terminated by %s. Tried adding %s" % (
614619
self, self.insns[-1], insn)
@@ -663,9 +668,3 @@ def is_empty(self):
663668
def reset(self):
664669
if self._clear:
665670
super().reset()
666-
667-
# Load these after everything has been defined - otherwise it is a recursive
668-
# dependency
669-
from .instructions import (DefineGlobal, DefineVariable, PushNewStackFrame,
670-
PopStack, Branch, RevokeEventAdvancement, Return,
671-
Branch)

cmd_ir/core_types.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ def _init_from_parser(cls, value):
99
return value
1010

1111
class NativeType(InsnArg):
12-
pass
12+
13+
@classmethod
14+
def typename(cls):
15+
return cls.__name__
1316

1417
class EntitySelection(NativeType, metaclass=abc.ABCMeta):
1518

cmd_ir/instructions/basic.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Basic Commands and Instructions"""
2+
13
from ._core import Insn, SingleCommandInsn, ConstructorInsn, BasicBlock
24
from ..core_types import (CmdFunction,
35
Opt,
@@ -20,9 +22,11 @@
2022
import commands as c
2123

2224
class RunCommand(SingleCommandInsn):
25+
"""Runs the command given in the command variable."""
2326

2427
args = [CmdFunction]
2528
argnames = 'cmd'
29+
argdocs = ["The command variable"]
2630
insn_name = 'run_cmd'
2731
is_branch = True
2832

@@ -38,9 +42,13 @@ def as_cmd(self):
3842
return self.var.read()
3943

4044
class Getter(ConstructorInsn):
45+
"""Returns a command variable that when run, gives the variable's value
46+
in the 'result' of the command's execution."""
4147

4248
args = [Variable]
4349
argnames = 'var'
50+
argdocs = ["The variable to create the getter for"]
51+
rettype = CmdFunction
4452
insn_name = 'getter'
4553

4654
def construct(self):
@@ -63,9 +71,14 @@ def as_cmd(self):
6371
return insn.as_single_cmd()
6472

6573
class AsSingleCmdInsn(ConstructorInsn):
74+
"""Forces a basic block to become a single command, the command is returned
75+
as a command variable. An error is raised if the block cannot be a single
76+
command."""
6677

6778
args = [BasicBlock]
6879
argnames = 'block'
80+
argdocs = ["The basic block"]
81+
rettype = CmdFunction
6982
insn_name = 'as_single_cmd'
7083

7184
def construct(self):
@@ -76,75 +89,98 @@ def declare(self):
7689
pass#self.block.usage()
7790

7891
class SetBlockInsn(SingleCommandInsn):
92+
"""Sets a block in the world at the given position to the given block
93+
type."""
7994

8095
args = [Position, BlockType]
8196
argnames = 'pos block'
97+
argdocs = ["The block position", "The block type"]
8298
insn_name = 'setblock'
8399

84100
def get_cmd(self):
85101
return c.Setblock(self.pos.as_blockpos(), self.block)
86102

87103
class ReplaceEntityItem(SingleCommandInsn):
104+
"""Replaces an item in an entity's inventory at a specified slot with the
105+
given item. See `/replaceitem` for details."""
88106

89107
args = [EntitySelection, VirtualString, ItemType, Opt(int)]
90108
argnames = 'target slot item amount'
109+
argdocs = ["Target entities to replace their items", "Inventory slot",
110+
"Replacement item", "Replace amount"]
91111
insn_name = 'replace_entity_item'
92112

93113
def get_cmd(self):
94114
return c.ReplaceItem(self.target.as_resolve().ref, str(self.slot),
95115
self.item, self.amount)
96116

97117
class ReplaceBlockItem(SingleCommandInsn):
118+
"""Replaces an item in a block's inventory at a specified slot with the
119+
given item. See `/replaceitem` for details."""
98120

99121
args = [Position, VirtualString, ItemType, Opt(int)]
100122
argnames = 'pos slot item amount'
123+
argdocs = ["Block position", "Inventory slot", "Replacement item",
124+
"Replace amount"]
101125
insn_name = 'replace_block_item'
102126

103127
def get_cmd(self):
104128
return c.ReplaceItem(self.pos.as_blockpos().ref, str(self.slot),
105129
self.item, self.amount)
106130

107131
class GiveInsn(SingleCommandInsn):
132+
"""Gives targetted entities an item."""
108133

109134
args = [EntitySelection, ItemType, int]
110135
argnames = 'targets item count'
136+
argdocs = ["Entities to give the item to", "The item to give", "Item count"]
111137
insn_name = 'give'
112138

113139
def get_cmd(self):
114140
return c.GiveItem(self.targets.as_resolve(), self.item, self.count)
115141

116142
class ClearInsn(SingleCommandInsn):
143+
"""Clears items matching the given item type from targetted entities."""
117144

118145
args = [EntitySelection, ItemType, int]
119146
argnames = 'targets item max_count'
147+
argdocs = ["Entities to clear the item from", "Item to clear",
148+
"Max count of items. -1 to clear all matching items."]
120149
insn_name = 'clear'
121150

122151
def get_cmd(self):
123152
return c.ClearItem(self.targets.as_resolve(), self.item, self.max_count)
124153

125154
class TeleportInsn(SingleCommandInsn):
155+
"""Moves target entities to a specified position."""
126156

127157
args = [EntitySelection, Position]
128158
argnames = 'target pos'
159+
argdocs = ["Entities to move", "Position to move to"]
129160
insn_name = 'teleport'
130161

131162
def get_cmd(self):
132163
return c.Teleport(self.target.as_resolve(), self.pos.as_worldpos())
133164

134165
class MoveToEntityInsn(SingleCommandInsn):
166+
"""Moves entities to another target entity."""
135167

136168
# Should be EntityRef
137169
args = [EntitySelection, EntitySelection]
138170
argnames = 'sources target'
171+
argdocs = ["Entities to move", "Destination entity"]
139172
insn_name = 'move_to_entity'
140173

141174
def get_cmd(self):
142175
return c.Teleport(self.sources.as_resolve(), self.target.as_resolve())
143176

144177
class TeleportWithRotInsn(SingleCommandInsn):
178+
"""Moves entities to the given position with a specific rotation."""
145179

146180
args = [EntitySelection, Position, PosType, PosType]
147181
argnames = 'target pos yrot xrot'
182+
argdocs = ["Entities to move", "Position to move to", "y rotation",
183+
"x rotation"]
148184
insn_name = 'tp_with_rot'
149185

150186
def get_cmd(self):
@@ -159,19 +195,27 @@ def _to_resolve(self, arg):
159195
else str(arg))
160196

161197
class CloneInsn(SingleCommandInsn):
198+
"""Clones a region of blocks specified by a lower left and upper right
199+
bound to a given destination position."""
162200

163201
args = [Position, Position, Position]
164202
argnames = 'src0 src1 dest'
203+
argdocs = ["Lower left position", "Upper right position",
204+
"Destination position"]
165205
insn_name = 'clone'
166206

167207
def get_cmd(self):
168208
return c.Clone(self.src0.as_blockpos(), self.src1.as_blockpos(),
169209
self.dest.as_blockpos())
170210

171211
class GiveEntityEffectInsn(SingleCommandInsn):
212+
"""Gives target entities the specified effect. See `/effect` for details."""
172213

173214
args = [EntitySelection, VirtualString, Opt(int), Opt(int), Opt(str)]
174215
argnames = 'target effect seconds amp hide_particles'
216+
argdocs = ["Entities to give the effect to", "The effect name",
217+
"Number of seconds the effect will last", "Amplifier",
218+
"Whether to hide particles (true|false)"]
175219
insn_name = 'give_effect'
176220

177221
def activate(self, seq):
@@ -183,20 +227,28 @@ def get_cmd(self):
183227
self.seconds, self.amp, self.hide_particles == 'true')
184228

185229
class SpawnEntityInsn(SingleCommandInsn):
230+
"""Spawns an entity of the given type at a position."""
186231

187232
args = [VirtualString, Opt(Position), Opt(NBTCompound)]
188233
argnames = 'entity pos data'
234+
argdocs = ["Entity type name", "Position. Defaults to the location of the" \
235+
+ " sender", "Optional NBT data for the entity"]
189236
insn_name = 'spawn_entity'
190237

191238
def get_cmd(self):
192239
return c.Summon(str(self.entity), self.pos.as_worldpos() \
193240
if self.pos else None, self.data)
194241

195242
class SpawnParticleInsn(SingleCommandInsn):
243+
"""Spawn particle effects with the given parameters. See `/particle`
244+
for details."""
196245

197246
args = [VirtualString, Position, Position, float, int, str,
198247
Opt(EntitySelection)]
199248
argnames = 'name pos delta speed count mode targets'
249+
argdocs = ["Particle name", "Position to spawn at", "Volume to spawn in",
250+
"Speed of the particle", "Number of particles", "Display " \
251+
+ "mode (normal|force)", "Players to show the particles to"]
200252
insn_name = 'spawn_particle'
201253

202254
def activate(self, seq):
@@ -208,9 +260,12 @@ def get_cmd(self):
208260
self.count, self.mode, self.targets)
209261

210262
class TitleInsn(Insn):
263+
"""Show a title text to the specified players."""
211264

212265
args = [EntitySelection, str, Opt(TextObject)]
213266
argnames = 'player action text'
267+
argdocs = ["Players to show to", "'clear' or 'reset', " \
268+
+ "otherwise title|subtitle|actionbar", "Text to show"]
214269
insn_name = 'title'
215270

216271
def activate(self, seq):
@@ -227,76 +282,95 @@ def apply(self, out, func):
227282
return c.Title(self.player.as_resolve(), self.action, *args)
228283

229284
class SetTitleTimes(SingleCommandInsn):
285+
"""Sets the timer parameters for a title being shown to the player."""
230286

231287
args = [EntitySelection, int, int, int]
232288
argnames = 'player fade_in stay fade_out'
289+
argdocs = ["Players to set the times on", "Title fade in time", "Title " + \
290+
"stay time", "Title fade out time"]
233291
insn_name = 'set_title_times'
234292

235293
def get_cmd(self):
236294
return c.Title(self.player.as_resolve(), 'times', str(self.fade_in),
237295
str(self.stay), str(self.fade_out))
238296

239297
class JoinTeamInsn(SingleCommandInsn):
298+
"""Adds the specified entities to the given team."""
240299

241300
args = [TeamRef, Opt(EntitySelection)]
242301
argnames = 'team members'
302+
argdocs = ["The team to join", "Members joining the team. If NULL then " + \
303+
"the current command sender is added."]
243304
insn_name = 'join_team'
244305

245306
def get_cmd(self):
246307
return c.JoinTeam(self.team.ref, self.members.as_resolve() \
247308
if self.members else None)
248309

249310
class TeamColorInsn(SingleCommandInsn):
311+
"""Sets the color for a team."""
250312

251313
args = [TeamRef, TextColor]
252314
argnames = 'team color'
315+
argdocs = ["Team to modify", "Color of the team"]
253316
insn_name = 'team_color'
254317

255318
def get_cmd(self):
256319
return c.TeamModify(self.team.ref, 'color', self.color.name)
257320

258321
class TeamCollisionInsn(SingleCommandInsn):
322+
"""Sets the collision behavious for a given team."""
259323

260324
args = [TeamRef, str]
261325
argnames = 'team behaviour'
326+
argdocs = ["Team to modify", "Collision behaviour"]
262327
insn_name = 'team_collision'
263328

264329
def get_cmd(self):
265330
return c.TeamModify(self.team.ref, 'collisionRule', self.behaviour)
266331

267332
class BarMaxInsn(SingleCommandInsn):
333+
"""Sets the maximum value for a bossbar."""
268334

269335
args = [BossbarRef, int]
270336
argnames = 'bar max'
337+
argdocs = ["Bar to modify", "Max value"]
271338
insn_name = 'bar_set_max'
272339

273340
def get_cmd(self):
274341
return c.BossbarSet(self.bar.ref, 'max', c.SimpleResolve(str(self.max)))
275342

276343
class BarSetPlayers(SingleCommandInsn):
344+
"""Set the players who can see the bossbar."""
277345

278346
args = [BossbarRef, Opt(EntitySelection)]
279347
argnames = 'bar players'
348+
argdocs = ["Bossbar", "Players. If NULL then no players will be shown " \
349+
+ "the bar"]
280350
insn_name = 'bar_set_players'
281351

282352
def get_cmd(self):
283353
return c.BossbarSet(self.bar.ref, 'players', None if not self.players \
284354
else self.players.as_resolve())
285355

286356
class BarSetValue(SingleCommandInsn):
357+
"""Sets the current value of the given bossbar."""
287358

288359
args = [BossbarRef, int]
289360
argnames = 'bar val'
361+
argdocs = ["Bossbar to modify", "Bar value"]
290362
insn_name = 'bar_set_value'
291363

292364
def get_cmd(self):
293365
return c.BossbarSet(self.bar.ref, 'value',
294366
c.SimpleResolve(str(self.val)))
295367

296368
class KillInsn(SingleCommandInsn):
369+
"""Despawns the target entities from the world."""
297370

298371
args = [EntitySelection]
299372
argnames = 'target'
373+
argdocs = ["Entities to despawn"]
300374
insn_name = 'kill'
301375

302376
def get_cmd(self):

0 commit comments

Comments
 (0)