| 1 | package net.minecraft.world.level.material;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2ByteLinkedOpenHashMap;
|
| 5 | import it.unimi.dsi.fastutil.shorts.Short2BooleanMap;
|
| 6 | import it.unimi.dsi.fastutil.shorts.Short2BooleanOpenHashMap;
|
| 7 | import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
|
| 8 | import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Map.Entry;
|
| 11 | import net.minecraft.SharedConstants;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.core.Direction;
|
| 14 | import net.minecraft.server.level.ServerLevel;
|
| 15 | import net.minecraft.tags.BlockTags;
|
| 16 | import net.minecraft.world.level.BlockGetter;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.LevelAccessor;
|
| 19 | import net.minecraft.world.level.LevelReader;
|
| 20 | import net.minecraft.world.level.block.Block;
|
| 21 | import net.minecraft.world.level.block.Blocks;
|
| 22 | import net.minecraft.world.level.block.IceBlock;
|
| 23 | import net.minecraft.world.level.block.LiquidBlockContainer;
|
| 24 | import net.minecraft.world.level.block.state.BlockState;
|
| 25 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 26 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 27 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 28 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 29 | import net.minecraft.world.phys.Vec3;
|
| 30 | import net.minecraft.world.phys.shapes.Shapes;
|
| 31 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 32 |
|
| 33 | public abstract class FlowingFluid extends Fluid {
|
| 34 | public static final BooleanProperty FALLING = BlockStateProperties.FALLING;
|
| 35 | public static final IntegerProperty LEVEL = BlockStateProperties.LEVEL_FLOWING;
|
| 36 | private static final int CACHE_SIZE = 200;
|
| 37 | private static final ThreadLocal<Object2ByteLinkedOpenHashMap<FlowingFluid.BlockStatePairKey>> OCCLUSION_CACHE = ThreadLocal.withInitial(() -> {
|
| 38 | Object2ByteLinkedOpenHashMap<FlowingFluid.BlockStatePairKey> map = new Object2ByteLinkedOpenHashMap<FlowingFluid.BlockStatePairKey>(200) {
|
| 39 | @Override
|
| 40 | protected void rehash(final int newN) {
|
| 41 | }
|
| 42 | };
|
| 43 | map.defaultReturnValue((byte)127);
|
| 44 | return map;
|
| 45 | });
|
| 46 | private final Map<FluidState, VoxelShape> shapes = Maps.newIdentityHashMap();
|
| 47 |
|
| 48 | @Override
|
| 49 | protected void createFluidStateDefinition(final StateDefinition.Builder<Fluid, FluidState> builder) {
|
| 50 | builder.add(FALLING);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public Vec3 getFlow(final BlockGetter level, final BlockPos pos, final FluidState fluidState) {
|
| 55 | double flowX = 0.0;
|
| 56 | double flowZ = 0.0;
|
| 57 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 58 |
|
| 59 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 60 | blockPos.setWithOffset(pos, direction);
|
| 61 | FluidState neighbourFluid = level.getFluidState(blockPos);
|
| 62 | if (this.affectsFlow(neighbourFluid)) {
|
| 63 | float neighborHeight = neighbourFluid.getOwnHeight();
|
| 64 | float distance = 0.0F;
|
| 65 | if (neighborHeight == 0.0F) {
|
| 66 | if (!level.getBlockState(blockPos).is(BlockTags.BLOCKS_FLUID_FLOW)) {
|
| 67 | BlockPos neighborPos = blockPos.below();
|
| 68 | FluidState belowNeighborState = level.getFluidState(neighborPos);
|
| 69 | if (this.affectsFlow(belowNeighborState)) {
|
| 70 | neighborHeight = belowNeighborState.getOwnHeight();
|
| 71 | if (neighborHeight > 0.0F) {
|
| 72 | distance = fluidState.getOwnHeight() - (neighborHeight - 0.8888889F);
|
| 73 | }
|
| 74 | }
|
| 75 | }
|
| 76 | } else if (neighborHeight > 0.0F) {
|
| 77 | distance = fluidState.getOwnHeight() - neighborHeight;
|
| 78 | }
|
| 79 |
|
| 80 | if (distance != 0.0F) {
|
| 81 | flowX += direction.getStepX() * distance;
|
| 82 | flowZ += direction.getStepZ() * distance;
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | Vec3 flow = new Vec3(flowX, 0.0, flowZ);
|
| 88 | if (fluidState.getValue(FALLING)) {
|
| 89 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 90 | blockPos.setWithOffset(pos, direction);
|
| 91 | if (this.isSolidFace(level, blockPos, direction) || this.isSolidFace(level, blockPos.above(), direction)) {
|
| 92 | flow = flow.normalize().add(0.0, -6.0, 0.0);
|
| 93 | break;
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | return flow.normalize();
|
| 99 | }
|
| 100 |
|
| 101 | private boolean affectsFlow(final FluidState neighbourFluid) {
|
| 102 | return neighbourFluid.isEmpty() || neighbourFluid.getType().isSame(this);
|
| 103 | }
|
| 104 |
|
| 105 | protected boolean isSolidFace(final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 106 | BlockState state = level.getBlockState(pos);
|
| 107 | FluidState fluidState = level.getFluidState(pos);
|
| 108 | if (fluidState.getType().isSame(this)) {
|
| 109 | return false;
|
| 110 | } else if (direction == Direction.UP) {
|
| 111 | return true;
|
| 112 | } else {
|
| 113 | return state.getBlock() instanceof IceBlock ? false : state.isFaceSturdy(level, pos, direction);
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | protected void spread(final ServerLevel level, final BlockPos pos, final BlockState state, final FluidState fluidState) {
|
| 118 | if (!fluidState.isEmpty()) {
|
| 119 | BlockPos belowPos = pos.below();
|
| 120 | BlockState belowState = level.getBlockState(belowPos);
|
| 121 | FluidState belowFluid = belowState.getFluidState();
|
| 122 | if (this.canMaybePassThrough(level, pos, state, Direction.DOWN, belowPos, belowState, belowFluid)) {
|
| 123 | FluidState newBelowFluid = this.getNewLiquid(level, belowPos, belowState);
|
| 124 | Fluid newBelowFluidType = newBelowFluid.getType();
|
| 125 | if (belowFluid.canBeReplacedWith(level, belowPos, newBelowFluidType, Direction.DOWN)
|
| 126 | && canHoldSpecificFluid(level, belowPos, belowState, newBelowFluidType)) {
|
| 127 | this.spreadTo(level, belowPos, belowState, Direction.DOWN, newBelowFluid);
|
| 128 | if (this.sourceNeighborCount(level, pos) >= 3) {
|
| 129 | this.spreadToSides(level, pos, fluidState, state);
|
| 130 | }
|
| 131 |
|
| 132 | return;
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | if (fluidState.isSource() || !this.isWaterHole(level, pos, state, belowPos, belowState)) {
|
| 137 | this.spreadToSides(level, pos, fluidState, state);
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | private void spreadToSides(final ServerLevel level, final BlockPos pos, final FluidState fluidState, final BlockState state) {
|
| 143 | int neighbor = fluidState.getAmount() - this.getDropOff(level);
|
| 144 | if (fluidState.getValue(FALLING)) {
|
| 145 | neighbor = 7;
|
| 146 | }
|
| 147 |
|
| 148 | if (neighbor > 0) {
|
| 149 | Map<Direction, FluidState> spreads = this.getSpread(level, pos, state);
|
| 150 |
|
| 151 | for (Entry<Direction, FluidState> entry : spreads.entrySet()) {
|
| 152 | Direction spread = entry.getKey();
|
| 153 | FluidState newNeighborFluid = entry.getValue();
|
| 154 | BlockPos neighborPos = pos.relative(spread);
|
| 155 | this.spreadTo(level, neighborPos, level.getBlockState(neighborPos), spread, newNeighborFluid);
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | protected FluidState getNewLiquid(final ServerLevel level, final BlockPos pos, final BlockState state) {
|
| 161 | int highestNeighbor = 0;
|
| 162 | int neighbourSources = 0;
|
| 163 | BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
|
| 164 |
|
| 165 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 166 | BlockPos relativePos = mutablePos.setWithOffset(pos, direction);
|
| 167 | BlockState blockState = level.getBlockState(relativePos);
|
| 168 | FluidState fluidState = blockState.getFluidState();
|
| 169 | if (fluidState.getType().isSame(this) && canPassThroughWall(direction, level, pos, state, relativePos, blockState)) {
|
| 170 | if (fluidState.isSource()) {
|
| 171 | neighbourSources++;
|
| 172 | }
|
| 173 |
|
| 174 | highestNeighbor = Math.max(highestNeighbor, fluidState.getAmount());
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | if (neighbourSources >= 2 && this.canConvertToSource(level)) {
|
| 179 | BlockState belowState = level.getBlockState(mutablePos.setWithOffset(pos, Direction.DOWN));
|
| 180 | FluidState belowFluid = belowState.getFluidState();
|
| 181 | if (belowState.isSolid() || this.isSourceBlockOfThisType(belowFluid)) {
|
| 182 | return this.getSource(false);
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | BlockPos abovePos = mutablePos.setWithOffset(pos, Direction.UP);
|
| 187 | BlockState aboveState = level.getBlockState(abovePos);
|
| 188 | FluidState aboveFluid = aboveState.getFluidState();
|
| 189 | if (!aboveFluid.isEmpty() && aboveFluid.getType().isSame(this) && canPassThroughWall(Direction.UP, level, pos, state, abovePos, aboveState)) {
|
| 190 | return this.getFlowing(8, true);
|
| 191 | }
|
| 192 |
|
| 193 | int amount = highestNeighbor - this.getDropOff(level);
|
| 194 | return amount <= 0 ? Fluids.EMPTY.defaultFluidState() : this.getFlowing(amount, false);
|
| 195 | }
|
| 196 |
|
| 197 | private static boolean canPassThroughWall(
|
| 198 | final Direction direction,
|
| 199 | final BlockGetter level,
|
| 200 | final BlockPos sourcePos,
|
| 201 | final BlockState sourceState,
|
| 202 | final BlockPos targetPos,
|
| 203 | final BlockState targetState
|
| 204 | ) {
|
| 205 | if (!SharedConstants.DEBUG_DISABLE_LIQUID_SPREADING && (!SharedConstants.DEBUG_ONLY_GENERATE_HALF_THE_WORLD || targetPos.getZ() >= 0)) {
|
| 206 | VoxelShape targetShape = targetState.getCollisionShape(level, targetPos);
|
| 207 | if (targetShape == Shapes.block()) {
|
| 208 | return false;
|
| 209 | }
|
| 210 |
|
| 211 | VoxelShape sourceShape = sourceState.getCollisionShape(level, sourcePos);
|
| 212 | if (sourceShape == Shapes.block()) {
|
| 213 | return false;
|
| 214 | }
|
| 215 |
|
| 216 | if (sourceShape == Shapes.empty() && targetShape == Shapes.empty()) {
|
| 217 | return true;
|
| 218 | }
|
| 219 |
|
| 220 | Object2ByteLinkedOpenHashMap<FlowingFluid.BlockStatePairKey> cache;
|
| 221 | if (!sourceState.getBlock().hasDynamicShape() && !targetState.getBlock().hasDynamicShape()) {
|
| 222 | cache = OCCLUSION_CACHE.get();
|
| 223 | } else {
|
| 224 | cache = null;
|
| 225 | }
|
| 226 |
|
| 227 | FlowingFluid.BlockStatePairKey key;
|
| 228 | if (cache != null) {
|
| 229 | key = new FlowingFluid.BlockStatePairKey(sourceState, targetState, direction);
|
| 230 | byte cached = cache.getAndMoveToFirst(key);
|
| 231 | if (cached != 127) {
|
| 232 | return cached != 0;
|
| 233 | }
|
| 234 | } else {
|
| 235 | key = null;
|
| 236 | }
|
| 237 |
|
| 238 | boolean result = !Shapes.mergedFaceOccludes(sourceShape, targetShape, direction);
|
| 239 | if (cache != null) {
|
| 240 | if (cache.size() == 200) {
|
| 241 | cache.removeLastByte();
|
| 242 | }
|
| 243 |
|
| 244 | cache.putAndMoveToFirst(key, (byte)(result ? 1 : 0));
|
| 245 | }
|
| 246 |
|
| 247 | return result;
|
| 248 | } else {
|
| 249 | return false;
|
| 250 | }
|
| 251 | }
|
| 252 |
|
| 253 | public abstract Fluid getFlowing();
|
| 254 |
|
| 255 | public FluidState getFlowing(final int amount, final boolean falling) {
|
| 256 | return this.getFlowing().defaultFluidState().setValue(LEVEL, amount).setValue(FALLING, falling);
|
| 257 | }
|
| 258 |
|
| 259 | public abstract Fluid getSource();
|
| 260 |
|
| 261 | public FluidState getSource(final boolean falling) {
|
| 262 | return this.getSource().defaultFluidState().setValue(FALLING, falling);
|
| 263 | }
|
| 264 |
|
| 265 | protected abstract boolean canConvertToSource(ServerLevel level);
|
| 266 |
|
| 267 | protected void spreadTo(final LevelAccessor level, final BlockPos pos, final BlockState state, final Direction direction, final FluidState target) {
|
| 268 | if (state.getBlock() instanceof LiquidBlockContainer container) {
|
| 269 | container.placeLiquid(level, pos, state, target);
|
| 270 | } else {
|
| 271 | if (!state.isAir()) {
|
| 272 | this.beforeDestroyingBlock(level, pos, state);
|
| 273 | }
|
| 274 |
|
| 275 | level.setBlockAndUpdate(pos, target.createLegacyBlock());
|
| 276 | }
|
| 277 | }
|
| 278 |
|
| 279 | protected abstract void beforeDestroyingBlock(LevelAccessor level, BlockPos pos, BlockState state);
|
| 280 |
|
| 281 | protected int getSlopeDistance(
|
| 282 | final LevelReader level, final BlockPos pos, final int pass, final Direction from, final BlockState state, final FlowingFluid.SpreadContext context
|
| 283 | ) {
|
| 284 | int lowest = 1000;
|
| 285 |
|
| 286 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 287 | if (direction != from) {
|
| 288 | BlockPos testPos = pos.relative(direction);
|
| 289 | BlockState testState = context.getBlockState(testPos);
|
| 290 | FluidState testFluidState = testState.getFluidState();
|
| 291 | if (this.canPassThrough(level, this.getFlowing(), pos, state, direction, testPos, testState, testFluidState)) {
|
| 292 | if (context.isHole(testPos)) {
|
| 293 | return pass;
|
| 294 | }
|
| 295 |
|
| 296 | if (pass < this.getSlopeFindDistance(level)) {
|
| 297 | int v = this.getSlopeDistance(level, testPos, pass + 1, direction.getOpposite(), testState, context);
|
| 298 | if (v < lowest) {
|
| 299 | lowest = v;
|
| 300 | }
|
| 301 | }
|
| 302 | }
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | return lowest;
|
| 307 | }
|
| 308 |
|
| 309 | private boolean isWaterHole(final BlockGetter level, final BlockPos topPos, final BlockState topState, final BlockPos bottomPos, final BlockState bottomState) {
|
| 310 | if (!canPassThroughWall(Direction.DOWN, level, topPos, topState, bottomPos, bottomState)) {
|
| 311 | return false;
|
| 312 | } else {
|
| 313 | return bottomState.getFluidState().getType().isSame(this) ? true : canHoldFluid(level, bottomPos, bottomState, this.getFlowing());
|
| 314 | }
|
| 315 | }
|
| 316 |
|
| 317 | private boolean canPassThrough(
|
| 318 | final BlockGetter level,
|
| 319 | final Fluid fluid,
|
| 320 | final BlockPos sourcePos,
|
| 321 | final BlockState sourceState,
|
| 322 | final Direction direction,
|
| 323 | final BlockPos testPos,
|
| 324 | final BlockState testState,
|
| 325 | final FluidState testFluidState
|
| 326 | ) {
|
| 327 | return this.canMaybePassThrough(level, sourcePos, sourceState, direction, testPos, testState, testFluidState)
|
| 328 | && canHoldSpecificFluid(level, testPos, testState, fluid);
|
| 329 | }
|
| 330 |
|
| 331 | private boolean canMaybePassThrough(
|
| 332 | final BlockGetter level,
|
| 333 | final BlockPos sourcePos,
|
| 334 | final BlockState sourceState,
|
| 335 | final Direction direction,
|
| 336 | final BlockPos testPos,
|
| 337 | final BlockState testState,
|
| 338 | final FluidState testFluidState
|
| 339 | ) {
|
| 340 | return !this.isSourceBlockOfThisType(testFluidState)
|
| 341 | && canHoldAnyFluid(testState)
|
| 342 | && canPassThroughWall(direction, level, sourcePos, sourceState, testPos, testState);
|
| 343 | }
|
| 344 |
|
| 345 | private boolean isSourceBlockOfThisType(final FluidState state) {
|
| 346 | return state.getType().isSame(this) && state.isSource();
|
| 347 | }
|
| 348 |
|
| 349 | protected abstract int getSlopeFindDistance(LevelReader level);
|
| 350 |
|
| 351 | private int sourceNeighborCount(final LevelReader level, final BlockPos pos) {
|
| 352 | int count = 0;
|
| 353 |
|
| 354 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 355 | BlockPos testPos = pos.relative(direction);
|
| 356 | FluidState testFluidState = level.getFluidState(testPos);
|
| 357 | if (this.isSourceBlockOfThisType(testFluidState)) {
|
| 358 | count++;
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | return count;
|
| 363 | }
|
| 364 |
|
| 365 | protected Map<Direction, FluidState> getSpread(final ServerLevel level, final BlockPos pos, final BlockState state) {
|
| 366 | int lowest = 1000;
|
| 367 | Map<Direction, FluidState> result = Maps.newEnumMap(Direction.class);
|
| 368 | FlowingFluid.SpreadContext context = null;
|
| 369 |
|
| 370 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 371 | BlockPos testPos = pos.relative(direction);
|
| 372 | BlockState testState = level.getBlockState(testPos);
|
| 373 | FluidState testFluidState = testState.getFluidState();
|
| 374 | if (this.canMaybePassThrough(level, pos, state, direction, testPos, testState, testFluidState)) {
|
| 375 | FluidState newFluid = this.getNewLiquid(level, testPos, testState);
|
| 376 | if (canHoldSpecificFluid(level, testPos, testState, newFluid.getType())) {
|
| 377 | if (context == null) {
|
| 378 | context = new FlowingFluid.SpreadContext(level, pos);
|
| 379 | }
|
| 380 |
|
| 381 | int distance;
|
| 382 | if (context.isHole(testPos)) {
|
| 383 | distance = 0;
|
| 384 | } else {
|
| 385 | distance = this.getSlopeDistance(level, testPos, 1, direction.getOpposite(), testState, context);
|
| 386 | }
|
| 387 |
|
| 388 | if (distance < lowest) {
|
| 389 | result.clear();
|
| 390 | }
|
| 391 |
|
| 392 | if (distance <= lowest) {
|
| 393 | if (testFluidState.canBeReplacedWith(level, testPos, newFluid.getType(), direction)) {
|
| 394 | result.put(direction, newFluid);
|
| 395 | }
|
| 396 |
|
| 397 | lowest = distance;
|
| 398 | }
|
| 399 | }
|
| 400 | }
|
| 401 | }
|
| 402 |
|
| 403 | return result;
|
| 404 | }
|
| 405 |
|
| 406 | private static boolean canHoldAnyFluid(final BlockState state) {
|
| 407 | Block block = state.getBlock();
|
| 408 | return block instanceof LiquidBlockContainer ? true : state.is(BlockTags.WASHED_AWAY_BY_FLUIDS);
|
| 409 | }
|
| 410 |
|
| 411 | private static boolean canHoldFluid(final BlockGetter level, final BlockPos pos, final BlockState state, final Fluid newFluid) {
|
| 412 | return canHoldAnyFluid(state) && canHoldSpecificFluid(level, pos, state, newFluid);
|
| 413 | }
|
| 414 |
|
| 415 | private static boolean canHoldSpecificFluid(final BlockGetter level, final BlockPos pos, final BlockState state, final Fluid newFluid) {
|
| 416 | return state.getBlock() instanceof LiquidBlockContainer container ? container.canPlaceLiquid(null, level, pos, state, newFluid) : true;
|
| 417 | }
|
| 418 |
|
| 419 | protected abstract int getDropOff(LevelReader level);
|
| 420 |
|
| 421 | protected int getSpreadDelay(final Level level, final BlockPos pos, final FluidState oldFluidState, final FluidState newFluidState) {
|
| 422 | return this.getTickDelay(level);
|
| 423 | }
|
| 424 |
|
| 425 | @Override
|
| 426 | public void tick(final ServerLevel level, final BlockPos pos, BlockState blockState, FluidState fluidState) {
|
| 427 | if (!fluidState.isSource()) {
|
| 428 | FluidState newFluidState = this.getNewLiquid(level, pos, level.getBlockState(pos));
|
| 429 | int tickDelay = this.getSpreadDelay(level, pos, fluidState, newFluidState);
|
| 430 | if (newFluidState.isEmpty()) {
|
| 431 | fluidState = newFluidState;
|
| 432 | blockState = Blocks.AIR.defaultBlockState();
|
| 433 | level.setBlockAndUpdate(pos, blockState);
|
| 434 | } else if (newFluidState != fluidState) {
|
| 435 | fluidState = newFluidState;
|
| 436 | blockState = fluidState.createLegacyBlock();
|
| 437 | level.setBlockAndUpdate(pos, blockState);
|
| 438 | level.scheduleTick(pos, fluidState.getType(), tickDelay);
|
| 439 | }
|
| 440 | }
|
| 441 |
|
| 442 | this.spread(level, pos, blockState, fluidState);
|
| 443 | }
|
| 444 |
|
| 445 | protected static int getLegacyLevel(final FluidState fluidState) {
|
| 446 | return fluidState.isSource() ? 0 : 8 - Math.min(fluidState.getAmount(), 8) + (fluidState.getValue(FALLING) ? 8 : 0);
|
| 447 | }
|
| 448 |
|
| 449 | private static boolean hasSameAbove(final FluidState fluidState, final BlockGetter level, final BlockPos pos) {
|
| 450 | return fluidState.getType().isSame(level.getFluidState(pos.above()).getType());
|
| 451 | }
|
| 452 |
|
| 453 | @Override
|
| 454 | public float getHeight(final FluidState fluidState, final BlockGetter level, final BlockPos pos) {
|
| 455 | return hasSameAbove(fluidState, level, pos) ? 1.0F : fluidState.getOwnHeight();
|
| 456 | }
|
| 457 |
|
| 458 | @Override
|
| 459 | public float getOwnHeight(final FluidState fluidState) {
|
| 460 | return fluidState.getAmount() / 9.0F;
|
| 461 | }
|
| 462 |
|
| 463 | @Override
|
| 464 | public abstract int getAmount(final FluidState fluidState);
|
| 465 |
|
| 466 | @Override
|
| 467 | public VoxelShape getShape(final FluidState state, final BlockGetter level, final BlockPos pos) {
|
| 468 | return state.getAmount() == 9 && hasSameAbove(state, level, pos)
|
| 469 | ? Shapes.block()
|
| 470 | : this.shapes.computeIfAbsent(state, fluidState -> Shapes.box(0.0, 0.0, 0.0, 1.0, fluidState.getHeight(level, pos), 1.0));
|
| 471 | }
|
| 472 |
|
| 473 | private record BlockStatePairKey(BlockState first, BlockState second, Direction direction) {
|
| 474 | @Override
|
| 475 | public boolean equals(final Object o) {
|
| 476 | return o instanceof FlowingFluid.BlockStatePairKey that && this.first == that.first && this.second == that.second && this.direction == that.direction;
|
| 477 | }
|
| 478 |
|
| 479 | @Override
|
| 480 | public int hashCode() {
|
| 481 | int result = System.identityHashCode(this.first);
|
| 482 | result = 31 * result + System.identityHashCode(this.second);
|
| 483 | return 31 * result + this.direction.hashCode();
|
| 484 | }
|
| 485 | }
|
| 486 |
|
| 487 | protected class SpreadContext {
|
| 488 | private final BlockGetter level;
|
| 489 | private final BlockPos origin;
|
| 490 | private final Short2ObjectMap<BlockState> stateCache = new Short2ObjectOpenHashMap<>();
|
| 491 | private final Short2BooleanMap holeCache = new Short2BooleanOpenHashMap();
|
| 492 |
|
| 493 | private SpreadContext(final BlockGetter level, final BlockPos origin) {
|
| 494 | this.level = level;
|
| 495 | this.origin = origin;
|
| 496 | }
|
| 497 |
|
| 498 | public BlockState getBlockState(final BlockPos pos) {
|
| 499 | return this.getBlockState(pos, this.getCacheKey(pos));
|
| 500 | }
|
| 501 |
|
| 502 | private BlockState getBlockState(final BlockPos pos, final short key) {
|
| 503 | return this.stateCache.computeIfAbsent(key, k -> this.level.getBlockState(pos));
|
| 504 | }
|
| 505 |
|
| 506 | public boolean isHole(final BlockPos pos) {
|
| 507 | return this.holeCache.computeIfAbsent(this.getCacheKey(pos), key -> {
|
| 508 | BlockState state = this.getBlockState(pos, key);
|
| 509 | BlockPos below = pos.below();
|
| 510 | BlockState belowState = this.level.getBlockState(below);
|
| 511 | return FlowingFluid.this.isWaterHole(this.level, pos, state, below, belowState);
|
| 512 | });
|
| 513 | }
|
| 514 |
|
| 515 | private short getCacheKey(final BlockPos pos) {
|
| 516 | int relativeX = pos.getX() - this.origin.getX();
|
| 517 | int relativeZ = pos.getZ() - this.origin.getZ();
|
| 518 | return (short)((relativeX + 128 & 0xFF) << 8 | relativeZ + 128 & 0xFF);
|
| 519 | }
|
| 520 | }
|
| 521 | }
|
| 522 |
|