2018-08-08 03:16:53 +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 General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2018-08-08 04:15:22 +00:00
|
|
|
* Baritone is distributed in the hope that it will be useful,
|
2018-08-08 03:16:53 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-08-22 20:15:56 +00:00
|
|
|
package baritone.utils;
|
2018-08-02 17:25:46 +00:00
|
|
|
|
2018-08-22 20:15:56 +00:00
|
|
|
import baritone.Baritone;
|
2018-09-11 17:28:03 +00:00
|
|
|
import baritone.cache.CachedRegion;
|
|
|
|
import baritone.cache.WorldData;
|
|
|
|
import baritone.cache.WorldProvider;
|
2018-08-02 17:25:46 +00:00
|
|
|
import net.minecraft.block.Block;
|
2018-08-06 21:32:54 +00:00
|
|
|
import net.minecraft.block.BlockLiquid;
|
2018-08-02 17:25:46 +00:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2018-08-06 21:32:54 +00:00
|
|
|
import net.minecraft.init.Blocks;
|
2018-08-02 17:25:46 +00:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2018-08-08 00:41:13 +00:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
|
|
|
|
2018-09-09 15:53:15 +00:00
|
|
|
/**
|
|
|
|
* Wraps get for chuck caching capability
|
|
|
|
*
|
|
|
|
* @author leijurv
|
|
|
|
*/
|
2018-08-08 00:41:13 +00:00
|
|
|
public class BlockStateInterface implements Helper {
|
2018-08-02 17:25:46 +00:00
|
|
|
|
2018-08-31 18:51:43 +00:00
|
|
|
private static Chunk prev = null;
|
2018-09-09 16:50:19 +00:00
|
|
|
private static CachedRegion prevCached = null;
|
|
|
|
|
|
|
|
private static IBlockState AIR = Blocks.AIR.getDefaultState();
|
|
|
|
public static final Block waterFlowing = Blocks.FLOWING_WATER;
|
|
|
|
public static final Block waterStill = Blocks.WATER;
|
|
|
|
public static final Block lavaFlowing = Blocks.FLOWING_LAVA;
|
|
|
|
public static final Block lavaStill = Blocks.LAVA;
|
2018-09-01 19:26:55 +00:00
|
|
|
|
2018-09-09 15:53:15 +00:00
|
|
|
public static IBlockState get(BlockPos pos) {
|
|
|
|
return get(pos.getX(), pos.getY(), pos.getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IBlockState get(int x, int y, int z) {
|
2018-08-08 00:41:13 +00:00
|
|
|
|
|
|
|
// Invalid vertical position
|
2018-09-09 15:53:15 +00:00
|
|
|
if (y < 0 || y >= 256) {
|
2018-09-09 16:50:19 +00:00
|
|
|
return AIR;
|
2018-09-08 04:32:25 +00:00
|
|
|
}
|
2018-08-08 00:41:13 +00:00
|
|
|
|
2018-08-22 19:04:44 +00:00
|
|
|
if (!Baritone.settings().pathThroughCachedOnly.get()) {
|
2018-08-31 18:51:43 +00:00
|
|
|
Chunk cached = prev;
|
|
|
|
// there's great cache locality in block state lookups
|
|
|
|
// generally it's within each movement
|
|
|
|
// if it's the same chunk as last time
|
|
|
|
// we can just skip the mc.world.getChunk lookup
|
|
|
|
// which is a Long2ObjectOpenHashMap.get
|
2018-09-09 15:53:15 +00:00
|
|
|
if (cached != null && cached.x == x >> 4 && cached.z == z >> 4) {
|
|
|
|
return cached.getBlockState(x, y, z);
|
2018-08-31 18:51:43 +00:00
|
|
|
}
|
2018-09-09 15:53:15 +00:00
|
|
|
Chunk chunk = mc.world.getChunk(x >> 4, z >> 4);
|
2018-08-22 19:04:44 +00:00
|
|
|
if (chunk.isLoaded()) {
|
2018-08-31 18:51:43 +00:00
|
|
|
prev = chunk;
|
2018-09-09 15:53:15 +00:00
|
|
|
return chunk.getBlockState(x, y, z);
|
2018-08-22 19:04:44 +00:00
|
|
|
}
|
2018-08-14 03:17:16 +00:00
|
|
|
}
|
2018-09-09 16:50:19 +00:00
|
|
|
// same idea here, skip the Long2ObjectOpenHashMap.get if at all possible
|
|
|
|
// except here, it's 512x512 tiles instead of 16x16, so even better repetition
|
|
|
|
CachedRegion cached = prevCached;
|
|
|
|
if (cached != null && cached.getX() == x >> 9 && cached.getZ() == z >> 9) {
|
|
|
|
IBlockState type = cached.getBlock(x & 511, y, z & 511);
|
|
|
|
if (type == null) {
|
|
|
|
return AIR;
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
2018-08-23 20:39:13 +00:00
|
|
|
WorldData world = WorldProvider.INSTANCE.getCurrentWorld();
|
|
|
|
if (world != null) {
|
2018-09-09 16:50:19 +00:00
|
|
|
CachedRegion region = world.cache.getRegion(x >> 9, z >> 9);
|
|
|
|
if (region != null) {
|
|
|
|
prevCached = region;
|
2018-09-09 16:55:59 +00:00
|
|
|
IBlockState type = region.getBlock(x & 511, y, z & 511);
|
|
|
|
if (type != null) {
|
|
|
|
return type;
|
|
|
|
}
|
2018-08-08 00:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-09 16:50:19 +00:00
|
|
|
return AIR;
|
2018-08-02 17:25:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 19:26:55 +00:00
|
|
|
public static void clearCachedChunk() {
|
|
|
|
prev = null;
|
2018-09-09 16:50:19 +00:00
|
|
|
prevCached = null;
|
2018-09-01 19:26:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:25:46 +00:00
|
|
|
public static Block getBlock(BlockPos pos) {
|
|
|
|
return get(pos).getBlock();
|
|
|
|
}
|
2018-08-06 21:32:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the specified block is
|
|
|
|
* water, regardless of whether or not it is flowing.
|
|
|
|
*
|
|
|
|
* @param b The block
|
|
|
|
* @return Whether or not the block is water
|
|
|
|
*/
|
|
|
|
public static boolean isWater(Block b) {
|
2018-09-09 15:53:15 +00:00
|
|
|
return b == waterFlowing || b == waterStill;
|
2018-08-06 21:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the block at the specified pos is
|
|
|
|
* water, regardless of whether or not it is flowing.
|
|
|
|
*
|
|
|
|
* @param bp The block pos
|
|
|
|
* @return Whether or not the block is water
|
|
|
|
*/
|
|
|
|
public static boolean isWater(BlockPos bp) {
|
2018-08-06 22:52:12 +00:00
|
|
|
return isWater(BlockStateInterface.getBlock(bp));
|
2018-08-06 21:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isLava(Block b) {
|
2018-09-09 15:53:15 +00:00
|
|
|
return b == lavaFlowing || b == lavaStill;
|
2018-08-06 21:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-07 01:18:16 +00:00
|
|
|
* Returns whether or not the specified pos has a liquid
|
2018-08-06 21:32:54 +00:00
|
|
|
*
|
2018-08-07 01:18:16 +00:00
|
|
|
* @param p The pos
|
2018-08-06 21:32:54 +00:00
|
|
|
* @return Whether or not the block is a liquid
|
|
|
|
*/
|
|
|
|
public static boolean isLiquid(BlockPos p) {
|
2018-08-07 01:18:16 +00:00
|
|
|
return BlockStateInterface.getBlock(p) instanceof BlockLiquid;
|
2018-08-06 21:32:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 22:56:13 +00:00
|
|
|
public static boolean isFlowing(IBlockState state) {
|
2018-08-07 01:25:46 +00:00
|
|
|
// Will be IFluidState in 1.13
|
2018-08-06 21:32:54 +00:00
|
|
|
return state.getBlock() instanceof BlockLiquid
|
|
|
|
&& state.getPropertyKeys().contains(BlockLiquid.LEVEL)
|
|
|
|
&& state.getValue(BlockLiquid.LEVEL) != 0;
|
|
|
|
}
|
2018-08-02 17:25:46 +00:00
|
|
|
}
|