Skip to content

Commit 55a6b8d

Browse files
committed
Blockstate pattern matching
1 parent 496b22e commit 55a6b8d

File tree

7 files changed

+255
-3
lines changed

7 files changed

+255
-3
lines changed

common/src/main/java/com/lambda/mixin/baritone/MixinBaritonePlayerContext.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ void syncRotationWithBaritone(CallbackInfoReturnable<Rotation> cir) {
2222
if (baritone != BaritoneUtils.getPrimary()) return;
2323

2424
RotationManager rm = RotationManager.INSTANCE;
25-
cir.setReturnValue(new Rotation((float) rm.getCurrentRotation().getYaw(), (float) rm.getCurrentRotation().getPitch()));
25+
cir.setReturnValue(new Rotation(
26+
(float) rm.getCurrentRotation().getYaw(), (float) rm.getCurrentRotation().getPitch())
27+
);
2628
}
2729
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.lambda.interaction.building
2+
3+
import com.lambda.interaction.building.verify.TargetState
4+
import net.minecraft.structure.StructureTemplate
5+
import net.minecraft.util.math.BlockBox
6+
import net.minecraft.util.math.BlockPos
7+
import net.minecraft.util.math.Box
8+
9+
data class Blueprint(
10+
private val structure: Map<BlockPos, TargetState>,
11+
var offset: BlockPos? = null,
12+
) {
13+
val origin: BlockPos
14+
get() = offset ?: structure.keys.firstOrNull() ?: BlockPos.ORIGIN
15+
16+
private val modifications = mutableMapOf<BlockPos, TargetState>()
17+
val structureMap: Map<BlockPos, TargetState>
18+
get() {
19+
offset?.let {
20+
val offsetMap = mutableMapOf<BlockPos, TargetState>()
21+
for ((pos, state) in structure) {
22+
offsetMap[pos.add(it)] = state
23+
}
24+
return offsetMap + modifications
25+
}
26+
27+
return structure + modifications
28+
}
29+
30+
companion object {
31+
fun Box.fromBox(targetState: TargetState) =
32+
Blueprint(BlockPos.stream(this).map { BlockPos(it) }.toList().associateWith { targetState })
33+
34+
fun BlockBox.fromBlockBox(targetState: TargetState) =
35+
Blueprint(BlockPos.stream(this).map { BlockPos(it) }.toList().associateWith { targetState })
36+
37+
fun BlockPos.fromBlockPos(targetState: TargetState) =
38+
Blueprint(setOf(this).associateWith { targetState })
39+
40+
// fun Schematic.fromSchematic() =
41+
// Blueprint(this.blockMap.map { it.key to TargetState.BlockState(it.value) }.toMap())
42+
43+
fun StructureTemplate.fromStructureTemplate() =
44+
Blueprint(
45+
blockInfoLists
46+
.flatMap { it.all }
47+
.associate { it.pos to TargetState.State(it.state) }
48+
)
49+
}
50+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.lambda.interaction.building.verify
2+
3+
import net.minecraft.block.BlockState
4+
import net.minecraft.client.world.ClientWorld
5+
import net.minecraft.item.ItemStack
6+
import net.minecraft.util.math.BlockPos
7+
8+
interface StateMatcher {
9+
fun matches(state: BlockState, pos: BlockPos, world: ClientWorld): Boolean
10+
fun getStack(world: ClientWorld, pos: BlockPos): ItemStack
11+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.lambda.interaction.building.verify
2+
3+
import com.lambda.util.ItemUtils.block
4+
import net.minecraft.block.BlockState
5+
import net.minecraft.client.world.ClientWorld
6+
import net.minecraft.item.ItemStack
7+
import net.minecraft.item.Items
8+
import net.minecraft.util.math.BlockPos
9+
import net.minecraft.util.math.Direction
10+
11+
sealed class TargetState : StateMatcher {
12+
data object Air : TargetState() {
13+
override fun matches(state: BlockState, pos: BlockPos, world: ClientWorld) = state.isAir
14+
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack = ItemStack.EMPTY
15+
}
16+
data object Solid : TargetState() {
17+
override fun matches(state: BlockState, pos: BlockPos, world: ClientWorld) =
18+
state.isSolidBlock(world, pos)
19+
override fun getStack(world: ClientWorld, pos: BlockPos) = ItemStack(Items.NETHERRACK)
20+
}
21+
data class Support(val direction: Direction) : TargetState() {
22+
override fun matches(state: BlockState, pos: BlockPos, world: ClientWorld) =
23+
world.getBlockState(pos.offset(direction)).isSolidBlock(world, pos.offset(direction))
24+
25+
override fun getStack(world: ClientWorld, pos: BlockPos) = ItemStack(Items.NETHERRACK)
26+
}
27+
data class State(val blockState: BlockState) : TargetState() {
28+
override fun matches(state: BlockState, pos: BlockPos, world: ClientWorld) =
29+
state == blockState
30+
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack =
31+
blockState.block.getPickStack(world, pos, blockState)
32+
}
33+
data class Block(val block: net.minecraft.block.Block) : TargetState() {
34+
override fun matches(state: BlockState, pos: BlockPos, world: ClientWorld) = state.block == block
35+
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack =
36+
block.getPickStack(world, pos, block.defaultState)
37+
}
38+
data class Stack(val itemStack: ItemStack) : TargetState() {
39+
override fun matches(state: BlockState, pos: BlockPos, world: ClientWorld) =
40+
state.block == itemStack.item.block
41+
42+
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack = itemStack
43+
}
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lambda.module.modules.player
2+
3+
import com.lambda.event.events.TickEvent
4+
import com.lambda.event.listener.SafeListener.Companion.listener
5+
import com.lambda.module.Module
6+
7+
object Nuker : Module(
8+
name = "Nuker",
9+
description = "Breaks blocks around you"
10+
) {
11+
private val flatten by setting("Flatten", false)
12+
13+
init {
14+
listener<TickEvent.Pre> {
15+
16+
}
17+
}
18+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.lambda.util
2+
3+
import net.minecraft.block.Block
4+
import net.minecraft.item.Item
5+
import net.minecraft.item.Items
6+
7+
object ItemUtils {
8+
val pickaxes = setOf(
9+
Items.WOODEN_PICKAXE,
10+
Items.STONE_PICKAXE,
11+
Items.IRON_PICKAXE,
12+
Items.GOLDEN_PICKAXE,
13+
Items.DIAMOND_PICKAXE,
14+
Items.NETHERITE_PICKAXE,
15+
)
16+
17+
val shovels = setOf(
18+
Items.WOODEN_SHOVEL,
19+
Items.STONE_SHOVEL,
20+
Items.IRON_SHOVEL,
21+
Items.GOLDEN_SHOVEL,
22+
Items.DIAMOND_SHOVEL,
23+
Items.NETHERITE_SHOVEL,
24+
)
25+
26+
val axes = setOf(
27+
Items.WOODEN_AXE,
28+
Items.STONE_AXE,
29+
Items.IRON_AXE,
30+
Items.GOLDEN_AXE,
31+
Items.DIAMOND_AXE,
32+
Items.NETHERITE_AXE,
33+
)
34+
35+
val hoes = setOf(
36+
Items.WOODEN_HOE,
37+
Items.STONE_HOE,
38+
Items.IRON_HOE,
39+
Items.GOLDEN_HOE,
40+
Items.DIAMOND_HOE,
41+
Items.NETHERITE_HOE,
42+
)
43+
44+
val swords = setOf(
45+
Items.WOODEN_SWORD,
46+
Items.STONE_SWORD,
47+
Items.IRON_SWORD,
48+
Items.GOLDEN_SWORD,
49+
Items.DIAMOND_SWORD,
50+
Items.NETHERITE_SWORD,
51+
)
52+
53+
val misc = setOf(
54+
Items.SHEARS,
55+
)
56+
57+
val shulkerBoxes = setOf(
58+
Items.SHULKER_BOX,
59+
Items.WHITE_SHULKER_BOX,
60+
Items.ORANGE_SHULKER_BOX,
61+
Items.MAGENTA_SHULKER_BOX,
62+
Items.LIGHT_BLUE_SHULKER_BOX,
63+
Items.YELLOW_SHULKER_BOX,
64+
Items.LIME_SHULKER_BOX,
65+
Items.PINK_SHULKER_BOX,
66+
Items.GRAY_SHULKER_BOX,
67+
Items.LIGHT_GRAY_SHULKER_BOX,
68+
Items.CYAN_SHULKER_BOX,
69+
Items.PURPLE_SHULKER_BOX,
70+
Items.BLUE_SHULKER_BOX,
71+
Items.BROWN_SHULKER_BOX,
72+
Items.GREEN_SHULKER_BOX,
73+
Items.RED_SHULKER_BOX,
74+
Items.BLACK_SHULKER_BOX,
75+
)
76+
77+
val chests = setOf(
78+
Items.CHEST,
79+
Items.TRAPPED_CHEST,
80+
Items.ENDER_CHEST,
81+
Items.BARREL,
82+
)
83+
84+
val tools = pickaxes + shovels + axes + hoes + swords + misc
85+
86+
val Item.block: Block get() = Block.getBlockFromItem(this)
87+
88+
fun Int.formatToHumanCount(): String {
89+
if (this < 0) {
90+
return "Invalid input"
91+
}
92+
93+
return buildString {
94+
val dubs = this@formatToHumanCount / (54 * 27)
95+
val shulkers = (this@formatToHumanCount % (54 * 27)) / 64
96+
val remainingItems = this@formatToHumanCount % 64
97+
98+
if (dubs > 0) {
99+
append("$dubs dub")
100+
if (dubs > 1) {
101+
append("s")
102+
}
103+
if (shulkers > 0 || remainingItems > 0) {
104+
append(" ")
105+
}
106+
}
107+
108+
if (shulkers > 0) {
109+
append("$shulkers shulker")
110+
if (shulkers > 1) {
111+
append("s")
112+
}
113+
if (remainingItems > 0) {
114+
append(" ")
115+
}
116+
}
117+
118+
if (remainingItems > 0) {
119+
append("$remainingItems item")
120+
if (remainingItems > 1) {
121+
append("s")
122+
}
123+
}
124+
}
125+
}
126+
}

common/src/main/resources/lambda.accesswidener

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ accessible field net/minecraft/client/MinecraftClient pausedTickDelta F
77

88
# World
99
accessible field net/minecraft/client/world/ClientWorld entityManager Lnet/minecraft/client/world/ClientEntityManager;
10+
accessible field net/minecraft/client/world/ClientEntityManager cache Lnet/minecraft/world/entity/SectionedEntityCache;
11+
accessible field net/minecraft/world/entity/EntityTrackingSection collection Lnet/minecraft/util/collection/TypeFilterableList;
1012

1113
# Entity
1214
accessible field net/minecraft/entity/projectile/FireworkRocketEntity shooter Lnet/minecraft/entity/LivingEntity;
@@ -30,5 +32,4 @@ accessible field net/minecraft/text/Style font Lnet/minecraft/util/Identifier;
3032
accessible method net/minecraft/text/Style <init> (Lnet/minecraft/text/TextColor;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Lnet/minecraft/text/ClickEvent;Lnet/minecraft/text/HoverEvent;Ljava/lang/String;Lnet/minecraft/util/Identifier;)V
3133

3234
# Other
33-
accessible field net/minecraft/client/world/ClientEntityManager cache Lnet/minecraft/world/entity/SectionedEntityCache;
34-
accessible field net/minecraft/world/entity/EntityTrackingSection collection Lnet/minecraft/util/collection/TypeFilterableList;
35+
accessible field net/minecraft/structure/StructureTemplate blockInfoLists Ljava/util/List;

0 commit comments

Comments
 (0)