baritone/src/main/java/baritone/utils/schematic/MapArtSchematic.java

71 lines
2.5 KiB
Java
Raw Normal View History

2019-01-09 04:45:02 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2019-01-16 19:45:32 +00:00
package baritone.utils.schematic;
2019-01-09 04:45:02 +00:00
2019-12-24 23:20:00 +00:00
import baritone.api.schematic.IStaticSchematic;
2019-12-19 17:58:47 +00:00
import baritone.api.schematic.MaskSchematic;
2019-12-24 23:20:00 +00:00
import net.minecraft.block.BlockAir;
2019-01-09 04:45:02 +00:00
import net.minecraft.block.state.IBlockState;
2019-12-24 23:20:00 +00:00
import java.util.OptionalInt;
import java.util.function.Predicate;
2019-12-19 17:58:47 +00:00
public class MapArtSchematic extends MaskSchematic {
2019-01-16 19:45:32 +00:00
2019-01-09 04:45:02 +00:00
private final int[][] heightMap;
2019-12-24 23:20:00 +00:00
public MapArtSchematic(IStaticSchematic schematic) {
2019-12-19 17:58:47 +00:00
super(schematic);
2019-12-24 23:20:00 +00:00
this.heightMap = generateHeightMap(schematic);
2019-01-09 04:45:02 +00:00
}
@Override
2019-12-19 17:58:47 +00:00
protected boolean partOfMask(int x, int y, int z, IBlockState currentState) {
2019-12-24 23:20:00 +00:00
return y >= this.heightMap[x][z];
}
private static int[][] generateHeightMap(IStaticSchematic schematic) {
int[][] heightMap = new int[schematic.widthX()][schematic.lengthZ()];
for (int x = 0; x < schematic.widthX(); x++) {
for (int z = 0; z < schematic.lengthZ(); z++) {
IBlockState[] column = schematic.getColumn(x, z);
OptionalInt lowestBlockY = lastIndexMatching(column, state -> !(state.getBlock() instanceof BlockAir));
if (lowestBlockY.isPresent()) {
heightMap[x][z] = lowestBlockY.getAsInt();
} else {
System.out.println("Column " + x + "," + z + " has no blocks, but it's apparently map art? wtf");
System.out.println("Letting it be whatever");
heightMap[x][z] = 256;
}
}
}
return heightMap;
}
private static <T> OptionalInt lastIndexMatching(T[] arr, Predicate<? super T> predicate) {
for (int y = arr.length - 1; y >= 0; y--) {
if (predicate.test(arr[y])) {
return OptionalInt.of(y);
}
}
return OptionalInt.empty();
2019-12-18 16:24:43 +00:00
}
2019-01-09 04:45:02 +00:00
}