Skip to content

Commit adff391

Browse files
committed
Merge branch 'schematic-formats'
2 parents a0014fd + 4a05837 commit adff391

20 files changed

+672
-106
lines changed

src/api/java/baritone/api/IBaritoneProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import baritone.api.cache.IWorldScanner;
2121
import baritone.api.command.ICommand;
2222
import baritone.api.command.ICommandSystem;
23+
import baritone.api.schematic.ISchematicSystem;
2324
import net.minecraft.client.entity.EntityPlayerSP;
2425

2526
import java.util.List;
@@ -82,4 +83,9 @@ default IBaritone getBaritoneForPlayer(EntityPlayerSP player) {
8283
* @return The {@link ICommandSystem} instance.
8384
*/
8485
ICommandSystem getCommandSystem();
86+
87+
/**
88+
* @return The {@link ISchematicSystem} instance.
89+
*/
90+
ISchematicSystem getSchematicSystem();
8591
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,12 @@ public final class Settings {
811811
*/
812812
public final Setting<Boolean> schematicOrientationZ = new Setting<>(false);
813813

814+
/**
815+
* The fallback used by the build command when no extension is specified. This may be useful if schematics of a
816+
* particular format are used often, and the user does not wish to have to specify the extension with every usage.
817+
*/
818+
public final Setting<String> schematicFallbackExtension = new Setting<>("schematic");
819+
814820
/**
815821
* Distance to scan every tick for updates. Expanding this beyond player reach distance (i.e. setting it to 6 or above)
816822
* is only necessary in very large schematics where rescanning the whole thing is costly.

src/api/java/baritone/api/schematic/AbstractSchematic.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public abstract class AbstractSchematic implements ISchematic {
2323
protected int y;
2424
protected int z;
2525

26+
public AbstractSchematic() {
27+
this(0, 0, 0);
28+
}
29+
2630
public AbstractSchematic(int x, int y, int z) {
2731
this.x = x;
2832
this.y = y;

src/api/java/baritone/api/schematic/FillSchematic.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public FillSchematic(int x, int y, int z, BlockOptionalMeta bom) {
3232
this.bom = bom;
3333
}
3434

35+
public FillSchematic(int x, int y, int z, IBlockState state) {
36+
this(x, y, z, new BlockOptionalMeta(state.getBlock(), state.getBlock().getMetaFromState(state)));
37+
}
38+
3539
public BlockOptionalMeta getBom() {
3640
return bom;
3741
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 baritone.api.command.registry.Registry;
21+
import baritone.api.schematic.format.ISchematicFormat;
22+
23+
import java.io.File;
24+
import java.util.Optional;
25+
26+
/**
27+
* @author Brady
28+
* @since 12/23/2019
29+
*/
30+
public interface ISchematicSystem {
31+
32+
/**
33+
* @return The registry of supported schematic formats
34+
*/
35+
Registry<ISchematicFormat> getRegistry();
36+
37+
/**
38+
* Attempts to find an {@link ISchematicFormat} that supports the specified schematic file.
39+
*
40+
* @param file A schematic file
41+
* @return The corresponding format for the file, {@link Optional#empty()} if no candidates were found.
42+
*/
43+
Optional<ISchematicFormat> getByFile(File file);
44+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.block.state.IBlockState;
21+
22+
/**
23+
* A static schematic is capable of providing the desired state at a given position without
24+
* additional context. Schematics of this type are expected to have non-varying contents.
25+
*
26+
* @see #getDirect(int, int, int)
27+
*
28+
* @author Brady
29+
* @since 12/24/2019
30+
*/
31+
public interface IStaticSchematic extends ISchematic {
32+
33+
/**
34+
* Gets the {@link IBlockState} for a given position in this schematic. It should be guaranteed
35+
* that the return value of this method will not change given that the parameters are the same.
36+
*
37+
* @param x The X block position
38+
* @param y The Y block position
39+
* @param z The Z block position
40+
* @return The desired state at the specified position.
41+
*/
42+
IBlockState getDirect(int x, int y, int z);
43+
44+
/**
45+
* Returns an {@link IBlockState} array of size {@link #heightY()} which contains all
46+
* desired block states in the specified vertical column. The index of {@link IBlockState}s
47+
* in the array are equivalent to their Y position in the schematic.
48+
*
49+
* @param x The X column position
50+
* @param z The Z column position
51+
* @return An {@link IBlockState} array
52+
*/
53+
default IBlockState[] getColumn(int x, int z) {
54+
IBlockState[] column = new IBlockState[this.heightY()];
55+
for (int i = 0; i < this.heightY(); i++) {
56+
column[i] = getDirect(x, i, z);
57+
}
58+
return column;
59+
}
60+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.format;
19+
20+
import baritone.api.schematic.ISchematic;
21+
import baritone.api.schematic.IStaticSchematic;
22+
23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
27+
/**
28+
* The base of a {@link ISchematic} file format
29+
*
30+
* @author Brady
31+
* @since 12/23/2019
32+
*/
33+
public interface ISchematicFormat {
34+
35+
/**
36+
* @return The parser for creating schematics of this format
37+
*/
38+
IStaticSchematic parse(InputStream input) throws IOException;
39+
40+
/**
41+
* @param file The file to check against
42+
* @return Whether or not the specified file matches this schematic format
43+
*/
44+
boolean isFileType(File file);
45+
}

src/api/java/baritone/api/utils/BlockOptionalMeta.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,27 @@ public BlockOptionalMeta(@Nonnull String selector) {
178178
normalizations = Collections.unmodifiableMap(_normalizations);
179179
}
180180

181-
private static <C extends Comparable<C>, P extends IProperty<C>> P castToIProperty(Object value) {
181+
public static <C extends Comparable<C>, P extends IProperty<C>> P castToIProperty(Object value) {
182182
//noinspection unchecked
183183
return (P) value;
184184
}
185185

186-
private static <C extends Comparable<C>, P extends IProperty<C>> C castToIPropertyValue(P iproperty, Object value) {
186+
public static <C extends Comparable<C>, P extends IProperty<C>> C castToIPropertyValue(P iproperty, Object value) {
187187
//noinspection unchecked
188188
return (C) value;
189189
}
190190

191+
/**
192+
* Normalizes the specified blockstate by setting meta-affecting properties which
193+
* are not being targeted by the meta parameter to their default values.
194+
* <p>
195+
* For example, block variant/color is the primary target for the meta value, so properties
196+
* such as rotation/facing direction will be set to default values in order to nullify
197+
* the effect that they have on the state's meta value.
198+
*
199+
* @param state The state to normalize
200+
* @return The normalized block state
201+
*/
191202
public static IBlockState normalize(IBlockState state) {
192203
IBlockState newState = state;
193204

@@ -220,6 +231,15 @@ public static IBlockState normalize(IBlockState state) {
220231
return newState;
221232
}
222233

234+
/**
235+
* Evaluate the target meta value for the specified state. The target meta value is
236+
* most often that which is influenced by the variant/color property of the block state.
237+
*
238+
* @see #normalize(IBlockState)
239+
*
240+
* @param state The state to check
241+
* @return The target meta of the state
242+
*/
223243
public static int stateMeta(IBlockState state) {
224244
return state.getBlock().getMetaFromState(normalize(state));
225245
}

src/main/java/baritone/BaritoneProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import baritone.api.IBaritoneProvider;
2222
import baritone.api.cache.IWorldScanner;
2323
import baritone.api.command.ICommandSystem;
24+
import baritone.api.schematic.ISchematicSystem;
2425
import baritone.command.BaritoneChatControl;
2526
import baritone.cache.WorldScanner;
2627
import baritone.command.CommandSystem;
28+
import baritone.utils.schematic.SchematicSystem;
2729

2830
import java.util.Collections;
2931
import java.util.List;
@@ -64,4 +66,9 @@ public IWorldScanner getWorldScanner() {
6466
public ICommandSystem getCommandSystem() {
6567
return CommandSystem.INSTANCE;
6668
}
69+
70+
@Override
71+
public ISchematicSystem getSchematicSystem() {
72+
return SchematicSystem.INSTANCE;
73+
}
6774
}

src/main/java/baritone/command/defaults/BuildCommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package baritone.command.defaults;
1919

20+
import baritone.Baritone;
2021
import baritone.api.IBaritone;
2122
import baritone.api.utils.BetterBlockPos;
2223
import baritone.api.command.Command;
@@ -26,11 +27,11 @@
2627
import baritone.api.command.exception.CommandInvalidStateException;
2728
import baritone.api.command.argument.IArgConsumer;
2829
import net.minecraft.client.Minecraft;
30+
import org.apache.commons.io.FilenameUtils;
2931

3032
import java.io.File;
3133
import java.util.Arrays;
3234
import java.util.List;
33-
import java.util.Locale;
3435
import java.util.stream.Stream;
3536

3637
public class BuildCommand extends Command {
@@ -44,8 +45,8 @@ public BuildCommand(IBaritone baritone) {
4445
@Override
4546
public void execute(String label, IArgConsumer args) throws CommandException {
4647
File file = args.getDatatypePost(RelativeFile.INSTANCE, schematicsDir).getAbsoluteFile();
47-
if (!file.getName().toLowerCase(Locale.US).endsWith(".schematic")) {
48-
file = new File(file.getAbsolutePath() + ".schematic");
48+
if (FilenameUtils.getExtension(file.getAbsolutePath()).isEmpty()) {
49+
file = new File(file.getAbsolutePath() + "." + Baritone.settings().schematicFallbackExtension);
4950
}
5051
BetterBlockPos origin = ctx.playerFeet();
5152
BetterBlockPos buildOrigin;

0 commit comments

Comments
 (0)