| 1 | package net.minecraft.world.level;
|
| 2 |
|
| 3 | import java.util.function.Predicate;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.tags.BlockTags;
|
| 7 | import net.minecraft.tags.FluidTags;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.entity.EntityTypes;
|
| 10 | import net.minecraft.world.level.block.Blocks;
|
| 11 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.gamerules.GameRules;
|
| 14 | import net.minecraft.world.level.material.FluidState;
|
| 15 | import net.minecraft.world.phys.Vec3;
|
| 16 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 17 | import net.minecraft.world.phys.shapes.EntityCollisionContext;
|
| 18 | import net.minecraft.world.phys.shapes.Shapes;
|
| 19 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 20 |
|
| 21 | public class ClipContext {
|
| 22 | private final Vec3 from;
|
| 23 | private final Vec3 to;
|
| 24 | private final ClipContext.Block block;
|
| 25 | private final ClipContext.Fluid fluid;
|
| 26 | private final CollisionContext collisionContext;
|
| 27 |
|
| 28 | public ClipContext(final Vec3 from, final Vec3 to, final ClipContext.Block block, final ClipContext.Fluid fluid, final Entity entity) {
|
| 29 | this(from, to, block, fluid, CollisionContext.of(entity));
|
| 30 | }
|
| 31 |
|
| 32 | public ClipContext(final Vec3 from, final Vec3 to, final ClipContext.Block block, final ClipContext.Fluid fluid, final CollisionContext collisionContext) {
|
| 33 | this.from = from;
|
| 34 | this.to = to;
|
| 35 | this.block = block;
|
| 36 | this.fluid = fluid;
|
| 37 | this.collisionContext = collisionContext;
|
| 38 | }
|
| 39 |
|
| 40 | public Vec3 getTo() {
|
| 41 | return this.to;
|
| 42 | }
|
| 43 |
|
| 44 | public Vec3 getFrom() {
|
| 45 | return this.from;
|
| 46 | }
|
| 47 |
|
| 48 | public VoxelShape getBlockShape(final BlockState blockState, final BlockGetter level, final BlockPos pos) {
|
| 49 | return this.block.get(blockState, level, pos, this.collisionContext);
|
| 50 | }
|
| 51 |
|
| 52 | public VoxelShape getFluidShape(final FluidState fluidState, final BlockGetter level, final BlockPos pos) {
|
| 53 | return this.fluid.canPick(fluidState) ? fluidState.getShape(level, pos) : Shapes.empty();
|
| 54 | }
|
| 55 |
|
| 56 | public enum Block implements ClipContext.ShapeGetter {
|
| 57 | COLLIDER(BlockBehaviour.BlockStateBase::getCollisionShape),
|
| 58 | OUTLINE(BlockBehaviour.BlockStateBase::getShape),
|
| 59 | VISUAL(BlockBehaviour.BlockStateBase::getVisualShape),
|
| 60 | FALLDAMAGE_RESETTING(
|
| 61 | (state, level, pos, collisionContext) -> {
|
| 62 | if (state.is(BlockTags.FALL_DAMAGE_RESETTING)) {
|
| 63 | return Shapes.block();
|
| 64 | }
|
| 65 |
|
| 66 | if (collisionContext instanceof EntityCollisionContext entityCollisionContext
|
| 67 | && entityCollisionContext.getEntity() != null
|
| 68 | && entityCollisionContext.getEntity().is(EntityTypes.PLAYER)) {
|
| 69 | if (state.is(Blocks.END_GATEWAY) || state.is(Blocks.END_PORTAL)) {
|
| 70 | return Shapes.block();
|
| 71 | }
|
| 72 |
|
| 73 | if (level instanceof ServerLevel serverLevel
|
| 74 | && state.is(Blocks.NETHER_PORTAL)
|
| 75 | && serverLevel.getGameRules().get(GameRules.PLAYERS_NETHER_PORTAL_DEFAULT_DELAY) == 0) {
|
| 76 | return Shapes.block();
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return Shapes.empty();
|
| 81 | }
|
| 82 | );
|
| 83 |
|
| 84 | private final ClipContext.ShapeGetter shapeGetter;
|
| 85 |
|
| 86 | Block(final ClipContext.ShapeGetter getShape) {
|
| 87 | this.shapeGetter = getShape;
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public VoxelShape get(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 92 | return this.shapeGetter.get(state, level, pos, context);
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public enum Fluid {
|
| 97 | NONE(state -> false),
|
| 98 | SOURCE_ONLY(FluidState::isSource),
|
| 99 | ANY(state -> !state.isEmpty()),
|
| 100 | WATER(fluidState -> fluidState.is(FluidTags.WATER));
|
| 101 |
|
| 102 | private final Predicate<FluidState> canPick;
|
| 103 |
|
| 104 | Fluid(final Predicate<FluidState> canPick) {
|
| 105 | this.canPick = canPick;
|
| 106 | }
|
| 107 |
|
| 108 | public boolean canPick(final FluidState fluidState) {
|
| 109 | return this.canPick.test(fluidState);
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | public interface ShapeGetter {
|
| 114 | VoxelShape get(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context);
|
| 115 | }
|
| 116 | }
|
| 117 |
|