| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.Vec3i;
|
| 9 | import net.minecraft.core.particles.ParticleTypes;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.server.level.ServerPlayer;
|
| 12 | import net.minecraft.sounds.SoundEvents;
|
| 13 | import net.minecraft.sounds.SoundSource;
|
| 14 | import net.minecraft.tags.FluidTags;
|
| 15 | import net.minecraft.util.Mth;
|
| 16 | import net.minecraft.util.RandomSource;
|
| 17 | import net.minecraft.world.InteractionHand;
|
| 18 | import net.minecraft.world.InteractionResult;
|
| 19 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 20 | import net.minecraft.world.entity.Entity;
|
| 21 | import net.minecraft.world.entity.EntityType;
|
| 22 | import net.minecraft.world.entity.player.Player;
|
| 23 | import net.minecraft.world.entity.vehicle.DismountHelper;
|
| 24 | import net.minecraft.world.item.ItemStack;
|
| 25 | import net.minecraft.world.item.Items;
|
| 26 | import net.minecraft.world.level.BlockGetter;
|
| 27 | import net.minecraft.world.level.CollisionGetter;
|
| 28 | import net.minecraft.world.level.Explosion;
|
| 29 | import net.minecraft.world.level.ExplosionDamageCalculator;
|
| 30 | import net.minecraft.world.level.Level;
|
| 31 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 32 | import net.minecraft.world.level.block.state.BlockState;
|
| 33 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 34 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 35 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 36 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 37 | import net.minecraft.world.level.material.FluidState;
|
| 38 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 39 | import net.minecraft.world.level.storage.LevelData;
|
| 40 | import net.minecraft.world.phys.BlockHitResult;
|
| 41 | import net.minecraft.world.phys.Vec3;
|
| 42 | import org.jspecify.annotations.Nullable;
|
| 43 |
|
| 44 | public class RespawnAnchorBlock extends Block {
|
| 45 | public static final int MIN_CHARGES = 0;
|
| 46 | public static final int MAX_CHARGES = 4;
|
| 47 | public static final IntegerProperty CHARGE = BlockStateProperties.RESPAWN_ANCHOR_CHARGES;
|
| 48 | private static final ImmutableList<Vec3i> RESPAWN_HORIZONTAL_OFFSETS = ImmutableList.of(
|
| 49 | new Vec3i(0, 0, -1),
|
| 50 | new Vec3i(-1, 0, 0),
|
| 51 | new Vec3i(0, 0, 1),
|
| 52 | new Vec3i(1, 0, 0),
|
| 53 | new Vec3i(-1, 0, -1),
|
| 54 | new Vec3i(1, 0, -1),
|
| 55 | new Vec3i(-1, 0, 1),
|
| 56 | new Vec3i(1, 0, 1)
|
| 57 | );
|
| 58 | private static final ImmutableList<Vec3i> RESPAWN_OFFSETS = new Builder<Vec3i>()
|
| 59 | .addAll(RESPAWN_HORIZONTAL_OFFSETS)
|
| 60 | .addAll(RESPAWN_HORIZONTAL_OFFSETS.stream().map(Vec3i::below).iterator())
|
| 61 | .addAll(RESPAWN_HORIZONTAL_OFFSETS.stream().map(Vec3i::above).iterator())
|
| 62 | .add(new Vec3i(0, 1, 0))
|
| 63 | .build();
|
| 64 |
|
| 65 | public RespawnAnchorBlock(final BlockBehaviour.Properties properties) {
|
| 66 | super(properties);
|
| 67 | this.registerDefaultState(this.stateDefinition.any().setValue(CHARGE, 0));
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected InteractionResult useItemOn(
|
| 72 | final ItemStack itemStack,
|
| 73 | final BlockState state,
|
| 74 | final Level level,
|
| 75 | final BlockPos pos,
|
| 76 | final Player player,
|
| 77 | final InteractionHand hand,
|
| 78 | final BlockHitResult hitResult
|
| 79 | ) {
|
| 80 | if (isRespawnFuel(itemStack) && canBeCharged(state)) {
|
| 81 | charge(player, level, pos, state);
|
| 82 | itemStack.consume(1, player);
|
| 83 | return InteractionResult.SUCCESS;
|
| 84 | } else {
|
| 85 | return hand == InteractionHand.MAIN_HAND && isRespawnFuel(player.getItemInHand(InteractionHand.OFF_HAND)) && canBeCharged(state)
|
| 86 | ? InteractionResult.PASS
|
| 87 | : InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 93 | if (state.getValue(CHARGE) == 0) {
|
| 94 | return InteractionResult.PASS;
|
| 95 | }
|
| 96 |
|
| 97 | if (level instanceof ServerLevel serverLevel) {
|
| 98 | if (!canSetSpawn(serverLevel, pos)) {
|
| 99 | this.explode(state, serverLevel, pos);
|
| 100 | return InteractionResult.SUCCESS_SERVER;
|
| 101 | }
|
| 102 |
|
| 103 | if (player instanceof ServerPlayer serverPlayer) {
|
| 104 | ServerPlayer.RespawnConfig respawnConfig = serverPlayer.getRespawnConfig();
|
| 105 | ServerPlayer.RespawnConfig newRespawnConfig = new ServerPlayer.RespawnConfig(LevelData.RespawnData.of(serverLevel.dimension(), pos, 0.0F, 0.0F), false);
|
| 106 | if (respawnConfig == null || !respawnConfig.isSamePosition(newRespawnConfig)) {
|
| 107 | serverPlayer.setRespawnPosition(newRespawnConfig, true);
|
| 108 | serverLevel.playSound(null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, SoundEvents.RESPAWN_ANCHOR_SET_SPAWN, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 109 | return InteractionResult.SUCCESS_SERVER;
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | return InteractionResult.CONSUME;
|
| 114 | } else {
|
| 115 | return InteractionResult.CONSUME;
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | private static boolean isRespawnFuel(final ItemStack itemInHand) {
|
| 120 | return itemInHand.is(Items.GLOWSTONE);
|
| 121 | }
|
| 122 |
|
| 123 | private static boolean canBeCharged(final BlockState state) {
|
| 124 | return state.getValue(CHARGE) < 4;
|
| 125 | }
|
| 126 |
|
| 127 | private static boolean isWaterThatWouldFlow(final BlockPos pos, final Level level) {
|
| 128 | FluidState fluid = level.getFluidState(pos);
|
| 129 | if (!fluid.is(FluidTags.WATER)) {
|
| 130 | return false;
|
| 131 | }
|
| 132 |
|
| 133 | if (fluid.isSource()) {
|
| 134 | return true;
|
| 135 | }
|
| 136 |
|
| 137 | float amount = fluid.getAmount();
|
| 138 | if (amount < 2.0F) {
|
| 139 | return false;
|
| 140 | }
|
| 141 |
|
| 142 | FluidState fluidBelow = level.getFluidState(pos.below());
|
| 143 | return !fluidBelow.is(FluidTags.WATER);
|
| 144 | }
|
| 145 |
|
| 146 | private void explode(final BlockState state, final ServerLevel level, final BlockPos pos) {
|
| 147 | level.removeBlock(pos, false);
|
| 148 | boolean anyWaterNeighbors = Direction.Plane.HORIZONTAL.stream().map(pos::relative).anyMatch(neighborPos -> isWaterThatWouldFlow(neighborPos, level));
|
| 149 | final boolean inWater = anyWaterNeighbors || level.getFluidState(pos.above()).is(FluidTags.WATER);
|
| 150 | ExplosionDamageCalculator damageCalculator = new ExplosionDamageCalculator() {
|
| 151 | @Override
|
| 152 | public Optional<Float> getBlockExplosionResistance(
|
| 153 | final Explosion explosion, final BlockGetter levelx, final BlockPos testPos, final BlockState block, final FluidState fluid
|
| 154 | ) {
|
| 155 | return testPos.equals(pos) && inWater
|
| 156 | ? Optional.of(Blocks.WATER.getExplosionResistance())
|
| 157 | : super.getBlockExplosionResistance(explosion, levelx, testPos, block, fluid);
|
| 158 | }
|
| 159 | };
|
| 160 | Vec3 boomPos = Vec3.atCenterOf(pos);
|
| 161 | level.explode(null, level.damageSources().badRespawnPointExplosion(boomPos), damageCalculator, boomPos, 5.0F, true, Level.ExplosionInteraction.BLOCK);
|
| 162 | }
|
| 163 |
|
| 164 | public static boolean canSetSpawn(final ServerLevel level, final BlockPos pos) {
|
| 165 | return level.environmentAttributes().getValue(EnvironmentAttributes.RESPAWN_ANCHOR_WORKS, pos);
|
| 166 | }
|
| 167 |
|
| 168 | public static void charge(final @Nullable Entity sourceEntity, final Level level, final BlockPos pos, final BlockState state) {
|
| 169 | BlockState newState = state.setValue(CHARGE, state.getValue(CHARGE) + 1);
|
| 170 | level.setBlockAndUpdate(pos, newState);
|
| 171 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(sourceEntity, newState));
|
| 172 | level.playSound(null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, SoundEvents.RESPAWN_ANCHOR_CHARGE, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 177 | if (state.getValue(CHARGE) != 0) {
|
| 178 | if (random.nextInt(100) == 0) {
|
| 179 | level.playLocalSound(pos, SoundEvents.RESPAWN_ANCHOR_AMBIENT, SoundSource.BLOCKS, 1.0F, 1.0F, false);
|
| 180 | }
|
| 181 |
|
| 182 | double x = pos.getX() + 0.5 + (0.5 - random.nextDouble());
|
| 183 | double y = pos.getY() + 1.0;
|
| 184 | double z = pos.getZ() + 0.5 + (0.5 - random.nextDouble());
|
| 185 | double ya = random.nextFloat() * 0.04;
|
| 186 | level.addParticle(ParticleTypes.REVERSE_PORTAL, x, y, z, 0.0, ya, 0.0);
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 192 | builder.add(CHARGE);
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 197 | return true;
|
| 198 | }
|
| 199 |
|
| 200 | public static int getScaledChargeLevel(final BlockState state, final int maximum) {
|
| 201 | return Mth.floor((state.getValue(CHARGE) - 0) / 4.0F * maximum);
|
| 202 | }
|
| 203 |
|
| 204 | @Override
|
| 205 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 206 | return getScaledChargeLevel(state, 15);
|
| 207 | }
|
| 208 |
|
| 209 | public static Optional<Vec3> findStandUpPosition(final EntityType<?> type, final CollisionGetter level, final BlockPos pos) {
|
| 210 | Optional<Vec3> safePosition = findStandUpPosition(type, level, pos, true);
|
| 211 | return safePosition.isPresent() ? safePosition : findStandUpPosition(type, level, pos, false);
|
| 212 | }
|
| 213 |
|
| 214 | private static Optional<Vec3> findStandUpPosition(final EntityType<?> type, final CollisionGetter level, final BlockPos pos, final boolean checkDangerous) {
|
| 215 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 216 |
|
| 217 | for (Vec3i offset : RESPAWN_OFFSETS) {
|
| 218 | blockPos.set(pos).move(offset);
|
| 219 | Vec3 position = DismountHelper.findSafeDismountLocation(type, level, blockPos, checkDangerous);
|
| 220 | if (position != null) {
|
| 221 | return Optional.of(position);
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | return Optional.empty();
|
| 226 | }
|
| 227 |
|
| 228 | @Override
|
| 229 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 230 | return false;
|
| 231 | }
|
| 232 | }
|
| 233 |
|