Skip to content

Instantly share code, notes, and snippets.

@Arcensoth
Last active April 30, 2020 07:23
Show Gist options
  • Save Arcensoth/a825165b92a59a00b3fabee62914c7cd to your computer and use it in GitHub Desktop.
Save Arcensoth/a825165b92a59a00b3fabee62914c7cd to your computer and use it in GitHub Desktop.
# Selectors with operator overloading:
# (-) Pollutes module scope with many imports
# (+) Arguably more expressive/readable
# (+) More creative potential more new functionality
from pyckaxe.gamemodes import spectator
from pyckaxe.selectors import a
from pyckaxe.selector_arguments import gamemode, tag, distance, scores, score, advancements, advancement, nbt, level, limit
from pyckaxe.selector_sorts import nearest
foo = tag("foo")
bar = tag("bar")
points = score("points")
some_quest=advancement("mypack:some/quest")
another_quest=advancement("mypack:another/quest")
s1 = a(
# gamemode=!spectator
gamemode != spectator,
# tag=a, tag=b, tag=!c
tag == "a",
tag == "b",
tag != "c",
# tag=d, tag=e, tag=f
tags == ["d", "e", "f"]
# tag=foo, tag=!bar
foo,
-bar,
# distance=5..9
5 <= distance <= 9,
# scores={deaths=0}
scores["deaths"] == 0,
# (using custom score object)
# scores={points=100..}
points >= 100,
# advancements={mypack:some/goal=true}
advancements["mypack:some/goal"],
# advancements={mypack:some/task=false}
-advancements["mypack:some/task"],
# advancements={mypack:some/job={criteria1=true}}
advancements["mypack:some/job"]["criteria1"],
# advancements={mypack:some/job={criteria2=false}}
-advancements["mypack:some/job"]["criteria2"],
# alternative form of advancements:
advancements == {
"mypack:some/goal": True,
"mypack:some/task": False,
"mypack:some/quest": {
"criteria1": True,
"criteria2": False
}
},
# (using custom advancement objects)
# advancements={mypack:some/quest=true, mypack:another/quest=false}
some_quest,
-another_quest,
# nbt={OnGround: true}
nbt == { "OnGround": True },
)
s2 = s1(tag == 'more', 21 <= level <= 30)
s3 = s2(sort == nearest, limit == 1)
# Selectors with method chaining:
# (+) Doesn't pollute module scope
# (+) Better support for auto-completion
# (-) Arguably less expressive/readable
# (-) More brackets and indentation
from pyckaxe import gamemodes, selectors, sorts
s1 = selectors.all_players
.gamemode(gamemodes.spectator)
.tag("a")
.tag("b")
.not_tag("c")
.tags("d", "e", "f")
.distance(5) # distance=5
.distance(5, ...) # distance=5..
.distance(..., 9) # distance=..9
.distance(5, 9) # distance=5..9
.scores(deaths=0, points=(100, ...))
.scores({
"mypack.deaths": 0,
"mypack.points": (100, ...)
})
.advancements("mypack:some/goal", ("mypack:some/job", "criteria1"))
.not_advancements("mypack:some/task", ("mypack:some/job", "criteria2"))
.advancements({
"mypack:some/goal": True,
"mypack:some/task": False,
"mypack:some/quest": {
"criteria1": True,
"criteria2": False
}
})
.nbt({ "OnGround": True })
s2 = s1.tag("more").level(21, 30)
s3 = s2.sort(sorts.nearest).limit(1)
from pyckaxe import blocks, commands, selectors, vec
s1 = selectors.all_players.tag("foo")
c1 = commands.execute
.as(s1)
.at(selectors.self)
.if_block(~vec(0, 0, 0), blocks.cobweb)
.if_score(selectors.self, "points")
.matches(100, ...)
c2 = commands.setblock(~vec(0, -1, 0), blocks.lava)
c3 = c1.run(c2)
c4 = commands.function("mypack:some/function")
c5 = c1.run(c4)
f1 = c2 + c4
c6 = c1.run.function(f1)
c7 = c1.run.function(
commands.function(f1)
+ commands.execute
.if_block(~vec(0, 0, -1), blocks.air)
.if_block(~vec(0, 0, 1), blocks.air)
.if_block(~vec(-1, 0, 1), blocks.air)
.if_block(~vec(1, 0, 0), blocks.air)
.run
.say("help")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment