Skip to content

Commit 04f57cf

Browse files
committed
✨ Setting to mirror schematics
1 parent 8fad0b5 commit 04f57cf

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

src/api/java/baritone/api/Settings.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.minecraft.world.item.Item;
3030
import net.minecraft.world.level.block.Block;
3131
import net.minecraft.world.level.block.Blocks;
32+
import net.minecraft.world.level.block.Mirror;
3233
import net.minecraft.world.level.block.Rotation;
3334

3435
import org.slf4j.Logger;
@@ -1097,6 +1098,16 @@ public final class Settings {
10971098
*/
10981099
public final Setting<Rotation> buildSchematicRotation = new Setting<>(Rotation.NONE);
10991100

1101+
/**
1102+
* Mirrors the schematic before building it.
1103+
* Possible values are
1104+
* <ul>
1105+
* <li> "front_back" or "fb": mirror the schematic along its local x axis </li>
1106+
* <li> "left_right" or "lr": mirror the schematic along its local z axis </li>
1107+
* </ul>
1108+
*/
1109+
public final Setting<Mirror> buildSchematicMirror = new Setting<>(Mirror.NONE);
1110+
11001111
/**
11011112
* The fallback used by the build command when no extension is specified. This may be useful if schematics of a
11021113
* particular format are used often, and the user does not wish to have to specify the extension with every usage.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* This file is part of Baritone.
3+
*
4+
* Baritone is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Baritone is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package baritone.api.schematic;
19+
20+
import net.minecraft.world.level.block.state.BlockState;
21+
import net.minecraft.world.level.block.Mirror;
22+
23+
import java.util.List;
24+
import java.util.stream.Collectors;
25+
26+
public class MirroredSchematic implements ISchematic {
27+
28+
private final ISchematic schematic;
29+
private final Mirror mirror;
30+
31+
public MirroredSchematic(ISchematic schematic, Mirror mirror) {
32+
this.schematic = schematic;
33+
this.mirror = mirror;
34+
}
35+
36+
@Override
37+
public boolean inSchematic(int x, int y, int z, BlockState currentState) {
38+
return schematic.inSchematic(
39+
mirrorX(x, widthX(), mirror),
40+
y,
41+
mirrorZ(z, lengthZ(), mirror),
42+
mirror(currentState, mirror)
43+
);
44+
}
45+
46+
@Override
47+
public BlockState desiredState(int x, int y, int z, BlockState current, List<BlockState> approxPlaceable) {
48+
return mirror(schematic.desiredState(
49+
mirrorX(x, widthX(), mirror),
50+
y,
51+
mirrorZ(z, lengthZ(), mirror),
52+
mirror(current, mirror),
53+
mirror(approxPlaceable, mirror)
54+
), mirror);
55+
}
56+
57+
@Override
58+
public void reset() {
59+
schematic.reset();
60+
}
61+
62+
@Override
63+
public int widthX() {
64+
return schematic.widthX();
65+
}
66+
67+
@Override
68+
public int heightY() {
69+
return schematic.heightY();
70+
}
71+
72+
@Override
73+
public int lengthZ() {
74+
return schematic.lengthZ();
75+
}
76+
77+
private static int mirrorX(int x, int sizeX, Mirror mirror) {
78+
switch (mirror) {
79+
case NONE:
80+
case LEFT_RIGHT:
81+
return x;
82+
case FRONT_BACK:
83+
return sizeX - x - 1;
84+
}
85+
throw new IllegalArgumentException("Unknown mirror");
86+
}
87+
88+
private static int mirrorZ(int z, int sizeZ, Mirror mirror) {
89+
switch (mirror) {
90+
case NONE:
91+
case FRONT_BACK:
92+
return z;
93+
case LEFT_RIGHT:
94+
return sizeZ - z - 1;
95+
}
96+
throw new IllegalArgumentException("Unknown mirror");
97+
}
98+
99+
private static BlockState mirror(BlockState state, Mirror mirror) {
100+
if (state == null) {
101+
return null;
102+
}
103+
return state.mirror(mirror);
104+
}
105+
106+
private static List<BlockState> mirror(List<BlockState> states, Mirror mirror) {
107+
if (states == null) {
108+
return null;
109+
}
110+
return states.stream()
111+
.map(s -> mirror(s, mirror))
112+
.collect(Collectors.toList());
113+
}
114+
}

src/api/java/baritone/api/utils/SettingsUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.minecraft.resources.ResourceLocation;
2828
import net.minecraft.world.item.Item;
2929
import net.minecraft.world.level.block.Block;
30+
import net.minecraft.world.level.block.Mirror;
3031
import net.minecraft.world.level.block.Rotation;
3132

3233
import java.awt.*;
@@ -222,6 +223,7 @@ private enum Parser implements ISettingParser {
222223
FLOAT(Float.class, Float::parseFloat),
223224
LONG(Long.class, Long::parseLong),
224225
STRING(String.class, String::new),
226+
MIRROR(Mirror.class, Mirror::valueOf, Mirror::name),
225227
ROTATION(Rotation.class, Rotation::valueOf, Rotation::name),
226228
COLOR(
227229
Color.class,

src/main/java/baritone/process/BuilderProcess.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import baritone.api.schematic.MaskSchematic;
3232
import baritone.api.schematic.SubstituteSchematic;
3333
import baritone.api.schematic.RotatedSchematic;
34+
import baritone.api.schematic.MirroredSchematic;
3435
import baritone.api.schematic.format.ISchematicFormat;
3536
import baritone.api.utils.*;
3637
import baritone.api.utils.input.Input;
@@ -120,6 +121,9 @@ public void build(String name, ISchematic schematic, Vec3i origin) {
120121
if (!Baritone.settings().buildSubstitutes.value.isEmpty()) {
121122
this.schematic = new SubstituteSchematic(this.schematic, Baritone.settings().buildSubstitutes.value);
122123
}
124+
if (Baritone.settings().buildSchematicMirror.value != net.minecraft.world.level.block.Mirror.NONE) {
125+
this.schematic = new MirroredSchematic(this.schematic, Baritone.settings().buildSchematicMirror.value);
126+
}
123127
if (Baritone.settings().buildSchematicRotation.value != net.minecraft.world.level.block.Rotation.NONE) {
124128
this.schematic = new RotatedSchematic(this.schematic, Baritone.settings().buildSchematicRotation.value);
125129
}

0 commit comments

Comments
 (0)