| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.particles.ParticleTypes;
|
| 9 | import net.minecraft.resources.ResourceKey;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import net.minecraft.sounds.SoundSource;
|
| 13 | import net.minecraft.util.BlockUtil;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.world.Difficulty;
|
| 16 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 17 | import net.minecraft.world.entity.Entity;
|
| 18 | import net.minecraft.world.entity.EntityDimensions;
|
| 19 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 20 | import net.minecraft.world.entity.EntityTypes;
|
| 21 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 22 | import net.minecraft.world.entity.Relative;
|
| 23 | import net.minecraft.world.entity.player.Player;
|
| 24 | import net.minecraft.world.item.ItemStack;
|
| 25 | import net.minecraft.world.level.BlockGetter;
|
| 26 | import net.minecraft.world.level.Level;
|
| 27 | import net.minecraft.world.level.LevelReader;
|
| 28 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 29 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 30 | import net.minecraft.world.level.block.state.BlockState;
|
| 31 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 32 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 33 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 34 | import net.minecraft.world.level.border.WorldBorder;
|
| 35 | import net.minecraft.world.level.dimension.DimensionType;
|
| 36 | import net.minecraft.world.level.gamerules.GameRules;
|
| 37 | import net.minecraft.world.level.portal.PortalShape;
|
| 38 | import net.minecraft.world.level.portal.TeleportTransition;
|
| 39 | import net.minecraft.world.phys.Vec3;
|
| 40 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 41 | import net.minecraft.world.phys.shapes.Shapes;
|
| 42 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 43 | import org.jspecify.annotations.Nullable;
|
| 44 | import org.slf4j.Logger;
|
| 45 |
|
| 46 | public class NetherPortalBlock extends Block implements Portal {
|
| 47 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 48 | public static final EnumProperty<Direction.Axis> AXIS = BlockStateProperties.HORIZONTAL_AXIS;
|
| 49 | private static final Map<Direction.Axis, VoxelShape> SHAPES = Shapes.rotateHorizontalAxis(Block.column(4.0, 16.0, 0.0, 16.0));
|
| 50 |
|
| 51 | public NetherPortalBlock(final BlockBehaviour.Properties properties) {
|
| 52 | super(properties);
|
| 53 | this.registerDefaultState(this.stateDefinition.any().setValue(AXIS, Direction.Axis.X));
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 58 | return SHAPES.get(state.getValue(AXIS));
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected void randomTick(final BlockState state, final ServerLevel level, BlockPos pos, final RandomSource random) {
|
| 63 | if (level.isSpawningMonsters()
|
| 64 | && level.getLevelData().getDifficulty() != Difficulty.PEACEFUL
|
| 65 | && level.environmentAttributes().getValue(EnvironmentAttributes.NETHER_PORTAL_SPAWNS_PIGLINS, pos)
|
| 66 | && random.nextInt(2000) < level.getDifficulty().getId()
|
| 67 | && level.anyPlayerCloseEnoughForSpawning(pos)) {
|
| 68 | while (level.getBlockState(pos).is(this)) {
|
| 69 | pos = pos.below();
|
| 70 | }
|
| 71 |
|
| 72 | if (level.getBlockState(pos).isValidSpawn(level, pos, EntityTypes.ZOMBIFIED_PIGLIN)) {
|
| 73 | Entity entity = EntityTypes.ZOMBIFIED_PIGLIN.spawn(level, pos.above(), EntitySpawnReason.STRUCTURE);
|
| 74 | if (entity != null) {
|
| 75 | entity.setPortalCooldown();
|
| 76 | Entity vehicle = entity.getVehicle();
|
| 77 | if (vehicle != null) {
|
| 78 | vehicle.setPortalCooldown();
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | protected BlockState updateShape(
|
| 87 | final BlockState state,
|
| 88 | final LevelReader level,
|
| 89 | final ScheduledTickAccess ticks,
|
| 90 | final BlockPos pos,
|
| 91 | final Direction directionToNeighbour,
|
| 92 | final BlockPos neighbourPos,
|
| 93 | final BlockState neighbourState,
|
| 94 | final RandomSource random
|
| 95 | ) {
|
| 96 | Direction.Axis updateAxis = directionToNeighbour.getAxis();
|
| 97 | Direction.Axis axis = state.getValue(AXIS);
|
| 98 | boolean wrongAxis = axis != updateAxis && updateAxis.isHorizontal();
|
| 99 | return !wrongAxis && !neighbourState.is(this) && !PortalShape.findAnyShape(level, pos, axis).isComplete()
|
| 100 | ? Blocks.AIR.defaultBlockState()
|
| 101 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected void entityInside(
|
| 106 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 107 | ) {
|
| 108 | if (entity.canUsePortal(false)) {
|
| 109 | entity.setAsInsidePortal(this, pos);
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public int getPortalTransitionTime(final ServerLevel level, final Entity entity) {
|
| 115 | return entity instanceof Player player
|
| 116 | ? Math.max(
|
| 117 | 0,
|
| 118 | level.getGameRules()
|
| 119 | .get(player.getAbilities().invulnerable ? GameRules.PLAYERS_NETHER_PORTAL_CREATIVE_DELAY : GameRules.PLAYERS_NETHER_PORTAL_DEFAULT_DELAY)
|
| 120 | )
|
| 121 | : 0;
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public @Nullable TeleportTransition getPortalDestination(final ServerLevel currentLevel, final Entity entity, final BlockPos portalEntryPos) {
|
| 126 | ResourceKey<Level> newDimension = currentLevel.dimension() == Level.NETHER ? Level.OVERWORLD : Level.NETHER;
|
| 127 | ServerLevel newLevel = currentLevel.getServer().getLevel(newDimension);
|
| 128 | if (newLevel == null) {
|
| 129 | return null;
|
| 130 | }
|
| 131 |
|
| 132 | boolean toNether = newLevel.dimension() == Level.NETHER;
|
| 133 | WorldBorder newWorldBorder = newLevel.getWorldBorder();
|
| 134 | double teleportationScale = DimensionType.getTeleportationScale(currentLevel.dimensionType(), newLevel.dimensionType());
|
| 135 | BlockPos approximateExitPos = newWorldBorder.clampToBounds(entity.getX() * teleportationScale, entity.getY(), entity.getZ() * teleportationScale);
|
| 136 | return this.getExitPortal(newLevel, entity, portalEntryPos, approximateExitPos, toNether, newWorldBorder);
|
| 137 | }
|
| 138 |
|
| 139 | private @Nullable TeleportTransition getExitPortal(
|
| 140 | final ServerLevel newLevel,
|
| 141 | final Entity entity,
|
| 142 | final BlockPos portalEntryPos,
|
| 143 | final BlockPos approximateExitPos,
|
| 144 | final boolean toNether,
|
| 145 | final WorldBorder worldBorder
|
| 146 | ) {
|
| 147 | Optional<BlockPos> exitPortalPos = newLevel.getPortalForcer().findClosestPortalPosition(approximateExitPos, toNether, worldBorder);
|
| 148 | BlockUtil.FoundRectangle exitPortal;
|
| 149 | TeleportTransition.PostTeleportTransition post;
|
| 150 | if (exitPortalPos.isPresent()) {
|
| 151 | BlockPos pos = exitPortalPos.get();
|
| 152 | BlockState portalState = newLevel.getBlockState(pos);
|
| 153 | exitPortal = BlockUtil.getLargestRectangleAround(
|
| 154 | pos, portalState.getValue(BlockStateProperties.HORIZONTAL_AXIS), 21, Direction.Axis.Y, 21, blockPos -> newLevel.getBlockState(blockPos) == portalState
|
| 155 | );
|
| 156 | post = TeleportTransition.PLAY_PORTAL_SOUND.then(e -> e.placePortalTicket(pos));
|
| 157 | } else {
|
| 158 | if (entity.isSpectator()) {
|
| 159 | return null;
|
| 160 | }
|
| 161 |
|
| 162 | Direction.Axis sourcePortalAxis = entity.level().getBlockState(portalEntryPos).getOptionalValue(AXIS).orElse(Direction.Axis.X);
|
| 163 | Optional<BlockUtil.FoundRectangle> createdExit = newLevel.getPortalForcer().createPortal(approximateExitPos, sourcePortalAxis);
|
| 164 | if (createdExit.isEmpty()) {
|
| 165 | LOGGER.error("Unable to create a portal, likely target out of worldborder");
|
| 166 | return null;
|
| 167 | }
|
| 168 |
|
| 169 | exitPortal = createdExit.get();
|
| 170 | post = TeleportTransition.PLAY_PORTAL_SOUND.then(TeleportTransition.PLACE_PORTAL_TICKET);
|
| 171 | }
|
| 172 |
|
| 173 | return getDimensionTransitionFromExit(entity, portalEntryPos, exitPortal, newLevel, post);
|
| 174 | }
|
| 175 |
|
| 176 | private static TeleportTransition getDimensionTransitionFromExit(
|
| 177 | final Entity entity,
|
| 178 | final BlockPos portalEntryPos,
|
| 179 | final BlockUtil.FoundRectangle exitPortal,
|
| 180 | final ServerLevel newLevel,
|
| 181 | final TeleportTransition.PostTeleportTransition postTeleportTransition
|
| 182 | ) {
|
| 183 | BlockState blockState = entity.level().getBlockState(portalEntryPos);
|
| 184 | Direction.Axis axis;
|
| 185 | Vec3 offset;
|
| 186 | if (blockState.hasProperty(BlockStateProperties.HORIZONTAL_AXIS)) {
|
| 187 | axis = blockState.getValue(BlockStateProperties.HORIZONTAL_AXIS);
|
| 188 | BlockUtil.FoundRectangle portalArea = BlockUtil.getLargestRectangleAround(
|
| 189 | portalEntryPos, axis, 21, Direction.Axis.Y, 21, pos -> entity.level().getBlockState(pos) == blockState
|
| 190 | );
|
| 191 | offset = entity.getRelativePortalPosition(axis, portalArea);
|
| 192 | } else {
|
| 193 | axis = Direction.Axis.X;
|
| 194 | offset = new Vec3(0.5, 0.0, 0.0);
|
| 195 | }
|
| 196 |
|
| 197 | return createDimensionTransition(newLevel, exitPortal, axis, offset, entity, postTeleportTransition);
|
| 198 | }
|
| 199 |
|
| 200 | private static TeleportTransition createDimensionTransition(
|
| 201 | final ServerLevel newLevel,
|
| 202 | final BlockUtil.FoundRectangle foundRectangle,
|
| 203 | final Direction.Axis portalAxis,
|
| 204 | final Vec3 offset,
|
| 205 | final Entity entity,
|
| 206 | final TeleportTransition.PostTeleportTransition postTeleportTransition
|
| 207 | ) {
|
| 208 | BlockPos bottomLeft = foundRectangle.minCorner;
|
| 209 | BlockState blockState = newLevel.getBlockState(bottomLeft);
|
| 210 | Direction.Axis axis = blockState.getOptionalValue(BlockStateProperties.HORIZONTAL_AXIS).orElse(Direction.Axis.X);
|
| 211 | double width = foundRectangle.axis1Size;
|
| 212 | double height = foundRectangle.axis2Size;
|
| 213 | EntityDimensions dimensions = entity.getDimensions(entity.getPose());
|
| 214 | int outputRotation = portalAxis == axis ? 0 : 90;
|
| 215 | double offsetRight = dimensions.width() / 2.0 + (width - dimensions.width()) * offset.x();
|
| 216 | double offsetUp = (height - dimensions.height()) * offset.y();
|
| 217 | double offsetForward = 0.5 + offset.z();
|
| 218 | boolean xAligned = axis == Direction.Axis.X;
|
| 219 | Vec3 targetPos = new Vec3(
|
| 220 | bottomLeft.getX() + (xAligned ? offsetRight : offsetForward), bottomLeft.getY() + offsetUp, bottomLeft.getZ() + (xAligned ? offsetForward : offsetRight)
|
| 221 | );
|
| 222 | Vec3 collisionFreePos = PortalShape.findCollisionFreePosition(targetPos, newLevel, entity, dimensions);
|
| 223 | return new TeleportTransition(
|
| 224 | newLevel, collisionFreePos, Vec3.ZERO, outputRotation, 0.0F, Relative.union(Relative.DELTA, Relative.ROTATION), postTeleportTransition
|
| 225 | );
|
| 226 | }
|
| 227 |
|
| 228 | @Override
|
| 229 | public Portal.Transition getLocalTransition() {
|
| 230 | return Portal.Transition.CONFUSION;
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 235 | if (random.nextInt(100) == 0) {
|
| 236 | level.playLocalSound(
|
| 237 | pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, SoundEvents.PORTAL_AMBIENT, SoundSource.BLOCKS, 0.5F, random.nextFloat() * 0.4F + 0.8F, false
|
| 238 | );
|
| 239 | }
|
| 240 |
|
| 241 | for (int i = 0; i < 4; i++) {
|
| 242 | double x = pos.getX() + random.nextDouble();
|
| 243 | double y = pos.getY() + random.nextDouble();
|
| 244 | double z = pos.getZ() + random.nextDouble();
|
| 245 | double xa = (random.nextFloat() - 0.5) * 0.5;
|
| 246 | double ya = (random.nextFloat() - 0.5) * 0.5;
|
| 247 | double za = (random.nextFloat() - 0.5) * 0.5;
|
| 248 | int flip = random.nextInt(2) * 2 - 1;
|
| 249 | if (!level.getBlockState(pos.west()).is(this) && !level.getBlockState(pos.east()).is(this)) {
|
| 250 | x = pos.getX() + 0.5 + 0.25 * flip;
|
| 251 | xa = random.nextFloat() * 2.0F * flip;
|
| 252 | } else {
|
| 253 | z = pos.getZ() + 0.5 + 0.25 * flip;
|
| 254 | za = random.nextFloat() * 2.0F * flip;
|
| 255 | }
|
| 256 |
|
| 257 | level.addParticle(ParticleTypes.PORTAL, x, y, z, xa, ya, za);
|
| 258 | }
|
| 259 | }
|
| 260 |
|
| 261 | @Override
|
| 262 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 263 | return ItemStack.EMPTY;
|
| 264 | }
|
| 265 |
|
| 266 | @Override
|
| 267 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 268 | switch (rotation) {
|
| 269 | case COUNTERCLOCKWISE_90:
|
| 270 | case CLOCKWISE_90:
|
| 271 | switch ((Direction.Axis)state.getValue(AXIS)) {
|
| 272 | case X:
|
| 273 | return state.setValue(AXIS, Direction.Axis.Z);
|
| 274 | case Z:
|
| 275 | return state.setValue(AXIS, Direction.Axis.X);
|
| 276 | default:
|
| 277 | return state;
|
| 278 | }
|
| 279 | default:
|
| 280 | return state;
|
| 281 | }
|
| 282 | }
|
| 283 |
|
| 284 | @Override
|
| 285 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 286 | builder.add(AXIS);
|
| 287 | }
|
| 288 | }
|
| 289 |
|